Brian Willis
2014-Apr-19 15:51 UTC
[R] Extracting the names of coefficients of random effects
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
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 *
This gives the same output above, but
*x[1,1]* only returns the numeric value -0.0981763413
x is a dataframe with only one field (column) with numeric values and the
names of the levels are not present or identified. There must be a variable
which defines ?Bolton?, Bradford? etc since if I use the write.table
function
*write.table (x, file="/desktop/Dummy.csv", sep = ",",
col.names = NA,
qmethod = "double")*
This outputs both the names (?Bolton?, Bradford?..) and their corresponding
numeric values to a spreadsheet.
Does anyone know how to do this without resorting to outputting to a
spreadsheet?
Regards,
Brian H Willis
Health and Population Sciences
University of Birmingham
--
View this message in context:
http://r.789695.n4.nabble.com/Extracting-the-names-of-coefficients-of-random-effects-tp4689109.html
Sent from the R help mailing list archive at Nabble.com.
Ben Bolker
2014-Apr-20 00:54 UTC
[R] Extracting the names of coefficients of random effects
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.1837142259I 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.