Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: vectors in STATA

From   "Nick Cox" <>
To   <>
Subject   st: RE: Re: vectors in STATA
Date   Fri, 13 Dec 2002 11:13:45 -0000

Alejandro Riaņo

> > I used to program in Gauss, that's why I still think
> > about matrixes and vectors when I'm doing my own
> > programs. My question is the following
> >
> > I want to store the correlation between 2 variables by
> > each pair of countries and then store this number(that
> > would be r(rho)) in a vector, so at the end I would
> > have a column with the correlation for each in pair of
> > countries as a position in this vector.
> >
> > Do you know how can i do this in STATA ???

Scott Merryman

> There is probably a better way to do this, but here is one method
> levels countries, local(levels)
> matrix A = 1
> foreach x of local levels {
>  qui corr var1 var2  if rep == `x'
>  scalar rho`x' = r(rho)
>  matrix a`x' = rho`x'
>  matrix A = A \ a`x'
>  }
> matdelrc A, r(1)

Comment on Scott's posting

Scott shows how to build up a solution
more or less from first principles,
which is always worth seeing.

To help understand his posting, note
that -levels- is user-written and on SSC
and that -matdelrc- is also user-written
and is part of a package issued in STB-50.

-matdelrc- could be avoided by using -nullmat()-.

Here is a shorter version (assuming i.a.
that he was mixing examples):

levels countries, local(levels)
foreach x of local levels {
	qui corr var1 var2  if countries == `x'
	matrix A = nullmat(A) \ r(rho)

and you will probably want to follow
with something like

matrix rownames A = `levels'

Alternative approach

Check out -vectorof- and
-matrixof-. They are both in the
-matrixof- package on SSC. Example:

. vectorof corr headroom-gear, y(mpg) r(rho) matname(corr)

produces a vector of correlations between a set of
variables and -mpg-.

But you may still need to program
your own, depending on your data structure.
At worst, this would be two nested -foreach- loops.


*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index