1

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?

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)

Graph: graph generated

Stat.Enthus
  • 335
  • 1
  • 12
  • you have a question and I answered it to the best of my abilities. From what I understand, you have some further questions that deviates significantly from the original question, please post it as another question. – StupidWolf Nov 28 '20 at 16:02
  • 1
    Look at your plot above, all the 95% confidence interval cross the 0 line, meaning most like their p-values are greater than 0.05. Please focus on the question, SO is not meant to be a Q & A or never-ending saga of question / answer / more question / more edit / more answers. – StupidWolf Nov 28 '20 at 16:04

1 Answers1

2

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>

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))

enter image description here

The other option is to do it from scratch using the actual coefficients from the model.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thanks! What is the equivilant of 0.05 in this p < 1e-06 setting? is it 1e^-03? And what is the X axis here? – Stat.Enthus Oct 24 '20 at 08:16
  • yes you can set 0.05. I used 1e-06 to show that it is possible to have different colors. The X axis is a scaled coefficient from dwplot, you have to read the help page https://cran.r-project.org/web/packages/dotwhisker/vignettes/dotwhisker-vignette.html – StupidWolf Oct 24 '20 at 21:27
  • Thanks! looks great. I am trying to print it with RMD, and it doesn't print. Do you have any idea why? ` safe_log <- safely(dotwhisker::dwplot) p<- safe_log(df$model[[i]]) #print(p) z<-(p + geom_vline(xintercept=0, linetype="dashed")+ geom_segment(aes(x=conf.low,y=term,xend=conf.high, yend=term))+ geom_point(aes(x=estimate,y=term,col=p.value<1e-3))) print(z)` – Stat.Enthus Nov 27 '20 at 15:47
  • I think it's an issue with how you are using rmarkdown. Sorry I don't use rmarkdown. Mostly jupyter. – StupidWolf Nov 27 '20 at 15:48
  • Thanks! I tested it and it's probably also related to the safely functioni use with dwplot. What kind of object is this? How can I easily add index and x,y captions? (A link to a nice guide would also work) – Stat.Enthus Nov 28 '20 at 11:47
  • Hi @Stat.Enthus, it is ggplot2. https://rafalab.github.io/dsbook/ggplot2.html – StupidWolf Nov 28 '20 at 11:52
  • Thanks! Iv'e learned a lot from this thread! Please check out my recent edit.. – Stat.Enthus Nov 28 '20 at 15:56