Noobie question, regression across levels
AllenL wrote:
This is what I ended up using:
Data.subset<-data.frame(PlotFinal,YearFinal,BioFinal,ritFinal)
###Subset of main dataset
Data.length<-sapply(split(Data.subset,PlotFinal),nrow) ### The
number of data points in each plot
Data.sub<-split(Data.subset,PlotFinal) ##Split Data.subset by
PlotFinal
Good.levels<-Data.sub[Data.length>10] ##Take only those Plots that
have >10 years of observation
lmfun<-function(Good.levels)
{lm(ritFinal~BioFinal+YearFinal,data=Good.levels)} ##Set up the
###regression function
Potentially slightly confusing to have the same name within the lmfun
function ... I probably would
have written this as
lmfun<-function(dat) {lm(ritFinal~BioFinal+YearFinal,data=dat)}
but the results would be identical.
lmList<-lapply(Good.levels,lmfun) ###Apply above function to all "good levels" coef.List<-lapply(lmList,coef) ###List of the coefficients of EACH above regression This seems to have worked beautifully. So, my current problem is extracting the coefficients from "coef.List" (and I think this is getting to the roof of my general confusion). coef.List seems to be a list of a list of lm objects, right? How come them I cannot call them by their position directly (ie. coef.List[1] gives me the first lm object. I cannot seem to call the first coefficient in this object (the intercept).) My goal is to have a bunch of new lists, each one listing a subset of coefficients from each regression (ie. one will be a list of all the intercepts, another the mean of all the "YearFinal" effects, etc).
lmList (not coef.List) should be a list of lm objects, coef.List is a list of vectors. I actually think your confusion is simpler/more fundamental. If you want the first element within the first element of coef.List you actually need coef.List[[1]][1], not coef.List[1][1] ... You should also consider sapply instead of lapply above, that will give you a matrix of coefficients (which may be easier to deal with)
View this message in context: http://www.nabble.com/Noobie-question%2C-regression-across-levels-tp21020222p21073282.html Sent from the R help mailing list archive at Nabble.com.