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

后端 未结 2 1800
不思量自难忘°
不思量自难忘° 2021-02-19 20:38

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

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 21:09

    @alex's approach will get you the confidence limits, but be careful about interpretation. Since glm is fundamentally a non-liner model, the coefficients usually have large covariance. You should at least take a look at the 95% confidence ellipse.

    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.

    Notice how the confidence limits produced by confint(...) are well with the ellipse. In that sense, the ellipse provides a more conservative estimate of the confidence limits.

提交回复
热议问题