17 GAM and LOESS smoothing

In this lesson I will show you how to create GAM and LOESS models and perform some basic tasks to interact with the R model objects that the functions create. In keeping with the goals of the course, we will primarily focus on using the models for visualization and not attempt a detailed statistical analysis of when and why you might use a particular model for inference. This means we will restrict our attention to some basic uses of these models using one predictor (x) and one response (y) variable.

17.1 Generalized Additive Models

Generalized additive models are a kind of linear regression, but instead of finding coefficients of predictor variables (e.g., intercepts, slopes), the model finds a “smooth” response function for each predictor. Typically this means that a piecewise cubic function (spline) is used to approximate the relationship between two variables. We can compute predicted values, confindence and prediction intervals, and show the smooth response function that arose from the model. We don’t provide a simple list of coefficients like we did with linear regression, because the spline curve is defined by many numbers which are not usually informative on their own.

Generalized additive models are most commonly used when there is no theoretical motivation for a functional relationship between the variables being studied. We’ll look at the Mauna Loa atmospheric CO2 concentration. These data increase year over year and have well-established interannual oscillations, but there is no clear function for either of these patterns.

We will start by using the last decade of data from Lesson 1.

Here is a plot of atmospheric CO2 concentration over time.

my_theme = theme_linedraw() + theme(text = element_text(size = 14))
p1 <- co2 %>% ggplot(aes(decimal_date, co2_avg)) + geom_point() + my_theme +
  labs(x = "Year (decimal)", y = "Atmospheric CO2 (ppm)")
p1

The next plot shows each observation, minus the annual mean, as a function of the time of year but not the actual year. Each year’s observations are overlapped.

p2 <- co2 %>% ggplot(aes(year_fraction, co2_anomaly)) + geom_point() + my_theme +
  labs(x = "Year fraction (decimal)", y = "Atmospheric CO2 anomaly (ppm)")
p2

Here are GAM fits to both of these pairs of data using the gam function from the mgcv package. There is a plot function in the mgcv package, but I’m using a ggplot function called draw in the gratia package.

g1 <- gam(co2_anomaly ~ s(year_fraction, bs="cs"), data = co2)
draw(g1, residuals=TRUE, rug=FALSE)
g2 <- gam(co2_avg ~ s(decimal_date, bs="cs"),  data = co2)
draw(g2, residuals=TRUE, rug=FALSE) 

Depending on your goal, you may find that the second plot is too smooth – the interannual oscillations are smoothed out completely. We can increase the number of “knots” (by setting k=25) in the spline to capture the oscillation, but this is not the way the smoothing is normally used:

g3 <- gam(co2_avg ~ s(decimal_date, bs="cs", k = 25),  data = co2)
draw(g3, residuals=TRUE, rug=FALSE) 

There is a summary function for GAM fits and the broom functions glance and tidy give access to these data, but the interpretation of this output is beyond the scope of the course:

## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## co2_anomaly ~ s(year_fraction, bs = "cs")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.364e-15  2.673e-02       0        1
## 
## Approximate significance of smooth terms:
##                    edf Ref.df     F p-value    
## s(year_fraction) 8.296      9 530.7  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.976   Deviance explained = 97.8%
## GCV = 0.092244  Scale est. = 0.085038  n = 119
glance(g1) %>% kable()
df logLik AIC BIC deviance df.residual nobs
9.296072 -17.3669 55.32595 83.94001 9.328994 109.7039 119
tidy(g1) %>% kable()
term edf ref.df statistic p.value
s(year_fraction) 8.296072 9 530.7013 0

It is often useful to compute residuals and extract predicted values.

co2 %>% add_residuals(g1) %>% add_fitted(g1) %>% kable() %>%  scroll_box(height = "200px")
year month decimal_date co2_avg year_fraction co2_anomaly .residual .value
2011 1 2011.042 391.33 0.0417 -0.3216667 0.4287396 -0.75040623
2011 2 2011.125 391.86 0.1250 0.2083333 0.3028578 -0.09452442
2011 3 2011.208 392.60 0.2083 0.9483333 0.1440604 0.80427289
2011 4 2011.292 393.25 0.2917 1.5983333 -0.6010968 2.19943018
2011 5 2011.375 394.19 0.3750 2.5383333 -0.5071870 3.04552036
2011 6 2011.458 393.74 0.4583 2.0883333 -0.1757651 2.26409839
2011 7 2011.542 392.51 0.5417 0.8583333 0.3875911 0.47074220
2011 8 2011.625 390.13 0.6250 -1.5216667 0.0541267 -1.57579337
2011 9 2011.708 389.08 0.7083 -2.5716667 0.3920488 -2.96371550
2011 10 2011.792 388.99 0.7917 -2.6616667 0.0151298 -2.67679645
2011 11 2011.875 390.28 0.8750 -1.3716667 -0.2731017 -1.09856499
2011 12 2011.958 391.86 0.9583 0.2083333 -0.2091522 0.41748550
2012 1 2012.042 393.12 0.0417 -0.7350000 0.0154062 -0.75040623
2012 2 2012.125 393.86 0.1250 0.0050000 0.0995244 -0.09452442
2012 3 2012.208 394.40 0.2083 0.5450000 -0.2592729 0.80427289
2012 4 2012.292 396.18 0.2917 2.3250000 0.1255698 2.19943018
2012 5 2012.375 396.74 0.3750 2.8850000 -0.1605204 3.04552036
2012 6 2012.458 395.71 0.4583 1.8550000 -0.4090984 2.26409839
2012 7 2012.542 394.36 0.5417 0.5050000 0.0342578 0.47074220
2012 8 2012.625 392.39 0.6250 -1.4650000 0.1107934 -1.57579337
2012 9 2012.708 391.13 0.7083 -2.7250000 0.2387155 -2.96371550
2012 10 2012.792 391.05 0.7917 -2.8050000 -0.1282035 -2.67679645
2012 11 2012.875 392.98 0.8750 -0.8750000 0.2235650 -1.09856499
2012 12 2012.958 394.34 0.9583 0.4850000 0.0675145 0.41748550
2013 1 2013.042 395.55 0.0417 -0.9700000 -0.2195938 -0.75040623
2013 2 2013.125 396.80 0.1250 0.2800000 0.3745244 -0.09452442
2013 3 2013.208 397.43 0.2083 0.9100000 0.1057271 0.80427289
2013 4 2013.292 398.41 0.2917 1.8900000 -0.3094302 2.19943018
2013 5 2013.375 399.78 0.3750 3.2600000 0.2144796 3.04552036
2013 6 2013.458 398.60 0.4583 2.0800000 -0.1840984 2.26409839
2013 7 2013.542 397.32 0.5417 0.8000000 0.3292578 0.47074220
2013 8 2013.625 395.20 0.6250 -1.3200000 0.2557934 -1.57579337
2013 9 2013.708 393.45 0.7083 -3.0700000 -0.1062845 -2.96371550
2013 10 2013.792 393.70 0.7917 -2.8200000 -0.1432035 -2.67679645
2013 11 2013.875 395.16 0.8750 -1.3600000 -0.2614350 -1.09856499
2013 12 2013.958 396.84 0.9583 0.3200000 -0.0974855 0.41748550
2014 1 2014.042 397.85 0.0417 -0.7925000 -0.0420938 -0.75040623
2014 2 2014.125 398.01 0.1250 -0.6325000 -0.5379756 -0.09452442
2014 3 2014.208 399.71 0.2083 1.0675000 0.2632271 0.80427289
2014 4 2014.292 401.33 0.2917 2.6875000 0.4880698 2.19943018
2014 5 2014.375 401.78 0.3750 3.1375000 0.0919796 3.04552036
2014 6 2014.458 401.25 0.4583 2.6075000 0.3434016 2.26409839
2014 7 2014.542 399.11 0.5417 0.4675000 -0.0032422 0.47074220
2014 8 2014.625 397.03 0.6250 -1.6125000 -0.0367066 -1.57579337
2014 9 2014.708 395.38 0.7083 -3.2625000 -0.2987845 -2.96371550
2014 10 2014.792 396.07 0.7917 -2.5725000 0.1042965 -2.67679645
2014 11 2014.875 397.28 0.8750 -1.3625000 -0.2639350 -1.09856499
2014 12 2014.958 398.91 0.9583 0.2675000 -0.1499855 0.41748550
2015 1 2015.042 399.98 0.0417 -0.8458333 -0.0954271 -0.75040623
2015 2 2015.125 400.35 0.1250 -0.4758333 -0.3813089 -0.09452442
2015 3 2015.208 401.52 0.2083 0.6941667 -0.1101062 0.80427289
2015 4 2015.292 403.15 0.2917 2.3241667 0.1247365 2.19943018
2015 5 2015.375 403.96 0.3750 3.1341667 0.0886463 3.04552036
2015 6 2015.458 402.80 0.4583 1.9741667 -0.2899317 2.26409839
2015 7 2015.542 401.29 0.5417 0.4641667 -0.0065755 0.47074220
2015 8 2015.625 398.93 0.6250 -1.8958333 -0.3200400 -1.57579337
2015 9 2015.708 397.63 0.7083 -3.1958333 -0.2321178 -2.96371550
2015 10 2015.792 398.29 0.7917 -2.5358333 0.1409631 -2.67679645
2015 11 2015.875 400.16 0.8750 -0.6658333 0.4327317 -1.09856499
2015 12 2015.958 401.85 0.9583 1.0241667 0.6066812 0.41748550
2016 1 2016.042 402.50 0.0417 -1.7225000 -0.9720938 -0.75040623
2016 2 2016.125 404.07 0.1250 -0.1525000 -0.0579756 -0.09452442
2016 3 2016.208 404.87 0.2083 0.6475000 -0.1567729 0.80427289
2016 4 2016.292 407.42 0.2917 3.1975000 0.9980698 2.19943018
2016 5 2016.375 407.72 0.3750 3.4975000 0.4519796 3.04552036
2016 6 2016.458 406.81 0.4583 2.5875000 0.3234016 2.26409839
2016 7 2016.542 404.40 0.5417 0.1775000 -0.2932422 0.47074220
2016 8 2016.625 402.26 0.6250 -1.9625000 -0.3867066 -1.57579337
2016 9 2016.708 401.05 0.7083 -3.1725000 -0.2087845 -2.96371550
2016 10 2016.792 401.60 0.7917 -2.6225000 0.0542965 -2.67679645
2016 11 2016.875 403.53 0.8750 -0.6925000 0.4060650 -1.09856499
2016 12 2016.958 404.44 0.9583 0.2175000 -0.1999855 0.41748550
2017 1 2017.042 406.17 0.0417 -0.3850000 0.3654062 -0.75040623
2017 2 2017.125 406.47 0.1250 -0.0850000 0.0095244 -0.09452442
2017 3 2017.208 407.23 0.2083 0.6750000 -0.1292729 0.80427289
2017 4 2017.292 409.03 0.2917 2.4750000 0.2755698 2.19943018
2017 5 2017.375 409.69 0.3750 3.1350000 0.0894796 3.04552036
2017 6 2017.458 408.89 0.4583 2.3350000 0.0709016 2.26409839
2017 7 2017.542 407.13 0.5417 0.5750000 0.1042578 0.47074220
2017 8 2017.625 405.12 0.6250 -1.4350000 0.1407934 -1.57579337
2017 9 2017.708 403.37 0.7083 -3.1850000 -0.2212845 -2.96371550
2017 10 2017.792 403.63 0.7917 -2.9250000 -0.2482035 -2.67679645
2017 11 2017.875 405.12 0.8750 -1.4350000 -0.3364350 -1.09856499
2017 12 2017.958 406.81 0.9583 0.2550000 -0.1624855 0.41748550
2018 1 2018.042 407.96 0.0417 -0.5600000 0.1904062 -0.75040623
2018 2 2018.125 408.32 0.1250 -0.2000000 -0.1054756 -0.09452442
2018 3 2018.208 409.39 0.2083 0.8700000 0.0657271 0.80427289
2018 4 2018.292 410.25 0.2917 1.7300000 -0.4694302 2.19943018
2018 5 2018.375 411.24 0.3750 2.7200000 -0.3255204 3.04552036
2018 6 2018.458 410.79 0.4583 2.2700000 0.0059016 2.26409839
2018 7 2018.542 408.70 0.5417 0.1800000 -0.2907422 0.47074220
2018 8 2018.625 406.97 0.6250 -1.5500000 0.0257934 -1.57579337
2018 9 2018.708 405.52 0.7083 -3.0000000 -0.0362845 -2.96371550
2018 10 2018.792 406.00 0.7917 -2.5200000 0.1567965 -2.67679645
2018 11 2018.875 408.02 0.8750 -0.5000000 0.5985650 -1.09856499
2018 12 2018.958 409.08 0.9583 0.5600000 0.1425145 0.41748550
2019 1 2019.042 410.83 0.0417 -0.6041667 0.1462396 -0.75040623
2019 2 2019.125 411.75 0.1250 0.3158333 0.4103578 -0.09452442
2019 3 2019.208 411.97 0.2083 0.5358333 -0.2684396 0.80427289
2019 4 2019.292 413.33 0.2917 1.8958333 -0.3035968 2.19943018
2019 5 2019.375 414.64 0.3750 3.2058333 0.1603130 3.04552036
2019 6 2019.458 413.93 0.4583 2.4958333 0.2317349 2.26409839
2019 7 2019.542 411.74 0.5417 0.3058333 -0.1649089 0.47074220
2019 8 2019.625 409.95 0.6250 -1.4841667 0.0916267 -1.57579337
2019 9 2019.708 408.54 0.7083 -2.8941667 0.0695488 -2.96371550
2019 10 2019.792 408.52 0.7917 -2.9141667 -0.2373702 -2.67679645
2019 11 2019.875 410.25 0.8750 -1.1841667 -0.0856017 -1.09856499
2019 12 2019.958 411.76 0.9583 0.3258333 -0.0916522 0.41748550
2020 1 2020.042 413.39 0.0417 -0.6154545 0.1349517 -0.75040623
2020 2 2020.125 414.11 0.1250 0.1045455 0.1990699 -0.09452442
2020 3 2020.208 414.51 0.2083 0.5045455 -0.2997274 0.80427289
2020 4 2020.292 416.21 0.2917 2.2045455 0.0051153 2.19943018
2020 5 2020.375 417.07 0.3750 3.0645455 0.0190251 3.04552036
2020 6 2020.458 416.38 0.4583 2.3745455 0.1104471 2.26409839
2020 7 2020.542 414.38 0.5417 0.3745455 -0.0961967 0.47074220
2020 8 2020.625 412.55 0.6250 -1.4554545 0.1203388 -1.57579337
2020 9 2020.708 411.29 0.7083 -2.7154545 0.2482610 -2.96371550
2020 10 2020.792 411.28 0.7917 -2.7254545 -0.0486581 -2.67679645
2020 11 2020.875 412.89 0.8750 -1.1154545 -0.0168896 -1.09856499

If we generate new data (dates), we can plot predictions too. Since the model is a piecewise cubic function, extrapolations are often dramatically unreliable. The downward facing cubic in the last “bump” is simply continued, with comically bad results. Extrapolation of models, and especially smooths, is somewhere between risky and foolish!

new_data <- tibble(decimal_date = seq(2017, 2022, by = 0.05))
new_data %>% add_fitted(g3) %>%
  ggplot(aes(decimal_date, .value)) + 
  geom_line(color = "blue", size = 2) +
  geom_point(aes(y= co2_avg), data = co2) + my_theme + xlim(2017, 2022)

If you want confidence intervals on the fitted values, use the confint function together with the name of the smooth you are extracting. Be aware that this function does not include the intercept (or grand mean) from the model, so the values are all centred on zero.

confint(g1, "s(year_fraction)", level = 0.95) %>% kable() %>%  scroll_box(height = "200px")
smooth by_variable year_fraction est se crit lower upper
s(year_fraction) NA 0.0417000 -0.7504062 0.0911643 1.959964 -0.9290849 -0.5717275
s(year_fraction) NA 0.0463060 -0.7162744 0.0858056 1.959964 -0.8844503 -0.5480985
s(year_fraction) NA 0.0509121 -0.6821033 0.0810032 1.959964 -0.8408666 -0.5233401
s(year_fraction) NA 0.0555181 -0.6478537 0.0768560 1.959964 -0.7984886 -0.4972188
s(year_fraction) NA 0.0601241 -0.6134862 0.0734532 1.959964 -0.7574518 -0.4695205
s(year_fraction) NA 0.0647302 -0.5789615 0.0708632 1.959964 -0.7178509 -0.4400722
s(year_fraction) NA 0.0693362 -0.5442405 0.0691214 1.959964 -0.6797160 -0.4087649
s(year_fraction) NA 0.0739422 -0.5092837 0.0682211 1.959964 -0.6429946 -0.3755729
s(year_fraction) NA 0.0785482 -0.4740520 0.0681097 1.959964 -0.6075445 -0.3405594
s(year_fraction) NA 0.0831543 -0.4385060 0.0686939 1.959964 -0.5731435 -0.3038685
s(year_fraction) NA 0.0877603 -0.4026064 0.0698502 1.959964 -0.5395102 -0.2657026
s(year_fraction) NA 0.0923663 -0.3663140 0.0714390 1.959964 -0.5063319 -0.2262961
s(year_fraction) NA 0.0969724 -0.3295895 0.0733172 1.959964 -0.4732886 -0.1858904
s(year_fraction) NA 0.1015784 -0.2923936 0.0753476 1.959964 -0.4400721 -0.1447150
s(year_fraction) NA 0.1061844 -0.2546870 0.0774045 1.959964 -0.4063970 -0.1029770
s(year_fraction) NA 0.1107905 -0.2164304 0.0793762 1.959964 -0.3720049 -0.0608559
s(year_fraction) NA 0.1153965 -0.1775846 0.0811657 1.959964 -0.3366665 -0.0185027
s(year_fraction) NA 0.1200025 -0.1381102 0.0826900 1.959964 -0.3001797 0.0239592
s(year_fraction) NA 0.1246085 -0.0979681 0.0838795 1.959964 -0.2623688 0.0664327
s(year_fraction) NA 0.1292146 -0.0571188 0.0846774 1.959964 -0.2230834 0.1088458
s(year_fraction) NA 0.1338206 -0.0155231 0.0850398 1.959964 -0.1821981 0.1511518
s(year_fraction) NA 0.1384266 0.0268582 0.0849363 1.959964 -0.1396139 0.1933303
s(year_fraction) NA 0.1430327 0.0700645 0.0843518 1.959964 -0.0952620 0.2353909
s(year_fraction) NA 0.1476387 0.1141377 0.0832991 1.959964 -0.0491256 0.2774010
s(year_fraction) NA 0.1522447 0.1591349 0.0818624 1.959964 -0.0013124 0.3195822
s(year_fraction) NA 0.1568508 0.2051180 0.0801551 1.959964 0.0480169 0.3622191
s(year_fraction) NA 0.1614568 0.2521491 0.0782995 1.959964 0.0986849 0.4056132
s(year_fraction) NA 0.1660628 0.3002902 0.0764232 1.959964 0.1505034 0.4500769
s(year_fraction) NA 0.1706688 0.3496033 0.0746556 1.959964 0.2032810 0.4959256
s(year_fraction) NA 0.1752749 0.4001506 0.0731215 1.959964 0.2568350 0.5434661
s(year_fraction) NA 0.1798809 0.4519940 0.0719334 1.959964 0.3110071 0.5929809
s(year_fraction) NA 0.1844869 0.5051955 0.0711820 1.959964 0.3656813 0.6447098
s(year_fraction) NA 0.1890930 0.5598173 0.0709273 1.959964 0.4208023 0.6988322
s(year_fraction) NA 0.1936990 0.6159213 0.0711913 1.959964 0.4763889 0.7554537
s(year_fraction) NA 0.1983050 0.6735696 0.0719558 1.959964 0.5325388 0.8146003
s(year_fraction) NA 0.2029111 0.7328242 0.0731645 1.959964 0.5894244 0.8762239
s(year_fraction) NA 0.2075171 0.7937471 0.0747300 1.959964 0.6472790 0.9402152
s(year_fraction) NA 0.2121231 0.8564004 0.0765431 1.959964 0.7063787 1.0064222
s(year_fraction) NA 0.2167291 0.9208462 0.0784822 1.959964 0.7670239 1.0746685
s(year_fraction) NA 0.2213352 0.9871464 0.0804214 1.959964 0.8295234 1.1447694
s(year_fraction) NA 0.2259412 1.0553631 0.0822369 1.959964 0.8941819 1.2165444
s(year_fraction) NA 0.2305472 1.1255584 0.0838113 1.959964 0.9612912 1.2898256
s(year_fraction) NA 0.2351533 1.1977942 0.0850372 1.959964 1.0311244 1.3644640
s(year_fraction) NA 0.2397593 1.2721327 0.0858192 1.959964 1.1039302 1.4403352
s(year_fraction) NA 0.2443653 1.3486358 0.0860772 1.959964 1.1799275 1.5173440
s(year_fraction) NA 0.2489714 1.4273300 0.0857595 1.959964 1.2592444 1.5954155
s(year_fraction) NA 0.2535774 1.5079637 0.0849111 1.959964 1.3415409 1.6743864
s(year_fraction) NA 0.2581834 1.5901544 0.0836310 1.959964 1.4262406 1.7540681
s(year_fraction) NA 0.2627894 1.6735188 0.0820324 1.959964 1.5127382 1.8342994
s(year_fraction) NA 0.2673955 1.7576738 0.0802399 1.959964 1.6004064 1.9149412
s(year_fraction) NA 0.2720015 1.8422362 0.0783857 1.959964 1.6886031 1.9958692
s(year_fraction) NA 0.2766075 1.9268225 0.0766047 1.959964 1.7766800 2.0769650
s(year_fraction) NA 0.2812136 2.0110497 0.0750289 1.959964 1.8639958 2.1581037
s(year_fraction) NA 0.2858196 2.0945345 0.0737781 1.959964 1.9499320 2.2391370
s(year_fraction) NA 0.2904256 2.1768937 0.0729507 1.959964 2.0339128 2.3198745
s(year_fraction) NA 0.2950317 2.2577439 0.0726133 1.959964 2.1154244 2.4000634
s(year_fraction) NA 0.2996377 2.3367020 0.0727931 1.959964 2.1940301 2.4793739
s(year_fraction) NA 0.3042437 2.4133848 0.0734745 1.959964 2.2693774 2.5573922
s(year_fraction) NA 0.3088497 2.4874090 0.0746011 1.959964 2.3411936 2.6336244
s(year_fraction) NA 0.3134558 2.5583913 0.0760825 1.959964 2.4092725 2.7075102
s(year_fraction) NA 0.3180618 2.6259486 0.0778040 1.959964 2.4734555 2.7784417
s(year_fraction) NA 0.3226678 2.6896975 0.0796371 1.959964 2.5336118 2.8457833
s(year_fraction) NA 0.3272739 2.7492550 0.0814477 1.959964 2.5896204 2.9088895
s(year_fraction) NA 0.3318799 2.8042376 0.0831041 1.959964 2.6413566 2.9671186
s(year_fraction) NA 0.3364859 2.8542622 0.0844814 1.959964 2.6886817 3.0198427
s(year_fraction) NA 0.3410920 2.8989455 0.0854659 1.959964 2.7314354 3.0664557
s(year_fraction) NA 0.3456980 2.9379044 0.0859585 1.959964 2.7694288 3.1063800
s(year_fraction) NA 0.3503040 2.9707810 0.0858845 1.959964 2.8024505 3.1391114
s(year_fraction) NA 0.3549101 2.9975136 0.0852632 1.959964 2.8304007 3.1646265
s(year_fraction) NA 0.3595161 3.0182316 0.0841816 1.959964 2.8532388 3.1832245
s(year_fraction) NA 0.3641221 3.0330676 0.0827435 1.959964 2.8708933 3.1952418
s(year_fraction) NA 0.3687281 3.0421539 0.0810659 1.959964 2.8832676 3.2010401
s(year_fraction) NA 0.3733342 3.0456231 0.0792748 1.959964 2.8902473 3.2009988
s(year_fraction) NA 0.3779402 3.0436077 0.0775011 1.959964 2.8917084 3.1955071
s(year_fraction) NA 0.3825462 3.0362403 0.0758747 1.959964 2.8875286 3.1849521
s(year_fraction) NA 0.3871523 3.0236534 0.0745173 1.959964 2.8776022 3.1697045
s(year_fraction) NA 0.3917583 3.0059794 0.0735325 1.959964 2.8618583 3.1501005
s(year_fraction) NA 0.3963643 2.9833509 0.0729971 1.959964 2.8402792 3.1264225
s(year_fraction) NA 0.4009704 2.9559004 0.0729517 1.959964 2.8129177 3.0988830
s(year_fraction) NA 0.4055764 2.9237603 0.0733964 1.959964 2.7799061 3.0676146
s(year_fraction) NA 0.4101824 2.8870633 0.0742905 1.959964 2.7414567 3.0326700
s(year_fraction) NA 0.4147884 2.8459419 0.0755574 1.959964 2.6978521 2.9940316
s(year_fraction) NA 0.4193945 2.8005284 0.0770931 1.959964 2.6494288 2.9516281
s(year_fraction) NA 0.4240005 2.7509556 0.0787759 1.959964 2.5965576 2.9053535
s(year_fraction) NA 0.4286065 2.6973558 0.0804760 1.959964 2.5396258 2.8550857
s(year_fraction) NA 0.4332126 2.6398615 0.0820626 1.959964 2.4790217 2.8007013
s(year_fraction) NA 0.4378186 2.5786054 0.0834106 1.959964 2.4151236 2.7420872
s(year_fraction) NA 0.4424246 2.5137199 0.0844044 1.959964 2.3482902 2.6791495
s(year_fraction) NA 0.4470307 2.4453374 0.0849424 1.959964 2.2788533 2.6118216
s(year_fraction) NA 0.4516367 2.3735903 0.0849444 1.959964 2.2071024 2.5400783
s(year_fraction) NA 0.4562427 2.2986049 0.0844157 1.959964 2.1331531 2.4640567
s(year_fraction) NA 0.4608487 2.2205026 0.0834376 1.959964 2.0569679 2.3840372
s(year_fraction) NA 0.4654548 2.1394047 0.0821100 1.959964 1.9784720 2.3003374
s(year_fraction) NA 0.4700608 2.0554325 0.0805465 1.959964 1.8975644 2.2133007
s(year_fraction) NA 0.4746668 1.9687074 0.0788695 1.959964 1.8141261 2.1232887
s(year_fraction) NA 0.4792729 1.8793506 0.0772064 1.959964 1.7280288 2.0306724
s(year_fraction) NA 0.4838789 1.7874834 0.0756838 1.959964 1.6391458 1.9358210
s(year_fraction) NA 0.4884849 1.6932271 0.0744196 1.959964 1.5473674 1.8390869
s(year_fraction) NA 0.4930910 1.5967031 0.0735144 1.959964 1.4526175 1.7407886
s(year_fraction) NA 0.4976970 1.4980325 0.0730422 1.959964 1.3548725 1.6411925
s(year_fraction) NA 0.5023030 1.3973368 0.0730422 1.959964 1.2541766 1.5404969
s(year_fraction) NA 0.5069090 1.2947372 0.0735146 1.959964 1.1506512 1.4388231
s(year_fraction) NA 0.5115151 1.1903549 0.0744199 1.959964 1.0444946 1.3362153
s(year_fraction) NA 0.5161211 1.0843114 0.0756842 1.959964 0.9359731 1.2326497
s(year_fraction) NA 0.5207271 0.9767279 0.0772069 1.959964 0.8254052 1.1280506
s(year_fraction) NA 0.5253332 0.8677257 0.0788699 1.959964 0.7131435 1.0223079
s(year_fraction) NA 0.5299392 0.7574261 0.0805469 1.959964 0.5995570 0.9152952
s(year_fraction) NA 0.5345452 0.6459504 0.0821105 1.959964 0.4850167 0.8068840
s(year_fraction) NA 0.5391513 0.5334198 0.0834380 1.959964 0.3698844 0.6969553
s(year_fraction) NA 0.5437573 0.4199558 0.0844161 1.959964 0.2545033 0.5854083
s(year_fraction) NA 0.5483633 0.3056796 0.0849446 1.959964 0.1391911 0.4721680
s(year_fraction) NA 0.5529693 0.1907130 0.0849426 1.959964 0.0242286 0.3571974
s(year_fraction) NA 0.5575754 0.0751960 0.0844045 1.959964 -0.0902337 0.2406258
s(year_fraction) NA 0.5621814 -0.0407101 0.0834106 1.959964 -0.2041918 0.1227716
s(year_fraction) NA 0.5667874 -0.1568430 0.0820625 1.959964 -0.3176826 0.0039967
s(year_fraction) NA 0.5713935 -0.2730401 0.0804759 1.959964 -0.4307699 -0.1153103
s(year_fraction) NA 0.5759995 -0.3891390 0.0787759 1.959964 -0.5435369 -0.2347412
s(year_fraction) NA 0.5806055 -0.5049773 0.0770931 1.959964 -0.6560770 -0.3538776
s(year_fraction) NA 0.5852116 -0.6203925 0.0755576 1.959964 -0.7684827 -0.4723024
s(year_fraction) NA 0.5898176 -0.7352222 0.0742909 1.959964 -0.8808297 -0.5896147
s(year_fraction) NA 0.5944236 -0.8493039 0.0733971 1.959964 -0.9931595 -0.7054483
s(year_fraction) NA 0.5990296 -0.9624751 0.0729527 1.959964 -1.1054597 -0.8194905
s(year_fraction) NA 0.6036357 -1.0745735 0.0729984 1.959964 -1.2176477 -0.9314992
s(year_fraction) NA 0.6082417 -1.1854364 0.0735342 1.959964 -1.3295608 -1.0413121
s(year_fraction) NA 0.6128477 -1.2949016 0.0745192 1.959964 -1.4409565 -1.1488467
s(year_fraction) NA 0.6174538 -1.4028066 0.0758769 1.959964 -1.5515225 -1.2540906
s(year_fraction) NA 0.6220598 -1.5089888 0.0775034 1.959964 -1.6608926 -1.3570850
s(year_fraction) NA 0.6266658 -1.6132859 0.0792771 1.959964 -1.7686662 -1.4579056
s(year_fraction) NA 0.6312719 -1.7155353 0.0810682 1.959964 -1.8744261 -1.5566446
s(year_fraction) NA 0.6358779 -1.8155748 0.0827457 1.959964 -1.9777533 -1.6533962
s(year_fraction) NA 0.6404839 -1.9132417 0.0841835 1.959964 -2.0782384 -1.7482449
s(year_fraction) NA 0.6450899 -2.0083736 0.0852649 1.959964 -2.1754898 -1.8412574
s(year_fraction) NA 0.6496960 -2.1008081 0.0858858 1.959964 -2.2691412 -1.9324750
s(year_fraction) NA 0.6543020 -2.1903826 0.0859595 1.959964 -2.3588600 -2.0219052
s(year_fraction) NA 0.6589080 -2.2769242 0.0854665 1.959964 -2.4444355 -2.1094129
s(year_fraction) NA 0.6635141 -2.3602442 0.0844816 1.959964 -2.5258252 -2.1946632
s(year_fraction) NA 0.6681201 -2.4401525 0.0831041 1.959964 -2.6030336 -2.2772714
s(year_fraction) NA 0.6727261 -2.5164591 0.0814477 1.959964 -2.6760936 -2.3568245
s(year_fraction) NA 0.6773322 -2.5889738 0.0796372 1.959964 -2.7450599 -2.4328877
s(year_fraction) NA 0.6819382 -2.6575066 0.0778046 1.959964 -2.8100008 -2.5050124
s(year_fraction) NA 0.6865442 -2.7218674 0.0760837 1.959964 -2.8709887 -2.5727460
s(year_fraction) NA 0.6911503 -2.7818661 0.0746033 1.959964 -2.9280859 -2.6356464
s(year_fraction) NA 0.6957563 -2.8373127 0.0734779 1.959964 -2.9813267 -2.6932986
s(year_fraction) NA 0.7003623 -2.8880170 0.0727979 1.959964 -3.0306982 -2.7453359
s(year_fraction) NA 0.7049683 -2.9337891 0.0726195 1.959964 -3.0761206 -2.7914576
s(year_fraction) NA 0.7095744 -2.9744387 0.0729582 1.959964 -3.1174342 -2.8314433
s(year_fraction) NA 0.7141804 -3.0097759 0.0737867 1.959964 -3.1543953 -2.8651566
s(year_fraction) NA 0.7187864 -3.0396106 0.0750383 1.959964 -3.1866830 -2.8925382
s(year_fraction) NA 0.7233925 -3.0637526 0.0766146 1.959964 -3.2139146 -2.9135907
s(year_fraction) NA 0.7279985 -3.0820120 0.0783956 1.959964 -3.2356646 -2.9283594
s(year_fraction) NA 0.7326045 -3.0941986 0.0802496 1.959964 -3.2514848 -2.9369123
s(year_fraction) NA 0.7372106 -3.1001223 0.0820412 1.959964 -3.2609202 -2.9393244
s(year_fraction) NA 0.7418166 -3.0995931 0.0836386 1.959964 -3.2635218 -2.9356643
s(year_fraction) NA 0.7464226 -3.0924208 0.0849173 1.959964 -3.2588557 -2.9259860
s(year_fraction) NA 0.7510286 -3.0784155 0.0857640 1.959964 -3.2465098 -2.9103212
s(year_fraction) NA 0.7556347 -3.0573880 0.0860800 1.959964 -3.2261017 -2.8886744
s(year_fraction) NA 0.7602407 -3.0293129 0.0858204 1.959964 -3.1975178 -2.8611079
s(year_fraction) NA 0.7648467 -2.9945125 0.0850374 1.959964 -3.1611827 -2.8278422
s(year_fraction) NA 0.7694528 -2.9533538 0.0838114 1.959964 -3.1176211 -2.7890865
s(year_fraction) NA 0.7740588 -2.9062038 0.0822379 1.959964 -3.0673870 -2.7450205
s(year_fraction) NA 0.7786648 -2.8534294 0.0804247 1.959964 -3.0110589 -2.6957998
s(year_fraction) NA 0.7832709 -2.7953975 0.0784894 1.959964 -2.9492338 -2.6415611
s(year_fraction) NA 0.7878769 -2.7324750 0.0765558 1.959964 -2.8825215 -2.5824284
s(year_fraction) NA 0.7924829 -2.6650289 0.0747497 1.959964 -2.8115356 -2.5185223
s(year_fraction) NA 0.7970889 -2.5934262 0.0731924 1.959964 -2.7368807 -2.4499717
s(year_fraction) NA 0.8016950 -2.5180337 0.0719928 1.959964 -2.6591369 -2.3769304
s(year_fraction) NA 0.8063010 -2.4392184 0.0712375 1.959964 -2.5788412 -2.2995955
s(year_fraction) NA 0.8109070 -2.3573472 0.0709819 1.959964 -2.4964691 -2.2182253
s(year_fraction) NA 0.8155131 -2.2727871 0.0712435 1.959964 -2.4124217 -2.1331524
s(year_fraction) NA 0.8201191 -2.1859049 0.0719994 1.959964 -2.3270212 -2.0447886
s(year_fraction) NA 0.8247251 -2.0970677 0.0731893 1.959964 -2.2405162 -1.9536192
s(year_fraction) NA 0.8293312 -2.0066423 0.0747221 1.959964 -2.1530950 -1.8601897
s(year_fraction) NA 0.8339372 -1.9149958 0.0764853 1.959964 -2.0649042 -1.7650873
s(year_fraction) NA 0.8385432 -1.8224949 0.0783544 1.959964 -1.9760666 -1.6689232
s(year_fraction) NA 0.8431492 -1.7295067 0.0802005 1.959964 -1.8866967 -1.5723167
s(year_fraction) NA 0.8477553 -1.6363981 0.0818966 1.959964 -1.7969125 -1.4758838
s(year_fraction) NA 0.8523613 -1.5435361 0.0833217 1.959964 -1.7068435 -1.3802286
s(year_fraction) NA 0.8569673 -1.4512874 0.0843634 1.959964 -1.6166366 -1.2859381
s(year_fraction) NA 0.8615734 -1.3599274 0.0849396 1.959964 -1.5264060 -1.1934488
s(year_fraction) NA 0.8661794 -1.2694627 0.0850398 1.959964 -1.4361376 -1.1027877
s(year_fraction) NA 0.8707854 -1.1798505 0.0846819 1.959964 -1.3458239 -1.0138771
s(year_fraction) NA 0.8753915 -1.0910483 0.0838995 1.959964 -1.2554883 -0.9266084
s(year_fraction) NA 0.8799975 -1.0030134 0.0827403 1.959964 -1.1651815 -0.8408454
s(year_fraction) NA 0.8846035 -0.9157033 0.0812655 1.959964 -1.0749807 -0.7564259
s(year_fraction) NA 0.8892095 -0.8290752 0.0795495 1.959964 -0.9849894 -0.6731610
s(year_fraction) NA 0.8938156 -0.7430866 0.0776811 1.959964 -0.8953386 -0.5908345
s(year_fraction) NA 0.8984216 -0.6576947 0.0757629 1.959964 -0.8061873 -0.5092021
s(year_fraction) NA 0.9030276 -0.5728571 0.0739126 1.959964 -0.7177231 -0.4279911
s(year_fraction) NA 0.9076337 -0.4885310 0.0722605 1.959964 -0.6301589 -0.3469031
s(year_fraction) NA 0.9122397 -0.4046738 0.0709465 1.959964 -0.5437264 -0.2656213
s(year_fraction) NA 0.9168457 -0.3212429 0.0701129 1.959964 -0.4586617 -0.1838242
s(year_fraction) NA 0.9214518 -0.2381957 0.0698940 1.959964 -0.3751854 -0.1012061
s(year_fraction) NA 0.9260578 -0.1554896 0.0704027 1.959964 -0.2934764 -0.0175028
s(year_fraction) NA 0.9306638 -0.0730818 0.0717185 1.959964 -0.2136475 0.0674839
s(year_fraction) NA 0.9352698 0.0090702 0.0738784 1.959964 -0.1357289 0.1538693
s(year_fraction) NA 0.9398759 0.0910090 0.0768756 1.959964 -0.0596645 0.2416825
s(year_fraction) NA 0.9444819 0.1727773 0.0806649 1.959964 0.0146771 0.3308776
s(year_fraction) NA 0.9490879 0.2544178 0.0851726 1.959964 0.0874825 0.4213531
s(year_fraction) NA 0.9536940 0.3359730 0.0903084 1.959964 0.1589717 0.5129742
s(year_fraction) NA 0.9583000 0.4174855 0.0959747 1.959964 0.2293786 0.6055924
confint(g1, "s(year_fraction)", level = 0.95) %>% 
  ggplot(aes(year_fraction)) + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.25) + 
  geom_line(aes(y = est)) + my_theme

You can also use lm to fit splines; these are similar to GAMs but there are some important differences. The mgcv package has a lot of features not available with lm.

s1 <- lm(co2_avg ~ splines::bs(decimal_date, df = 5), data = co2)
tidy(s1) %>% kable(digits = 2)
term estimate std.error statistic p.value
(Intercept) 391.73 1.13 347.53 0.00
splines::bs(decimal_date, df = 5)1 0.64 2.16 0.30 0.77
splines::bs(decimal_date, df = 5)2 6.87 1.47 4.69 0.00
splines::bs(decimal_date, df = 5)3 14.49 1.98 7.31 0.00
splines::bs(decimal_date, df = 5)4 21.01 1.62 13.01 0.00
splines::bs(decimal_date, df = 5)5 22.14 1.64 13.50 0.00
glance(s1) %>% kable(digits = 2)
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual nobs
0.91 0.9 2.31 219.14 0 5 -265.4 544.8 564.26 602.91 113 119

We can use augment to generate data to plot and combine it with the original data.

a1 <- augment(s1, data = co2, se_fit = TRUE, interval="prediction") 
a1 %>%  ggplot(aes(decimal_date)) + 
  geom_ribbon(aes(ymin = .lower, ymax = .upper), alpha = 0.2) + 
  geom_line(aes(y= .fitted)) +
  geom_point(aes(y = co2_avg)) + 
  my_theme

For a spline that closely traces the data, increase df to 26 or more. This increases the numbers of knots or separate cubics used to approximate the data.

17.2 Locally Estimated Scatterplot Smoothing (LOESS)

LOESS smooths are constructed by making a large number of quadratic (or possibly linear) regression lines as a window moves along the x-axis. The degree of the fits and the width of the window (and other details) can be adjusted. The predictions from these many local regressions are then computed and plotted as a “smooth” of the data. We usually just imagine the line is drawn through the data in a way that allows it to track fluctuations without specifying a model.

l1 <- loess(co2_anomaly ~ year_fraction, data = co2)

As with the GAMs, the summary function is not particularly easy to interpret and we will not explore the details, but you can see that this is a quadratic model with a “span” of 0.75, meaning that 75% of the data are used for each regression. (The weighting of each point in the regression varies as a function of x, resulting a continuous change in the predictions.)

## Call:
## loess(formula = co2_anomaly ~ year_fraction, data = co2)
## 
## Number of Observations: 119 
## Equivalent Number of Parameters: 4.58 
## Residual Standard Error: 0.4647 
## Trace of smoother matrix: 5  (exact)
## 
## Control settings:
##   span     :  0.75 
##   degree   :  2 
##   family   :  gaussian
##   surface  :  interpolate      cell = 0.2
##   normalize:  TRUE
##  parametric:  FALSE
## drop.square:  FALSE

We can use predict, residuals and augment on these model objects as we did for lm fits in the previous lesson.

augment(l1, se_fit = TRUE) %>% kable() %>%  scroll_box(height = "200px")
co2_anomaly year_fraction .fitted .se.fit .resid
-0.3216667 0.0417 -1.1874816 0.1278142 0.8658149
0.2083333 0.1250 0.3297748 0.0783764 -0.1214414
0.9483333 0.2083 1.4858927 0.0756092 -0.5375594
1.5983333 0.2917 2.2386031 0.0811237 -0.6402698
2.5383333 0.3750 2.7453971 0.0876098 -0.2070637
2.0883333 0.4583 2.1417191 0.0876044 -0.0533857
0.8583333 0.5417 0.4302506 0.0876044 0.4280828
-1.5216667 0.6250 -1.5283681 0.0876098 0.0067014
-2.5716667 0.7083 -2.4561226 0.0811840 -0.1155441
-2.6616667 0.7917 -2.3873488 0.0757368 -0.2743178
-1.3716667 0.8750 -1.4172800 0.0803002 0.0456133
0.2083333 0.9583 0.4905675 0.1329762 -0.2822341
-0.7350000 0.0417 -1.1874816 0.1278142 0.4524816
0.0050000 0.1250 0.3297748 0.0783764 -0.3247748
0.5450000 0.2083 1.4858927 0.0756092 -0.9408927
2.3250000 0.2917 2.2386031 0.0811237 0.0863969
2.8850000 0.3750 2.7453971 0.0876098 0.1396029
1.8550000 0.4583 2.1417191 0.0876044 -0.2867191
0.5050000 0.5417 0.4302506 0.0876044 0.0747494
-1.4650000 0.6250 -1.5283681 0.0876098 0.0633681
-2.7250000 0.7083 -2.4561226 0.0811840 -0.2688774
-2.8050000 0.7917 -2.3873488 0.0757368 -0.4176512
-0.8750000 0.8750 -1.4172800 0.0803002 0.5422800
0.4850000 0.9583 0.4905675 0.1329762 -0.0055675
-0.9700000 0.0417 -1.1874816 0.1278142 0.2174816
0.2800000 0.1250 0.3297748 0.0783764 -0.0497748
0.9100000 0.2083 1.4858927 0.0756092 -0.5758927
1.8900000 0.2917 2.2386031 0.0811237 -0.3486031
3.2600000 0.3750 2.7453971 0.0876098 0.5146029
2.0800000 0.4583 2.1417191 0.0876044 -0.0617191
0.8000000 0.5417 0.4302506 0.0876044 0.3697494
-1.3200000 0.6250 -1.5283681 0.0876098 0.2083681
-3.0700000 0.7083 -2.4561226 0.0811840 -0.6138774
-2.8200000 0.7917 -2.3873488 0.0757368 -0.4326512
-1.3600000 0.8750 -1.4172800 0.0803002 0.0572800
0.3200000 0.9583 0.4905675 0.1329762 -0.1705675
-0.7925000 0.0417 -1.1874816 0.1278142 0.3949816
-0.6325000 0.1250 0.3297748 0.0783764 -0.9622748
1.0675000 0.2083 1.4858927 0.0756092 -0.4183927
2.6875000 0.2917 2.2386031 0.0811237 0.4488969
3.1375000 0.3750 2.7453971 0.0876098 0.3921029
2.6075000 0.4583 2.1417191 0.0876044 0.4657809
0.4675000 0.5417 0.4302506 0.0876044 0.0372494
-1.6125000 0.6250 -1.5283681 0.0876098 -0.0841319
-3.2625000 0.7083 -2.4561226 0.0811840 -0.8063774
-2.5725000 0.7917 -2.3873488 0.0757368 -0.1851512
-1.3625000 0.8750 -1.4172800 0.0803002 0.0547800
0.2675000 0.9583 0.4905675 0.1329762 -0.2230675
-0.8458333 0.0417 -1.1874816 0.1278142 0.3416483
-0.4758333 0.1250 0.3297748 0.0783764 -0.8056081
0.6941667 0.2083 1.4858927 0.0756092 -0.7917261
2.3241667 0.2917 2.2386031 0.0811237 0.0855636
3.1341667 0.3750 2.7453971 0.0876098 0.3887696
1.9741667 0.4583 2.1417191 0.0876044 -0.1675524
0.4641667 0.5417 0.4302506 0.0876044 0.0339161
-1.8958333 0.6250 -1.5283681 0.0876098 -0.3674653
-3.1958333 0.7083 -2.4561226 0.0811840 -0.7397107
-2.5358333 0.7917 -2.3873488 0.0757368 -0.1484845
-0.6658333 0.8750 -1.4172800 0.0803002 0.7514466
1.0241667 0.9583 0.4905675 0.1329762 0.5335992
-1.7225000 0.0417 -1.1874816 0.1278142 -0.5350184
-0.1525000 0.1250 0.3297748 0.0783764 -0.4822748
0.6475000 0.2083 1.4858927 0.0756092 -0.8383927
3.1975000 0.2917 2.2386031 0.0811237 0.9588969
3.4975000 0.3750 2.7453971 0.0876098 0.7521029
2.5875000 0.4583 2.1417191 0.0876044 0.4457809
0.1775000 0.5417 0.4302506 0.0876044 -0.2527506
-1.9625000 0.6250 -1.5283681 0.0876098 -0.4341319
-3.1725000 0.7083 -2.4561226 0.0811840 -0.7163774
-2.6225000 0.7917 -2.3873488 0.0757368 -0.2351512
-0.6925000 0.8750 -1.4172800 0.0803002 0.7247800
0.2175000 0.9583 0.4905675 0.1329762 -0.2730675
-0.3850000 0.0417 -1.1874816 0.1278142 0.8024816
-0.0850000 0.1250 0.3297748 0.0783764 -0.4147748
0.6750000 0.2083 1.4858927 0.0756092 -0.8108927
2.4750000 0.2917 2.2386031 0.0811237 0.2363969
3.1350000 0.3750 2.7453971 0.0876098 0.3896029
2.3350000 0.4583 2.1417191 0.0876044 0.1932809
0.5750000 0.5417 0.4302506 0.0876044 0.1447494
-1.4350000 0.6250 -1.5283681 0.0876098 0.0933681
-3.1850000 0.7083 -2.4561226 0.0811840 -0.7288774
-2.9250000 0.7917 -2.3873488 0.0757368 -0.5376512
-1.4350000 0.8750 -1.4172800 0.0803002 -0.0177200
0.2550000 0.9583 0.4905675 0.1329762 -0.2355675
-0.5600000 0.0417 -1.1874816 0.1278142 0.6274816
-0.2000000 0.1250 0.3297748 0.0783764 -0.5297748
0.8700000 0.2083 1.4858927 0.0756092 -0.6158927
1.7300000 0.2917 2.2386031 0.0811237 -0.5086031
2.7200000 0.3750 2.7453971 0.0876098 -0.0253971
2.2700000 0.4583 2.1417191 0.0876044 0.1282809
0.1800000 0.5417 0.4302506 0.0876044 -0.2502506
-1.5500000 0.6250 -1.5283681 0.0876098 -0.0216319
-3.0000000 0.7083 -2.4561226 0.0811840 -0.5438774
-2.5200000 0.7917 -2.3873488 0.0757368 -0.1326512
-0.5000000 0.8750 -1.4172800 0.0803002 0.9172800
0.5600000 0.9583 0.4905675 0.1329762 0.0694325
-0.6041667 0.0417 -1.1874816 0.1278142 0.5833149
0.3158333 0.1250 0.3297748 0.0783764 -0.0139414
0.5358333 0.2083 1.4858927 0.0756092 -0.9500594
1.8958333 0.2917 2.2386031 0.0811237 -0.3427698
3.2058333 0.3750 2.7453971 0.0876098 0.4604363
2.4958333 0.4583 2.1417191 0.0876044 0.3541143
0.3058333 0.5417 0.4302506 0.0876044 -0.1244172
-1.4841667 0.6250 -1.5283681 0.0876098 0.0442014
-2.8941667 0.7083 -2.4561226 0.0811840 -0.4380441
-2.9141667 0.7917 -2.3873488 0.0757368 -0.5268178
-1.1841667 0.8750 -1.4172800 0.0803002 0.2331133
0.3258333 0.9583 0.4905675 0.1329762 -0.1647341
-0.6154545 0.0417 -1.1874816 0.1278142 0.5720271
0.1045455 0.1250 0.3297748 0.0783764 -0.2252293
0.5045455 0.2083 1.4858927 0.0756092 -0.9813473
2.2045455 0.2917 2.2386031 0.0811237 -0.0340576
3.0645455 0.3750 2.7453971 0.0876098 0.3191484
2.3745455 0.4583 2.1417191 0.0876044 0.2328264
0.3745455 0.5417 0.4302506 0.0876044 -0.0557051
-1.4554545 0.6250 -1.5283681 0.0876098 0.0729135
-2.7154545 0.7083 -2.4561226 0.0811840 -0.2593319
-2.7254545 0.7917 -2.3873488 0.0757368 -0.3381057
-1.1154545 0.8750 -1.4172800 0.0803002 0.3018254
augment(l1, se_fit = TRUE) %>%
  ggplot(aes(x = year_fraction, y = .fitted)) + 
  geom_line() + 
  geom_ribbon(aes(ymin = .fitted - 2*.se.fit, ymax = .fitted + 2*.se.fit), alpha = 0.20) + 
  my_theme

This line is only drawn using 12 points, so we might want to generate a smoother prediction by creating a new set of dates. We also add the original data to the plot.

new_data = tibble(year_fraction = seq(min(co2$year_fraction), 
                                      max(co2$year_fraction),
                                      length = 100))
augment(l1, newdata = new_data, se_fit = TRUE) %>%
  ggplot(aes(x = year_fraction, y = .fitted)) + 
  geom_line() + 
  geom_ribbon(aes(ymin = .fitted - 2*.se.fit,
                  ymax = .fitted + 2*.se.fit), 
              alpha = 0.20) + 
  geom_point(aes(y = co2_anomaly), data = co2) +
  my_theme

The LOESS function will not predict outside of the range of data provided, so I had to select the range of new_data above carefully to get the line to start and end near the first and last points.

Now we make two plots that contrast the results with different sized windows (span) and degree of the local regression models.

l2 <- loess(co2_avg ~ decimal_date, data = co2, 
            degree = 1, span = 0.05)
l3 <- loess(co2_avg ~ decimal_date, data = co2, 
            degree = 1, span = 0.25)
new_data = tibble(decimal_date = seq(min(co2$decimal_date), 
                                      max(co2$decimal_date),
                                     length = 300))
a2 <- augment(l2, newdata = new_data, se_fit = TRUE) 
a3 <- augment(l3, newdata = new_data, se_fit = TRUE) 
ggplot(a2, aes(x = decimal_date, y = .fitted)) + 
  geom_line(col="green") + 
  geom_ribbon(aes(ymin = .fitted - 2*.se.fit, 
                  ymax = .fitted + 2*.se.fit), 
              alpha = 0.20, fill="green") + 
  geom_point(aes(y = co2_avg), data = co2) +
  geom_line(data = a3, 
            col="blue") + 
  geom_ribbon(data = a3, 
              aes(ymin = .fitted - 2*.se.fit, 
                  ymax = .fitted + 2*.se.fit), 
              alpha = 0.20, fill="blue") + 
  my_theme

LOESS fits are slow and take a lot of memory compared to other methods (both time and storage requirements increase like the square of the number of points), so they are usually only used for small data sets.