---
title: "ADA2: Class 03, Ch 02 Introduction to Multiple Linear Regression"
author: Your Name
date: last-modified
description: |
[Advanced Data Analysis 2](https://StatAcumen.com/teach/ada2),
Stat 428/528, Spring 2023, Prof. Erik Erhardt, UNM
format:
html:
theme: litera
highlight-style: atom-one
page-layout: full # article, full # https://quarto.org/docs/output-formats/page-layout.html
toc: true
toc-location: body # body, left, right
number-sections: false
self-contained: false # !!! this can cause a render error
code-overflow: scroll # scroll, wrap
code-block-bg: true
code-block-border-left: "#30B0E0"
code-copy: false # true, false, hover a copy buttom in top-right of code block
fig-width: 6
fig-height: 4
fig-align: center # default, left, right, or center
execute: # https://quarto.org/docs/computations/execution-options.html, https://quarto.org/docs/computations/r.html
cache: false # false, true
echo: true # true, false Include the source code in output
warning: true # true, false Include warnings in the output.
error: true # true, false Include errors in the output (note that this implies that errors executing code will not halt processing of the document).
---
# Auction selling price of antique grandfather clocks
The data include the selling price in pounds sterling at auction of 32 antique grandfather clocks,
the age of the clock in years, and the number of people who made a bid.
In the sections below, describe the relationship between variables and develop a model
for predicting selling `Price` given `Age` and `Bidders`.
```{R}
library(erikmisc)
library(tidyverse)
dat_auction <- read_csv("ADA2_CL_03_auction.csv")
str(dat_auction)
summary(dat_auction)
```
## __(1 p)__ Scatterplot matrix
_In a scatterplot matrix below interpret the relationship between each pair of variables.
If a transformation is suggested by the plot (that is, because there is a curved relationship),
also plot the data on the transformed scale and
perform the following analysis on the transformed scale.
Otherwise indicate that no transformation is necessary._
```{R}
library(ggplot2)
library(GGally)
p <- ggpairs(dat_auction)
print(p)
```
### Solution
## __(1 p)__ Correlation matrix
_Below is the correlation matrix and tests for the hypothesis that each correlation is equal to zero.
Interpret the hypothesis tests and relate this to the plot that you produced above._
```{R}
# correlation matrix and associated p-values testing "H0: rho == 0"
#library(Hmisc)
Hmisc::rcorr(as.matrix(dat_auction))
```
### Solution
## __(1 p)__ Plot interpretation
_Below are two plots.
The first has $y =$ Price, $x =$ Age, and colour = Bidders,
and the second has $y =$ Price, $x =$ Bidders, and colour = Age.
Interpret the relationships between all three variables, simultaneously.
For example, say how Price relates to Age,
then also how Price relates to Bidders conditional on Age being a specific value._
```{R, fig.height = 4, fig.width = 10, echo=FALSE}
dat_auction <-
dat_auction %>%
mutate(
id = row_number()
)
# ggplot: Plot the data with linear regression fit and confidence bands
library(ggplot2)
p1 <- ggplot(dat_auction, aes(x = Age, y = Price, label = id))
p1 <- p1 + geom_point(aes(colour = Bidders), size=3)
# plot labels next to points
p1 <- p1 + geom_text(hjust = 0.5, vjust = -0.5, alpha = 1/4, colour = 2)
# plot regression line and confidence band
p1 <- p1 + geom_smooth(method = lm)
p1 <- p1 + labs(title="Selling Price by Age with colored Bidders")
#print(p1)
# ggplot: Plot the data with linear regression fit and confidence bands
library(ggplot2)
p2 <- ggplot(dat_auction, aes(x = Bidders, y = Price, label = id))
p2 <- p2 + geom_point(aes(colour = Age), size=3)
# plot labels next to points
p2 <- p2 + geom_text(hjust = 0.5, vjust = -0.5, alpha = 1/4, colour = 2)
# plot regression line and confidence band
p2 <- p2 + geom_smooth(method = lm)
p2 <- p2 + labs(title="Selling Price by Bidders with colored Age")
#print(p2)
library(gridExtra)
grid.arrange(grobs = list(p1, p2), nrow=1)
```
### Solution
## __(2 p)__ Multiple regression assumptions (assessing model fit)
_Below the multiple regression is fit.
Start by assessing the model assumptions by interpretting what you learn from the first six plots (save the added variable plots for the next question)._
_If assumptions are not met, attempt to address by transforming a variable and
restart at the beginning using the new transformed variable._
```{R}
# fit the simple linear regression model
lm_p_a_b <- lm(Price ~ Age + Bidders, data = dat_auction)
```
Plot diagnostics.
```{R, fig.height = 3, fig.width = 10}
# plot diagnostics
e_plot_lm_diagostics(lm_p_a_b, sw_plot_set = "simpleAV")
```
### Solution
From the diagnostic plots above,
(1)
(2)
(3)
(4)
(5)
(6)
## __(1 p)__ Added variable plots
_Use partial regression residual plots (added variable plots)
to check for the need for transformations.
If linearity is not supported, address and restart at the beginning._
### Solution
## __(1 p)__ Multiple regression hypothesis tests
_State the hypothesis test and conclusion for each regression coefficient._
```{R}
# fit the simple linear regression model
lm_p_a_b <- lm(Price ~ Age + Bidders, data = dat_auction)
# use summary() to get t-tests of parameters (slope, intercept)
summary(lm_p_a_b)
```
### Solution
## __(1 p)__ Multiple regression interpret coefficients
_Interpret the coefficients of the multiple regression model._
### Solution
## __(1 p)__ Multiple regression $R^2$
_Interpret the Multiple R-squared value._
### Solution
## __(1 p)__ Summary
_Summarize your findings in one sentence._
### Solution
```{R}
## Aside: I generally recommend against 3D plots for a variety of reasons.
## However, here's a 3D version of the plot so you can visualize the surface fit in 3D.
## I will point out a feature in this plot that we wouldn't see in other plots
## and it would typically only be detected by careful consideration
## of a "more complicated" second-order model that includes curvature.
# library(rgl)
# library(car)
# scatter3d(Price ~ Age + Bidders, data = dat_auction)
```