简体   繁体   English

在R中使用glm(..)获得95%的置信区间

[英]Get 95% confidence interval with glm(..) in R

Here are some data 这是一些数据

dat = data.frame(y = c(9,7,7,7,5,6,4,6,3,5,1,5), x = c(1,1,2,2,3,3,4,4,5,5,6,6), color = rep(c('a','b'),6))

and the plot of these data if you wish 以及这些数据的图表,如果你愿意的话

require(ggplot)
ggplot(dat, aes(x=x,y=y, color=color)) + geom_point() + geom_smooth(method='lm')

When running a model with the function MCMCglmm() 使用MCMCglmm()函数运行模型时...

require(MCMCglmm)
summary(MCMCglmm(fixed = y~x/color, data=dat))

I get the lower and upper 95% interval for the estimate allowing me to know if the two slopes (color = a and color = b) are significantly different. 我得到估计的下限和上限95%,允许我知道两个斜率(颜色= a和颜色= b)是否显着不同。

When looking at this output... 看这个输出时......

summary(glm(y~x/color, data=dat))

... I can't see the confidence interval! ......我看不到置信区间!

My question is: 我的问题是:

How can I have these lower and upper 95% interval confidence for the estimates when using the function glm() ? 使用函数glm()时,如何使用这些估计值的上下95%区间置信度?

use confint 使用confint

mod = glm(y~x/color, data=dat)
summary(mod)
Call:
glm(formula = y ~ x/color, data = dat)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-1.11722  -0.40952  -0.04908   0.32674   1.35531  

Coefficients:
            Estimate Std. Error t value     Pr(>|t|)
(Intercept)   8.8667     0.4782  18.540 0.0000000177
x            -1.2220     0.1341  -9.113 0.0000077075
x:colorb      0.4725     0.1077   4.387      0.00175

(Dispersion parameter for gaussian family taken to be 0.5277981)

    Null deviance: 48.9167  on 11  degrees of freedom
Residual deviance:  4.7502  on  9  degrees of freedom
AIC: 30.934

Number of Fisher Scoring iterations: 2

confint(mod)
Waiting for profiling to be done...
                 2.5 %     97.5 %
(Intercept)  7.9293355  9.8039978
x           -1.4847882 -0.9591679
x:colorb     0.2614333  0.6836217

@alex's approach will get you the confidence limits, but be careful about interpretation. @ alex的方法会让你有信心限制,但要小心解释。 Since glm is fundamentally a non-liner model, the coefficients usually have large covariance. 由于glm基本上是非线性模型,因此系数通常具有较大的协方差。 You should at least take a look at the 95% confidence ellipse. 你应该至少看看95%置信度椭圆。

mod <- glm(y~x/color, data=dat)
require(ellipse)
conf.ellipse <- data.frame(ellipse(mod,which=c(2,3)))
ggplot(conf.ellipse, aes(x=x,y=x.colorb)) + 
  geom_path()+
  geom_point(x=mod$coefficient[2],y=mod$coefficient[3], size=5, color="red")

Produces this, which is the 95% confidence ellipse for x and the interaction term. 产生这个,这是x和交互项的95%置信椭圆。

Notice how the confidence limits produced by confint(...) are well with the ellipse. 注意confint(...)产生的置信限是如何与椭圆一致的。 In that sense, the ellipse provides a more conservative estimate of the confidence limits. 在这个意义上,椭圆提供了更保守的置信限度估计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 中的 GLM:具有 95% 置信区间的正态 QQ 图 - GLM in R: Normal Q-Q Plot with the 95% confidence interval 如何获得R中的95%置信区间? - How to get the 95% confidence interval in R? 如何计算 R 中某个比例的 95% 置信区间? - How to calculate 95% confidence interval for a proportion in R? 如何使用方程式中的95%置信区间在r中使用95%置信区间? - How do I use the 95% confidence interval in r using the equation for a 95% confidence interval? 分段glm的置信区间 - confidence interval for segmented glm 尝试使用引导程序 R 找到预测值的 95% 置信区间 - trying to find the 95% confidence interval for a predicted value using bootstrap R R 中以下变量均值的均值、标准差和 95% 置信区间 - The mean, standard deviation and 95% confidence interval for the mean of the following variables in R 如何计算R中线性回归模型中斜率的95%置信区间 - How to calculate the 95% confidence interval for the slope in a linear regression model in R 在 R 中一次计算多个列的 95% 置信区间 - Calculating 95% Confidence Interval for Several Columns at Once in R 如何在R中绘制逻辑glm预测值和置信区间 - How to plot logistic glm predicted values and confidence interval in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM