Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using margins after lmer #56

Closed
benwhalley opened this issue Feb 8, 2017 · 14 comments
Closed

Error when using margins after lmer #56

benwhalley opened this issue Feb 8, 2017 · 14 comments
Labels

Comments

@benwhalley
Copy link

@benwhalley benwhalley commented Feb 8, 2017

I'm not sure what sort of shape the lme4 support is supposed to be in yet so apologies if premature, but I've hit a bug which I thought I'd share:

m <- lmer(Reaction~Days+(1|Subject), data=sleepstudy)
margins(m)

produces: this error:

Error in `[[<-`(`*tmp*`, "model", value = NULL) : 
  [[<- defined for objects of type "S4" only for subclasses of environment

After poking around a little:

newd <- data_frame(Subject=330, Days=4)
build_margins(m, newd)

Produces:

Error in model[["coefficients"]] : this S4 class is not subsettable
In addition: Warning message:
In marginal_effects.merMod(model = model, data = data, type = type,  :
  factor variables are not handled as factor for models of class 'merMod'

Which is referring to something here https://github.com/leeper/margins/blob/f44c149465d0497175a878d9e8db17c135632c2f/R/delta.R

@leeper

This comment has been minimized.

Copy link
Owner

@leeper leeper commented Feb 8, 2017

This is not currently supported. If you want to poke around and make some suggestions on lmer support, I'm open to it.

@leeper leeper added the enhancement label Mar 23, 2017
@leeper

This comment has been minimized.

Copy link
Owner

@leeper leeper commented Apr 18, 2017

I think this should be working now:

library("lme4")
m <- lmer(Reaction~Days+(1|Subject), data=sleepstudy)
(mar <- margins(m))
## Average marginal effects
##   Days
##  10.47
## Warning message:
## In marginal_effects.merMod(model = model, data = data, type = type,  :
##   factor variables are not handled as factor for models of class 'merMod'
summary(mar)
##  factor     AME SE  z  p lower upper
##    Days 10.4673 NA NA NA    NA    NA
@jeffmax

This comment has been minimized.

Copy link

@jeffmax jeffmax commented Apr 27, 2017

I've gotten the following error when using margins on a random intercept model (1 | location) with v 0.3.5

Error in model[["weights"]] : this S4 class is not subsettable
leeper added a commit that referenced this issue Apr 27, 2017
@leeper

This comment has been minimized.

Copy link
Owner

@leeper leeper commented Apr 27, 2017

@jeffmax Ah, sorry. Apparently the margins.merMod() method was not exported, so this was hitting the default (which won't work). Can you reinstall from Github and try again?

@jeffmax

This comment has been minimized.

Copy link

@jeffmax jeffmax commented Apr 27, 2017

@leeper, I am still getting the same error (confirmed help says 0.3.6). Thanks for looking into this so fast.

@leeper

This comment has been minimized.

Copy link
Owner

@leeper leeper commented Apr 27, 2017

@jeffmax what's the class of the object you're passing to margins()?

@jeffmax

This comment has been minimized.

Copy link

@jeffmax jeffmax commented Apr 27, 2017

> class(m_int)
[1] "glmerMod"
attr(,"package")
[1] "lme4
@leeper

This comment has been minimized.

Copy link
Owner

@leeper leeper commented Apr 28, 2017

@jeffmax Can you try to make a reproducible example? The package works with examples from ? glmer so I need more information to track down what's happening here.

@tamaravdd

This comment has been minimized.

Copy link

@tamaravdd tamaravdd commented Apr 28, 2017

Hi @leeper, I am having the exact same issue. I re-downloaded margins from Github and continue to have the follow error as well even when I use the sleep study dataset.

install_github("leeper/margins")
library(margins)
m <- lmer(Reaction~Days+(1|Subject), data=sleepstudy)
(mar <- margins(m))
## Error in model[["weights"]] : this S4 class is not subsettable`

Thank you so much for all your work on this package!

@fkeusch01

This comment has been minimized.

Copy link

@fkeusch01 fkeusch01 commented Nov 24, 2017

Hi @leeper, this seems to be an awesome package. Much appreciate your work on this!
Unfortunately, I have the same issue like @jeffmax and @tamaravdd. Is there any update on the what might cause the error message?

@zakku78

This comment has been minimized.

Copy link

@zakku78 zakku78 commented Feb 28, 2018

yeh @leeper it is really great to see this package because it can get us average marginal effects. Please share if you have any updates. I am getting similar Error message
"defined for objects of type "S4" only for subclasses of environment"

@jacob-long

This comment has been minimized.

Copy link
Contributor

@jacob-long jacob-long commented Mar 18, 2018

With the latest Github versions of margins and prediction, I do not get any errors using the first example models from lmer and glmer.

I do get (for model fm1 from lmer examples):

Warning messages:
1: In marginal_effects.merMod(model = model, data = data, variables = variables,  :
  factor variables are not handled as factor for models of class 'merMod'
2: In predict.merMod(model, newdata = out, type = type, ...) :
  unused arguments ignored

and (for model gm1 from glmer examples):

Warning messages:
1: In marginal_effects.merMod(model = model, data = data, variables = variables,  :
  factor variables are not handled as factor for models of class 'merMod'
2: In predict.merMod(model, newdata = out, type = type, ...) :
  unused arguments ignored
3: In predict.merMod(model, newdata = out, type = type, ...) :
  unused arguments ignored
4: In predict.merMod(model, newdata = out, type = type, ...) :
  unused arguments ignored
5: In predict.merMod(model, newdata = out, type = type, ...) :
  unused arguments ignored

But at first glance these warnings are more or less spurious in terms of their bearing on the accuracy of the output.

@chrishanretty

This comment has been minimized.

Copy link

@chrishanretty chrishanretty commented Apr 19, 2018

The latest version on Github seems to have regressed from when @jacob-long commented. MWE:

library(margins)
library(lme4)
library(lattice)

gm1 <- glmer(cbind(incidence, size - incidence) ~ period +
                 (1 | herd),
             data = cbpp,
             family = binomial)

m <- margins(gm1, data = cbpp)
traceback()
sessionInfo()

and the output

> traceback()
5: terms(model$model)
4: find_terms_in_model.default(model, variables = variables)
3: find_terms_in_model(model, variables = variables)
2: margins.default(gm1, data = cbpp)
1: margins(gm1, data = cbpp)
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 17.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lattice_0.20-35 lme4_1.1-14     Matrix_1.2-11   margins_0.3.22 

loaded via a namespace (and not attached):
 [1] minqa_1.2.4         MASS_7.3-47         compiler_3.4.2     
 [4] tools_3.4.2         Rcpp_0.12.13        splines_3.4.2      
 [7] nlme_3.1-131        grid_3.4.2          data.table_1.10.4-3
[10] nloptr_1.0.4        prediction_0.3.2   
`
leeper added a commit that referenced this issue May 11, 2018
@leeper

This comment has been minimized.

Copy link
Owner

@leeper leeper commented May 11, 2018

Thanks all for your bug reports. I'm about to push an update that will include much more extensive unit testing for mixed effects models so that we shouldn't encounter these regression issues again. I believe all of these examples will now run successfully. Let me know if not.

@leeper leeper closed this May 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.