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

时间:2017-04-08 19:22:20

标签: r plot ggplot2 interaction data-fitting

我正在尝试使用ggplot2为两个不同的模型绘制模型输出。当我想象我的情节时,尽管它们已经装好,但它们看起来完全相同。

两个模型之间的唯一区别是在第二个模型中添加了变量“age”(以及包含“age”的所有双向交互)。

第一个模型如下:

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

而且,我用来可视化第二个模型的代码是:

fitted2<- function (fit) {
    ggplot(data=fit$rel_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")
}
plot2<-fitted2(glm(MastN~LRS_Bin)) 

这是结果图: enter image description here

两个情节看起来完全一样!

我无法弄清楚我的代码是否犯了错误,或者两个模型(尽管第二个模型与第一个模型不同)会产生相同的输出......

我的数据和代码可以找到here

1 个答案:

答案 0 :(得分:1)

您使用的代码中存在一些错误。我不确定你是如何得到你附加的输出。但是,如果您只想显示拟合值。你可以这样做: -

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)

Plot of fitted models