class: center, middle, inverse, title-slide # Data Visualization ## Working with models ### Andrew Irwin,
a.irwin@dal.ca
### Math & Stats, Dalhousie University ### 2021-02-08 (updated: 2021-02-05) --- class: middle ## Models and visualizations * Adding regression and smoothing lines to ggplot visualizations * Describing, fitting, and using models * Linear regression * Other regression * Generalized additive models * LOESS smooths --- class: middle, inverse ## Examples * Linear regression * Polynomial model * Quantile regression (fitting line to medians or other quantiles) * Generalized additive model (piecewise splines) * LOESS (local linear and quadratic regression) * Putting multiple smooths on one plot * Smooths on facetted plots --- class: middle ### Start with a basic graph ```r mpg %>% mutate(city_l100km = 235.214583 / cty) %>% ggplot(aes(x =displ, y= city_l100km)) + geom_point() + labs(x = "Engine displacement (L)", y = "Fuel economy, city (L/100km)") + my_theme ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" /> --- class: middle ### Add a regression line ```r p + geom_smooth(method = "lm", formula = y ~ x) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> --- class: middle ### Add a regression line for each group ```r p + geom_smooth(aes(color = factor(cyl)), method = "lm", formula = y ~ x) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- class: middle ### Polynomial regression ```r p + geom_smooth(method = "lm", formula = y ~ poly(x,2)) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- class: middle ### Quantile regression ```r p + geom_quantile(method = "rqss", formula = y ~ x, quantiles = c(0.1, 0.5, 0.9)) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- class: middle ### Generalized additive model ```r p + geom_smooth(method = "lm", formula = y ~ splines::bs(x, df = 5)) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- class: middle ### Generalized additive model ```r p + geom_smooth(method = "gam", formula = y ~ s(x, bs="cs")) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- class: middle ### LOESS ```r p + geom_smooth(method = "loess", formula = y ~ x) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- class: middle ### LOESS ```r p + geom_smooth(method = "loess", formula = y ~ x, span = 0.3) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- class: middle ### Putting multiple smooths on one plot ```r p + geom_smooth(aes(color = "Linear"), method = "lm", se=FALSE) + geom_smooth(aes(color = "GAM"), method = "gam", se=FALSE) + geom_smooth(aes(color = "LOESS"), method = "loess", formula = y ~ x, se=FALSE) + labs(color = "Type") ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> --- class: middle ### Smooths on facetted plots ```r p + geom_smooth(method = "lm", se=FALSE, formula = y ~ x) + facet_wrap( ~ class) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- class: middle ### Other customizations ```r p + geom_smooth(method = "lm", se=TRUE, fullrange = TRUE, level = 0.50) + xlim(1,8) ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- class: middle ### Modelling x as a function of y ```r p + geom_smooth(method = "loess", formula = y ~ x, orientation = "y") ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- class: middle ### Modelling x as a function of y ```r p + geom_smooth(method = "lm", formula = y ~ x, orientation = "y") + geom_smooth(method = "lm", formula = y ~ x, color = "red") ``` <img src="15-working-with-models_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> --- class: middle # Summary * Regression lines (based on a model) and "smooths" (no simple model) can be used to draw attention to patterns in data * We are using smooths for their visual effect * Logical reasoning for the lines you draw is important (next lessons) --- class: middle # Further reading * Course notes * Healy Chapter 6. Work with models. --- class: middle, inverse ## Task Bonus task for this lesson to practice these functions. Nothing to hand in.