Intro Thoughts

Status Quo

library(tidyverse)
library(ggpmisc)
## Warning: package 'ggpmisc' was built under R version 4.4.1
## Loading required package: ggpp
## Registered S3 methods overwritten by 'ggpp':
##   method                  from   
##   heightDetails.titleGrob ggplot2
##   widthDetails.titleGrob  ggplot2
## 
## Attaching package: 'ggpp'
## The following object is masked from 'package:ggplot2':
## 
##     annotate
anscombe |>
  ggplot() + 
  aes(x2, y2) + 
  geom_point() + 
  geom_smooth(method = lm, formula = y ~ 1) +
  ggpmisc::stat_fit_deviations(geom = ggsprings::GeomSpring,
                               diameter = .3,
                               formula = y ~ 1)
## Warning in ggpmisc::stat_fit_deviations(geom = ggsprings::GeomSpring, diameter
## = 0.3, : Ignoring unknown parameters: `diameter`

compute_fit_mx_plus_b <- function(data, scales, slope = 0, intercept = NULL){
  
  intercept <- intercept %||% mean(data$y)
  
  data |>
    dplyr::mutate(yend = y,
                  y = slope * .data$x + intercept) |> 
    dplyr::mutate(xend = .data$x)
    
  
}


anscombe |>
  rename(x = x2, y = y2) |>
  compute_fit_mx_plus_b()
##    x1  x x3 x4    y1        y    y3    y4 yend xend
## 1  10 10 10  8  8.04 7.500909  7.46  6.58 9.14   10
## 2   8  8  8  8  6.95 7.500909  6.77  5.76 8.14    8
## 3  13 13 13  8  7.58 7.500909 12.74  7.71 8.74   13
## 4   9  9  9  8  8.81 7.500909  7.11  8.84 8.77    9
## 5  11 11 11  8  8.33 7.500909  7.81  8.47 9.26   11
## 6  14 14 14  8  9.96 7.500909  8.84  7.04 8.10   14
## 7   6  6  6  8  7.24 7.500909  6.08  5.25 6.13    6
## 8   4  4  4 19  4.26 7.500909  5.39 12.50 3.10    4
## 9  12 12 12  8 10.84 7.500909  8.15  5.56 9.13   12
## 10  7  7  7  8  4.82 7.500909  6.42  7.91 7.26    7
## 11  5  5  5  8  5.68 7.500909  5.73  6.89 4.74    5
anscombe |>
  ggplot() + 
  aes(x2, y2) + 
  geom_point(aes(y = y2)) + 
  geom_smooth(method = lm, formula = y ~ x) +
  ggpmisc::stat_fit_deviations(geom = ggsprings::GeomSpring,
                               diameter = .3,
                               formula = y ~ x)
## Warning in ggpmisc::stat_fit_deviations(geom = ggsprings::GeomSpring, diameter
## = 0.3, : Ignoring unknown parameters: `diameter`

anscombe |>
  ggplot() + 
  aes(x2 - mean(x2), y2- mean(y2)) + 
  geom_point() -> p

library(statexpress)

p +
  geom_smooth(method = lm, formula = y ~ x) +
  ggpmisc::stat_fit_deviations(geom = ggsprings::GeomSpring,
                               diameter = .3,
                               formula = y ~ x)
## Warning in ggpmisc::stat_fit_deviations(geom = ggsprings::GeomSpring, diameter
## = 0.3, : Ignoring unknown parameters: `diameter`

p + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = ggsprings::GeomSpring) + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = "line")

p + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = ggsprings::GeomSpring
         ,slope = 2) + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = "line", slope = 2)

p + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = ggsprings::GeomSpring
         ,slope = 1) + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = "line", slope = 1) + 
  geom_smooth(method = lm)
## `geom_smooth()` using formula = 'y ~ x'

p + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = ggsprings::GeomSpring
         ,slope = .5) + 
  qlayer(stat = qstat(compute_fit_mx_plus_b),
                      geom = "line", slope = .5) + 
  geom_smooth(method = lm)
## `geom_smooth()` using formula = 'y ~ x'

Closing remarks, Other Relevant Work, Caveats