[R] Extract Variance Components

robgriffin247 rg.rforum at hotmail.co.uk
Mon May 14 10:57:28 CEST 2012


Hi,
I'm still having problems putting the variance components of my model in to
a data frame, it is a continuation of this discussion,
http://r.789695.n4.nabble.com/ANOVA-problem-td4609062.html, but now focussed
on the problem of extracting variance components. I have got my mixed
effects model now 

/narrow$line<-as.factor(narrow$line)
rg.lmer <- lapply(split(narrow,
narrow$gene),function(x)lmer(value~sex+(1|line:sex)+(1|line),data=x)) 
str(lme4::VarCorr(rg.lmer[[1]]))
(rg.lmer[[1]])/

and want the variance components from each model (one per gene) put in to a
data frame, this works for one gene:

/## SUCCESS! for one gene...
alpha<-as.matrix(lme4::VarCorr(rg.lmer[[1]]))
beta<-data.frame(c(alpha,attr(alpha,"sc")^2))
colnames(beta)<-c("sexline","line","residual")
beta$varcomp<-beta$residual/sum(beta$sexline,beta$line,beta$residual)
rownames(beta)<-gene.list[[1]] #gene.list is just a list of all my focal
genes/

Produces this:
 	 	row.names    #	sexline	                #line                   
#residual	        #varcomp	#gene
1	        (Intercept)	#0.005087154	#0.007013756	#0.01839387	#0.6031809
#CG10000

I have thought about creating a loop to then produce my big full data frame
but I can't get it to work (nor can my friend at work who is really good
with R) we have tried numerous ways and are now at our wits end... 

/## try to loop for all genes...

newobj <- matrix(nrow=0, ncol=3)

        for (i in 1:nlevels(narrow$gene)) {
        x<-as.matrix(lme4::VarCorr(rg.lmer[[i]]))
        newobj <- data.frame(c((lme4::VarCorr(rg.lmer[[i]])), attr(narrow,
"sc")^2))}

newobj<-data.frame(newobj, row.names=levels(narrow$gene))

colnames(newobj)<-c("sexline", "line", "residual")/


any suggestions welcome, plus I am fairly new to creating loops so
annotations are really helpful, that way I will learn how to do these things
for myself next time, Thanks
Rob

--
View this message in context: http://r.789695.n4.nabble.com/Extract-Variance-Components-tp4629895.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list