# Re: st: working with a vector and scalars (fwd)

 From "Cathy L. Antonakos" To statalist@hsphsun2.harvard.edu Subject Re: st: working with a vector and scalars (fwd) Date Mon, 9 Jun 2008 20:01:16 -0400 (EDT)

Several syntax lines wrapped in the earlier post. Here they are again, with line breaks.

----

*Calculate reliabilities for place/hour groups.

egen N = count(crowdindx), by(place hour)
gen nr=1/N
mean nr, over(place hour)
mat b=e(b)
mat N=b'
matrix rownames N = `e(over_labels)'

loneway crowdindx placeXhr

matrix sj = N * (r(sd_w) * r(sd_w))

local tau = r(sd_b) * r(sd_b)

matrix t = J(48,1,`tau')

matrix J = t,sj

matrix rownames J = `e(over_labels)'

matrix colnames J = tau sigma_nj

preserve
drop _all
svmat J, names(col)
tempfile mat
save `mat'
use `mat'
gen lambda_j = tau / (tau + sigma_nj)
restore

*End of reliability analysis.

---------- Forwarded message ----------
Date: Mon, 9 Jun 2008 19:48:09 -0400 (EDT)
From: Cathy L. Antonakos <cathya@umich.edu>
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: working with a vector and scalars

Here's a solution to my question about working with vectors and scalars. Thank you, Austin Nichols, for providing initial code and a hint about difficulty generating element-wise reciprocals.

I initially had a problem using the "placeXhr" variable in the -mean- command, because "placeXhr" is a string variable. That problem is solved by using both "place" and "hour" variables in -mean-.

The reliabilities in the sub-groups turned out to be very similar.

Cathy
----

*Calculate reliabilities for place/hour groups.

egen N = count(crowdindx), by(place hour)
gen nr=1/N
mean nr, over(place hour)
mat b=e(b)
mat N=b'
matrix rownames N = `e(over_labels)'

loneway crowdindx placeXhr
matrix sj = N * (r(sd_w) * r(sd_w)) local tau = r(sd_b) * r(sd_b) matrix t = J(48,1,`tau') matrix J = t,sj matrix rownames J = `e(over_labels)'
matrix colnames J = tau sigma_nj

preserve
drop _all
svmat J, names(col)
tempfile mat
save `mat'
use `mat'
gen lambda_j = tau / (tau + sigma_nj)
restore

*End of reliability analysis.

On Mon, 9 Jun 2008, Cathy L. Antonakos wrote:

Date: Mon, 9 Jun 2008 08:52:07 -0400 (EDT)
From: Cathy L. Antonakos <cathya@umich.edu>
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: working with a vector and scalars

You're right. The "d" in the equation should be "s" (sigma-squared). I can get the reliability reported by -loneway- for the overall sample mean, but I want the reliabilities for each group.

Yes, I think it would be easier in Mata, but I don't know how to do that.

When I ran the syntax you provided, the -mean- command gave an error:

. mean nr, over(placeXhr)
no observations
r(2000);

If I can figure out that error, I think the approach will work.

On Sun, 8 Jun 2008, Austin Nichols wrote:

Date: Sun, 8 Jun 2008 21:00:38 -0400
From: Austin Nichols <austinnichols@gmail.com>
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: working with a vector and scalars

Cathy L. Antonakos <cathya@umich.edu>
I take it you don't want the reliability reported by -loneway-? Not
sure what you want from your description... but I don't think that
equation looks right, and I don't have the ref handy. Can you confirm
that t cancels out? Is your calculated s equiv to d in your formula?
I don't see where you've tried to calculate something and gotten an
error, but try:

egen N = count(crowdindx), by(placeXhr)
g nr=1/N
mean nr, over(placeXhr)
mat b=e(b)
mat N=b'
loneway crowdindx placeXhr
scalar t = r(sd_b) * r(sd_b)
scalar s = r(sd_w) * r(sd_w)
mat lambda = s*N
mat li lamdba

But if you need to calculate the element-wise reciprocal, it's easier in Mata...

On Sun, Jun 8, 2008 at 8:23 PM, Cathy L. Antonakos <cathya@umich.edu> wrote:

```I am working with scalars and a vector to estimate sample mean reliability
by group, using a formula from Bryk & Raudenbush (1992) Hierarchical Linear
Models:

lambda-j = t / [ t / (d /Nj)]

lambda-j is the reliability estimate for group j, t is between-group
variance, d is within-group variance, and Nj is the group N.

Nj is a vector. The other parameters (t, d) are scalars.

Here's what I tried using -matrix- and -scalar-. The vector comes from a
collapsed dataset. The scalars come from -loneway- using the original
(uncollapsed) dataset. I have no trouble producing the vector and scalars,
but can't figure out how to get them into the same matrix or file together
so I can calculate the reliability esimates.

Any help will be much appreciated.

Cathy Antonakos

--------------
. bys placeXhr: egen N = count(crowdindx)
. preserve
. collapse (max) N, by(placeXhr)
. mkmat N
. levelsof placeXhr, local(l)
. matrix rownames N = `l' . matrix list N

N[49,1]
N
2_10    2
2_11   81
2_12  150
2_13  340
2_14  138
2_15   82
2_16  102
2_17  119
2_18  288
2_19  160
2_8   48
2_9  265
3_10  166
3_11   71
3_12   63
3_13   25
3_14   53
3_15   68
3_16   44
3_17   61
3_18   33
3_19   35
3_8   36
3_9   16
6_10   33
6_11   69
6_12   82
6_13   30
6_14   87
6_15   90
6_16   90
6_17   92
6_18   63
6_19   79
6_8   86
6_9   41
9_10   57
9_11  135
9_12  113
9_13   83
9_14  152
9_15  168
9_16  160
9_17  151
9_18   97
9_19  147
9_8  216
9_9  166
9_9  190

. restore

. *create matrices from -loneway- parameter estimates.
. preserve
. loneway crowdindx placeXhr

One-way Analysis of Variance for crowdindx: Crowding Index (wtd. sum)

Number of obs =      5121
R-squared =    0.5384

Source                SS         df      MS            F     Prob > F
-------------------------------------------------------------------------
Between placeXhr       682650.69     47    14524.483    125.87     0.0000
Within placeXhr        585364.53   5073    115.38824
-------------------------------------------------------------------------
Total                  1268015.2   5120    247.65922

Intraclass       Asy.
correlation      S.E.       [95% Conf. Interval]
------------------------------------------------
0.54152     0.06135       0.42126     0.66177

Estimated SD of placeXhr effect         11.67412
Estimated SD within placeXhr            10.74189
Est. reliability of a placeXhr mean      0.99206
(evaluated at n=105.73)

. return list

scalars:
r(lb) =  .4212643949911408
r(ub) =  .6617675540515819
r(se) =  .0613539740927638
r(sd_w) =  10.74189173110937
r(sd_b) =  11.67412402231762
r(rho_t) =  .9920556043556278
r(rho) =  .5415159745213614
r(N) =  5121

. scalar t = r(sd_b) * r(sd_b) . scalar s = r(sd_w) * r(sd_w)
. restore

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```