# st: intraclass correlations

 From David Airey To statalist@hsphsun2.harvard.edu Subject st: intraclass correlations Date Thu, 19 Feb 2004 10:31:29 -0600

Previously May Boggess (mboggess@stata.com) posted the following answer to how to calculate the intraclass correlations with 2 random factors. I had two questions about this code. The first in interspersed and the second follows.

OK, now for two random factors. For these formulas I must turn to the
comprehensive experimental design text by Winer et. al. "Statistical
Principles of Experimental Design" (page 413). The formula is what you
would expect it to be: ICC=variance of A/(total variance). So again, we
count the number of repetitions, and this time the number of levels of
each factor, compute the variance components, and then ICC.

This time I am going to generate my own balanced dataset. Again, if your
design is not balanced, you just have to work harder to get the variance
components (the formulas for this are in Winer).

*----two random factors--------------*
*--- generate balanced data

clear
set obs 36
generate A=int(_n/6+1)
replace A=1 if A==7
table A
bysort A: generate B=int(_n/2+1)
replace B=1 if B==4
generate response=uniform()

*----------------
local depvar="response"
local A="A"
local B="B"

table `A' `B'
local N=r(N)
display `N'
egen tag=tag(`A')
egen sum=sum(tag)
local p= sum[1]
display `p'

drop sum tag
egen tag=tag(`B')
egen sum=sum(tag)
local q= sum[1]
display `q'
drop sum tag

local n=`N'/(`p'*`q')
display `n'

anova `depvar' `A' `B' `A'*`B'
test `A'
local MSA=r(ss)/r(df)
display `MSA'
display `MSE'
Here May has picked up MSE from the test of A. Should she not have picked up MSE immediately after the anova command via the ereturn list?

test `B'
local MSB=r(ss)/r(df)
display `MSB'
test `A'*`B'
local MSAB=r(ss)/r(df)
display `MSAB'

local sigA=(`MSA'-`MSAB')/(`n'*`q')
local sigB=(`MSB'-`MSAB')/(`n'*`p')
local sigAB=(`MSAB'-`MSE')/`n'

local ICC=(`sigA')/(`sigA'+`sigB'+`sigAB'+`MSE')
display `ICC'

*---------------------

Notice, we have to work a bit harder to get the mean squares for each of
the factors this time, since they are not in the saved results after
-anova-. But, that is where -test- comes in handy, because they are in
the results after -test-.
Secondly, I came across a more general formula for the intraclass correlation in a text by Kirk:

phat_I = (F - 1) / ( (n - 1) + F)

I could not quite understand what to put in "n" for above. In a one random factor ANOVA it's the number of observations in a given level of the random factor. So if you have 10 levels of a random factor with 5 obs per level, "n" is 5. Does this mean that when random factors A and B are present, that "n" is the number of observations per level of A, per level of B, and per A*B combination when using this formula? So that say we have the following table, where animal is random and side is fixed, then "n" for animal is 4 while "n" for side*animal is 2 when using the formula above?

-------------------------------
| side
animal | left right Total
----------+--------------------
1 | 2 2 4
3 | 2 2 4
4 | 2 2 4
5 | 2 2 4
6 | 2 2 4
8 | 2 2 4
9 | 2 2 4
10 | 2 2 4
|
Total | 16 16 32
-------------------------------

-Dave

*
* 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/