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

`afex_plot` not fully compatible with glmm #65

Closed
mattansb opened this issue Jan 16, 2019 · 5 comments
Closed

`afex_plot` not fully compatible with glmm #65

mattansb opened this issue Jan 16, 2019 · 5 comments

Comments

@mattansb
Copy link
Contributor

@mattansb mattansb commented Jan 16, 2019

When fitting a glmm model (either with lme4::glmer or with afex::mixed), I've found that I cannot use afex_plot to plot the following:

  • Original data is not plotted (I suspect due to the link-transformed scale?)
  • When trying to plot the response scale (by passing emmeans_arg = list(type = "response")), an error is produced.
> library(afex)
> library(mlmRev)
>
> gm1 <- mixed(use ~ age + I(age^2) + urban + livch + (1 | district), method = 'LRT',
+              family = binomial, data = Contraception)
>
> afex_plot(gm1,~age,emmeans_arg = list(type = "response"),data_plot = TRUE)
Aggregating data over: district
Error in FUN(X[[i]], ...) : object 'y' not found
In addition: There were 50 or more warnings (use warnings() to see the first 50)

I suspect that I am pushing afex_plot to its limit, perhaps beyond its intended use.

Thanks!

@singmann
Copy link
Owner

@singmann singmann commented Jan 17, 2019

This should be solved in the development version: devtools::install_github("singmann/afex@master")

You do not even need to pass the extra emmeans_arg argument. The following should produce a plot:

afex_plot(gm1, "urban", data_plot = TRUE)

However, it still does not print the raw data in the background as the original dv is a factor.

@mattansb
Copy link
Contributor Author

@mattansb mattansb commented Jan 17, 2019

Great, thank!

I also managed to plot the data:

library(afex)
library(mlmRev)

Contraception <- within(Contraception,use <- as.numeric(use)-1) # for plotting the data

gm1 <- mixed(use ~ age + I(age^2) + urban + livch + (1 | district), method = 'LRT',
             family = binomial, data = Contraception)

afex_plot(gm1,~age,data_plot = TRUE)
@mattansb
Copy link
Contributor Author

@mattansb mattansb commented Jan 17, 2019

Any chance that the x-axis will not automatically be treated as a factor? Would allow for plotting continuous variables on it?

@singmann
Copy link
Owner

@singmann singmann commented Jan 17, 2019

Unfortunately this is not easily possible. This really requires thinking about and constructing the plot in a very different manner. The reason is that this requires plotting a linear slope or other function, thus completely different geoms, and the usage of emtrends instead of emmeans. So it really would be quite a different function then it is today.

I think your best bet here is to use the effects package, which was developed exactly for that (but is not so convenient for factorial designs for which afex_plot was developed).

library(afex)
library(mlmRev)
gm1 <- mixed(use ~ age + I(age^2) + urban + livch + (1 | district), method = 'LRT',
             family = binomial, data = Contraception, set_data_arg = TRUE)
library(effects)
plot(Effect("age", gm1$full_model))
@mattansb
Copy link
Contributor Author

@mattansb mattansb commented Jan 17, 2019

Thanks for the suggestion!
I really like how afex_plot also has the original data, and it seems that effects is having some difficulties with this with this a glmm model (I can only guess do to the residuals being put through the link function):

library(afex)
library(mlmRev)
Contraception <- within(Contraception,use <- as.numeric(use)-1) # for plotting the data
gm1 <- mixed(use ~ age + I(age^2) + urban + livch + (1 | district), method = 'LRT',
             family = binomial, data = Contraception, set_data_arg = TRUE)
library(effects)
plot(Effect("age", gm1$full_model,residuals = TRUE))

image

I guess I didn't think about the need to change geoms etc. Though I think it might still be enough to just coerce the x back to continuous via as.numeric(as.charachter(x)) and keep the discrete geoms (doesn't look too bad):

afex_plot(gm1,~age,data_plot = TRUE, emmeans_arg = list(cov.red = FALSE))

image

So for the time being... If I ever need a continuous x-axis, I will use with afex_plot with return = "data", and reconstruct my plot (in a very very very roundabout way):

library(ggplot2)
X <- afex_plot(gm1,~age,data_plot = TRUE, emmeans_arg = list(cov.red = FALSE),return = 'data')
ggplot(mapping = aes(as.numeric(as.character(x)),y)) + 
  geom_point(data = X$data, alpha = .1) + 
  geom_ribbon(aes(ymin = lower, ymax = upper),data = X$means, color = NA, alpha = .4) + 
  geom_line(data = X$means)

image

@mattansb mattansb closed this May 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.