Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

From |
"Heidi M Pitts" <hpitts@unm.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Using a loop to calculate vector differences |

Date |
Fri, 21 Sep 2012 09:12:26 -0600 |

Thank you very much. I will try both suggestions. Many thanks. Heidi On Fri, 21 Sep 2012 00:59:15 +0100 Nick Cox <njcoxstata@gmail.com> wrote:

As you want to use Stata's matrix language, a program-matgop-published in 1999 is a direct solution. See dm69 and ascontext aprequel and a sequel.STB-56 dm79 . . . . . . . . . . . . . . . . . . Yetmore new matrix commands(help matcorr, matewmf, matvsort, svmat2 ifinstalled) . . N. J. Cox7/00 pp.4--8; STB Reprints Vol 10, pp.17--23commands to produce a correlation matrix,elementwise monadicfunction of another matrix, selected subsets ofmatrix rowsand columns, vec or vech of a matrix, elementssorted withina vector, matrix from a vector, and commands tosave matricessee mata matrix language incorporated into Stata9.0STB-50 dm69 . . . . . . . . . . . . . . . . . .Further new matrix commands(help matdelrc, matewm, matmad, matpow ifinstalled) . . . N. J. Cox7/99 pp.5--9; STB Reprints Vol 9, pp.29--34collection of new matrix commands providingadditional matrixchecking, management, element-wise operators,maximum absolutedifference, and powerSTB-39 dm49 . . . . . . . . . . . . . . . . . . . .Some new matrix commands(help matfunc, varfunc if installed) . . . . . .. . . . . J. Weesie9/97 pp.17--20; STB Reprints Vol 7, pp.43--48collection of new matrix commands; several forexplicit matricesand a few for implicit matrices (i.e., variables)see mata matrix language incorporated into Stata9You 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 c6c7 c8c9 c10 c11r1 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r2 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r3 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r4 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r5 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r6 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r7 0.000 0.000 0.000 0.000 0.000 -0.050-0.300 -0.600-0.050 0.000 0.000r8 0.100 0.100 0.100 0.100 0.100 0.050-0.200 -0.5000.050 0.100 0.100r9 0.400 0.400 0.400 0.400 0.400 0.3500.100 -0.2000.350 0.400 0.400r10 0.400 0.400 0.400 0.400 0.400 0.3500.100 -0.2000.350 0.400 0.400r11 0.100 0.100 0.100 0.100 0.100 0.050-0.200 -0.5000.050 0.100 0.100Here -...gop- is generalized outer product, and thegeneralisationinspired 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_femaleand -- as Stata does tell you -- it can't find -x_male1-(which inthis context can _only_ be a variable with that name orfailing that ascalar with that name). You want the first element ofthe Stata vector-x_male- which would be -x_male[1,1]-. So, the problemis that-x_male1- doesn't exist, not that it is something otherthan what youwant.If that bug were fixed, there is another in the loop:second timearound the loop the -generate- statement would fail as-y- alreadyexists.Your code shows other problems, as you appear to want tostore amatrix in the variable -diff-.All that said, my reference to -matgop- is largely justparentalindulgence. 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) } diff1 2 3 4 5 6 78 910 11 +------------------------------------------------------------------------------+1 | 0 0 0 0 0 -.05 -.3-.6 -.050 0 |2 | 0 0 0 0 0 -.05 -.3-.6 -.050 0 |3 | 0 0 0 0 0 -.05 -.3-.6 -.050 0 |4 | 0 0 0 0 0 -.05 -.3-.6 -.050 0 |5 | 0 0 0 0 0 -.05 -.3-.6 -.050 0 |6 | 0 0 0 0 0 -.05 -.3-.6 -.050 0 |7 | 0 0 0 0 0 -.05 -.3-.6 -.050 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 | +------------------------------------------------------------------------------+ NickOn Thu, Sep 20, 2012 at 10:45 PM, Heidi M Pitts<hpitts@unm.edu> 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 betweeneach element `i? in Mand the whole vector F. So the first vector ofdifferences would be [M1=0minus 0,0,0,0,0,.05,.3,.6,.05,0,0]. Then I wouldcalculate a second set ofdifferences, M2=0 - same F vector, and stack it underthe diff vector.I have written a simple loop to calculate eachdifference. But the errormessage I get is: ?x_male1 not found?. Seems that mycode is not recognizingthat I want the first element of M to subtract eachelement of F andpopulate the vector ?diff?. Can anyone help? Seemsthat the first issue isthat ?x_male1? is not equal to 0, the first element inthe 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/

Heidi M. Pitts Graduate Student Department of Economics University of New Mexico * * 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/

**References**:**st: Using a loop to calculate vector differences***From:*"Heidi M Pitts" <hpitts@unm.edu>

**Re: st: Using a loop to calculate vector differences***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**RE: st: Access 2010 32bit vs Stata 64bit** - Next by Date:
**Re: st: Access 2010 32bit vs Stata 64bit** - Previous by thread:
**Re: st: Using a loop to calculate vector differences** - Next by thread:
**st: how to make wmpstata recognize a global variable** - Index(es):