Determine which explanatory variables have a significant effect on the mean of the quantitative response variable.

## Simple Linear Regression

Simple linear regression is a good analysis technique when the data consists of a single quantitative response variable $$Y$$ and a single quantitative explanatory variable $$X$$.

### Overview

Mathematical Model

The true regression model assumed by a regression analysis is given by

$$Y_i$$ The response variable. The “i” denotes that this is the y-value for individual “i”, where “i” is 1, 2, 3,… and so on up to $$n$$, the sample size. $$=$$ This states that we are assuming $$Y_i$$ was created, or is “equal to” the formula that will follow on the right-hand-side of the equation. $$\underbrace{\overbrace{\beta_0}^\text{y-intercept} + \overbrace{\beta_1}^\text{slope} X_i \ }_\text{true regression relation}$$ The true regression relation is a line, a line that is typically unknown in real life. It can be likened to “God’s Law” or “Natural Law”. Something that governs the way the data behaves, but is unkown to us. $$+$$ This plus sign emphasizes that the actual data, the $$Y_i$$, is created by adding together the value from the true line $$\beta_0 + \beta_1 X_i$$ and an individual error term $$\epsilon_i$$, which allows each dot in the regression to be off of the line by a certain amount called $$\epsilon_i$$. $$\overbrace{\epsilon_i}^\text{error term}$$ Error term for each individual $$i$$. The error terms are “random” and unique for each individual. This provides the statistical relationship of the regression. It is what allows each dot to be different, while still coming from the same line, or underlying law. $$\quad \text{where}$$ Some extra comments are needed about $$\epsilon_i$$ $$\ \overbrace{\epsilon_i \sim N(0, \sigma^2)}^\text{error term normally distributed}$$ The error terms $$\epsilon_i$$ are assumed to be normally distributed with constant variance. Pay special note that the $$\sigma$$ does not have an $$i$$ in it, so it is the same for each individual. In other words, the variance is constant. The mean of the errors is zero, which causes the dots to be spread out symmetrically both above and below the line.

The estimated regression line obtained from a regression analysis, pronounced “y-hat”, is written as

$$\hat{Y}_i$$ The estimated average y-value for individual $$i$$ is denoted by $$\hat{Y}_i$$. It is important to recognize that $$Y_i$$ is the actual value for individual $$i$$, and $$\hat{Y}_i$$ is the average y-value for all individuals with the same $$X_i$$ value. $$=$$ The formula for the average y-value, $$\hat{Y}_i$$ is equal to what follows… $$\underbrace{\overbrace{\ b_0 \ }^\text{y-intercept} + \overbrace{b_1}^\text{slope} X_i \ }_\text{estimated regression relation}$$ Two things are important to notice about this equation. First, it uses $$b_0$$ and $$b_1$$ instead of $$\beta_0$$ and $$\beta_1$$. This is because $$b_0$$ and $$b_1$$ are the estimated y-intercept and slope, respectively, not the true y-intercept $$\beta_0$$ and true slope $$\beta_1$$. Second, this equation does not include $$\epsilon_i$$. In other words, it is the estimated regression line, so it only describes the average y-values, not the actual y-values.

Note: see the Explanation tab The Mathematical Model for details about these equations.

Hypotheses

$\left.\begin{array}{ll} H_0: \beta_1 = 0 \\ H_a: \beta_1 \neq 0 \end{array} \right\} \ \text{Slope Hypotheses}^{\quad \text{(most common)}}\quad\quad$

$\left.\begin{array}{ll} H_0: \beta_0 = 0 \\ H_a: \beta_0 \neq 0 \end{array} \right\} \ \text{Intercept Hypotheses}^{\quad\text{(sometimes useful)}}$

If $$\beta_1 = 0$$, then the model reduces to $$Y_i = \beta_0 + \epsilon_i$$, which is a flat line. This means $$X$$ does not improve our understanding of the mean of $$Y$$ if the null hypothesis is true.

If $$\beta_0 = 0$$, then the model reduces to $$Y_i = \beta_1 X + \epsilon_i$$, a line going through the origin. This means the average $$Y$$-value is $$0$$ when $$X=0$$ if the null hypothesis is true.

Assumptions

This regression model is appropriate for the data when five assumptions can be made.

1. Linear Relation: the true regression relation between $$Y$$ and $$X$$ is linear.

2. Normal Errors: the error terms $$\epsilon_i$$ are normally distributed with a mean of zero.

3. Constant Variance: the variance $$\sigma^2$$ of the error terms is constant (the same) over all $$X_i$$ values.

4. Fixed X: the $$X_i$$ values can be considered fixed and measured without error.

5. Independent Errors: the error terms $$\epsilon_i$$ are independent.

Note: see the Explanation tab Residual Plots & Regression Assumptions for details about checking the regression assumptions.

Interpretation

The slope is interpreted as, “the change in the average y-value for a one unit change in the x-value.” It is not the average change in y. It is the change in the average y-value.

The y-intercept is interpreted as, “the average y-value when x is zero.” It is often not meaningful, but is sometimes useful. It just depends if x being zero is meaningful or not within the context of your analysis. For example, knowing the average price of a car with zero miles is useful. However, pretending to know the average height of adult males that weigh zero pounds, is not useful.

### Explanation

Linear regression has a rich mathematical theory behind it. This is because it uses a mathematical function and a random error term to describe the regression relation between a response variable $$Y$$ and an explanatory variable called $$X$$.

Regression Cheat Sheet (Expand)

#### The Mathematical Model (Expand)

$$Y_i$$, $$\hat{Y}_i$$, and $$E\{Y_i\}$$

#### Interpreting the Model Parameters (Expand)

$$\beta_0$$ (intercept) and $$\beta_1$$ (slope), estimated by $$b_0$$ and $$b_1$$, interpreted as…

#### Residuals and Errors (Expand)

$$r_i$$, the residual, estimates $$\epsilon_i$$, the true error…

#### Assessing the Fit of a Regression (Expand)

$$R^2$$, SSTO, SSR, and SSE…

#### Residual Plots & Regression Assumptions (Expand)

Residuals vs. fitted-values, Q-Q Plot of the residuals, and residuals vs. order plots…

The material below this section is meant for Math 425 students only.

#### Estimating the Model Parameters (Expand)

How to get $$b_0$$ and $$b_1$$: least squares & maximum likelihood…

#### Estimating the Model Variance (Expand)

Estimating $$\sigma^2$$ with MSE…

#### Transformations (Expand)

$$Y'$$, $$X'$$, and returning to the original space…

#### Inference for the Model Parameters (Expand)

t test formulas, sampling distributions, confidence intervals, and F tests…

#### Prediction and Confidence Intervals for $$\hat{Y}_h$$(Expand)

predict(…, interval=“prediction”)…

#### Lowess (and Loess) Curves (Expand)

A non-parametric approach to estimating $$E\{Y_i\}$$

Examples: bodyweight, cars

## Multiple Linear Regression

Multiple regression allows for more than one explanatory variable to be included in the modeling of the expected value of the quantitative response variable $$Y_i$$. There are infinitely many possible multiple regression models to choose from. Here are a few “basic” models that work as building blocks to more complicated models.

### Overview

Select a model to see interpretation details, an example, and R Code help.

 $Y_i = \overbrace{\underbrace{\beta_0 + \beta_1 X_i}_{E\{Y_i\}}}^\text{Simple Model} + \epsilon_i$

The Simple Linear Regression model uses a single x-variable once: $$X_i$$.

Parameter Effect
$$\beta_0$$ Y-intercept of the Model
$$\beta_1$$ Slope of the line

 $Y_i = \overbrace{\underbrace{\beta_0 + \beta_1 X_i + \beta_2 X_i^2}_{E\{Y_i\}}}^\text{Quadratic Model} + \epsilon_i$

The Quadratic model uses the same $$X$$-variable twice, once with a $$\beta_1 X_i$$ term and once with a $$\beta_2 X_i^2$$ term. The $$X_i^2$$ term is called the “quadratic” term.

Parameter Effect
$$\beta_0$$ Y-intercept of the Model.
$$\beta_1$$ Controls the x-position of the vertex of the parabola by $$\frac{-\beta_1}{2\cdot\beta_2}$$.
$$\beta_2$$ Controls the concavity and “steepness” of the Model: negative values face down, positive values face up; large values imply “steeper” parabolas and low values imply “flatter” parabolas. Also involved in the position of the vertex, see $$\beta_1$$’s explanation.

An Example

Using the airquality data set, we run the following “quadratic” regression. Pay careful attention to how the mathematical model for $$Y_i = \ldots$$ is translated to R-Code inside of lm(...).

$\underbrace{Y_i}_\text{Temp} \underbrace{=}_{\sim} \overbrace{\beta_0}^{\text{y-int}} + \overbrace{\beta_1}^{\stackrel{\text{slope}}{\text{term}}} \underbrace{X_{i}}_\text{Month} \underbrace{+}_{+} \overbrace{\beta_2}^{\stackrel{\text{quadratic}}{\text{term}}} \underbrace{X_{i}^2}_\text{I(Month^2)} + \epsilon_i$

lm.quad <- A name we made up for our “quadratic” regression. lm( R function lm used to perform linear regressions in R. The lm stands for “linear model”. Temp Y-variable, should be quantitative.  ~  The tilde ~ is what lm(…) uses to state the regression equation $$Y_i = ...$$. Notice that the ~ is not followed by $$\beta_0 + \beta_1$$ like $$Y_i = ...$$. Instead, $$X_{i}$$ (Month in this case) is the first term following ~. This is because the $$\beta$$’s are going to be estimated by the lm(…). These “Estimates” can be found using summary(lmObject) and looking at the Estimates column in the output. Month $$X_{i}$$, should be quantitative.  +  The plus + is used between each term in the model. Note that only the x-variables are included in the lm(…) from the $$Y_i = ...$$ model. No beta’s are included. I(Month^2) $$X_{i}^2$$, where the function I(…) protects the squaring of Month from how lm(…) would otherwise interpret that statement. The I(…) function must be used anytime you raise an x-variable to a power in the lm(…) statement. , data=airquality This is the data set we are using for the regression. )
Closing parenthsis for the lm(…) function.

Press Enter to run the code.
…  Click to View Output.

lm.quad <- lm(Temp ~ Month + I(Month^2), data=airquality)
emphasize.strong.cols(1)
pander(summary(lm.quad)$coefficients, ) Estimate Std. Error t value Pr(>|t|) (Intercept) -95.73 15.24 -6.281 3.458e-09 Month 48.72 4.489 10.85 1.29e-20 I(Month^2) -3.283 0.3199 -10.26 4.737e-19 The estimates shown in the summary output table above approximate the $$\beta$$’s in the regression model: • $$\beta_0$$ is estimated by the (Intercept) value of -95.73, • $$\beta_1$$ is estimated by the Month value of 48.72, and • $$\beta_2$$ is estimated by the I(Month^2) value of -3.283. Because the estimate of the $$\beta_2$$ term is negative (-3.283), this parabola will “open down” (concave). This tells us that average temperatures will increase to a point, then decrease again. The vertex of this parabola will be at $$-b_1/(2b_2) = -(48.72)/(2\cdot (-3.283)) = 7.420043$$ months, which tells us that the highest average temperature will occur around mid July (7.42 months to be exact). The y-intercept is -95.73, which would be awfully cold if it were possible for the month to be “month zero.” Since this is not possible, the y-intercept is not meaningful for this model. Note that interpreting either $$\beta_1$$ or $$\beta_2$$ by themselves is quite difficult because they both work with together with $$X_{i}$$. $\hat{Y}_i = \overbrace{-95.73}^\text{y-int} + \overbrace{48.72}^{\stackrel{\text{slope}}{\text{term}}} X_{i} + \overbrace{-3.283}^{\stackrel{\text{quadratic}}{\text{term}}} X_{i}^2$ The regression function is drawn as follows. Be sure to look at the “Code” to understand how this graph was created using the ideas in the equation above.  Using Base R plot(Temp ~ Month, data=airquality, col="skyblue", pch=21, bg="gray83", main="Quadratic Model using airquality data set", cex.main=1) #get the "Estimates" automatically: b <- coef(lm.quad) # Then b will have 3 numbers stored inside: # b[1] is the estimate of beta_0: -95.73 # b[2] is the estimate of beta_1: 48.72 # b[3] is the estimate of beta_2: -3.28 curve(b[1] + b[2]*x + b[3]*x^2, col="skyblue", lwd=2, add=TRUE) Using ggplot2 #get the "Estimates" automatically: b <- coef(lm.quad) # Then b will have 3 estimates: # b[1] is the estimate of beta_0: 35.38 # b[2] is the estimate of beta_1: -7.099 # b[3] is the estimate of beta_2: 0.4759 ggplot(airquality, aes(y=Temp, x=Month)) + geom_point(pch=21, bg="gray83", color="skyblue") + #geom_smooth(method="lm", se=F, formula = y ~ poly(x, 2)) + #easy way, but the more involved manual way using stat_function (see below) is more dynamic. stat_function(fun = function(x) b[1] + b[2]*x + b[3]*x^2, color="skyblue") + labs(title="Quadratic Model using airquality data set")   $Y_i = \overbrace{\underbrace{\beta_0 + \beta_1 X_{1i} + \beta_2 X_{2i} + \beta_3 X_{1i} X_{2i}}_{E\{Y_i\}}}^\text{Two-lines Model} + \epsilon_i$ $X_{2i} = \left\{\begin{array}{ll} 1, & \text{Group B} \\ 0, & \text{Group A} \end{array}\right.$ The so called “two-lines” model uses a quantitative $$X_{1i}$$ variable and a 0,1 indicator variable $$X_{2i}$$. It is a basic example of how a “dummy variable” or “indicator variable” can be used to turn qualitative variables into quantitative terms. In this case, the indicator variable $$X_{2i}$$, which is either 0 or 1, produces two separate lines: one line for Group A, and one line for Group B. Parameter Effect $$\beta_0$$ Y-intercept of the Model. $$\beta_1$$ Controls the slope of the “base-line” of the model, the “Group 0” line. $$\beta_2$$ Controls the change in y-intercept for the second line in the model as compared to the y-intercept of the “base-line” line. $$\beta_3$$ Called the “interaction” term. Controls the change in the slope for the second line in the model as compared to the slope of the “base-line” line. An Example Using the mtcars data set, we run the following “two-lines” regression. Note that am has only 0 or 1 values: View(mtcars). $\underbrace{Y_i}_\text{mpg} \underbrace{=}_{\sim} \overbrace{\beta_0}^{\stackrel{\text{y-int}}{\text{baseline}}} + \overbrace{\beta_1}^{\stackrel{\text{slope}}{\text{baseline}}} \underbrace{X_{1i}}_\text{qsec} + \overbrace{\beta_2}^{\stackrel{\text{change in}}{\text{y-int}}} \underbrace{X_{2i}}_\text{am} + \overbrace{\beta_3}^{\stackrel{\text{change in}}{\text{slope}}} \underbrace{X_{1i}X_{2i}}_\text{qsec:am} + \epsilon_i$ lm.2lines <- A name we made up for our “two-lines” regression. lm( R function lm used to perform linear regressions in R. The lm stands for “linear model”. mpg Y-variable, should be quantitative. ~ The tilde ~ is what lm(…) uses to state the regression equation $$Y_i = ...$$. Notice that the ~ is not followed by $$\beta_0 + \beta_1$$ like $$Y_i = ...$$. Instead, $$X_{1i}$$ is the first term following ~. This is because $$\beta$$’s are going to be estimated by the lm(…). These estimates can be found using summary(lmObject). qsec $$X_{1i}$$, should be quantitative. + The plus + is used between each term in the model. Note that only the x-variables are included in the lm(…) from the $$Y_i = ...$$ model. No beta’s are included. am $$X_{2i}$$, an indicator or 0,1 variable. This term allows the y-intercept of the two lines to differ. + The plus + is used between each term in the model. Note that only the x-variables are included in the lm(…) from the $$Y_i = ...$$ model. No beta’s are included. qsec:am $$X_{1i}X_{2i}$$ the interaction term. This allows the slopes of the two lines to differ. , data=mtcars This is the data set we are using for the regression. ) Closing parenthsis for the lm(…) function. Press Enter to run the code. … Click to View Output. lm.2lines <- lm(mpg ~ qsec + am + qsec:am, data=mtcars) pander(summary(lm.2lines)$coefficients)
Estimate Std. Error t value Pr(>|t|)
(Intercept) -9.01 8.218 -1.096 0.2823
qsec 1.439 0.45 3.197 0.003432
am -14.51 12.48 -1.163 0.2548
qsec:am 1.321 0.7017 1.883 0.07012

The estimates shown above approximate the $$\beta$$’s in the regression model: $$\beta_0$$ is estimated by the (Intercept), $$\beta_1$$ is estimated by the qsec value of 1.439, $$\beta_2$$ is estimated by the am value of -14.51, and $$\beta_3$$ is estimated by the qsec:am value of 1.321.

This gives two separate equations of lines.

Automatic Transmission (am==0, $$X_{2i} = 0$$) Line

$\hat{Y}_i = \overbrace{-9.01}^{\stackrel{\text{y-int}}{\text{baseline}}} + \overbrace{1.439}^{\stackrel{\text{slope}}{\text{baseline}}} X_{1i}$

Manual Transmission (am==1 , $$X_{2i} = 1$$) Line

$\hat{Y}_i = \underbrace{(\overbrace{-9.01}^{\stackrel{\text{y-int}}{\text{baseline}}} + \overbrace{-14.51}^{\stackrel{\text{change in}}{\text{y-int}}})}_{\stackrel{\text{y-intercept}}{-23.52}} + \underbrace{(\overbrace{1.439}^{\stackrel{\text{slope}}{\text{baseline}}} +\overbrace{1.321}^{\stackrel{\text{change in}}{\text{slope}}})}_{\stackrel{\text{slope}}{2.76}} X_{1i}$

These lines are drawn as follows. Be sure to look at the “Code” to understand how this graph was created using the ideas in the two equations above.

 Using Base R plot(mpg ~ qsec, data=mtcars, col=c("skyblue","orange")[as.factor(am)], pch=21, bg="gray83", main="Two-lines Model using mtcars data set", cex.main=1) legend("topleft", legend=c("Baseline (am==0)", "Changed-line (am==1)"), bty="n", lty=1, col=c("skyblue","orange"), cex=0.8) #get the "Estimates" automatically: b <- coef(lm.2lines) # Then b will have 4 estimates: # b[1] is the estimate of beta_0: -9.0099 # b[2] is the estimate of beta_1: 1.4385 # b[3] is the estimate of beta_2: -14.5107 # b[4] is the estimate of beta_3: 1.3214 curve(b[1] + b[2]*x, col="skyblue", lwd=2, add=TRUE) #baseline (in blue) curve((b[1] + b[3]) + (b[2] + b[4])*x, col="orange", lwd=2, add=TRUE) #changed line (in orange) Using ggplot2 #get the "Estimates" automatically: b <- coef(lm.2lines) # Then b will have 4 estimates: # b[1] is the estimate of beta_0: -9.0099 # b[2] is the estimate of beta_1: 1.4385 # b[3] is the estimate of beta_2: -14.5107 # b[4] is the estimate of beta_3: 1.3214 ggplot(mtcars, aes(y=mpg, x=qsec, color=factor(am))) + geom_point(pch=21, bg="gray83") + #geom_smooth(method="lm", se=F) + #easy way, but only draws the full interaction model. The manual way using stat_function (see below) is more involved, but more dynamic. stat_function(fun = function(x) b[1] + b[2]*x, color="skyblue") + #am==0 line stat_function(fun = function(x) (b[1]+b[3]) + (b[2]+b[4])*x,color="orange") + #am==1 line scale_color_manual(name="Transmission (am)", values=c("skyblue","orange")) + labs(title="Two-lines Model using mtcars data set") 

 $Y_i = \overbrace{\underbrace{\beta_0 + \beta_1 X_{1i} + \beta_2 X_{2i} + \beta_3 X_{1i}X_{2i}}_{E\{Y_i\}}}^\text{3D Model} + \epsilon_i$

The so called “3D” regression model uses two different quantitative x-variables, an $$X_{1i}$$ and an $$X_{2i}$$. Unlike the two-lines model where $$X_{2i}$$ could only be a 0 or a 1, this $$X_{2i}$$ variable is quantitative, and can take on any quantitative value.

Parameter Effect
$$\beta_0$$ Y-intercept of the Model
$$\beta_1$$ Slope of the line in the $$X_1$$ direction.
$$\beta_2$$ Slope of the line in the $$X_2$$ direction.
$$\beta_3$$ Interaction term that allows the model, which is a plane in three-dimensional space, to “bend”. If this term is zero, then the regression surface is just a flat plane.

An Example

Here is what a 3D regression looks like when there is no interaction term. The two x-variables of Month and Temp are being used to predict the y-variable of Ozone.

$\underbrace{Y_i}_\text{Ozone} \underbrace{=}_{\sim} \overbrace{\beta_0}^{\stackrel{\text{y-int}}{\text{baseline}}} + \overbrace{\beta_1}^{\stackrel{\text{slope}}{\text{baseline}}} \underbrace{X_{1i}}_\text{Temp} + \overbrace{\beta_2}^{\stackrel{\text{change in}}{\text{y-int}}} \underbrace{X_{2i}}_\text{Month} + \epsilon_i$

air_lm <- lm(Ozone ~ Temp + Month, data= airquality)
pander(air_lm$coefficients) (Intercept) Temp Month -139.6 2.659 -3.522 Notice how the slope, $$\beta_1$$, in the “Temp” direction is estimated to be 2.659 and the slope in the “Month” direction, $$\beta_2$$, is estimated to be -3.522. Also, the y-intercept, $$\beta_0$$, is estimated to be -139.6. ## Hint: library(car) has a scatterplot 3d function which is simple to use # but the code should only be run in your console, not knit. ## library(car) ## scatter3d(Y ~ X1 + X2, data=yourdata) ## To embed the 3d-scatterplot inside of your html document is harder. #library(plotly) #library(reshape2) #Perform the multiple regression air_lm <- lm(Ozone ~ Temp + Month, data= airquality) #Graph Resolution (more important for more complex shapes) graph_reso <- 0.5 #Setup Axis axis_x <- seq(min(airquality$Temp), max(airquality$Temp), by = graph_reso) axis_y <- seq(min(airquality$Month), max(airquality$Month), by = graph_reso) #Sample points air_surface <- expand.grid(Temp = axis_x, Month = axis_y, KEEP.OUT.ATTRS=F) air_surface$Z <- predict.lm(air_lm, newdata = air_surface)
air_surface <- acast(air_surface, Month ~ Temp, value.var = "Z") #y ~ x

#Create scatterplot
plot_ly(airquality,
x = ~Temp,
y = ~Month,
z = ~Ozone,
text = rownames(airquality),
type = "scatter3d",
mode = "markers") %>%
x = axis_x,
y = axis_y,
type = "surface")

Here is a second view of this same regression with what is called a contour plot, contour map, or density plot.

mycolorpalette <- colorRampPalette(c("skyblue2", "orange"))
filled.contour(x=axis_x, y=axis_y, z=matrix(air_surface$Z, length(axis_x), length(axis_y)), col=mycolorpalette(26)) Including the Interaction Term Here is what a 3D regression looks like when the interaction term is present. The two x-variables of Month and Temp are being used to predict the y-variable of Ozone. $\underbrace{Y_i}_\text{Ozone} \underbrace{=}_{\sim} \overbrace{\beta_0}^{\stackrel{\text{y-int}}{\text{baseline}}} + \overbrace{\beta_1}^{\stackrel{\text{slope}}{\text{baseline}}} \underbrace{X_{1i}}_\text{Temp} + \overbrace{\beta_2}^{\stackrel{\text{change in}}{\text{y-int}}} \underbrace{X_{2i}}_\text{Month} + \overbrace{\beta_3}^{\stackrel{\text{change in}}{\text{slope}}} \underbrace{X_{1i}X_{2i}}_\text{Temp:Month} + \epsilon_i$ air_lm <- lm(Ozone ~ Temp + Month + Temp:Month, data= airquality) pander(air_lm$coefficients)
(Intercept) Temp Month Temp:Month
-3.915 0.77 -23.01 0.2678

Notice how all coefficient estimates have changed. The y-intercept, $$\beta_0$$ is now estimated to be $$-3.915$$. The slope term, $$\beta_1$$, in the Temp-direction is estimated as $$0.77$$, while the slope term, $$\beta_2$$, in the Month-direction is estimated to be $$-23.01$$. This change in estimated coefficiets is due to the presence of the interaction term’s coefficient, $$\beta_3$$, which is estimated to be $$0.2678$$. As you should notice in the graphic, the interaction model allows the “slopes” in each direction to change, creating a “curved” surface for the regression surface instead of a flat surface.

#Perform the multiple regression
air_lm <- lm(Ozone ~ Temp + Month + Temp:Month, data= airquality)

#Graph Resolution (more important for more complex shapes)
graph_reso <- 0.5

#Setup Axis
axis_x <- seq(min(airquality$Temp), max(airquality$Temp), by = graph_reso)
axis_y <- seq(min(airquality$Month), max(airquality$Month), by = graph_reso)

#Sample points
air_surface <- expand.grid(Temp = axis_x, Month = axis_y, KEEP.OUT.ATTRS=F)
air_surface <- air_surface %>% mutate(Z=predict.lm(air_lm, newdata = air_surface))
air_surface <- acast(air_surface, Month ~ Temp, value.var = "Z") #y ~ x

#Create scatterplot
plot_ly(airquality,
x = ~Temp,
y = ~Month,
z = ~Ozone,
text = rownames(airquality),
type = "scatter3d",
mode = "markers") %>%
x = axis_x,
y = axis_y,
type = "surface")

And here is that same plot as a contour plot.

air_surface <- expand.grid(Temp = axis_x, Month = axis_y, KEEP.OUT.ATTRS=F)
air_surface$Z <- predict.lm(air_lm, newdata = air_surface) mycolorpalette <- colorRampPalette(c("skyblue2", "orange")) filled.contour(x=axis_x, y=axis_y, z=matrix(air_surface$Z, length(axis_x), length(axis_y)), col=mycolorpalette(27))

The coefficient $$\beta_j$$ is interpreted as the change in the expected value of $$Y$$ for a unit increase in $$X_{j}$$, holding all other variables constant, for $$j=1,\ldots,p-1$$. However, this interpretation breaks down when higher order terms (like $$X^2$$) or interaction terms (like $$X1:X2$$) are included in the model.

See the Explanation tab for details about possible hypotheses here.

### Explanation

#### Assessing the Model Fit (Expand)

$$R^2$$, adjusted $$R^2$$, AIC, BIC…

#### Model Selection (Expand)

pairs plots, added variable plots, and pattern recognition…

#### Model Validation (Expand)

Verifying a model’s ability to generalize to new data…

#### Interpretation (Expand)

$$\beta_j$$ is the change in the average y-value…

When to add another $$X$$-variable to the model…

#### Outlier Analysis (Expand)

Cook’s Distances and Leverage Values…

#### Inference for the Model Parameters (Expand)

t Tests and F tests in multiple regression…