微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用dwplot通过参数显示不同颜色的系数

如何解决使用dwplot通过参数显示不同颜色的系数

我正在尝试显示具有多个系数的图,其中一些系数很重要,而有些系数则没有。 另外,当我尝试使用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)

图:

graph generated

解决方法

您可以在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))

enter image description here

另一种选择是使用模型中的实际系数从头开始。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。