不带交互作用的ggplot GLM拟合曲线

7

我希望在ggplot上添加来自GLM的拟合函数。默认情况下,它会自动创建交互作用图。我想知道是否可以绘制模型中没有交互作用的拟合函数。例如:

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)

提供与交互的情节,Fig-1

然而,我想要来自模型的图表,

`num_awards` = ß0 + ß1*`math` + ß2*`prog` + error

我曾试图通过这种方式获取它,

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")

输出的图表是 Fig2

在 ggplot 中有没有简单的方法来高效地完成这个操作?

3个回答

6

Ben提出的绘制特定模型项响应预测值的想法,启发了我改进sjp.glm函数的type = "y.pc"选项。新版本更新已经发布在GitHub上,版本号为1.9.4-3。

现在,您可以绘制特定项的预测值,其中一个用作x轴,另一个用作分组因子:

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

这将给出以下图表:

enter image description here

如果您的模型有两个以上的项,vars参数是必需的,用于指定x轴范围和分组术语。

您也可以对组进行分面处理:

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

enter image description here


5

我不知道有什么方法可以欺骗geom_smooth()来实现这一点,但是你可以做得比你现在做的更好。你仍然需要自己拟合模型并添加线条,但是你可以使用predict()方法生成预测值,并将它们加载到与原始数据结构相同的数据框中...

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)

这里唯一的区别是,我所做的预测覆盖了所有组的水平范围,就像在geom_smooth()中指定了fullrange=TRUE一样。
原则上,sjPlot包似乎应该能够处理这种情况,但是看起来用于绘制此图的相关代码是硬编码为假设二项式GLM...好吧。

1
我不确定,但你写了“without interaction”-也许你正在寻找效果图?(如果不是,请原谅我完全假设...)
例如,您可以使用effects package进行此操作。
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

另一个选项是sjPlot包,正如Ben所建议的那样 - 但是,CRAN上当前版本仅支持逻辑回归模型的效果图。但是在GitHub上的当前开发版本中,我添加了对各种模型系列和链接函数的支持,因此如果您愿意,可以下载该快照。 sjPlot包使用ggplot而不是lattice(我认为effects包使用lattice):

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

enter image description here

或者以非多面体的方式:

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

enter image description here

enter image description here


谢谢,但我认为这并不完全符合OP的要求。 “无交互”意味着他们想绘制加性(~math+prog)模型的预测... - Ben Bolker
好的,那么你的回答看起来是获得情节的最短途径。对我来说,“没有互动”和num_awards = ß0 + ß1*math + ß2*prog + error似乎像OP正在寻找的边际效应。 - Daniel
我尝试了 sjp.glm(mod,type="y.pc",axisLabels="math"),这应该是OP想要的,使用的是sjPlot版本1.9.4.2(直接从Github获取,除非我搞砸了),但是得到了一个空白图和一个警告信息... - Ben Bolker
这种图形类型将坐标轴限制在0到1之间(假设是二项式GLM),所以你刚好找到了那个不固定匹配不同模型家族的类型;-) 无论如何,这个函数并不能完全实现这个功能,因为x轴只是从1到nrow的值。我不知道如何以一种通用的方式处理响应预测。如果模型有更多的协变量,或者只有连续的协变量,那该怎么办?那我就不知道应该选择哪些术语作为分组因子了。老实说,当前的y.pc选项并没有做太多有用的事情。 - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接