使用 ggplot2 绘制拟合的 glm 输出以进行交互
Plotting fitted glm output using ggplot2 for interactions
我正在尝试使用 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))
这是结果图:
两个情节看起来一模一样!
我不知道我的代码是否有误,或者这两个模型(尽管第二个模型与第一个模型不同)是否导致相同的输出...
我的数据和代码可以找到here。
您使用的代码存在一些错误。我不确定您是如何获得附加的输出的。但是,如果您只想可视化拟合值。你可以这样做:-
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)
我正在尝试使用 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))
这是结果图:
两个情节看起来一模一样!
我不知道我的代码是否有误,或者这两个模型(尽管第二个模型与第一个模型不同)是否导致相同的输出...
我的数据和代码可以找到here。
您使用的代码存在一些错误。我不确定您是如何获得附加的输出的。但是,如果您只想可视化拟合值。你可以这样做:-
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)