# Re: st: calculating statistics for multiple variables

 From "Austin Nichols" To statalist@hsphsun2.harvard.edu Subject Re: st: calculating statistics for multiple variables Date Mon, 25 Aug 2008 14:20:30 -0400

```Jon--
If all you want is means and variances you can compute them from the
indiv stats on v1 and v2.  Is that all you want?  But it's not clear
to me what restriction you are imposing with the prohibition on
"calculating it by its parts" below.  You can also do a "reshape" on
the fly in Mata by vectorizing the matrix of v1 and v2 (help mf_vec)
then calculate the mean and variance for the vector.

clear
input v1 v2
1  2
2  4
3  6
4  8
end
* method 1
qui su v1
loc m1=r(mean)
loc v1=r(Var)
loc n1=r(N)
qui su v2
loc m2=r(mean)
loc v2=r(Var)
loc n2=r(N)
loc ss=`v1'*(`n1'-1)+`m1'^2*`n1'+`v2'*(`n2'-1)+`m2'^2*`n2'
di (`m1'*`n1'+`m2'*`n2')/(`n1'+`n2')
di `ss'/(`n1'+`n2'-1)-(`m1'*`n1'+`m2'*`n2')^2/(`n1'+`n2')/(`n1'+`n2'-1)
* method 2
mata:meanvariance(vec(st_data(.,("v1", "v2"))))
* method 3
g i=_n
qui reshape long v, i(i)
su v
di r(sd)^2

On Thu, Aug 21, 2008 at 3:18 PM, Jon Schwabish <jschwabi@yahoo.com> wrote:
> Does anyone know of a quick way of calculating sample statistics for multiple variables without reshaping the data? For example, if I have three variables (columns), how do I calculate means, standard deviations, etc without reshaping the data into one variable (or, obviously, doing the calculations by hand)?
>
> For example, calculate the standard deviation of v1 and v2, by treating them as one variable. Thus, treat
> v1 v2
> 1  2
> 2  4
> 3  6
> 4  8
>
> as
>
> v1
> 1
> 2
> 3
> 4
> 2
> 4
> 6
> 8
>
> (instead of calculating it by its parts).
>
> Thanks,
> jon
*
*   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/
```