[R] Extracting level-1 variance from lmer()
David Afshartous
dafshartous at med.miami.edu
Tue Feb 5 20:37:05 CET 2008
All,
How does one extract the level-1 variance from a model fit via lmer()?
In the code below the level-2 variance component may be obtained via
subscripting, but what about the level-1 variance, viz., the 3.215072 term?
(actually this term squared) Didn't see anything in the archives on this.
Cheers,
David
> fm <- lmer( dv ~ time.num*drug + (1 | Patient.new), data=dat.new )
> VarCorr(fm)
$Patient.new
1 x 1 Matrix of class "dpoMatrix"
(Intercept)
(Intercept) 8.519916
attr(,"sc")
scale
3.215072
> VarCorr(fm)[[1]][1]
[1] 8.519916
> VarCorr(fm)[[2]][1]
Error in VarCorr(fm)[[2]] : subscript out of bounds
##########################################################
set.seed(500)
n.timepoints <- 4
n.subj.per.tx <- 20
sd.d <- 5;
sd.p <- 2;
sd.res <- 1.3
drug <- factor(rep(c("D", "P"), each = n.timepoints, times =
n.subj.per.tx))
drug.baseline <- rep( c(0,5), each=n.timepoints, times=n.subj.per.tx )
#Patient <- rep(1:(n.subj.per.tx*2), each = n.timepoints)
Patient.baseline <- rep( rnorm( n.subj.per.tx*2, sd=c(sd.d, sd.p) ),
each=n.timepoints )
time.baseline <- rep(1:n.timepoints,n.subj.per.tx*2)*as.numeric(drug=="D")
dv <- rnorm( n.subj.per.tx*n.timepoints*2,
mean=time.baseline+Patient.baseline+drug.baseline, sd=sd.res )
dat.new <- data.frame(drug, dv)
dat.new$Dind <- as.numeric(dat.new$drug == "D")
dat.new$Pind <- as.numeric(dat.new$drug == "P")
dat.new$time.num = rep(1:n.timepoints, n.subj.per.tx*2)
dat.new$Patient.new = rep(1:20, each=8)
More information about the R-help
mailing list