使用dwplot按参数显示不同颜色的系数 [英] Using dwplot to display coefficients in different colors by paramater
问题描述
我正在尝试显示具有多个系数的图,其中一些系数很重要,而有些系数则没有.另外,当我尝试使用m1的其他配置时,会返回错误.
I'm trying to display a plot with several coefficients, some are significant and some are not. Plus, when I try the other configuration of m1, an error is returned.
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
我该如何为具有统计意义或没有统计意义的系数分配不同的颜色?
How can I dedicate different colors to coefficients with or without statistical significance?
这段代码确实很棒,但是当我将参数更改为0.05时,我得到的所有结果都显示为橙色.有什么想法吗?
EDIT 1 : This code works really great but when I change the paramter to 0.05 i get all results in orange as displayed. Any thoughts?
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,y=term,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
参数.不幸的是,我认为您只能指定点的颜色,该行的参数不起作用(至少在我手中).
You can add the geom_vline argument outside the dwplot function, and to add colors, you have to specify them before hand and add them using dot_args=
and line_args
arguments. Unfortunately, i think you can only specify the color of the dots, the argument for the line doesn't work (at least in my hands).
首先,您可以看到数据存储如下:
First you can see the data is stored like this:
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<1e-06是有效的,使dep_time成为唯一的有效变量,以便查看不同的颜色:
So we just plot over, and assume something with p < 1e-06 is significant, making dep_time the only significant variable, so as to see the different colors:
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,y=term,col=p.value<1e-6))
另一种选择是使用模型中的实际系数从头开始.
The other option is to do it from scratch using the actual coefficients from the model.
这篇关于使用dwplot按参数显示不同颜色的系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!