ggplot中的GLM Gamma系列:曲线表示中的geom_line()和stat_smooth()之间的差异

时间:2018-12-07 14:06:53

标签: r ggplot2 glm

我想在ggplot中绘制带有Gamma系列的glm模型。我的想法是使用stat_smooth()作为我调整后的模型的置信区间表示。在下面的示例中,带有点的geom_line()曲线与stat_smooth()曲线完全不同:

#Simulation data set
set.seed(123)
d<-NULL
N<-50
d$Production <- rgamma(N,10)
d$Feature <- ifelse(d$Production >7 & d$Production<10, c("green"),ifelse(d$Production>11, 
c("red"), c("blue")))  
d$Temp<-rnorm(N,20,5)
d<-as.data.frame(d)
#

# Gamma glm model
mG<- glm(Production~ Feature + Temp, family= Gamma, data = d)
summary(mG)
anova(mG,test="Chi")

# Pair-wise Feature factor
library(multcomp)
PW<-summary(glht(mG, linfct = mcp(Feature= "Tukey")))  
PW 
cld(PW)

# If for example green = blue
Feature2<-d$Feature
levels(Feature2) 
levels(Feature2)[1]<-"blue&green"
levels(Feature2)[2]<-"blue&green"
levels(Feature2)
d$Feature2<-Feature2
#New GLM model
mG2<- glm(Production~ Feature2 + Temp, family= Gamma, data = d)

# Prediction values
pred.data = data.frame(
Feature2<-d$Feature2,
Temp<-d$Temp
)  
pred.data$Production = predict(mG2, newdata=pred.data, type="response")

#Final plot
library("ggplot2")
ggplot(d, aes(Temp, Production, colour = Feature2)) +
  geom_point() +
  geom_line(data=pred.data) +
  stat_smooth(method = "glm", formula = y ~ x, family = Gamma)
#

Difference between geom_line() and stat_smooth() in curve representation 请,我需要一些帮助来纠正此问题,谢谢

0 个答案:

没有答案