R ggplot GLM拟合曲线无交互作用
我想在R ggplot GLM拟合曲线无交互作用,r,plot,ggplot2,glm,interaction,R,Plot,Ggplot2,Glm,Interaction,我想在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", "Vocatio
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)
我试着这样做
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")
mod据我所知,没有办法欺骗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)
mod我不确定,但是你写了“没有交互”——也许你在寻找效果图?(如果不是,请原谅,我认为完全错了…)
例如,您可以使用
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))
或者以非平面的方式:
sjp.glm(mod, type = "eff", facet.grid = F, show.ci = T)
Ben绘制特定模型术语响应预测值的想法启发我改进了sjp.glm
函数的type=“y.pc”
选项。新更新为,版本号为1.9.4-3
现在,您可以绘制特定术语的预测值,其中一个沿x轴使用,另一个用作分组因子:
sjp.glm(mod, type = "y.pc", vars = c("math", "prog"))
它给出了以下曲线图:
如果您的模型有两个以上的术语,则需要使用vars
参数来指定x轴范围的术语和分组的术语
您还可以分为以下组:
sjp.glm(mod, type = "y.pc", vars = c("math", "prog"), show.ci = T, facet.grid = T)
谢谢,但我认为这并不是OP想要的。“无交互”意味着他们想要绘制一个加法(~math+prog
)模型的预测……好吧,你的答案看起来是获得该图的最短方法。“没有互动”和num_awards=ß0+ß1*数学+ß2*程序+错误
在我看来,边缘效应是OP想要的。。。。我尝试了sjp.glm(mod,type=“y.pc”,axisLabels=“math”)
,这是我认为OP想要的,sjPlot版本1.9.4.2(刚从Github发布,除非我出错)得到了一个空白绘图和一条警告消息……这种绘图类型将轴限制从0设置为1(假设为二项glm),因此,您只找到了一种不固定的打印类型,以匹配不同的模型族;-)无论如何,这个函数不能完全做到这一点,因为x轴只是从1到nrow
的值。我不知道如何以一种通用的方式处理响应预测。如果模型有更多的协变量,或者只有连续的协变量,会怎么样?那么我就不知道选择哪个术语作为分组因子了。老实说,当前的type选项y.pc
没有做很多有用的事情。
sjp.glm(mod, type = "y.pc", vars = c("math", "prog"))
sjp.glm(mod, type = "y.pc", vars = c("math", "prog"), show.ci = T, facet.grid = T)