Skip to content
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

emtrends: max.degree > 1 does not work with "overall" spec #197

Closed
mattansb opened this issue May 19, 2020 · 4 comments
Closed

emtrends: max.degree > 1 does not work with "overall" spec #197

mattansb opened this issue May 19, 2020 · 4 comments

Comments

@mattansb
Copy link

mattansb commented May 19, 2020

Getting an error when using the "overall" spec in emtrends and setting max.degree > 1:

library(emmeans)

model <- lm(Sepal.Length ~ poly(Petal.Length,2), data = iris)

# works
emtrends(model, ~ 1, "Petal.Length", max.degree = 1)
#>  1       Petal.Length.trend     SE  df lower.CL upper.CL
#>  overall              0.448 0.0181 147    0.413    0.484
#> 
#> Confidence level used: 0.95


# does not work
emtrends(model, ~ 1, "Petal.Length", max.degree = 2)
#> Error in emmeans(object = new("emmGrid", model.info = list(call = lm(formula = Sepal.Length ~ : No variable named 1 in the reference grid

Created on 2020-05-19 by the reprex package (v0.3.0)

@rvlenth
Copy link
Owner

rvlenth commented May 19, 2020

Thanks for the report. It turns out this error isn't actually in emtrends(). It occurs in the very last line when it runs emmeans() on the result:

> emtrends(model, ~ 1, "Petal.Length", max.degree = 2)
 Error in emmeans(object = new("emmGrid", model.info = list(call = lm(formula = Sepal.Length ~  : 
  No variable named 1 in the reference grid 

> ### The reference grid is saved before the error occurs:
> .Last.ref_grid
'emmGrid' object with variables:
    Petal.Length = 3.758
    degree = linear, quadratic

> ### We can run `emmeans() on this result:
> emmeans(.Last.ref_grid, ~1|degree)
degree = linear:
 Petal.Length.trend     SE  df lower.CL upper.CL
             0.4474 0.0180 147   0.4119    0.483

degree = quadratic:
 Petal.Length.trend     SE  df lower.CL upper.CL
             0.0815 0.0132 147   0.0554    0.108

Confidence level used: 0.95 

> ### ... but not the way it is called from emtrends()
> emmeans(.Last.ref_grid, ~1, by = "degree")
Error in emmeans(.Last.ref_grid, ~1, by = "degree") : 
  No variable named 1 in the reference grid

Gotta go run errands. But I will track that down and report again.

@rvlenth
Copy link
Owner

rvlenth commented May 19, 2020

OK, It's fixed:

> emtrends(model, ~ 1, "Petal.Length", max.degree = 2)
degree = linear:
 Petal.Length.trend     SE  df lower.CL upper.CL
             0.4474 0.0180 147   0.4119    0.483

degree = quadratic:
 Petal.Length.trend     SE  df lower.CL upper.CL
             0.0815 0.0132 147   0.0554    0.108

Confidence level used: 0.95 

> ### This would have worked before...
> emtrends(model, ~degree, "Petal.Length", max.degree = 2)
 degree    Petal.Length.trend     SE  df lower.CL upper.CL
 linear                0.4474 0.0180 147   0.4119    0.483
 quadratic             0.0815 0.0132 147   0.0554    0.108

Confidence level used: 0.95 

> ### But this still errors, because we don't create a `degree` variable when it is only 1
> emtrends(model, ~ degree, "Petal.Length", max.degree = 1)
 Error in emmeans(object = new("emmGrid", model.info = list(call = lm(formula = Sepal.Length ~  : 
  No variable named degree in the reference grid 

rvlenth added a commit that referenced this issue May 19, 2020
Plus loose ends from previous commit
@rvlenth
Copy link
Owner

rvlenth commented May 19, 2020

Going ahead and closing this. Thanks again for the report and your usual easy reproducibility of the bug in question.

@rvlenth rvlenth closed this as completed May 19, 2020
@mattansb
Copy link
Author

@rvlenth Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants