问题描述
我正在尝试显示具有多个系数的图,其中一些系数很重要,而有些系数则没有。 另外,当我尝试使用m1的其他配置时,会返回错误。
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)
summary(m1)
dwplot(m1)
dwplot(m1 + geom_vline(xintercept=0,lty=2)) ## This is meant to add a line on the CI
编辑1: 这段代码确实很棒,但是当我将参数更改为0.05时,我得到的所有结果都显示为橙色。有什么想法吗?
df <- mtcars
nested_inter <- mtcars %>% group_by(gear) %>%
nest() ## groups all the data by the sub series
nested_inter <- nested_inter %>%
mutate (model = map(data,~lm(formula = mpg ~ cyl + drat + hp +wt,data = .)))
p<- dotwhisker::dwplot(nested_inter$model[[2]])
#print(p)
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,col=p.value<0.05)) +
xlab("standardized coefficient") +
ylab("coefficient") +
ggtitle("coefficients in the model and significance")
print(z)
解决方法
您可以在dwplot函数外部添加geom_vline参数,并且要添加颜色,必须事先指定颜色并使用dot_args=
和line_args
参数添加颜色。不幸的是,我认为您只能指定点的颜色,该行的参数不起作用(至少在我手中)。
首先,您可以看到数据存储如下:
p = dwplot(m1)
p$data
# A tibble: 3 x 10
term estimate std.error statistic p.value conf.low conf.high by_2sd model
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <fct>
1 dep_… 28.0 4.18 6.71 5.54e-11 19.8 36.2 TRUE one
2 air_… 143. 30.0 4.76 2.55e- 6 84.0 201. TRUE one
3 dist… -143. 30.0 -4.78 2.33e- 6 -202. -84.5 TRUE one
# … with 1 more variable: y_ind <dbl>
因此,我们仅作图,并假设p
p +
geom_vline(xintercept=0,linetype="dashed")+
geom_segment(aes(x=conf.low,y=term,xend=conf.high,yend=term,col=p.value<1e-6))+
geom_point(aes(x=estimate,col=p.value<1e-6))
另一种选择是使用模型中的实际系数从头开始。