---
title: 'Chapter 10, section 3: Least squares regression'
author: "Dr. Paul Pearson, Hope College"
date: "Fall 2024"
#output: slidy_presentation
#output: html_document
output: word_document
#output: 
#  pdf_document: 
#    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "", warning = FALSE, message = FALSE)

library(ggplot2) 
# ggsave(ggplot_object) can save svg files without xml annotations

library(BrailleR) # works with ggplot
# VI(ggplot_object) for annotations for in text graph annotations
# MakeAccessibleSVG(ggplot_object) to save accessible standalone html (=svg + xml annotations)

library(tactileR) # works with base plots
# Use braille in graphs for embossed printing
# brl_begin(file='tactile_histogram_SWL.pdf', pt=14, paper='special', font='SWL')
# base or lattice plot code here
# brl_end()

library(infer)
#library(tibble)

library(tidyr) # for reshaping data, etc.

library(car) # for fancy scatterplots

library(ddpcr) # for quiet() function to suppress output

set.seed(1)
options(digits = 4)
```


```{r, include = FALSE, message = FALSE}
make_scatterplot <-  function(df, explanatory, response, title_string, xlab_string, ylab_string, filename) {

  formula_string <- paste(substitute(response), "~", substitute(explanatory))
  # winner: https://win-vector.com/2019/07/06/programming-over-lm-in-r/
  # https://stackoverflow.com/questions/27261232/passing-argument-to-lm-in-r-within-function
  model <- eval(bquote(   lm( as.formula(formula_string), data = df)   ))
  #print(model)  
  
  # ggplot scatterplot without line
  sp <- capture.output(ggplot(df, aes( x = {{ explanatory }}, y = {{ response }} )) +
    geom_point(size = 4) + 
    ggtitle( title_string ) +
    xlab( xlab_string ) + 
    ylab( ylab_string ) +
    theme_bw(base_size = 18))
  
  #VI(sp, Describe = FALSE)
  print(sp) # generates both graph and VI(ts) automatically 
  
  MakeAccessibleSVG.ggplot( sp, file = paste0("reader-", filename))

  # tactile scatterplot with no line  
  brl_begin(paste0( 'tactile-', filename, '-scatterplot.pdf'), 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
    # to get the variable values, use data[[substitute(varname)]] instead of data$varname
    plot(df[[substitute(explanatory)]],
         df[[substitute(response)]],
         main = title_string,
         xlab = xlab_string,
         ylab = ylab_string
         )
  invisible(brl_end()) # suppress printed output of the form png 2 using invisible()
  
    
}


make_scatterplot_with_line <-  function(df, explanatory, response, title_string, xlab_string, ylab_string, filename) {

  formula_string <- paste(substitute(response), "~", substitute(explanatory))
  # winner: https://win-vector.com/2019/07/06/programming-over-lm-in-r/
  # https://stackoverflow.com/questions/27261232/passing-argument-to-lm-in-r-within-function
  model <- eval(bquote(   lm( as.formula(formula_string), data = df)   ))
  #print(model)  
  
  # ggplot scatterplot without line
  sp <- capture.output(ggplot(df, aes( x = {{ explanatory }}, y = {{ response }} )) +
    geom_point(size = 4) + 
    ggtitle( title_string ) +
    xlab( xlab_string ) + 
    ylab( ylab_string ) +
    theme_bw(base_size = 18) +
    geom_smooth(method='lm', se=FALSE, size=2, color = "black") )

  # ggplot scatterplot with line
  #sp <- sp + 

  print(sp) # generates both graph and VI(ts) automatically
  
  MakeAccessibleSVG.ggplot(sp, file = paste0("reader-", filename))

  # tactile scatterplot with line  
  brl_begin(paste0( 'tactile-', filename, '-scatterplot-and-line.pdf'), 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
    # to get the variable values, use data[[substitute(varname)]] instead of data$varname
    plot(df[[substitute(explanatory)]],
         df[[substitute(response)]],
         main = title_string,
         xlab = xlab_string,
         ylab = ylab_string
         )
    abline( model, col = "black", lwd = 3 )
  invisible(brl_end()) # suppress printed output of the form png 2 using invisible()

  # tactile scatterplot with line and ellipses and boxplots 
  brl_begin(paste0( 'tactile-', filename, '-scatterplot-and-line-and-ellipses-and-boxplots.pdf'), 
         pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  scatterplot( as.formula(formula_string), data = df,
      main = title_string,
      xlab = xlab_string,
      ylab = ylab_string,
             pch = 16, cex = 2, col = "black",
             smooth = FALSE, grid = FALSE, frame = TRUE,
             ellipse = list(levels=c(.25, .75), robust=TRUE, fill=FALSE, fill.alpha=0, lwd = 3) )
  invisible(brl_end()) # suppress printed output of the form png 2 using invisible()
      
}
```








# Chapter 10: Two quantitative variables

## Section 10.3: Least squares regression

In Sections 10.1 and 10.2, we used graphical and numerical methods to describe the observed association between two quantitative variables and used  correlation coefficient simulation for drawing inference. For a linear association, it is helpful to develop a mathematical model of it. This allows us to make predictions about the response variable from the explanatory variable. The most common way to do this is with a least squares regression line.


# Example 10.3: Height and winning at tennis (continued from Exploration 10.1)

Open the [Correlation/Regression](https://www.rossmanchance.com/applets/2021/regshuffle/regshuffle.htm?hideExtras=2) applet and select the "WimbledonMF" data set in the applet, which we looked at in Exploration 10.1.

The scatterplot shows a weak positive association because of the trend of increasing height in more recent years. This is supported by the correlation coefficient of r = 0.189. 

The association appears to be roughly linear, and the regression line is
$$
\widehat{height} = 104.7 + 0.0378 * year.
$$
This equation has the form 
$$
\widehat{y} = a + b x
$$
where

- $a$ is the y-intercept,
- $b$ is the slope, 
- $x$ is the value of the explanatory variable, and
- $\widehat{y}$ is a point on the line that represents the predicted value of the response variable.

Questions

1. What are the units on $\widehat{height}$?
2. What are the units on 104.7?
3. What are the units on 0.0378? What is the practical significance of this slope?
4. What are the units on $year$?

# Key ideas

- The slope of a least squares regression line is interpreted as the predicted change in the average response variable for a one-unit change in the explanatory variable. 
- For a given data set, the signs (positive, negative, or zero) for the slope of the regression line, b, and the correlation coefficient, r, and must be the same. 
- Predicting values for the response variable for values of the explanatory variable that are outside of the range of the original data is known as **extrapolation**, and can give very misleading predictions.
- The y-intercept, a, of a regression line is interpreted as the predicted value of the response variable when the explanatory variable has a value of zero.
- Be wary of extrapolation in interpreting the intercept or other values outside the original data range. 

# Key idea residuals and coefficient of determination

- A residual is a difference between an observed response and the predicted response from the least square regression line: $residual = observed - predicted$.
- The least squares regression line minimizes the sum of the squared residuals.
- The coefficient of determination ($r^2$) is the percentage of variation in the response variable that is explained by the linear relationship with the explanatory variable.
$$
r^2 = \frac{SSE(\bar{y}) - SSE(regression line)}{SSE(\bar{y})}.
$$
- In the Wimbledon example, because correlation coefficient is $r = 0.189$, the coefficient of determination is $r^2 = 0.189^2 = `r 0.189**2`$. This means that `r 100*(0.189**2)`% of the observed variation in height of Wimbledon championship winners is explained by knowing the year that they won.


# Exploration 10.3: Arctic sea ice extent

- The [google document with questions is linked here](https://docs.google.com/document/d/1-lOygkf2tF-8sIvJ-CTzQIjfqmeygTofZ74PWDWT7uk/copy)
- The [google spreadsheet with data is linked here](https://docs.google.com/spreadsheets/d/1G6ufQq8Yu4TH9coyXY3rB5aSWSHAJUvVi6bszSS8QIw/copy)
