[R] Extracting the names of coefficients of random effects

Ben Bolker bbolker at gmail.com
Sun Apr 20 02:54:10 CEST 2014


Brian Willis <b.h.willis <at> bham.ac.uk> writes:

> 
> Hi All,
> I need to be able to manipulate the names of the coefficients from
> *ranef()*.
> 
> If there is any missing data when fitting a mixed model using lmer, no
> estimate is returned for the associated level for that random effect. Thus
> if the data input for regions had levels 
> 	*Region*
>         Bolton
> 	Bradford                    
>         Cambridge
>         Durham						 
> and there was missing data on Bradford then 
> * ranef(model)*					gives 
> 	                                                     (Intercept)
> Bolton:                               	          -0.0981763413
> Cambridge		                                   0.0151102347
> Durham						   0.1837142259


  I think you want to use rownames():

library(lme4)
d <- expand.grid(f=factor(LETTERS[1:10]),rep=1:10)
d$y <- rnorm(100)
m <- lmer(y~(1|f),data=d)

rownames(ranef(m)[[1]])

> This becomes a problem if I want to use *predict( )* on new data where there
> is no missing data on Bradford. In such an instance
> 
> 	*predict (model, newdata = newInput) *
> gives the following error message 
> 
>              ‘Error in (function (x, n)  : new levels detected in newdata’
> 
> I could get round this by checking the Region field of the new data
> ‘newInput’ against the names of the levels of the intercept coefficients
> from* ranef().*
> However, I’m not sure how to access these since if 
> *x<- ranef(model)
> x *

  You should also check the allow.new.levels argument in
?predict.merMod, and send followups to r-sig-mixed-models at r-project.org.




More information about the R-help mailing list