在多个glm Poisson回归中绘制置信区间(IC)

问题描述 投票:0回答:1

我想在多个glm Poisson回归中绘制置信区间(IC)而没有成功。如果我做:

#Artificial data set
Consumption <- c(501, 502, 503, 504, 26, 27, 55, 56, 68, 69, 72, 93)
Gender <- gl(n = 2, k = 6, length = 2*6, labels = c("Male", "Female"), ordered = FALSE)
Income <- c(5010, 5020, 5030, 5040, 260, 270, 550, 560, 680, 690, 720, 930)
df3 <- data.frame(Consumption, Gender, Income)
df3

创建glm Poisson多元回归模型:

fm1 <- glm(Consumption~Gender+Income, data=df3, family=poisson)
summary(fm1)

看到变量的意义:

# ANOVA
anova(fm1,test="Chi")

预测值并计算IC:

df3 = cbind(df3, pred = predict(fm1, type = "response"))#Estimate values
df3 = cbind(df3, se = predict(fm1, type="link",se.fit = TRUE)) ## Confidence interval of estimated
df3 = cbind(df3, ucl=exp(df3$pred + 1.96*df3$se.fit))
df3 = cbind(df3, lcl=exp(df3$pred - 1.96*df3$se.fit))

现在,如果我试图绘制这个:

#Plot
ggplot(data=df3, mapping=aes(x=Income, y=Consumption, color=Gender)) + 
  geom_point() +  
  geom_line(mapping=aes(y=pred)) +
  geom_smooth(data=df3, aes(ymin = lcl, ymax = ucl), stat="identity") 

#

不起作用:

Error in if ((w[1] * sm + w[2] * cm + w[3] * dm + w[4]) < best$score) break : 
  missing value where TRUE/FALSE needed

任何成员都可以帮助我吗?

提前致谢!

r ggplot2 plot glm poisson
1个回答
1
投票

我建议使用以下代码计算预测值的置信区间:

pred <- predict(fm1, type="link", se.fit = TRUE)
df3 = cbind(df3, pred = pred$fit)
df3 = cbind(df3, se = pred$se.fit) 
df3 = cbind(df3, ucl=exp(df3$pred + 1.96*df3$se))
df3 = cbind(df3, lcl=exp(df3$pred - 1.96*df3$se))

要么

pred <- predict(fm1, type="response", se.fit = TRUE)
df3 = cbind(df3, pred = pred$fit)
df3 = cbind(df3, se = pred$se.fit) 
df3 = cbind(df3, ucl=df3$pred + 1.96*df3$se)
df3 = cbind(df3, lcl=df3$pred - 1.96*df3$se)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.