[R] Problem with storing a sequence of lmer() model fit into a list
Rolf Turner
r.turner at auckland.ac.nz
Mon Jun 22 23:07:14 CEST 2009
(a) Your code is unnecessarily convoluted.
(b) The example of things *not* working is not reproducible. (Read
the posting guide!!!)
(c) Nonetheless the phenomenon you describe is weird/interesting.
On my system, the following runs without error:
fit.list <- NULL
a <- factor(rep(1:10,each=20))
set.seed(42)
for(i in 1:10) {
x1<-runif(200)
x2<-rnorm(200)
y <- x1+x2
testdf<-data.frame(y=y, x1=x1)
fit <- lm(y~x1+a,data=testdf)
fit.list[[i]] <- fit
}
However if the call to lm() is replace by a call to lmer()
library(lme4)
fit.list <- NULL
a <- factor(rep(1:10,each=20))
set.seed(42)
for(i in 1:10) {
x1<-runif(200)
x2<-rnorm(200)
y <- x1+x2
testdf<-data.frame(y=y, x1=x1)
fit <- lmer(y~x1 + (1|a),data=testdf)
fit.list[[i]] <- fit
}
I get the same error that you reported, namely
Error in fit.list[[i]] <- fit :
invalid type/length (S4/0) in vector allocation
Finally if fit.list is initialized to an empty list (which is the way
it *should*
be initialized anyway) rather than to NULL, then things appear to
work smoothly even
with a call to lmer():
library(lme4)
fit.list <- list()
a <- factor(rep(1:10,each=20))
set.seed(42)
for(i in 1:10) {
x1<-runif(200)
x2<-rnorm(200)
y <- x1+x2
testdf<-data.frame(y=y, x1=x1)
fit <- lmer(y~x1 + (1|a),data=testdf)
fit.list[[i]] <- fit
}
So a ``fix'' for the problem would seem to be to start with fit.list
being an empty list,
rather than NULL.
But why the problem shows up only with calls to lmer() is completely
mysterious to me.
Perhaps others will be able to shed light.
HTH
cheers,
Rolf Turner
On 23/06/2009, at 6:34 AM, Sean Zhang wrote:
> Dear R-helpers:
> May I ask a question related to storing a number of lmer model fit
> into a
> list.
>
> Basically, I have a for-loop (see towards the bottom of this email)
> in the loop, I am very sure that the i-th model fit (i.e.,fit_i) is
> successfully generated and the character string (i.e., tmp_i) is
> created
> correctly.
> The problem stems from the following line in the for-loop
> #trouble making line below
> fit.list[[tmp_i]] <- fit_i
>
>
> I tried the following example which stores glm() model fit without a
> problem.
> #the following code can store glm() model fit into a list
> ---------------------------------------------------
> x1<-runif(200)
> x2<-rnorm(200)
> y<-x1+x2
> testdf<-data.frame(y=y, x1=x1, x2=x2)
> indepvec<-c("x1","x2")
> fit.list<-NULL
> fit_1<-glm(y~x1,data=testdf)
> fit_2<-glm(y~x2,data=testdf)
> fit.list[[paste('fit_',indepvec[1],sep='')]]<-fit_1
> fit.list[[paste('fit_',indepvec[12],sep='')]]<-fit_2
> ----------------------------------------------------
> so why cannot I store lmer() model fit in a list?
> Would someone kindly explain to me what the R error message(last
> line of
> this email) really means?
> Your kind help will be highly appreciated!
>
> -Sean
>
> #the following for-loop intends to store lmer() random poisson
> model output
> into list (fit.list), it does not work
> ----------------------------------------------------------------------
> -----
> fit.list<-NULL
> for (i in seq_along(depvar_vec))
> {
> #I found that s_sex, ses1 and race are not useful
> fit_i <- lmer(as.formula(gen.ranpoisson.fml.jh(depvar_vec[i],
> offsetvar ,factorindepvars, nonfactorindepvars ,ranintvar )),
> family=quasipoisson(link="log"),verbose=F, data=indf)
> tmp_i<-paste('ranpoi_', depvar_vec[i], sep='')
> fit.list[[tmp_i]] <- fit_i
> #assign also does not work
> #assign(fit.list$parse(text = tmp_i), fit_i)
> }
> ---------------------------------------------------------------
>
>
> #R gives the following error message.
>
> Error in fit.list[[tmp_i]] <- fit_i : invalid type/length (S4/0) in
> vector
> allocation
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list