简体   繁体   中英

displaying confidence intervals and not standardized coefficients in R

I have this code that displays standartized coefficients and not confidencce intervals. How can I show the CI instead? Some examples iv'e seen are a bit different of a graph


library(nycflights13)
library(dplyr)
library(dotwhisker)
library(MASS)

flights <- nycflights13::flights
flights<- sample_n (flights, 500)

m1<- glm(formula = arr_delay ~ dep_time + origin+ air_time+ distance , data = flights)
#m1<- glm(formula = arr_delay ~ . , data = flights)

m1<- stepAIC(m1)
  p<- dotwhisker::dwplot(m1)
  z<- p + 
    geom_vline(xintercept=0, linetype="dashed")+
    geom_segment(aes(x=conf.low,y=term,xend=conf.high,
                     yend=term,col=p.value<0.05)) + 
    geom_point(aes(x=estimate,y=term,col=p.value<0.05)) +
  xlab("standardized coefficient") + 
  ylab("coefficient") +
  ggtitle("coefficients in the model and significance")
  print(z)

I guess you could get what you are looking for directly from the model without using dotwhisker . The problem is that, since they are not standardised, the raw confidence intervals are orders of magnitude apart and don't display well on a single plot.

df1 <- as.data.frame(coefficients(summary(m1)))
df1$variable <- rownames(df1)
df1$lower <- df1$Estimate - 1.96 * df1$`Std. Error`
df1$upper <- df1$Estimate + 1.96 * df1$`Std. Error`

ggplot(df1, aes(Estimate, variable, color = `Pr(>|t|)` < 0.05)) +
  geom_segment(aes(yend = variable, x = lower, xend = upper)) +
  geom_point() +
  geom_vline(xintercept = 0, lty = 2)

在此处输入图像描述

dotwhisker takes the by_2sd argument which standardizes the coefficients and confidence intervals; setting it to FALSE give unstandardized coefficients. As many were confused by this behavior, the most recent version of the package (0.6) does this by default .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM