Thanks to Renaud Lancelot, here's a working solution:
icc<-function(x) {
v<-as.numeric(VarCorr(x)[,2])
v<-as.numeric(na.omit(v))
v^2 / sum(v^2)
}
Best,
Andy
----------------------------------------------------------------------
Andrew J Perrin - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
clists at perrin.socsci.unc.edu * andrew_perrin (at) unc.edu
On Tue, 20 May 2003, Renaud Lancelot wrote:
> Andrew Perrin wrote:
> > Sorry if this is obvious, but my S skills aren't great and I
haven't been
> > able to find it documented anywhere.
> >
> > I want to write a new function for use with lme objects; the function
will
> > simply calculate an ICC (aka "rho") for each level of a
mixed-effects
> > model. What I need for this is pretty simple:
> >
> > (c(var1..varn, residual)) / sum(c(var1..varn, residual))
> >
> > where var1..varn are the variances of the intercepts of each
level's
> > equation, and residual is the residual variance. The problem is
getting
> > access to var1..varn (or, as R generally reports it, sd1..sdn)
> > programmatically. I can get the residual standard deviation with:
> >
> > print(model$sigma)
> >
> > and I can view the standard deviations with:
> >
> > summary(model$modelStruct$lmeStruct)
> >
> > But I can't figure out how to get at the standard deviations
without the
> > human-readable print format.
> >
> > Thanks-
> > Andy Perrin
> >
> > ----------------------------------------------------------------------
> > Andrew J Perrin - http://www.unc.edu/~aperrin
> > Assistant Professor of Sociology, U of North Carolina, Chapel Hill
> > clists at perrin.socsci.unc.edu * andrew_perrin (at) unc.edu
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
>
> Here is an example:
>
> > library(nlme)
> > data(Orthodont)
> > fm <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
> > summary(fm)
> [...]
> Random effects:
> Formula: ~1 | Subject
> (Intercept) Residual
> StdDev: 1.807425 1.431592
> [...]
>
> > ## std dev.
> > fm$sigma
> [1] 1.431592
>
> > ## var-cov
> > VarCorr(fm)
> Subject = pdLogChol(1)
> Variance StdDev
> (Intercept) 3.266784 1.807425
> Residual 2.049456 1.431592
>
> > ## variance components
> > v <- sqrt(as.numeric(VarCorr(fm)[,1]))
> > v
> [1] 1.807425 1.431592
> > v/sum(v)
> [1] 0.5580165 0.4419835
>
> Best,
>
> Renaud
>
> --
> Dr Renaud Lancelot, v?t?rinaire
> CIRAD, D?partement Elevage et M?decine V?t?rinaire (CIRAD-Emvt)
> Programme Productions Animales
> http://www.cirad.fr/fr/pg_recherche/page.php?id=14
>
> ISRA-LNERV tel +221 832 49 02
> BP 2057 Dakar-Hann fax +221 821 18 79 (CIRAD)
> Senegal e-mail renaud.lancelot at cirad.fr
>
>