R 使用ggplot2绘制拟合glm输出以进行交互

R 使用ggplot2绘制拟合glm输出以进行交互,r,plot,ggplot2,interaction,data-fitting,R,Plot,Ggplot2,Interaction,Data Fitting,我正在尝试使用ggplot2为两个不同的模型绘制模型输出。当我想象我的情节时,它们看起来完全一样,尽管它们是合适的 这两个模型之间的唯一区别是在第二个模型中增加了一个变量“年龄”(以及包括“年龄”在内的所有双向交互) 第一种模式如下: ab_bi_f= glm(LRS_Bin ~ as.factor(DispersalFate) + MastN + as.factor(DispersalFate)*MastN, data = female, family = binomial(link = lo

我正在尝试使用
ggplot2
为两个不同的模型绘制模型输出。当我想象我的情节时,它们看起来完全一样,尽管它们是合适的

这两个模型之间的唯一区别是在第二个模型中增加了一个变量“年龄”(以及包括“年龄”在内的所有双向交互)

第一种模式如下:

ab_bi_f= glm(LRS_Bin ~ as.factor(DispersalFate) + MastN + as.factor(DispersalFate)*MastN, data = female, family = binomial(link = logit))
rel_bi_f = glm(LRS_Bin ~ as.factor(DispersalFate) + Age + MastN + as.factor(DispersalFate)*MastN + Age*MastN +as.factor(DispersalFate)*Age, data = female, family = binomial(link = logit))
第二种模式如下:

ab_bi_f= glm(LRS_Bin ~ as.factor(DispersalFate) + MastN + as.factor(DispersalFate)*MastN, data = female, family = binomial(link = logit))
rel_bi_f = glm(LRS_Bin ~ as.factor(DispersalFate) + Age + MastN + as.factor(DispersalFate)*MastN + Age*MastN +as.factor(DispersalFate)*Age, data = female, family = binomial(link = logit))
我用于拟合和可视化第一个模型的代码是:

fitted1<- function (fit) {
    ggplot(data=fit$ab_bi_f, aes(x=MastN, y=LRS_Bin, colour=factor(DispersalFate))) + 
         stat_smooth() + 
         ggtitle("Binary absolute LRS model - female") + 
         labs(x="Total masts in female's lifespan", y="Mean fitted binary absolute LRS", colour="Dispersal type")+
         scale_x_continuous(breaks=c(0,1,2))+
         theme_classic()+annotate("text", x = 0, y = 0.89, label = "a",size=6)+
         coord_cartesian(ylim=c(0.1, 0.9))+
         scale_y_continuous(breaks=c(0.1,0.3,0.5,0.7,0.9))+
         scale_color_manual(values=c("#000000", "#999999"))+
         theme(legend.position=c(0.3,0.85),
         plot.title = element_text(size = 10),
         legend.title=element_text(size=10),
         axis.title=element_text(size=8), 
         legend.key = element_rect(size = 0.1),
         legend.key.size = unit(0.5, "cm"),
         legend.direction="horizontal")
}
plot1<-fitted1(glm(MastN~LRS_Bin)) 

fitted1您正在使用的代码中有一些错误。我不确定你是如何得到你所附的输出的。但是,如果您只想可视化拟合值。您可以按如下方式进行操作:-

ab_bi_f= glm(LRS_Bin ~ as.factor(DispersalFate) + MastN + as.factor(DispersalFate)*MastN, data = female, family = binomial(link = logit))

rel_bi_f = glm(LRS_Bin ~ as.factor(DispersalFate) + Age + MastN + as.factor(DispersalFate)*MastN + Age*MastN +as.factor(DispersalFate)*Age, data = female, family = binomial(link = logit))

female$fit1 <- predict(ab_bi_f, type = 'response')
female$fit2 <- predict(rel_bi_f, type = 'response')

plot1 <- ggplot(data=female, aes(x=MastN, y=fit1, colour=factor(DispersalFate))) + 
        geom_point() + 
        stat_smooth() + 
        ggtitle("Binary absolute LRS model - female") + 
        labs(x="Total masts in female's lifespan", y="Mean fitted binary absolute LRS", colour="Dispersal type")+
        scale_x_continuous(breaks=c(0,1,2))+
        theme_classic()+annotate("text", x = 0, y = 0.89, label = "a",size=6)+
        coord_cartesian(ylim=c(0.1, 0.9))+
        scale_y_continuous(breaks=c(0.1,0.3,0.5,0.7,0.9))+
        scale_color_manual(values=c("#000000", "#999999"))+
        theme(legend.position=c(0.3,0.85),
        plot.title = element_text(size = 10),
        legend.title=element_text(size=10),
        axis.title=element_text(size=8), 
        legend.key = element_rect(size = 0.1),
        legend.key.size = unit(0.5, "cm"),
        legend.direction="horizontal")

plot2 <- ggplot(data=female, aes(x=MastN, y=fit2, colour=factor(DispersalFate))) + 
        geom_point() + 
        stat_smooth() + 
        ggtitle("Binary absolute LRS model - female") + 
        labs(x="Total masts in female's lifespan", y="Mean fitted binary absolute LRS", colour="Dispersal type")+
        scale_x_continuous(breaks=c(0,1,2))+
        theme_classic()+annotate("text", x = 0, y = 0.89, label = "a",size=6)+
        coord_cartesian(ylim=c(0.1, 0.9))+
        scale_y_continuous(breaks=c(0.1,0.3,0.5,0.7,0.9))+
        scale_color_manual(values=c("#000000", "#999999"))+
        theme(legend.position=c(0.3,0.85),
        plot.title = element_text(size = 10),
        legend.title=element_text(size=10),
        axis.title=element_text(size=8), 
        legend.key = element_rect(size = 0.1),
        legend.key.size = unit(0.5, "cm"),
        legend.direction="horizontal")

grid.arrange(plot1, plot2)
ab_bi_f=glm(LRS_Bin~as.factor(DispersalFate)+MastN+as.factor(DispersalFate)*MastN,数据=女性,家庭=二项式(link=logit))
rel_bi_f=glm(LRS_Bin~as.factor(DispersalFate)+年龄+MastN+as.factor(DispersalFate)*MastN+Age*MastN+as.factor(DispersalFate)*年龄,数据=女性,家庭=二项式(link=logit))

女性$fit1您的plot1是否像plot2一样在线条周围有置信区间?使用您的代码,CI仅显示在第二个绘图中,而不会显示。我已经编辑了绘图以包括点。您可以看到,图1没有足够的点来建立置信区间(ggplot也为您提供了相同的警告)。这是因为模型1的公式和拟合值。哦,谢谢你澄清这一点。我不明白这是怎么回事。谢谢你的帮助!