Ggplot GLM installed curve without interaction

I want to add an installed function from GLM to ggplot . By default, it automatically creates a graph with the interaction. I am wondering if I can build a function created from a model without interaction. For instance,

 dta <- read.csv("http://www.ats.ucla.edu/stat/data/poisson_sim.csv") dta <- within(dta, { prog <- factor(prog, levels=1:3, labels=c("General", "Academic", "Vocational")) id <- factor(id) }) plt <- ggplot(dta, aes(math, num_awards, col = prog)) + geom_point(size = 2) + geom_smooth(method = "glm", , se = F, method.args = list(family = "poisson")) print(plt) 

gives a graph with the interaction, Rice-1

However, I need a graph from the model,

 `num_awards` = รŸ0 + รŸ1*`math` + รŸ2*`prog` + error 

I tried to do it this way

 mod <- glm(num_awards ~ math + prog, data = dta, family = "poisson") fun.gen <- function(awd) exp(mod$coef[1] + mod$coef[2] * awd) fun.acd <- function(awd) exp(mod$coef[1] + mod$coef[2] * awd + mod$coef[3]) fun.voc <- function(awd) exp(mod$coef[1] + mod$coef[2] * awd + mod$coef[4]) ggplot(dta, aes(math, num_awards, col = prog)) + geom_point() + stat_function(fun = fun.gen, col = "red") + stat_function(fun = fun.acd, col = "green") + stat_function(fun = fun.voc, col = "blue") + geom_smooth(method = "glm", se = F, method.args = list(family = "poisson"), linetype = "dashed") 

Withdrawal chart Fig2

Is there an easy way in ggplot to do this efficiently?

+5
source share
3 answers

I don't know how to trick geom_smooth() , but you can do a little better than you did. You still need to fit the model yourself and add lines, but you can use the predict() method to generate forecasts and load them into a data frame with the same structure as the original data ...

 mod <- glm(num_awards ~ math + prog, data = dta, family = "poisson") ## generate prediction frame pframe <- with(dta, expand.grid(math=seq(min(math),max(math),length=51), prog=levels(prog))) ## add predicted values (on response scale) to prediction frame pframe$num_awards <- predict(mod,newdata=pframe,type="response") ggplot(dta, aes(math, num_awards, col = prog)) + geom_point() + geom_smooth(method = "glm", se = FALSE, method.args = list(family = "poisson"), linetype = "dashed")+ geom_line(data=pframe) ## use prediction data here ## (inherits aesthetics etc. from main ggplot call) 

(the only difference here is that the way I did it, the forecasts cover the entire horizontal range for all groups, as if you had specified fullrange=TRUE in geom_smooth() ).

In principle, it seems that the sjPlot package should be able to handle such things, but it looks like the corresponding bit of code for this type of chart is hardcoded to accept binomial GLM ... well, good.

+4
source

Ben's idea of โ€‹โ€‹planning a predicted response value for specific model conditions inspired me to improve the type = "y.pc" option of the type = "y.pc" function. New update on GitHub with version number 1.9.4-3.

Now you can display the predicted values โ€‹โ€‹for certain terms that are used along the x axis, and the second is used as a grouping factor:

 sjp.glm(mod, type = "y.pc", vars = c("math", "prog")) 

which gives you the following graph:

enter image description here

The vars argument vars needed if your model has more than two terms to indicate a term for a range on the x axis and a term for grouping.

You can also group groups:

 sjp.glm(mod, type = "y.pc", vars = c("math", "prog"), show.ci = T, facet.grid = T) 

enter image description here

+3
source

I'm not sure, but you wrote "no interaction" - maybe you're looking for sections of the effects? (If not, I'm sorry that I'm assuming something is completely wrong ...)

You can, for example, use the effects package for this.

 dta <- read.csv("http://www.ats.ucla.edu/stat/data/poisson_sim.csv") dta <- within(dta, { prog <- factor(prog, levels=1:3, labels=c("General", "Academic", "Vocational")) id <- factor(id) }) mod <- glm(num_awards ~ math + prog, data = dta, family = "poisson") library(effects) plot(allEffects(mod)) 

enter image description here

Another option would be the sjPlot package, as Ben suggested, but the current version on CRAN supports the logistic regression models correctly for effect sections. But in the current version of GitHub development, I added support for various families of models and link functions, so if you like, you can download this snapshot. The sjPlot package uses ggplot instead of the grating (which, it seems to me, is used by the effects package):

 sjp.glm(mod, type = "eff", show.ci = T) 

enter image description here

Or unlimited:

 sjp.glm(mod, type = "eff", facet.grid = F, show.ci = T) 

enter image description here

enter image description here

0
source

Source: https://habr.com/ru/post/1246583/


All Articles