class: left background-image: url(https://images.unsplash.com/photo-1533930086187-0fc58e5a92e2?q=80&w=1587&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D) background-size: cover # Prediction with [{ggsmoothfit}](https://github.com/EvaMaeRey/ggsmoothfit) Gina Reynolds | 2023-10-25 |Image credit: Elena Koycheva, Upsplash ### stat_smooth()'s xseq argument specifies positions at which to make predictions -- ### {ggsmoothfit} piggybacks on StatSmooth, to easily draw fitted values, residuals, and more. <br><br><br><br><br> <br> -- --- ```r library(ggchalkboard) library(tidyverse) theme_minimal <- function(base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, board_color = "darkseagreen4", chalk_color = "lightyellow") { theme_bw( base_size = base_size, base_family = base_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% theme( rect = ggplot2::element_rect(fill = board_color), text = ggplot2::element_text(color = chalk_color, face = "italic", size = 15), panel.background = ggplot2::element_rect(fill = board_color), panel.border = element_blank(), legend.key = ggplot2::element_blank(), legend.title = ggplot2::element_blank(), axis.text = ggplot2::element_text(color = chalk_color), axis.ticks = ggplot2::element_line(color = chalk_color), panel.grid = ggplot2::element_blank(), complete = TRUE ) } theme_set(theme_minimal()) color = "lightyellow"; fill = color ggplot2::update_geom_defaults("point", list(colour = color, size = 2.5, alpha = 0.75)) ggplot2::update_geom_defaults("segment", list(colour = color, linewidth = 1.25, alpha = 0.75)) ggplot2::update_geom_defaults("rug", list(colour = color, size = 1, alpha = 0.75)) ggplot2::update_geom_defaults("rect", list(colour = color, size = 1, alpha = 0.75)) ggplot2::update_geom_defaults("label", list(fill = fill, color = "grey35", size = 5)) ``` --- count: false .panel1-feature-auto[ ```r *ggplot(mtcars) + aes(wt, mpg) ``` ] .panel2-feature-auto[ ![](ggsmoothfit_files/figure-html/feature_auto_01_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + * geom_point() ``` ] .panel2-feature-auto[ ![](ggsmoothfit_files/figure-html/feature_auto_02_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + * geom_smooth(se = F, method = lm) ``` ] .panel2-feature-auto[ ![](ggsmoothfit_files/figure-html/feature_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + geom_smooth(se = F, method = lm) + * ggsmoothfit:::geom_fit(method = lm) ``` ] .panel2-feature-auto[ ![](ggsmoothfit_files/figure-html/feature_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + geom_smooth(se = F, method = lm) + ggsmoothfit:::geom_fit(method = lm) + * ggsmoothfit:::geom_residuals(method = lm) ``` ] .panel2-feature-auto[ ![](ggsmoothfit_files/figure-html/feature_auto_05_output-1.png)<!-- --> ] <style> .panel1-feature-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-feature-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-feature-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- --- count: false .panel1-feature2-auto[ ```r *ggplot(mtcars) + aes(wt, mpg) ``` ] .panel2-feature2-auto[ ![](ggsmoothfit_files/figure-html/feature2_auto_01_output-1.png)<!-- --> ] --- count: false .panel1-feature2-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + * geom_point() + geom_smooth(se = F, method = lm) ``` ] .panel2-feature2-auto[ ![](ggsmoothfit_files/figure-html/feature2_auto_02_output-1.png)<!-- --> ] --- count: false .panel1-feature2-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + geom_smooth(se = F, method = lm) + * ggsmoothfit:::geom_smooth_predict( * xseq = 0, method = lm, size = 5) ``` ] .panel2-feature2-auto[ ![](ggsmoothfit_files/figure-html/feature2_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-feature2-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + geom_smooth(se = F, method = lm) + ggsmoothfit:::geom_smooth_predict( xseq = 0, method = lm, size = 5) + * ggsmoothfit:::geom_smooth_predict( * xseq = 2, method = lm, size = 5) ``` ] .panel2-feature2-auto[ ![](ggsmoothfit_files/figure-html/feature2_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-feature2-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + geom_smooth(se = F, method = lm) + ggsmoothfit:::geom_smooth_predict( xseq = 0, method = lm, size = 5) + ggsmoothfit:::geom_smooth_predict( xseq = 2, method = lm, size = 5) + * ggsmoothfit:::geom_smooth_predict( * xseq = 3, method = lm, size = 5) ``` ] .panel2-feature2-auto[ ![](ggsmoothfit_files/figure-html/feature2_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-feature2-auto[ ```r ggplot(mtcars) + aes(wt, mpg) + geom_point() + geom_smooth(se = F, method = lm) + ggsmoothfit:::geom_smooth_predict( xseq = 0, method = lm, size = 5) + ggsmoothfit:::geom_smooth_predict( xseq = 2, method = lm, size = 5) + ggsmoothfit:::geom_smooth_predict( xseq = 3, method = lm, size = 5) + * ggsmoothfit:::geom_smooth_step( * xseq = 2:3, method = lm) ``` ] .panel2-feature2-auto[ ![](ggsmoothfit_files/figure-html/feature2_auto_06_output-1.png)<!-- --> ] <style> .panel1-feature2-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-feature2-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-feature2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- --- ### Contribute - https://github.com/EvaMaeRey/ggsmoothfit --- ### Check out stat_smooth() which does the computation in ggsmoothfit - https://ggplot2.tidyverse.org/reference/geom_smooth.html - examples with xseq, https://evamaerey.github.io/featurette/2023-10-30-stat-smooth-mtcars/stat-smooth-mtcars.html#10 - xseq documentation discussion https://github.com/tidyverse/ggplot2/issues/5246 --- ### Check out flipbookr, used to build this featurette - https://github.com/EvaMaeRey/flipbookr - discussion: https://github.com/EvaMaeRey/flipbookr/blob/master/docs/draft_jasa_submission.pdf --- ### Check out more featurettes - https://EvaMaeRey.github.io/featurette <style type="text/css"> .remark-code{line-height: 1.5; font-size: 100%} @media print { .has-continuation { display: block; } } </style>