解决泊松 glm 拟合与 R (ggplot2) 中的正则二次拟合之间的差异?
Resolving discrepancy between poisson glm fits and regular quadratic fit in R (ggplot2)?
我 运行 对我的一些计数数据进行了毒害模型(此处仅显示一个示例)。我尝试通过模型(下图 1)进行二次曲线拟合,并使用 ggplot2 中的内置函数进行常规拟合。我不确定为什么它们会有如此大的不同。我在我的几个图表中看到了这一点(我测试了它是否是泊松分布的)。我想知道我的预测函数是否在做一些奇怪的事情?
library(investr)
library(ggplot2)
y.test <- c(3.09601,3.546579, 12.115740, 2.226694, 1.180938, 4.420249, 2.001162, 3.788012, 21.170732, 7.494421 , 5.602522 , 3.300510, 11.404264 ,23.115029,
19.371686, 25.444904, 17.094280 ,1.368615 ,19.343291 , 9.724363 , 8.086256 ,13.021972 ,10.740431 , 2.768960 ,14.494745 ,19.040086 , 7.072040, 8.748415,
10.012655, 14.759963 , 6.669221, 9.179184, 14.069743 ,12.132714, 8.517986, 18.095548, 9.076304, 9.197501, 7.972339 , 3.111373, 10.802117, 16.874861,
2.977454 ,15.195754, 5.433059 , 8.569472, 24.479745 , 3.756167 ,7.028482 , 7.412065 , 6.298529 , 3.585942 , 4.706638 , 9.002232, 5.276891)
x.test <- c(1:55)
df.test <- data.frame(x.test, y.test)
mod <- glm(y.test ~ x.test + I(x.test^2), data = df.test, family = poisson)
predicted.spp <- data.frame(predFit(mod, interval='confidence', level=.95))
df.test$predicted.mean <- predicted.spp$fit
df.test$predicted.upr <- predicted.spp$lwr
df.test$predicted.lwr <- predicted.spp$upr
ggplot(df.test, aes(x = x.test, y = y.test)) + geom_point() +
geom_line(aes(y=predicted.mean), colour="blue") +
geom_ribbon(aes(ymin=predicted.lwr, ymax=predicted.upr), alpha=0.8) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")
my.formula = y~x + I(x^2)
ggplot(df.test, aes(x = x.test, y = y.test)) + geom_point() +
geom_smooth(method="lm", formula = my.formula, color = "black" ) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")
默认情况下 predFit
(以及大多数 GLM 预测机制)returns 结果在 link 尺度 上,在本例中为对数尺度.在您的 glm
示例中,您需要 predFit(mod, interval='confidence', type = "response" level=.95)
。 (如果你愿意,你也可以对 predFit
给你自己的结果求幂。)
您也可以使用
geom_smooth(method="glm", formula = my.formula,
method.args = list(family = "poisson"), color = "black" )
我 运行 对我的一些计数数据进行了毒害模型(此处仅显示一个示例)。我尝试通过模型(下图 1)进行二次曲线拟合,并使用 ggplot2 中的内置函数进行常规拟合。我不确定为什么它们会有如此大的不同。我在我的几个图表中看到了这一点(我测试了它是否是泊松分布的)。我想知道我的预测函数是否在做一些奇怪的事情?
library(investr)
library(ggplot2)
y.test <- c(3.09601,3.546579, 12.115740, 2.226694, 1.180938, 4.420249, 2.001162, 3.788012, 21.170732, 7.494421 , 5.602522 , 3.300510, 11.404264 ,23.115029,
19.371686, 25.444904, 17.094280 ,1.368615 ,19.343291 , 9.724363 , 8.086256 ,13.021972 ,10.740431 , 2.768960 ,14.494745 ,19.040086 , 7.072040, 8.748415,
10.012655, 14.759963 , 6.669221, 9.179184, 14.069743 ,12.132714, 8.517986, 18.095548, 9.076304, 9.197501, 7.972339 , 3.111373, 10.802117, 16.874861,
2.977454 ,15.195754, 5.433059 , 8.569472, 24.479745 , 3.756167 ,7.028482 , 7.412065 , 6.298529 , 3.585942 , 4.706638 , 9.002232, 5.276891)
x.test <- c(1:55)
df.test <- data.frame(x.test, y.test)
mod <- glm(y.test ~ x.test + I(x.test^2), data = df.test, family = poisson)
predicted.spp <- data.frame(predFit(mod, interval='confidence', level=.95))
df.test$predicted.mean <- predicted.spp$fit
df.test$predicted.upr <- predicted.spp$lwr
df.test$predicted.lwr <- predicted.spp$upr
ggplot(df.test, aes(x = x.test, y = y.test)) + geom_point() +
geom_line(aes(y=predicted.mean), colour="blue") +
geom_ribbon(aes(ymin=predicted.lwr, ymax=predicted.upr), alpha=0.8) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")
my.formula = y~x + I(x^2)
ggplot(df.test, aes(x = x.test, y = y.test)) + geom_point() +
geom_smooth(method="lm", formula = my.formula, color = "black" ) +
stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE, size = 2.5, col = "black")
默认情况下 predFit
(以及大多数 GLM 预测机制)returns 结果在 link 尺度 上,在本例中为对数尺度.在您的 glm
示例中,您需要 predFit(mod, interval='confidence', type = "response" level=.95)
。 (如果你愿意,你也可以对 predFit
给你自己的结果求幂。)
您也可以使用
geom_smooth(method="glm", formula = my.formula,
method.args = list(family = "poisson"), color = "black" )