Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Using a loop to calculate vector differences


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Using a loop to calculate vector differences
Date   Fri, 21 Sep 2012 00:59:15 +0100

As you want to use Stata's matrix language, a program -matgop-
published in 1999 is a direct solution. See dm69 and as context a
prequel and a sequel.

STB-56  dm79  . . . . . . . . . . . . . . . . . . Yet more new matrix commands
        (help matcorr, matewmf, matvsort, svmat2 if installed)  . .  N. J. Cox
        7/00    pp.4--8; STB Reprints Vol 10, pp.17--23
        commands to produce a correlation matrix, elementwise monadic
        function of another matrix, selected subsets of matrix rows
        and columns, vec or vech of a matrix, elements sorted within
        a vector, matrix from a vector, and commands to save matrices
        see mata matrix language incorporated into Stata 9.0

STB-50  dm69  . . . . . . . . . . . . . . . . . .  Further new matrix commands
        (help matdelrc, matewm, matmad, matpow if installed)  . . .  N. J. Cox
        7/99    pp.5--9; STB Reprints Vol 9, pp.29--34
        collection of new matrix commands providing additional matrix
        checking, management, element-wise operators, maximum absolute
        difference, and power

STB-39  dm49  . . . . . . . . . . . . . . . . . . . . Some new matrix commands
        (help matfunc, varfunc if installed)  . . . . . . . . . . .  J. Weesie
        9/97    pp.17--20; STB Reprints Vol 7, pp.43--48
        collection of new matrix commands; several for explicit matrices
        and a few for implicit matrices (i.e., variables)
        see mata matrix language incorporated into Stata 9

You can get clickable links to install by typing

. search matrix, historical stb

The code could be

. mat M=[0, 0, 0, 0, 0, 0, 0, .1, .4, .4, .1]'

. mat F=[0, 0, 0, 0, 0, .05, .3, .6, .05, 0, 0]

. matgop M F diff, op(-)

diff[11,11]
         c1      c2      c3      c4      c5      c6      c7      c8
  c9     c10     c11
 r1   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r2   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r3   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r4   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r5   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r6   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r7   0.000   0.000   0.000   0.000   0.000  -0.050  -0.300  -0.600
-0.050   0.000   0.000
 r8   0.100   0.100   0.100   0.100   0.100   0.050  -0.200  -0.500
0.050   0.100   0.100
 r9   0.400   0.400   0.400   0.400   0.400   0.350   0.100  -0.200
0.350   0.400   0.400
r10   0.400   0.400   0.400   0.400   0.400   0.350   0.100  -0.200
0.350   0.400   0.400
r11   0.100   0.100   0.100   0.100   0.100   0.050  -0.200  -0.500
0.050   0.100   0.100

Here -...gop- is generalized outer product, and the generalisation
inspired by APL

http://en.wikipedia.org/wiki/APL_(programming_language)

is to allow binary operators other than multiplication.

In your code you loop around

gen y=x_male`i'-y_female

and when the macro `i' is 1 this becomes

gen y = x_male1 - y_female

and -- as Stata does tell you -- it can't find -x_male1- (which in
this context can _only_ be a variable with that name or failing that a
scalar with that name). You want the first element of the Stata vector
-x_male- which would be -x_male[1,1]-. So, the problem is that
-x_male1- doesn't exist, not that it is something other than what you
want.

If that bug were fixed, there is another in the loop: second time
around the loop the -generate- statement would fail as -y- already
exists.

Your code shows other problems, as you appear to want to store a
matrix in the variable -diff-.

All that said, my reference to -matgop- is largely just parental
indulgence. It's best just to use some Mata:

mata :
M= (0, 0, 0, 0, 0, 0, 0, .1, .4, .4, .1)'
F= (0, 0, 0, 0, 0, .05, .3, .6, .05, 0, 0)
len = length(F)
diff = J(0, len, .)

for(j = 1; j <= len; j++) {
        diff = diff \ (M[j] :- F)
}

diff
           1      2      3      4      5      6      7      8      9
  10     11
     +------------------------------------------------------------------------------+
   1 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   2 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   3 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   4 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   5 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   6 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   7 |     0      0      0      0      0   -.05    -.3    -.6   -.05
   0      0  |
   8 |    .1     .1     .1     .1     .1    .05    -.2    -.5    .05
  .1     .1  |
   9 |    .4     .4     .4     .4     .4    .35     .1    -.2    .35
  .4     .4  |
  10 |    .4     .4     .4     .4     .4    .35     .1    -.2    .35
  .4     .4  |
  11 |    .1     .1     .1     .1     .1    .05    -.2    -.5    .05
  .1     .1  |
     +------------------------------------------------------------------------------+

Nick

On Thu, Sep 20, 2012 at 10:45 PM, Heidi M Pitts <[email protected]> wrote:

> Can anyone help fix my code for the following issue:
>
> I have two vectors:
> M=[0, 0, 0, 0, 0, 0, 0, .1, .4, .4, .1]
> F=[0, 0, 0, 0, 0, .05, .3, .6, .05, 0, 0]
> so n=11 obs for each vector.
>
> I’m trying to calculate the vector difference between each element `i’ in M
> and the whole vector F.    So the first vector of differences would be [M1=0
> minus 0,0,0,0,0,.05,.3,.6,.05,0,0].  Then I would calculate a second set of
> differences, M2=0 - same F vector, and stack it under the diff vector.
>
> I have written a simple loop to calculate each difference. But the error
> message I get is: “x_male1 not found”. Seems that my code is not recognizing
> that I want the first element of M to subtract each element of F and
> populate the vector ‘diff’.  Can anyone help?  Seems that the first issue is
> that ‘x_male1’ is not equal to 0, the first element in the vector.
>
>
> /************  program to calculate difference of
> distributions***************/
> set matsize 100
>
> mkmat x_male
> mkmat y_female
> mkmat y_fem_inv
>
> set obs 11
> gen n=_n
> gen diff=.
> mkmat diff
> forvalues i=1(1)11 {
>         gen y=x_male`i'-y_female
>         replace diff=y if `i'< n
>
> }
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index