antR
antR

Reputation: 907

Error with `glmer{lme4}`: cannot coerce class "family" to a data.frame

I am running a generalized linear model with random effects using the glmer() function from the package lme4.

The model code looks like this:

mod6 <- glmer((Ndifference+74337) ~ netm1011 + d1011 + 
           b0001 + (1|region), Gamma(link = "identity"))

Ndifference is count data of population differences between 200 and 2010 of 50 states (and DC). There is one negative value (Michigan at -74336) so I added a constant to make sure my response was all positive.

All the predictors (aside from the random effect of region) are proportions or percentages. Netm1011 (rate of immigration to the states in 2010) and d1011 (rate of deaths per 1000 people) both have several negative values. B0001 contains all positive proportions (birth rate/1000 people).

When I run the model I keep getting this error:

Error in as.data.frame.default(data) : 
  cannot coerce class ""family"" to a data.frame

I've tried different families of distributions as well (Gamma, inverse.gaussian). What exactly does this error code mean?

Upvotes: 1

Views: 918

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73385

glmer(formula, data = NULL, family = gaussian, control = glmerControl(),
      start = NULL, verbose = 0L, nAGQ = 1L, subset, weights, na.action,
      offset, contrasts = NULL, mustart, etastart,
      devFunOnly = FALSE, ...)

You want to specify formal argument, too:

family = Gamma(link = "identity")

When no formal argument is specified, argument is matched by position. The 2nd argument is data, but you passed Gamma function to it. So model.frame can not work.

This may be a specific issue to glmer. Compare with glm:

glm(formula, family = gaussian, data, weights, subset,
    na.action, start = NULL, etastart, mustart, offset,
    control = list(...), model = TRUE, method = "glm.fit",
    x = FALSE, y = TRUE, contrasts = NULL, ...)

The 2nd argument is family and data is the 3rd. So there will be no problem is you do:

glm((Ndifference+74337) ~ netm1011 + d1011, Gamma(link = "identity"))

Also consider gam from mgcv:

gam(formula,family=gaussian(),data=list(),weights=NULL,subset=NULL,
    na.action,offset=NULL,method="GCV.Cp",
    optimizer=c("outer","newton"),control=list(),scale=0,
    select=FALSE,knots=NULL,sp=NULL,min.sp=NULL,H=NULL,gamma=1,
    fit=TRUE,paraPen=NULL,G=NULL,in.out,drop.unused.levels=TRUE,
    drop.intercept=NULL,...)

We see the family is at 2nd position, too.

Upvotes: 4

Related Questions