class: center, middle, inverse, title-slide # Visualizing Linear Models ### Dr. D’Agostino McGowan --- layout: true <div class="my-footer"> <span> Dr. Lucy D'Agostino McGowan </span> </div> --- ## Visualizing linear models * Forest plots of the coefficients and confidence intervals -- ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- ## Building up to this plot! First, fit the model and tidy it ```r library(broom) library(ggplot2) mod <- lm(mpg ~ cyl + am + vs + gear, data = mtcars) mod_tidy <- tidy(mod, conf.int = TRUE) mod_tidy <- mod_tidy[-1, ] # drop the intercept ``` --- ## Building up to this plot! First, fit the model and tidy it ```r library(broom) library(ggplot2) *mod <- lm(mpg ~ cyl + am + vs + gear, data = mtcars) mod_tidy <- tidy(mod, conf.int = TRUE) mod_tidy <- mod_tidy[-1, ] # drop the intercept ``` --- ## Building up to this plot! First, fit the model and tidy it ```r library(broom) library(ggplot2) mod <- lm(mpg ~ cyl + am + vs + gear, data = mtcars) *mod_tidy <- tidy(mod, conf.int = TRUE) mod_tidy <- mod_tidy[-1, ] # drop the intercept ``` --- ## Building up to this plot! First, fit the model and tidy it ```r library(broom) library(ggplot2) mod <- lm(mpg ~ cyl + am + vs + gear, data = mtcars) mod_tidy <- tidy(mod, conf.int = TRUE) *mod_tidy <- mod_tidy[-1, ] # drop the intercept ``` --- ## Create a forest plot .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = term, y = estimate, ymin = conf.low, ymax = conf.high)) + * geom_pointrange() ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-8-1.png)<!-- --> ] -- .question[ What could be better? ] --- ## Flip the x- and y-axis .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = term, y = estimate, ymin = conf.low, ymax = conf.high)) + geom_pointrange() + * coord_flip() ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-10-1.png)<!-- --> ] --- ## Axis labels .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = term, y = estimate, ymin = conf.low, ymax = conf.high)) + geom_pointrange() + coord_flip() + * labs(x = "", * y = "Coefficient (95% CI)") ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-12-1.png)<!-- --> ] --- ## Term labels .pull-left[ .small[ ```r mod_tidy$labels <- c( * "No. cylinders", * "Manual transmission", * "Straight engine", * "No. forward gears") ggplot(mod_tidy, * aes(x = labels, y = estimate, ymin = conf.low, ymax = conf.high)) + geom_pointrange() + coord_flip() + labs(x = "", y = "Coefficient (95% CI)") ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-14-1.png)<!-- --> ] -- Better ways to do this (especially if you have lots of variables!) * `merge()` * `left_join()` --- ## Add a reference line at the null .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = labels, y = estimate, ymin = conf.low, ymax = conf.high)) + geom_pointrange() + * geom_hline(yintercept = 0, lty = 2) + coord_flip() + labs(x = "", y = "Coefficient (95% CI)") ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-16-1.png)<!-- --> ] --- ## Update the theme .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = labels, y = estimate, ymin = conf.low, ymax = conf.high)) + geom_pointrange() + geom_hline(yintercept = 0, lty = 2) + coord_flip() + labs(x = "", y = "Coefficient (95% CI)") + * theme_minimal() ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-18-1.png)<!-- --> ] -- * Check out the available themes here: https://ggplot2.tidyverse.org/reference/ggtheme.html --- ## Higlight points of interest .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = labels, y = estimate, ymin = conf.low, ymax = conf.high, * color = * ifelse(p.value < 0.05, * "p < 0.05", * "p > 0.05"))) + geom_pointrange() + geom_hline(yintercept = 0, lty = 2) + coord_flip() + * scale_color_manual("", * values = c( * "blue", * "black")) + labs(x = "", y = "Coefficient (95% CI)") + theme_minimal() ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-20-1.png)<!-- --> ] --- ## Annotate an important point .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = labels, y = estimate, ymin = conf.low, ymax = conf.high, color = ifelse(p.value < 0.05, "p < 0.05", "p > 0.05"))) + geom_pointrange() + geom_hline(yintercept = 0, lty = 2) + coord_flip() + scale_color_manual("", values = c( "blue", "black")) + labs(x = "", y = "Coefficient (95% CI)") + * annotate("text", x = 2.25, y = 5.9, * label = "Having manual transmission \n(versus automatic) \nyeilds an expected change in \nmiles per gallon of 4.32 \n(95% CI: 0.1, 8.5)", * size = 2) + theme_minimal() ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-22-1.png)<!-- --> ] --- ## Annotate an important point .pull-left[ .small[ ```r ggplot(mod_tidy, aes(x = labels, y = estimate, ymin = conf.low, ymax = conf.high, color = ifelse(p.value < 0.05, "p < 0.05", "p > 0.05"))) + geom_pointrange() + geom_hline(yintercept = 0, lty = 2) + coord_flip() + scale_color_manual("", values = c( "blue", "black")) + labs(x = "", y = "Coefficient (95% CI)") + annotate("text", x = 2.25, y = 5.9, label = "Having manual transmission \n(versus automatic) \nyeilds an expected change in \nmiles per gallon of 4.32 \n(95% CI: 0.1, 8.5) holding all other \nvariables constant", size = 2) + * annotate("curve", x = 1.5, y = 4.75, * xend = 1.2, yend = 4.32, * arrow = arrow(length = unit(0.03, "npc"))) + theme_minimal() ``` ] ] .pull-right[ ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-24-1.png)<!-- --> ] --- ## And there you have it! ![](23-visualizing-linear-models_files/figure-html/unnamed-chunk-25-1.png)<!-- --> --- class: inverse ## <svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 640 512"><path d="M624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"/></svg>`Application Exercise` * Take the model you chose in the previous application exercise and create a figure like the one discussed today. Include a caption that explains what the figure demonstrates.