datatran.src

来自「没有说明」· SRC 代码 · 共 840 行 · 第 1/2 页

SRC
840
字号
**              highest (right-most) category is unbounded on the
**              right.
**
**  Format:     y = dummy(x,v);
**
**  Input:      x    Nx1 vector of data that is to be "broken up"
**                   into dummy variables.
**
**              v    K-1x1 vector specifying the k-1 breakpoints
**                   (these must be in ascending order) that
**                   determine the k categories to be used in
**                   computing the dummy variables.
**
**  Output:     y    NxK matrix containing the k dummy variables.
**
**  Remarks:    Missings are deleted before the dummy variables are
**              created.
**
**              All categories are open on the left (i.e., do not
**              contain their left boundaries) and all but the
**              highest are closed on the right (i.e., do contain
**              their right boundaries). The highest (right-most)
**              category is unbounded on the right. Thus, only k-1
**              breakpoints are required to specify k dummy
**              variables.
**
**              The function dummybr is similar to dummy, but in
**              that function the highest category is bounded on the
**              right. The function dummydn is also similar to
**              dummy, but in that function a specified column of
**              dummies is dropped.
**
**  Example:    let x = 0 2 4 6 ;
**              v = 1|5|7;
**              y = dummy(x,v);
**
**              The result y looks like this:
**
**                   1     0     0     0
**                   0     1     0     0
**                   0     1     0     0
**                   0     0     1     0
**
**              The vector v will produce 4 dummies satisfying the
**              following conditions:
**
**                   x .<= 1
**                   1 .< x .and x .<= 5
**                   5 .< x .and x .<= 7
**                   7 .< x.
**
**  Globals:    None
**
**  See Also:   dummybr, dummydn
*/

proc dummy(x,v);
    local n, k, d, i, m;
    /* check for complex input */
    if iscplx(x);
        if hasimag(x);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            x = real(x);
        endif;
    endif;
    if iscplx(v);
        if hasimag(v);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            v = real(v);
        endif;
    endif;

    x = packr(x);           /* Get rid of missings. */
    if scalmiss(x);
        errorlog "ERROR: NO ROWS REMAIN AFTER DELETING ROWS WITH MISSINGS";
        end;
    endif;
    n = rows(x);
    m = cols(x);
    if m > 1;
        errorlog "ERROR: X cannot have more than 1 column.";
        end;
    endif;
    k = rows(v);
    d = zeros(n,k+1);
    d[.,1] = (x .<= v[1,.]);
    d[.,k+1] = (v[k,.] .< x);
    i = 2;
    do until i > k;
        d[.,i] = ((v[i-1,.] .< x) .and (x .<= v[i,.]));
        i = i+1;
    endo;
    retp(d);
endp;

/*
**> missex
**
**  Purpose:    Converts numeric values to the missing value code
**              according to the values given in a logical
**              expression.
**
**  Format:     y = missex(x,e);
**
**  Input:      x    NxK matrix.
**
**              e    NxK logical matrix (matrix of 0's and 1's) that
**                   serves as a "mask" for x;  the 1's in e
**                   correspond to the values in x that are to be
**                   converted into missing values.
**
**  Output:     y    NxK matrix that equals x, but with those
**                   elements that correspond to the 1's in e
**                   converted to missing.
**
**  Remarks:    The matrix e will usually be created by a logical
**              expression.  For instance, to convert all numbers
**              between 10 and 15 in x to missing, the following
**              code could be used:
**
**                   y = missex( x, (x .> 10) .and (x .< 15) );
**
**              Note that "dot" operators MUST be used in
**              constructing the logical expressions.
**
**              This function is like miss, but is more general in
**              that a range of values can be converted into
**              missings.
**
**  Example:    x = rndu(3,2);
**              /* logical expression */
**              e = (x .> .10) .and (x .< .20);
**              y = missex(x,e);
**
**              A 3x2 matrix of random uniform numbers is created.
**              All values in the interval (.10,.20) are converted
**              to missing.
**
**  Globals:    None
**
**  See Also:   miss, missrv
*/

proc missex(x,e);
    local y,control;
    control = ndpcntrl(0,0);
    call ndpcntrl(1,1);
    y = x + miss(e,1);
    if iscplx(x);
        y = complex( real(y), (imag(x).*(.not e)) );
    endif;
    ndpclex;
    call ndpcntrl(control,0xffff);
    retp(y);
endp;

/*
**> recode
**
**  Purpose:    Changes the values of an existing vector, from a
**              vector of new values. Used in data transformations.
**
**  Format:     y = recode(x,e,v);
**
**  Input:      x    Nx1 vector, to be recoded (changed).
**
**              e    NxK matrix of 1's and 0's.
**
**              v    Kx1 vector containing the new values to be assigned
**                   to the recoded variable.
**
**  Output:     y    Nx1 vector containing the recoded values of x.
**
**  Remarks:    There should be no more than a single 1 in any
**              row of e.
**
**              For any given row N of x and e, if the Kth column
**              of e is 1, the Kth element of v will replace the
**              original element of x.
**
**  Example:    x = { 20,
**                    45,
**                    32,
**                    63,
**                    29 };
**
**              e1 = (20 .lt x) .and (x .le 30);
**              e2 = (30 .lt x) .and (x .le 40);
**              e3 = (40 .lt x) .and (x .le 50);
**              e4 = (50 .lt x) .and (x .le 60);
**
**              e = e1~e2~e3~e4;
**
**              v = { 1,
**                    2,
**                    3,
**                    4 };
**
**              y = recode(x,e,v);
**
**                    20
**                    45
**              x =   32
**                    63
**                    29
**
**                     0    0    0    0
**                     0    0    1    0
**              e =    0    1    0    0
**                     0    0    0    0
**                     1    0    0    0
**
**                     1
**              v =    2
**                     3
**                     4
**
**                    20
**                     3
**             y =     2
**                    63
**                     1
**
**  See Also:   code, substute
*/

proc recode(x,e,v);
    local nxv;
    if type(v) == 13;
        v = 0 $+ v;
    endif;
    /* check for complex input */
    if iscplx(x);
        if hasimag(x);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            x = real(x);
        endif;
    endif;
    if iscplx(e);
        if hasimag(e);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            e = real(e);
        endif;
    endif;
    if iscplx(v);
        if hasimag(v);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            v = real(v);
        endif;
    endif;

    if ismiss(x) or ismiss(v);
        nxv = abs(maxc(v|x))*1.1+1;
        v = missrv(v,nxv);
        x = missrv(x,nxv);
        retp( miss( (x.*(.not (maxc(e')))) + e*v, nxv ) );
    else;
        retp( (x.*(.not (maxc(e')))) + e*v );
    endif;
endp;

/*
**> selif
**
**  Purpose:    Selects rows from a matrix. Those selected are those
**              for which there is a 1 in the corresponding row of
**              e.
**
**  Format:     y = selif(x,e);
**
**  Input:      x    NxK data matrix.
**
**              e    Nx1 logical vector (vector of 0's and 1's).
**
**  Output:     y    MxK data matrix consisting of the rows of y for
**                   which there is a 1 (actually, a nonzero value)
**                   in the corresponding row of e; y will be a
**                   scalar missing if no rows remain.
**
**  Remarks:    The argument e will usually be generated by a
**              logical expression.  For instance:  y=selif(x,x .>
**              100) selects all rows of x that are greater than 100
**              and puts them into y.
**
**  Example:    let x[3,3] = 0 10 20
**                          30 40 50
**                          60 70 80;
**              /* logical vector */
**              e = (x[.,1] .gt 0) .and (x[.,3] .lt 100);
**              y = selif(x,e);
**
**              The resulting matrix y is:
**
**                   30     40     50
**                   60     70     80
**
**              All rows for which the element in column 1 is
**              greater than 0 and the element in column 3 is less
**              than 100 are placed into the matrix y.
**
**  See Also:   delif
*/

proc selif(x,e);
    /* check for complex input */
    if iscplx(x);
        if hasimag(x);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            x = real(x);
        endif;
    endif;
    if iscplx(e);
        if hasimag(e);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            e = real(e);
        endif;
    endif;

    retp( submat(x,submat(packr(seqa(1,1,rows(x))~miss(e,0)),0,1),0) );
endp;

/*
**> substute
**
**  Purpose:    Substitutes new values for old values in a matrix,
**              depending on the outcome of a logical expression.
**
**  Format:     y = substute(x,e,v);
**
**  Input:      x    NxK matrix containing the data to be changed.
**
**              e    LxM matrix, ExE conformable with x containing
**                   1's and 0's.
**
**                   Elements of x will be changed if the
**                   corresponding element of e is 1.
**
**              v    PxQ matrix, ExE conformable with x and e,
**                   containing the values to be substituted for the
**                   original values of x when the corresponding element
**                   of e is 1.
**
**  Output:     y    max of N,L,P by max of K,M,Q matrix.
**
**  Remarks:    The e matrix is usually the result of an expression
**              or set of expressions using "dot" conditional and
**              boolean operators.
**
**  Example:    x = { Y 55 30,
**                    N 57 18,
**                    Y 24  3,
**                    N 63 38,
**                    Y 55 32,
**                    N 37 11 };
**
**              e = x[.,1] .$== "Y" .and x[.,2] .>= 55 .and x[.,3] .>= 30;
**
**              x[.,1] = substute(x[.,1],e,"R");
**
**
**                    R   55   30
**                    N   57   18
**              x =   Y   24    3
**                    N   63   38
**                    R   55   32
**                    N   37   11
**
**  See Also:   code, recode
*/

proc substute(x,e,v);

    /* check for complex input */
    if iscplx(x);
        if hasimag(x);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            x = real(x);
        endif;
    endif;
    if iscplx(e);
        if hasimag(e);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            e = real(e);
        endif;
    endif;
    if iscplx(v);
        if hasimag(v);
            errorlog "ERROR: Not implemented for complex matrices.";
            end;
        else;
            v = real(v);
        endif;
    endif;

    if type(v) == 13;
        v = 0 $+ v;
    endif;
    x = (x.*(.not e)) + e.*v;
    ndpclex;
    retp(x);
endp;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?