Multiple and Logistic Regression

Michael Taylor

2018/05/17

library(openintro)
library(broom)
library(dplyr)
library(ggplot2)
library(modelr)
library(plotly)
knitr::opts_chunk$set(cache = TRUE)

Fitting a parallel slopes model

We use the lm() function to fit linear models to data. In this case, we want to understand how the price of MarioKart games sold at auction varies as a function of not only the number of wheels included in the package, but also whether the item is new or used. Obviously, it is expected that you might have to pay a premium to buy these new. But how much is that premium? Can we estimate its value after controlling for the number of wheels?

We will fit a parallel slopes model using lm(). In addition to the data argument, lm() needs to know which variables you want to include in your regression model, and how you want to include them. It accomplishes this using a formula argument. A simple linear regression formula looks like y ~ x, where y is the name of the response variable, and x is the name of the explanatory variable. Here, we will simply extend this formula to include multiple explanatory variables. A parallel slopes model has the form y ~ x + z, where z is a categorical explanatory variable, and x is a numerical explanatory variable.

The output from lm() is a model object, which when printed, will show the fitted coefficients.

data("marioKart")
mario_kart <- filter(marioKart, totalPr < 80)
glimpse(mario_kart)
## Observations: 141
## Variables: 12
## $ ID         <dbl> 150377422259, 260483376854, 320432342985, 280405224...
## $ duration   <int> 3, 7, 3, 3, 1, 3, 1, 1, 3, 7, 1, 1, 1, 1, 7, 7, 3, ...
## $ nBids      <int> 20, 13, 16, 18, 20, 19, 13, 15, 29, 8, 15, 15, 13, ...
## $ cond       <fct> new, used, new, new, new, new, used, new, used, use...
## $ startPr    <dbl> 0.99, 0.99, 0.99, 0.99, 0.01, 0.99, 0.01, 1.00, 0.9...
## $ shipPr     <dbl> 4.00, 3.99, 3.50, 0.00, 0.00, 4.00, 0.00, 2.99, 4.0...
## $ totalPr    <dbl> 51.55, 37.04, 45.50, 44.00, 71.00, 45.00, 37.02, 53...
## $ shipSp     <fct> standard, firstClass, firstClass, standard, media, ...
## $ sellerRate <int> 1580, 365, 998, 7, 820, 270144, 7284, 4858, 27, 201...
## $ stockPhoto <fct> yes, yes, no, yes, yes, yes, yes, yes, yes, no, yes...
## $ wheels     <int> 1, 1, 1, 1, 2, 0, 0, 2, 1, 1, 2, 2, 2, 2, 1, 0, 1, ...
## $ title      <fct> ~~ Wii MARIO KART &amp; WHEEL ~ NINTENDO Wii ~ BRAN...
(mod <- lm(formula = totalPr ~ wheels + cond, 
           data = mario_kart))
## 
## Call:
## lm(formula = totalPr ~ wheels + cond, data = mario_kart)
## 
## Coefficients:
## (Intercept)       wheels     condused  
##      42.370        7.233       -5.585

Using geom_line() and augment()

Parallel slopes models are so-named because we can visualize these models in the data space as not one line, but two parallel lines. To do this, we’ll draw two things:

Our plotting strategy is to compute the fitted values, plot these, and connect the points to form a line. The augment() function from the broom package provides an easy way to add the fittted values to our data frame, and the geom_line() function can then use that data frame to plot the points and connect them.

Note that this approach has the added benefit of automatically coloring the lines apropriately to match the data.

You already know how to use ggplot() and geom_point() to make the scatterplot. The only twist is that now you’ll pass your augment()-ed model as the data argument in your ggplot() call. When you add your geom_line(), instead of letting the y aesthetic inherit its values from the ggplot() call, you can set it to the .fitted column of the augment()-ed model. This has the advantage of automatically coloring the lines for you.

# Augment the model
augmented_mod <- augment(mod)
glimpse(augmented_mod)
## Observations: 141
## Variables: 10
## $ totalPr    <dbl> 51.55, 37.04, 45.50, 44.00, 71.00, 45.00, 37.02, 53...
## $ wheels     <int> 1, 1, 1, 1, 2, 0, 0, 2, 1, 1, 2, 2, 2, 2, 1, 0, 1, ...
## $ cond       <fct> new, used, new, new, new, new, used, new, used, use...
## $ .fitted    <dbl> 49.60260, 44.01777, 49.60260, 49.60260, 56.83544, 4...
## $ .se.fit    <dbl> 0.7087865, 0.5465195, 0.7087865, 0.7087865, 0.67645...
## $ .resid     <dbl> 1.9473995, -6.9777674, -4.1026005, -5.6026005, 14.1...
## $ .hat       <dbl> 0.02103158, 0.01250410, 0.02103158, 0.02103158, 0.0...
## $ .sigma     <dbl> 4.902339, 4.868399, 4.892414, 4.881308, 4.750591, 4...
## $ .cooksd    <dbl> 1.161354e-03, 8.712334e-03, 5.154337e-03, 9.612441e...
## $ .std.resid <dbl> 0.40270893, -1.43671086, -0.84838977, -1.15857953, ...
# scatterplot, with color
data_space <- ggplot(augmented_mod, aes(y = totalPr, x = wheels, color = cond)) + 
  geom_point()
# single call to geom_line()
data_space + 
  geom_line(aes(y = .fitted))

Intercept interpretation

mod
## 
## Call:
## lm(formula = totalPr ~ wheels + cond, data = mario_kart)
## 
## Coefficients:
## (Intercept)       wheels     condused  
##      42.370        7.233       -5.585

The expected price of a used MarioKart is $5.86 less than that of a new one with the same number of wheels.

Common slope interpretation

For each additional wheel, the expected price of a MarioKart increases by $7.23 regardless of whether it is new or used.

Syntax from math

The babies data set contains observations about the birthweight and other characteristics of children born in the San Francisco Bay area from 1960–1967.

We would like to build a model for birthweight as a function of the mother’s age and whether this child was her first (parity == 0). Use the mathematical specification below to code the model in R. \[birthweight = \beta_0 + \beta_1 \cdot age + \beta_2 \cdot parity + \epsilon\]

data("babies")
glimpse(babies)
## Observations: 1,236
## Variables: 8
## $ case      <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1...
## $ bwt       <int> 120, 113, 128, 123, 108, 136, 138, 132, 120, 143, 14...
## $ gestation <int> 284, 282, 279, NA, 282, 286, 244, 245, 289, 299, 351...
## $ parity    <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ age       <int> 27, 33, 28, 36, 23, 25, 33, 23, 25, 30, 27, 32, 23, ...
## $ height    <int> 62, 64, 64, 69, 67, 62, 62, 65, 62, 66, 68, 64, 63, ...
## $ weight    <int> 100, 135, 115, 190, 125, 93, 178, 140, 125, 136, 120...
## $ smoke     <int> 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1...
# build model
lm(bwt ~ age + parity, data = babies)
## 
## Call:
## lm(formula = bwt ~ age + parity, data = babies)
## 
## Coefficients:
## (Intercept)          age       parity  
##   118.27782      0.06315     -1.65248

Syntax from plot

This time, we’d like to build a model for birthweight as a function of the length of gestation and the mother’s smoking status.

# build model
mod <- lm(formula=bwt ~ gestation + smoke,data = babies)
augment(mod) %>% 
  ggplot(aes(y = bwt, x = gestation, color = factor(smoke))) +
  geom_point()

Evaluating and extending parallel slopes model

R-squared vs. adjusted R-squared

Two common measures of how well a model fits to data are \(R^2\) (the coefficient of determination) and the adjusted \(R^2\). The former measures the percentage of the variability in the response variable that is explained by the model. To compute this, we define
\[R^2 = 1 - \frac{SSE}{SST} \,,\]

where \(SSE\) and \(SST\) are the sum of the squared residuals, and the total sum of the squares, respectively. One issue with this measure is that the \(SSE\) can only decrease as new variable are added to the model, while the \(SST\) depends only on the response variable and therefore is not affected by changes to the model. This means that you can increase \(R^2\) by adding any additional variable to your model—even random noise.

The adjusted \(R^2\) includes a term that penalizes a model for each additional explanatory variable (where p is the number of explanatory variables). \[R^2_{adj} = 1 - \frac{SSE}{SST} \cdot \frac{n-1}{n-p-1} \,,\]

We can see both measures in the output of the summary() function on our model object.

(mod <- lm(formula = totalPr ~ wheels + cond, data = mario_kart))
## 
## Call:
## lm(formula = totalPr ~ wheels + cond, data = mario_kart)
## 
## Coefficients:
## (Intercept)       wheels     condused  
##      42.370        7.233       -5.585
  • Use summary() to compute \(R^2\) and adjusted \(R^2\) on the model object called mod.
# R^2 and adjusted R^2
summary(mod)
## 
## Call:
## lm(formula = totalPr ~ wheels + cond, data = mario_kart)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11.0078  -3.0754  -0.8254   2.9822  14.1646 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  42.3698     1.0651  39.780  < 2e-16 ***
## wheels        7.2328     0.5419  13.347  < 2e-16 ***
## condused     -5.5848     0.9245  -6.041 1.35e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.887 on 138 degrees of freedom
## Multiple R-squared:  0.7165, Adjusted R-squared:  0.7124 
## F-statistic: 174.4 on 2 and 138 DF,  p-value: < 2.2e-16
  • Use mutate() and rnorm() to add a new variable called noise to the mario_kart data set that consists of random noise. Save the new dataframe as mario_kart_noisy.
# add random noise
mario_kart_noisy <- mario_kart %>% mutate(noise = rnorm(nrow(mario_kart)) )
  • Use lm() to fit a model that includes wheels, cond, and the random noise term.
# compute new model
mod2 <- lm( totalPr ~ wheels + cond + noise, data=mario_kart_noisy)
  • Use summary() to compute \(R^2\) and adjusted \(R^2\) on the new model object. Did the value of \(R^2\) increase? What about adjusted \(R^2\)?
# new R^2 and adjusted R^2
summary(mod2)
## 
## Call:
## lm(formula = totalPr ~ wheels + cond + noise, data = mario_kart_noisy)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.5633  -3.0762  -0.6037   3.0086  14.7586 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  42.0659     1.0578  39.769  < 2e-16 ***
## wheels        7.5077     0.5473  13.717  < 2e-16 ***
## condused     -5.4652     0.9123  -5.991 1.74e-08 ***
## noise         0.9528     0.4183   2.278   0.0243 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.815 on 137 degrees of freedom
## Multiple R-squared:  0.7269, Adjusted R-squared:  0.7209 
## F-statistic: 121.5 on 3 and 137 DF,  p-value: < 2.2e-16

Prediction

Once we have fit a regression model, we can use it to make predictions for unseen observations or retrieve the fitted values. Here, we explore two methods for doing the latter.

A traditional way to return the fitted values (i.e. the \(\hat{y}\)’s) is to run the predict() function on the model object. This will return a vector of the fitted values. Note that predict() will take an optional newdata argument that will allow you to make predictions for observations that are not in the original data.

A newer alternative is the augment() function from the broom package, which returns a data.frame with the response varible (\(y\)), the relevant explanatory variables (the \(x\)’s), the fitted value (\(\hat{y}\)) and some information about the residuals (\(e\)). augment() will also take a newdata argument that allows you to make predictions.

  • Compute the fitted values of the model as a vector using predict().
# return a vector
predict(mod)[1:10]
##        1        2        3        4        5        6        7        8 
## 49.60260 44.01777 49.60260 49.60260 56.83544 42.36976 36.78493 56.83544 
##        9       10 
## 44.01777 44.01777
  • Compute the fitted values of the model as a vector using predict().
augment(mod)
##     totalPr wheels cond  .fitted   .se.fit       .resid       .hat
## 1     51.55      1  new 49.60260 0.7087865   1.94739955 0.02103158
## 2     37.04      1 used 44.01777 0.5465195  -6.97776738 0.01250410
## 3     45.50      1  new 49.60260 0.7087865  -4.10260045 0.02103158
## 4     44.00      1  new 49.60260 0.7087865  -5.60260045 0.02103158
## 5     71.00      2  new 56.83544 0.6764502  14.16455915 0.01915635
## 6     45.00      0  new 42.36976 1.0651119   2.63023994 0.04749321
## 7     37.02      0 used 36.78493 0.7065565   0.23507301 0.02089945
## 8     53.99      2  new 56.83544 0.6764502  -2.84544085 0.01915635
## 9     47.00      1 used 44.01777 0.5465195   2.98223262 0.01250410
## 10    50.00      1 used 44.01777 0.5465195   5.98223262 0.01250410
## 11    54.99      2  new 56.83544 0.6764502  -1.84544085 0.01915635
## 12    56.01      2  new 56.83544 0.6764502  -0.82544085 0.01915635
## 13    48.00      2  new 56.83544 0.6764502  -8.83544085 0.01915635
## 14    56.00      2  new 56.83544 0.6764502  -0.83544085 0.01915635
## 15    43.33      1 used 44.01777 0.5465195  -0.68776738 0.01250410
## 16    46.00      0 used 36.78493 0.7065565   9.21507301 0.02089945
## 17    46.71      1  new 49.60260 0.7087865  -2.89260045 0.02103158
## 18    46.00      1  new 49.60260 0.7087865  -3.60260045 0.02103158
## 19    55.99      2  new 56.83544 0.6764502  -0.84544085 0.01915635
## 20    31.00      0 used 36.78493 0.7065565  -5.78492699 0.02089945
## 21    53.98      2  new 56.83544 0.6764502  -2.85544085 0.01915635
## 22    64.95      2  new 56.83544 0.6764502   8.11455915 0.01915635
## 23    50.50      2  new 56.83544 0.6764502  -6.33544085 0.01915635
## 24    46.50      1 used 44.01777 0.5465195   2.48223262 0.01250410
## 25    55.00      2  new 56.83544 0.6764502  -1.83544085 0.01915635
## 26    34.50      0 used 36.78493 0.7065565  -2.28492699 0.02089945
## 27    36.00      0 used 36.78493 0.7065565  -0.78492699 0.02089945
## 28    40.00      0 used 36.78493 0.7065565   3.21507301 0.02089945
## 29    47.00      1  new 49.60260 0.7087865  -2.60260045 0.02103158
## 30    43.00      0 used 36.78493 0.7065565   6.21507301 0.02089945
## 31    31.00      0 used 36.78493 0.7065565  -5.78492699 0.02089945
## 32    41.99      1 used 44.01777 0.5465195  -2.02776738 0.01250410
## 33    49.49      2 used 51.25061 0.8279109  -1.76060777 0.02869514
## 34    41.00      1 used 44.01777 0.5465195  -3.01776738 0.01250410
## 35    44.78      1 used 44.01777 0.5465195   0.76223262 0.01250410
## 36    47.00      0 used 36.78493 0.7065565  10.21507301 0.02089945
## 37    44.00      1 used 44.01777 0.5465195  -0.01776738 0.01250410
## 38    63.99      2  new 56.83544 0.6764502   7.15455915 0.01915635
## 39    53.76      2  new 56.83544 0.6764502  -3.07544085 0.01915635
## 40    46.03      1  new 49.60260 0.7087865  -3.57260045 0.02103158
## 41    42.25      1 used 44.01777 0.5465195  -1.76776738 0.01250410
## 42    46.00      2 used 51.25061 0.8279109  -5.25060777 0.02869514
## 43    51.99      2  new 56.83544 0.6764502  -4.84544085 0.01915635
## 44    55.99      2  new 56.83544 0.6764502  -0.84544085 0.01915635
## 45    41.99      1 used 44.01777 0.5465195  -2.02776738 0.01250410
## 46    53.99      2  new 56.83544 0.6764502  -2.84544085 0.01915635
## 47    39.00      0 used 36.78493 0.7065565   2.21507301 0.02089945
## 48    38.06      0 used 36.78493 0.7065565   1.27507301 0.02089945
## 49    46.00      1 used 44.01777 0.5465195   1.98223262 0.01250410
## 50    59.88      2  new 56.83544 0.6764502   3.04455915 0.01915635
## 51    28.98      0 used 36.78493 0.7065565  -7.80492699 0.02089945
## 52    36.00      1 used 44.01777 0.5465195  -8.01776738 0.01250410
## 53    51.99      0  new 42.36976 1.0651119   9.62023994 0.04749321
## 54    43.95      0 used 36.78493 0.7065565   7.16507301 0.02089945
## 55    32.00      0 used 36.78493 0.7065565  -4.78492699 0.02089945
## 56    40.06      1 used 44.01777 0.5465195  -3.95776738 0.01250410
## 57    48.00      1 used 44.01777 0.5465195   3.98223262 0.01250410
## 58    36.00      0 used 36.78493 0.7065565  -0.78492699 0.02089945
## 59    31.00      0 used 36.78493 0.7065565  -5.78492699 0.02089945
## 60    53.99      2  new 56.83544 0.6764502  -2.84544085 0.01915635
## 61    30.00      0 used 36.78493 0.7065565  -6.78492699 0.02089945
## 62    58.00      2  new 56.83544 0.6764502   1.16455915 0.01915635
## 63    38.10      0 used 36.78493 0.7065565   1.31507301 0.02089945
## 64    61.76      2 used 51.25061 0.8279109  10.50939223 0.02869514
## 65    53.99      2  new 56.83544 0.6764502  -2.84544085 0.01915635
## 66    40.00      1 used 44.01777 0.5465195  -4.01776738 0.01250410
## 67    64.50      3 used 58.48345 1.2882085   6.01655183 0.06947257
## 68    49.01      2 used 51.25061 0.8279109  -2.24060777 0.02869514
## 69    47.00      1  new 49.60260 0.7087865  -2.60260045 0.02103158
## 70    40.10      1 used 44.01777 0.5465195  -3.91776738 0.01250410
## 71    41.50      1  new 49.60260 0.7087865  -8.10260045 0.02103158
## 72    56.00      2  new 56.83544 0.6764502  -0.83544085 0.01915635
## 73    64.95      2  new 56.83544 0.6764502   8.11455915 0.01915635
## 74    49.00      2 used 51.25061 0.8279109  -2.25060777 0.02869514
## 75    48.00      1 used 44.01777 0.5465195   3.98223262 0.01250410
## 76    38.00      0 used 36.78493 0.7065565   1.21507301 0.02089945
## 77    45.00      0 used 36.78493 0.7065565   8.21507301 0.02089945
## 78    41.95      0 used 36.78493 0.7065565   5.16507301 0.02089945
## 79    43.36      1 used 44.01777 0.5465195  -0.65776738 0.01250410
## 80    54.99      2  new 56.83544 0.6764502  -1.84544085 0.01915635
## 81    45.21      1 used 44.01777 0.5465195   1.19223262 0.01250410
## 82    65.02      4 used 65.71629 1.7946635  -0.69628856 0.13483640
## 83    45.75      1 used 44.01777 0.5465195   1.73223262 0.01250410
## 84    64.00      2  new 56.83544 0.6764502   7.16455915 0.01915635
## 85    36.00      0 used 36.78493 0.7065565  -0.78492699 0.02089945
## 86    54.70      1  new 49.60260 0.7087865   5.09739955 0.02103158
## 87    49.91      1  new 49.60260 0.7087865   0.30739955 0.02103158
## 88    47.00      0 used 36.78493 0.7065565  10.21507301 0.02089945
## 89    43.00      1 used 44.01777 0.5465195  -1.01776738 0.01250410
## 90    35.99      0 used 36.78493 0.7065565  -0.79492699 0.02089945
## 91    54.49      2 used 51.25061 0.8279109   3.23939223 0.02869514
## 92    46.00      1 used 44.01777 0.5465195   1.98223262 0.01250410
## 93    31.06      0 used 36.78493 0.7065565  -5.72492699 0.02089945
## 94    55.60      2 used 51.25061 0.8279109   4.34939223 0.02869514
## 95    40.10      0  new 42.36976 1.0651119  -2.26976006 0.04749321
## 96    52.59      2  new 56.83544 0.6764502  -4.24544085 0.01915635
## 97    44.00      2 used 51.25061 0.8279109  -7.25060777 0.02869514
## 98    38.26      1 used 44.01777 0.5465195  -5.75776738 0.01250410
## 99    51.00      2 used 51.25061 0.8279109  -0.25060777 0.02869514
## 100   48.99      2  new 56.83544 0.6764502  -7.84544085 0.01915635
## 101   66.44      2  new 56.83544 0.6764502   9.60455915 0.01915635
## 102   63.50      2  new 56.83544 0.6764502   6.66455915 0.01915635
## 103   42.00      0 used 36.78493 0.7065565   5.21507301 0.02089945
## 104   47.00      1  new 49.60260 0.7087865  -2.60260045 0.02103158
## 105   55.00      2 used 51.25061 0.8279109   3.74939223 0.02869514
## 106   33.01      1 used 44.01777 0.5465195 -11.00776738 0.01250410
## 107   53.76      2  new 56.83544 0.6764502  -3.07544085 0.01915635
## 108   46.00      1  new 49.60260 0.7087865  -3.60260045 0.02103158
## 109   43.00      0 used 36.78493 0.7065565   6.21507301 0.02089945
## 110   42.55      1 used 44.01777 0.5465195  -1.46776738 0.01250410
## 111   52.50      2 used 51.25061 0.8279109   1.24939223 0.02869514
## 112   57.50      2  new 56.83544 0.6764502   0.66455915 0.01915635
## 113   75.00      3  new 64.06828 1.0000415  10.93171876 0.04186751
## 114   48.92      1 used 44.01777 0.5465195   4.90223262 0.01250410
## 115   45.99      1  new 49.60260 0.7087865  -3.61260045 0.02103158
## 116   40.05      1 used 44.01777 0.5465195  -3.96776738 0.01250410
## 117   45.00      1  new 49.60260 0.7087865  -4.60260045 0.02103158
## 118   50.00      2 used 51.25061 0.8279109  -1.25060777 0.02869514
## 119   49.75      0  new 42.36976 1.0651119   7.38023994 0.04749321
## 120   47.00      1 used 44.01777 0.5465195   2.98223262 0.01250410
## 121   56.00      2  new 56.83544 0.6764502  -0.83544085 0.01915635
## 122   41.00      1 used 44.01777 0.5465195  -3.01776738 0.01250410
## 123   46.00      1  new 49.60260 0.7087865  -3.60260045 0.02103158
## 124   34.99      1 used 44.01777 0.5465195  -9.02776738 0.01250410
## 125   49.00      2 used 51.25061 0.8279109  -2.25060777 0.02869514
## 126   61.00      2  new 56.83544 0.6764502   4.16455915 0.01915635
## 127   62.89      2  new 56.83544 0.6764502   6.05455915 0.01915635
## 128   46.00      1  new 49.60260 0.7087865  -3.60260045 0.02103158
## 129   64.95      2  new 56.83544 0.6764502   8.11455915 0.01915635
## 130   36.99      0 used 36.78493 0.7065565   0.20507301 0.02089945
## 131   44.00      1 used 44.01777 0.5465195  -0.01776738 0.01250410
## 132   41.35      1 used 44.01777 0.5465195  -2.66776738 0.01250410
## 133   37.00      0 used 36.78493 0.7065565   0.21507301 0.02089945
## 134   58.98      2  new 56.83544 0.6764502   2.14455915 0.01915635
## 135   39.00      0 used 36.78493 0.7065565   2.21507301 0.02089945
## 136   40.70      1 used 44.01777 0.5465195  -3.31776738 0.01250410
## 137   39.51      0 used 36.78493 0.7065565   2.72507301 0.02089945
## 138   52.00      2 used 51.25061 0.8279109   0.74939223 0.02869514
## 139   47.70      1  new 49.60260 0.7087865  -1.90260045 0.02103158
## 140   38.76      0 used 36.78493 0.7065565   1.97507301 0.02089945
## 141   54.51      2  new 56.83544 0.6764502  -2.32544085 0.01915635
##       .sigma      .cooksd   .std.resid
## 1   4.902339 1.161354e-03  0.402708933
## 2   4.868399 8.712334e-03 -1.436710863
## 3   4.892414 5.154337e-03 -0.848389768
## 4   4.881308 9.612441e-03 -1.158579529
## 5   4.750591 5.574926e-02  2.926332759
## 6   4.899816 5.053659e-03  0.551419180
## 7   4.905181 1.681147e-05  0.048608215
## 8   4.899077 2.249739e-03 -0.587854989
## 9   4.898517 1.591419e-03  0.614036807
## 10  4.878184 6.403658e-03  1.231731888
## 11  4.902639 9.463096e-04 -0.381259589
## 12  4.904706 1.893237e-04 -0.170532281
## 13  4.845644 2.169149e-02 -1.825361432
## 14  4.904693 1.939387e-04 -0.172598235
## 15  4.904866 8.464177e-05 -0.141610176
## 16  4.840263 2.583436e-02  1.905485609
## 17  4.898859 2.562308e-03 -0.598170028
## 18  4.895349 3.974537e-03 -0.744993181
## 19  4.904680 1.986092e-04 -0.174664189
## 20  4.879726 1.018113e-02 -1.196202689
## 21  4.899034 2.265580e-03 -0.589920943
## 22  4.855017 1.829628e-02  1.676430592
## 23  4.874681 1.115287e-02 -1.308872933
## 24  4.900578 1.102520e-03  0.511087627
## 25  4.902666 9.360817e-04 -0.379193635
## 26  4.901254 1.588345e-03 -0.472475419
## 27  4.904754 1.874384e-04 -0.162306590
## 28  4.897361 3.144719e-03  0.664810290
## 29  4.900072 2.074290e-03 -0.538200007
## 30  4.875781 1.175148e-02  1.285147949
## 31  4.879726 1.018113e-02 -1.196202689
## 32  4.902124 7.357628e-04 -0.417513979
## 33  4.902848 1.315656e-03 -0.365515142
## 34  4.898356 1.629570e-03 -0.621353356
## 35  4.904785 1.039625e-04  0.156942447
## 36  4.825276 3.174557e-02  2.112264829
## 37  4.905222 5.648698e-08 -0.003658274
## 38  4.866239 1.422325e-02  1.478099008
## 39  4.898043 2.628135e-03 -0.635371930
## 40  4.895513 3.908619e-03 -0.738789386
## 41  4.902867 5.591802e-04 -0.363980405
## 42  4.884059 1.170136e-02 -1.090064849
## 43  4.887380 6.523784e-03 -1.001045788
## 44  4.904680 1.986092e-04 -0.174664189
## 45  4.902124 7.357628e-04 -0.417513979
## 46  4.899077 2.249739e-03 -0.587854989
## 47  4.901493 1.492713e-03  0.458031070
## 48  4.903987 4.946184e-04  0.263658603
## 49  4.902261 7.030898e-04  0.408138446
## 50  4.898186 2.575620e-03  0.628991915
## 51  4.858711 1.853266e-02 -1.613896713
## 52  4.856546 1.150293e-02 -1.650845158
## 53  4.832389 6.760627e-02  2.016844445
## 54  4.866054 1.561857e-02  1.481588208
## 55  4.887793 6.965475e-03 -0.989423469
## 56  4.893406 2.802864e-03 -0.814897814
## 57  4.893260 2.837624e-03  0.819935167
## 58  4.904754 1.874384e-04 -0.162306590
## 59  4.879726 1.018113e-02 -1.196202689
## 60  4.899077 2.249739e-03 -0.587854989
## 61  4.870114 1.400524e-02 -1.402981909
## 62  4.904194 3.768392e-04  0.240592564
## 63  4.903908 5.261382e-04  0.271929772
## 64  4.819876 4.687834e-02  2.181827236
## 65  4.899077 2.249739e-03 -0.587854989
## 66  4.893045 2.888492e-03 -0.827251716
## 67  4.876193 4.052940e-02  1.276155547
## 68  4.901375 2.130829e-03 -0.465166678
## 69  4.900072 2.074290e-03 -0.538200007
## 70  4.893644 2.746495e-03 -0.806661880
## 71  4.855070 2.010496e-02 -1.675562463
## 72  4.904693 1.939387e-04 -0.172598235
## 73  4.855017 1.829628e-02  1.676430592
## 74  4.901341 2.149892e-03 -0.467242751
## 75  4.893260 2.837624e-03  0.819935167
## 76  4.904101 4.491639e-04  0.251251850
## 77  4.853667 2.053161e-02  1.698706389
## 78  4.884908 8.116206e-03  1.068029769
## 79  4.904897 7.741876e-05 -0.135433225
## 80  4.902639 9.463096e-04 -0.381259589
## 81  4.904152 2.543452e-04  0.245478742
## 82  4.904806 1.218734e-03 -0.153165404
## 83  4.902961 5.369254e-04  0.356663856
## 84  4.866129 1.426303e-02  1.480164962
## 85  4.904754 1.874384e-04 -0.162306590
## 86  4.885435 7.957044e-03  1.054107431
## 87  4.905151 2.893749e-05  0.063568128
## 88  4.825276 3.174557e-02  2.112264829
## 89  4.904442 1.853526e-04 -0.209556635
## 90  4.904742 1.922448e-04 -0.164374382
## 91  4.897178 4.453937e-03  0.672521687
## 92  4.902261 7.030898e-04  0.408138446
## 93  4.880253 9.971030e-03 -1.183795936
## 94  4.890710 8.029235e-03  0.902965863
## 95  4.901197 3.763354e-03 -0.475846028
## 96  4.891531 5.008164e-03 -0.877088548
## 97  4.864786 2.231340e-02 -1.505279580
## 98  4.880180 5.932118e-03 -1.185514863
## 99  4.905174 2.665668e-05 -0.052028020
## 100 4.858308 1.710282e-02 -1.620831987
## 101 4.834741 2.563232e-02  1.984257737
## 102 4.871414 1.234172e-02  1.376867262
## 103 4.884512 8.274103e-03  1.078368730
## 104 4.900072 2.074290e-03 -0.538200007
## 105 4.894442 5.966763e-03  0.778401443
## 106 4.813060 2.168204e-02 -2.266481255
## 107 4.898043 2.628135e-03 -0.635371930
## 108 4.895349 3.974537e-03 -0.744993181
## 109 4.875781 1.175148e-02  1.285147949
## 110 4.903599 3.854926e-04 -0.302210897
## 111 4.904027 6.625438e-04  0.259383029
## 112 4.904888 1.227157e-04  0.137294864
## 113 4.811529 7.605411e-02  2.285052621
## 114 4.887082 4.300207e-03  1.009361659
## 115 4.895294 3.996633e-03 -0.747061113
## 116 4.893346 2.817046e-03 -0.816956798
## 117 4.889096 6.487255e-03 -0.951786355
## 118 4.904024 6.638336e-04 -0.259635386
## 119 4.862490 3.978837e-02  1.547237493
## 120 4.898517 1.591419e-03  0.614036807
## 121 4.904693 1.939387e-04 -0.172598235
## 122 4.898356 1.629570e-03 -0.621353356
## 123 4.895349 3.974537e-03 -0.744993181
## 124 4.843427 1.458352e-02 -1.858802502
## 125 4.901341 2.149892e-03 -0.467242751
## 126 4.892049 4.819157e-03  0.860378763
## 127 4.877336 1.018587e-02  1.250844068
## 128 4.895349 3.974537e-03 -0.744993181
## 129 4.855017 1.829628e-02  1.676430592
## 130 4.905191 1.279432e-05  0.042404838
## 131 4.905222 5.648698e-08 -0.003658274
## 132 4.899857 1.273496e-03 -0.549288929
## 133 4.905187 1.407252e-05  0.044472630
## 134 4.901733 1.277936e-03  0.443056056
## 135 4.901493 1.492713e-03  0.458031070
## 136 4.896922 1.969670e-03 -0.683122864
## 137 4.899576 2.259209e-03  0.563488472
## 138 4.904792 2.383611e-04  0.155579346
## 139 4.902471 1.108535e-03 -0.393444786
## 140 4.902257 1.186770e-03  0.408404057
## 141 4.901119 1.502601e-03 -0.480425381

Fitting a model with interaction

Including an interaction term in a model is easy—we just have to tell lm() that we want to include that new variable. An expression of the form

lm(y ~ x + z + x:z, data = mydata)

will do the trick. The use of the colon (:) here means that the interaction between x and z will be a third term in the model.

  • Use lm() to fit a model for the price of a MarioKart as a function of its condition and the duration of the auction, with interaction.
lm(formula = totalPr ~ cond + 
     duration + 
     cond:duration,
   data = mario_kart)
## 
## Call:
## lm(formula = totalPr ~ cond + duration + cond:duration, data = mario_kart)
## 
## Coefficients:
##       (Intercept)           condused           duration  
##            58.268            -17.122             -1.966  
## condused:duration  
##             2.325

Visualizing interaction models

Interaction allows the slope of the regression line in each group to vary. In this case, this means that the relationship between the final price and the length of the auction is moderated by the condition of each item.

Interaction models are easy to visualize in the data space with ggplot2 because they have the same coefficients as if the models were fit independently to each group defined by the level of the categorical variable. In this case, new and used MarioKarts each get their own regression line. To see this, we can set an aesthetic (e.g. color) to the categorical variable, and then add a geom_smooth() layer to overlay the regression line for each color.

  • Use the color aesthetic and the geom_smooth() function to plot the interaction model between duration and condition in the data space. Make sure you set the method and se arguments of geom_smooth().
# interaction plot
ggplot(mario_kart, aes(y= totalPr, 
                       x= duration, 
                       color=cond)) + 
  geom_point() + 
  geom_smooth(method = "lm",se=0)

Consequences of Simpson’s paradox

In the simple linear regression model for average SAT score, (total) as a function of average teacher salary (salary), the fitted coefficient was -5.02 points per thousand dollars. This suggests that for every additional thousand dollars of salary for teachers in a particular state, the expected SAT score for a student from that state is about 5 points lower.

In the model that includes the percentage of students taking the SAT, the coefficient on salary becomes 1.84 points per thousand dollars. Choose the correct interpretation of this slope coefficient.

For every additional thousand dollars of salary for teachers in a particular state, the expected SAT score for a student from that state is about 2 points higher, after controlling for the percentage of students taking the SAT.

Simpson’s paradox in action

A mild version of (Simpson’s paradox)[https://en.wikipedia.org/wiki/Simpson%27s_paradox] can be observed in the MarioKart auction data. Consider the relationship between the final auction price and the length of the auction. It seems reasonable to assume that longer auctions would result in higher prices, since—other things being equal—a longer auction gives more bidders more time to see the auction and bid on the item.

However, a simple linear regression model reveals the opposite: longer auctions are associated with lower final prices. The problem is that all other things are not equal. In this case, the new MarioKarts—which people pay a premium for—were mostly sold in one-day auctions, while a plurality of the used MarioKarts were sold in the standard seven-day auctions.

Our simple linear regression model is misleading, in that it suggests a negative relationship between final auction price and duration. However, for the used MarioKarts, the relationship is positive.

  • Fit a simple linear regression model for final auction price (totalPr) as a function of duration (duration).
slr <- ggplot(mario_kart, 
              aes(y = totalPr, 
                  x = duration)) + 
  geom_point() + 
  geom_smooth(method = "lm", se = 0)
# model with one slope
lm(totalPr ~ duration, data = mario_kart)
## 
## Call:
## lm(formula = totalPr ~ duration, data = mario_kart)
## 
## Coefficients:
## (Intercept)     duration  
##      52.374       -1.317
  • Use aes() to add a color aesthetic that’s mapped to the condition variable to the slr object.
slr +  aes(color = cond)

Multiple Regression

Fitting a MLR model

In terms of the R code, fitting a multiple linear regression model is easy: simply add variables to the model formula you specify in the lm() command.

In a parallel slopes model, we had two explanatory variables: one was numeric and one was categorical. Here, we will allow both explanatory variables to be numeric.

  • Fit a multiple linear regression model for total price as a function of the duration of the auction and the starting price
# Fit the model using duration and startPr
(mod <- lm(totalPr ~ duration + startPr, mario_kart))
## 
## Call:
## lm(formula = totalPr ~ duration + startPr, data = mario_kart)
## 
## Coefficients:
## (Intercept)     duration      startPr  
##      51.030       -1.508        0.233

Tiling the plane

One method for visualizing a multiple linear regression model is to create a heatmap of the fitted values in the plane defined by the two explanatory variables. This heatmap will illustrate how the model output changes over different combinations of the explanatory variables.

This is a multistep process:

  • First, create a grid of the possible pairs of values of the explanatory variables. The grid should be over the actual range of the data present in each variable. Store the result as a data frame called grid.
grid <- mario_kart %>% 
  data_grid(duration = seq_range(duration, by = 1),
            startPr = seq_range(startPr, by = 1)
            )
  • Use augment() with the newdata argument to find the \(\hat{y}\)’s corresponding to the values in grid.
(price_hats <- augment(mod, newdata = grid))
##     duration startPr  .fitted   .se.fit
## 1          1    0.01 49.52371 0.9965480
## 2          1    1.01 49.75666 0.9848633
## 3          1    2.01 49.98962 0.9749931
## 4          1    3.01 50.22257 0.9669932
## 5          1    4.01 50.45553 0.9609101
## 6          1    5.01 50.68848 0.9567805
## 7          1    6.01 50.92144 0.9546298
## 8          1    7.01 51.15439 0.9544713
## 9          1    8.01 51.38734 0.9563059
## 10         1    9.01 51.62030 0.9601223
## 11         1   10.01 51.85325 0.9658970
## 12         1   11.01 52.08621 0.9735951
## 13         1   12.01 52.31916 0.9831715
## 14         1   13.01 52.55212 0.9945718
## 15         1   14.01 52.78507 1.0077343
## 16         1   15.01 53.01802 1.0225908
## 17         1   16.01 53.25098 1.0390687
## 18         1   17.01 53.48393 1.0570921
## 19         1   18.01 53.71689 1.0765836
## 20         1   19.01 53.94984 1.0974647
## 21         1   20.01 54.18279 1.1196579
## 22         1   21.01 54.41575 1.1430866
## 23         1   22.01 54.64870 1.1676765
## 24         1   23.01 54.88166 1.1933558
## 25         1   24.01 55.11461 1.2200558
## 26         1   25.01 55.34757 1.2477109
## 27         1   26.01 55.58052 1.2762590
## 28         1   27.01 55.81347 1.3056416
## 29         1   28.01 56.04643 1.3358035
## 30         1   29.01 56.27938 1.3666933
## 31         1   30.01 56.51234 1.3982626
## 32         1   31.01 56.74529 1.4304664
## 33         1   32.01 56.97825 1.4632629
## 34         1   33.01 57.21120 1.4966132
## 35         1   34.01 57.44415 1.5304809
## 36         1   35.01 57.67711 1.5648325
## 37         1   36.01 57.91006 1.5996369
## 38         1   37.01 58.14302 1.6348651
## 39         1   38.01 58.37597 1.6704903
## 40         1   39.01 58.60892 1.7064876
## 41         1   40.01 58.84188 1.7428340
## 42         1   41.01 59.07483 1.7795081
## 43         1   42.01 59.30779 1.8164901
## 44         1   43.01 59.54074 1.8537614
## 45         1   44.01 59.77370 1.8913051
## 46         1   45.01 60.00665 1.9291052
## 47         1   46.01 60.23960 1.9671469
## 48         1   47.01 60.47256 2.0054165
## 49         1   48.01 60.70551 2.0439012
## 50         1   49.01 60.93847 2.0825890
## 51         1   50.01 61.17142 2.1214689
## 52         1   51.01 61.40438 2.1605305
## 53         1   52.01 61.63733 2.1997640
## 54         1   53.01 61.87028 2.2391605
## 55         1   54.01 62.10324 2.2787115
## 56         1   55.01 62.33619 2.3184091
## 57         1   56.01 62.56915 2.3582459
## 58         1   57.01 62.80210 2.3982149
## 59         1   58.01 63.03505 2.4383097
## 60         1   59.01 63.26801 2.4785242
## 61         1   60.01 63.50096 2.5188525
## 62         1   61.01 63.73392 2.5592894
## 63         1   62.01 63.96687 2.5998298
## 64         1   63.01 64.19983 2.6404688
## 65         1   64.01 64.43278 2.6812021
## 66         1   65.01 64.66573 2.7220254
## 67         1   66.01 64.89869 2.7629347
## 68         1   67.01 65.13164 2.8039261
## 69         1   68.01 65.36460 2.8449963
## 70         1   69.01 65.59755 2.8861418
## 71         2    0.01 48.01558 0.8524393
## 72         2    1.01 48.24854 0.8368861
## 73         2    2.01 48.48149 0.8233544
## 74         2    3.01 48.71445 0.8119450
## 75         2    4.01 48.94740 0.8027485
## 76         2    5.01 49.18036 0.7958417
## 77         2    6.01 49.41331 0.7912845
## 78         2    7.01 49.64626 0.7891176
## 79         2    8.01 49.87922 0.7893606
## 80         2    9.01 50.11217 0.7920114
## 81         2   10.01 50.34513 0.7970460
## 82         2   11.01 50.57808 0.8044196
## 83         2   12.01 50.81104 0.8140685
## 84         2   13.01 51.04399 0.8259132
## 85         2   14.01 51.27694 0.8398607
## 86         2   15.01 51.50990 0.8558081
## 87         2   16.01 51.74285 0.8736461
## 88         2   17.01 51.97581 0.8932612
## 89         2   18.01 52.20876 0.9145392
## 90         2   19.01 52.44171 0.9373669
## 91         2   20.01 52.67467 0.9616339
## 92         2   21.01 52.90762 0.9872340
## 93         2   22.01 53.14058 1.0140663
## 94         2   23.01 53.37353 1.0420357
## 95         2   24.01 53.60649 1.0710530
## 96         2   25.01 53.83944 1.1010353
## 97         2   26.01 54.07239 1.1319061
## 98         2   27.01 54.30535 1.1635946
## 99         2   28.01 54.53830 1.1960358
## 100        2   29.01 54.77126 1.2291701
## 101        2   30.01 55.00421 1.2629429
## 102        2   31.01 55.23717 1.2973045
## 103        2   32.01 55.47012 1.3322091
## 104        2   33.01 55.70307 1.3676154
## 105        2   34.01 55.93603 1.4034852
## 106        2   35.01 56.16898 1.4397839
## 107        2   36.01 56.40194 1.4764800
## 108        2   37.01 56.63489 1.5135445
## 109        2   38.01 56.86784 1.5509510
## 110        2   39.01 57.10080 1.5886753
## 111        2   40.01 57.33375 1.6266953
## 112        2   41.01 57.56671 1.6649908
## 113        2   42.01 57.79966 1.7035432
## 114        2   43.01 58.03262 1.7423355
## 115        2   44.01 58.26557 1.7813519
## 116        2   45.01 58.49852 1.8205781
## 117        2   46.01 58.73148 1.8600007
## 118        2   47.01 58.96443 1.8996076
## 119        2   48.01 59.19739 1.9393875
## 120        2   49.01 59.43034 1.9793299
## 121        2   50.01 59.66330 2.0194252
## 122        2   51.01 59.89625 2.0596645
## 123        2   52.01 60.12920 2.1000394
## 124        2   53.01 60.36216 2.1405424
## 125        2   54.01 60.59511 2.1811663
## 126        2   55.01 60.82807 2.2219043
## 127        2   56.01 61.06102 2.2627505
## 128        2   57.01 61.29397 2.3036990
## 129        2   58.01 61.52693 2.3447445
## 130        2   59.01 61.75988 2.3858819
## 131        2   60.01 61.99284 2.4271066
## 132        2   61.01 62.22579 2.4684142
## 133        2   62.01 62.45875 2.5098007
## 134        2   63.01 62.69170 2.5512621
## 135        2   64.01 62.92465 2.5927949
## 136        2   65.01 63.15761 2.6343958
## 137        2   66.01 63.39056 2.6760614
## 138        2   67.01 63.62352 2.7177890
## 139        2   68.01 63.85647 2.7595755
## 140        2   69.01 64.08943 2.8014185
## 141        3    0.01 46.50746 0.7686211
## 142        3    1.01 46.74041 0.7492544
## 143        3    2.01 46.97337 0.7319796
## 144        3    3.01 47.20632 0.7169480
## 145        3    4.01 47.43928 0.7043033
## 146        3    5.01 47.67223 0.6941758
## 147        3    6.01 47.90518 0.6866769
## 148        3    7.01 48.13814 0.6818934
## 149        3    8.01 48.37109 0.6798826
## 150        3    9.01 48.60405 0.6806690
## 151        3   10.01 48.83700 0.6842431
## 152        3   11.01 49.06995 0.6905615
## 153        3   12.01 49.30291 0.6995499
## 154        3   13.01 49.53586 0.7111070
## 155        3   14.01 49.76882 0.7251100
## 156        3   15.01 50.00177 0.7414204
## 157        3   16.01 50.23473 0.7598895
## 158        3   17.01 50.46768 0.7803642
## 159        3   18.01 50.70063 0.8026910
## 160        3   19.01 50.93359 0.8267198
## 161        3   20.01 51.16654 0.8523067
## 162        3   21.01 51.39950 0.8793156
## 163        3   22.01 51.63245 0.9076197
## 164        3   23.01 51.86541 0.9371016
## 165        3   24.01 52.09836 0.9676537
## 166        3   25.01 52.33131 0.9991777
## 167        3   26.01 52.56427 1.0315846
## 168        3   27.01 52.79722 1.0647938
## 169        3   28.01 53.03018 1.0987325
## 170        3   29.01 53.26313 1.1333351
## 171        3   30.01 53.49608 1.1685428
## 172        3   31.01 53.72904 1.2043025
## 173        3   32.01 53.96199 1.2405663
## 174        3   33.01 54.19495 1.2772915
## 175        3   34.01 54.42790 1.3144392
## 176        3   35.01 54.66086 1.3519747
## 177        3   36.01 54.89381 1.3898666
## 178        3   37.01 55.12676 1.4280864
## 179        3   38.01 55.35972 1.4666087
## 180        3   39.01 55.59267 1.5054100
## 181        3   40.01 55.82563 1.5444694
## 182        3   41.01 56.05858 1.5837679
## 183        3   42.01 56.29154 1.6232880
## 184        3   43.01 56.52449 1.6630139
## 185        3   44.01 56.75744 1.7029313
## 186        3   45.01 56.99040 1.7430270
## 187        3   46.01 57.22335 1.7832889
## 188        3   47.01 57.45631 1.8237061
## 189        3   48.01 57.68926 1.8642684
## 190        3   49.01 57.92221 1.9049666
## 191        3   50.01 58.15517 1.9457921
## 192        3   51.01 58.38812 1.9867372
## 193        3   52.01 58.62108 2.0277945
## 194        3   53.01 58.85403 2.0689574
## 195        3   54.01 59.08699 2.1102197
## 196        3   55.01 59.31994 2.1515757
## 197        3   56.01 59.55289 2.1930201
## 198        3   57.01 59.78585 2.2345479
## 199        3   58.01 60.01880 2.2761546
## 200        3   59.01 60.25176 2.3178360
## 201        3   60.01 60.48471 2.3595881
## 202        3   61.01 60.71767 2.4014072
## 203        3   62.01 60.95062 2.4432898
## 204        3   63.01 61.18357 2.4852328
## 205        3   64.01 61.41653 2.5272331
## 206        3   65.01 61.64948 2.5692880
## 207        3   66.01 61.88244 2.6113948
## 208        3   67.01 62.11539 2.6535510
## 209        3   68.01 62.34834 2.6957543
## 210        3   69.01 62.58130 2.7380025
## 211        4    0.01 44.99933 0.7651698
## 212        4    1.01 45.23229 0.7436173
## 213        4    2.01 45.46524 0.7240558
## 214        4    3.01 45.69820 0.7066507
## 215        4    4.01 45.93115 0.6915648
## 216        4    5.01 46.16410 0.6789528
## 217        4    6.01 46.39706 0.6689545
## 218        4    7.01 46.63001 0.6616885
## 219        4    8.01 46.86297 0.6572453
## 220        4    9.01 47.09592 0.6556824
## 221        4   10.01 47.32887 0.6570204
## 222        4   11.01 47.56183 0.6612416
## 223        4   12.01 47.79478 0.6682913
## 224        4   13.01 48.02774 0.6780815
## 225        4   14.01 48.26069 0.6904954
## 226        4   15.01 48.49365 0.7053947
## 227        4   16.01 48.72660 0.7226255
## 228        4   17.01 48.95955 0.7420256
## 229        4   18.01 49.19251 0.7634294
## 230        4   19.01 49.42546 0.7866736
## 231        4   20.01 49.65842 0.8115999
## 232        4   21.01 49.89137 0.8380584
## 233        4   22.01 50.12433 0.8659085
## 234        4   23.01 50.35728 0.8950204
## 235        4   24.01 50.59023 0.9252749
## 236        4   25.01 50.82319 0.9565637
## 237        4   26.01 51.05614 0.9887886
## 238        4   27.01 51.28910 1.0218611
## 239        4   28.01 51.52205 1.0557014
## 240        4   29.01 51.75500 1.0902380
## 241        4   30.01 51.98796 1.1254070
## 242        4   31.01 52.22091 1.1611507
## 243        4   32.01 52.45387 1.1974178
## 244        4   33.01 52.68682 1.2341621
## 245        4   34.01 52.91978 1.2713423
## 246        4   35.01 53.15273 1.3089211
## 247        4   36.01 53.38568 1.3468652
## 248        4   37.01 53.61864 1.3851447
## 249        4   38.01 53.85159 1.4237324
## 250        4   39.01 54.08455 1.4626039
## 251        4   40.01 54.31750 1.5017373
## 252        4   41.01 54.55046 1.5411125
## 253        4   42.01 54.78341 1.5807116
## 254        4   43.01 55.01636 1.6205180
## 255        4   44.01 55.24932 1.6605169
## 256        4   45.01 55.48227 1.7006947
## 257        4   46.01 55.71523 1.7410390
## 258        4   47.01 55.94818 1.7815385
## 259        4   48.01 56.18113 1.8221828
## 260        4   49.01 56.41409 1.8629625
## 261        4   50.01 56.64704 1.9038689
## 262        4   51.01 56.88000 1.9448940
## 263        4   52.01 57.11295 1.9860303
## 264        4   53.01 57.34591 2.0272713
## 265        4   54.01 57.57886 2.0686105
## 266        4   55.01 57.81181 2.1100422
## 267        4   56.01 58.04477 2.1515611
## 268        4   57.01 58.27772 2.1931622
## 269        4   58.01 58.51068 2.2348410
## 270        4   59.01 58.74363 2.2765931
## 271        4   60.01 58.97659 2.3184146
## 272        4   61.01 59.20954 2.3603019
## 273        4   62.01 59.44249 2.4022514
## 274        4   63.01 59.67545 2.4442599
## 275        4   64.01 59.90840 2.4863246
## 276        4   65.01 60.14136 2.5284425
## 277        4   66.01 60.37431 2.5706111
## 278        4   67.01 60.60726 2.6128279
## 279        4   68.01 60.84022 2.6550906
## 280        4   69.01 61.07317 2.6973970
## 281        5    0.01 43.49121 0.8430732
## 282        5    1.01 43.72416 0.8216643
## 283        5    2.01 43.95712 0.8020601
## 284        5    3.01 44.19007 0.7843959
## 285        5    4.01 44.42302 0.7688053
## 286        5    5.01 44.65598 0.7554169
## 287        5    6.01 44.88893 0.7443493
## 288        5    7.01 45.12189 0.7357075
## 289        5    8.01 45.35484 0.7295775
## 290        5    9.01 45.58779 0.7260231
## 291        5   10.01 45.82075 0.7250820
## 292        5   11.01 46.05370 0.7267645
## 293        5   12.01 46.28666 0.7310524
## 294        5   13.01 46.51961 0.7379002
## 295        5   14.01 46.75257 0.7472378
## 296        5   15.01 46.98552 0.7589730
## 297        5   16.01 47.21847 0.7729969
## 298        5   17.01 47.45143 0.7891872
## 299        5   18.01 47.68438 0.8074138
## 300        5   19.01 47.91734 0.8275422
## 301        5   20.01 48.15029 0.8494370
## 302        5   21.01 48.38325 0.8729654
## 303        5   22.01 48.61620 0.8979991
## 304        5   23.01 48.84915 0.9244157
## 305        5   24.01 49.08211 0.9521001
## 306        5   25.01 49.31506 0.9809451
## 307        5   26.01 49.54802 1.0108512
## 308        5   27.01 49.78097 1.0417270
## 309        5   28.01 50.01392 1.0734890
## 310        5   29.01 50.24688 1.1060607
## 311        5   30.01 50.47983 1.1393727
## 312        5   31.01 50.71279 1.1733620
## 313        5   32.01 50.94574 1.2079714
## 314        5   33.01 51.17870 1.2431491
## 315        5   34.01 51.41165 1.2788482
## 316        5   35.01 51.64460 1.3150263
## 317        5   36.01 51.87756 1.3516448
## 318        5   37.01 52.11051 1.3886690
## 319        5   38.01 52.34347 1.4260672
## 320        5   39.01 52.57642 1.4638109
## 321        5   40.01 52.80937 1.5018738
## 322        5   41.01 53.04233 1.5402324
## 323        5   42.01 53.27528 1.5788652
## 324        5   43.01 53.50824 1.6177524
## 325        5   44.01 53.74119 1.6568762
## 326        5   45.01 53.97415 1.6962201
## 327        5   46.01 54.20710 1.7357693
## 328        5   47.01 54.44005 1.7755100
## 329        5   48.01 54.67301 1.8154296
## 330        5   49.01 54.90596 1.8555166
## 331        5   50.01 55.13892 1.8957604
## 332        5   51.01 55.37187 1.9361511
## 333        5   52.01 55.60483 1.9766799
## 334        5   53.01 55.83778 2.0173382
## 335        5   54.01 56.07073 2.0581186
## 336        5   55.01 56.30369 2.0990138
## 337        5   56.01 56.53664 2.1400173
## 338        5   57.01 56.76960 2.1811230
## 339        5   58.01 57.00255 2.2223252
## 340        5   59.01 57.23550 2.2636186
## 341        5   60.01 57.46846 2.3049984
## 342        5   61.01 57.70141 2.3464599
## 343        5   62.01 57.93437 2.3879990
## 344        5   63.01 58.16732 2.4296115
## 345        5   64.01 58.40028 2.4712939
## 346        5   65.01 58.63323 2.5130426
## 347        5   66.01 58.86618 2.5548544
## 348        5   67.01 59.09914 2.5967263
## 349        5   68.01 59.33209 2.6386553
## 350        5   69.01 59.56505 2.6806388
## 351        6    0.01 41.98308 0.9831789
## 352        6    1.01 42.21603 0.9632646
## 353        6    2.01 42.44899 0.9449471
## 354        6    3.01 42.68194 0.9283208
## 355        6    4.01 42.91490 0.9134781
## 356        6    5.01 43.14785 0.9005073
## 357        6    6.01 43.38081 0.8894901
## 358        6    7.01 43.61376 0.8805001
## 359        6    8.01 43.84671 0.8735996
## 360        6    9.01 44.07967 0.8688386
## 361        6   10.01 44.31262 0.8662522
## 362        6   11.01 44.54558 0.8658601
## 363        6   12.01 44.77853 0.8676651
## 364        6   13.01 45.01149 0.8716536
## 365        6   14.01 45.24444 0.8777959
## 366        6   15.01 45.47739 0.8860471
## 367        6   16.01 45.71035 0.8963490
## 368        6   17.01 45.94330 0.9086319
## 369        6   18.01 46.17626 0.9228167
## 370        6   19.01 46.40921 0.9388171
## 371        6   20.01 46.64216 0.9565421
## 372        6   21.01 46.87512 0.9758977
## 373        6   22.01 47.10807 0.9967889
## 374        6   23.01 47.34103 1.0191213
## 375        6   24.01 47.57398 1.0428023
## 376        6   25.01 47.80694 1.0677421
## 377        6   26.01 48.03989 1.0938547
## 378        6   27.01 48.27284 1.1210581
## 379        6   28.01 48.50580 1.1492749
## 380        6   29.01 48.73875 1.1784322
## 381        6   30.01 48.97171 1.2084620
## 382        6   31.01 49.20466 1.2393009
## 383        6   32.01 49.43762 1.2708900
## 384        6   33.01 49.67057 1.3031747
## 385        6   34.01 49.90352 1.3361045
## 386        6   35.01 50.13648 1.3696331
## 387        6   36.01 50.36943 1.4037174
## 388        6   37.01 50.60239 1.4383179
## 389        6   38.01 50.83534 1.4733983
## 390        6   39.01 51.06829 1.5089251
## 391        6   40.01 51.30125 1.5448676
## 392        6   41.01 51.53420 1.5811973
## 393        6   42.01 51.76716 1.6178882
## 394        6   43.01 52.00011 1.6549163
## 395        6   44.01 52.23307 1.6922594
## 396        6   45.01 52.46602 1.7298971
## 397        6   46.01 52.69897 1.7678106
## 398        6   47.01 52.93193 1.8059826
## 399        6   48.01 53.16488 1.8443970
## 400        6   49.01 53.39784 1.8830389
## 401        6   50.01 53.63079 1.9218946
## 402        6   51.01 53.86375 1.9609515
## 403        6   52.01 54.09670 2.0001977
## 404        6   53.01 54.32965 2.0396224
## 405        6   54.01 54.56261 2.0792152
## 406        6   55.01 54.79556 2.1189670
## 407        6   56.01 55.02852 2.1588687
## 408        6   57.01 55.26147 2.1989124
## 409        6   58.01 55.49442 2.2390903
## 410        6   59.01 55.72738 2.2793954
## 411        6   60.01 55.96033 2.3198211
## 412        6   61.01 56.19329 2.3603611
## 413        6   62.01 56.42624 2.4010097
## 414        6   63.01 56.65920 2.4417614
## 415        6   64.01 56.89215 2.4826111
## 416        6   65.01 57.12510 2.5235542
## 417        6   66.01 57.35806 2.5645861
## 418        6   67.01 57.59101 2.6057026
## 419        6   68.01 57.82397 2.6468998
## 420        6   69.01 58.05692 2.6881740
## 421        7    0.01 40.47495 1.1632240
## 422        7    1.01 40.70791 1.1450791
## 423        7    2.01 40.94086 1.1283308
## 424        7    3.01 41.17382 1.1130422
## 425        7    4.01 41.40677 1.0992742
## 426        7    5.01 41.63973 1.0870846
## 427        7    6.01 41.87268 1.0765270
## 428        7    7.01 42.10563 1.0676498
## 429        7    8.01 42.33859 1.0604952
## 430        7    9.01 42.57154 1.0550982
## 431        7   10.01 42.80450 1.0514860
## 432        7   11.01 43.03745 1.0496769
## 433        7   12.01 43.27041 1.0496803
## 434        7   13.01 43.50336 1.0514961
## 435        7   14.01 43.73631 1.0551150
## 436        7   15.01 43.96927 1.0605186
## 437        7   16.01 44.20222 1.0676797
## 438        7   17.01 44.43518 1.0765632
## 439        7   18.01 44.66813 1.0871270
## 440        7   19.01 44.90108 1.0993226
## 441        7   20.01 45.13404 1.1130963
## 442        7   21.01 45.36699 1.1283904
## 443        7   22.01 45.59995 1.1451440
## 444        7   23.01 45.83290 1.1632940
## 445        7   24.01 46.06586 1.1827761
## 446        7   25.01 46.29881 1.2035257
## 447        7   26.01 46.53176 1.2254783
## 448        7   27.01 46.76472 1.2485705
## 449        7   28.01 46.99767 1.2727403
## 450        7   29.01 47.23063 1.2979274
## 451        7   30.01 47.46358 1.3240739
## 452        7   31.01 47.69654 1.3511240
## 453        7   32.01 47.92949 1.3790245
## 454        7   33.01 48.16244 1.4077250
## 455        7   34.01 48.39540 1.4371774
## 456        7   35.01 48.62835 1.4673365
## 457        7   36.01 48.86131 1.4981595
## 458        7   37.01 49.09426 1.5296065
## 459        7   38.01 49.32721 1.5616396
## 460        7   39.01 49.56017 1.5942236
## 461        7   40.01 49.79312 1.6273253
## 462        7   41.01 50.02608 1.6609139
## 463        7   42.01 50.25903 1.6949603
## 464        7   43.01 50.49199 1.7294375
## 465        7   44.01 50.72494 1.7643202
## 466        7   45.01 50.95789 1.7995850
## 467        7   46.01 51.19085 1.8352097
## 468        7   47.01 51.42380 1.8711738
## 469        7   48.01 51.65676 1.9074581
## 470        7   49.01 51.88971 1.9440447
## 471        7   50.01 52.12267 1.9809168
## 472        7   51.01 52.35562 2.0180588
## 473        7   52.01 52.58857 2.0554560
## 474        7   53.01 52.82153 2.0930948
## 475        7   54.01 53.05448 2.1309624
## 476        7   55.01 53.28744 2.1690467
## 477        7   56.01 53.52039 2.2073366
## 478        7   57.01 53.75334 2.2458216
## 479        7   58.01 53.98630 2.2844917
## 480        7   59.01 54.21925 2.3233378
## 481        7   60.01 54.45221 2.3623512
## 482        7   61.01 54.68516 2.4015236
## 483        7   62.01 54.91812 2.4408475
## 484        7   63.01 55.15107 2.4803157
## 485        7   64.01 55.38402 2.5199213
## 486        7   65.01 55.61698 2.5596580
## 487        7   66.01 55.84993 2.5995198
## 488        7   67.01 56.08289 2.6395010
## 489        7   68.01 56.31584 2.6795963
## 490        7   69.01 56.54880 2.7198005
## 491        8    0.01 38.96683 1.3675234
## 492        8    1.01 39.19978 1.3509679
## 493        8    2.01 39.43274 1.3356334
## 494        8    3.01 39.66569 1.3215625
## 495        8    4.01 39.89865 1.3087957
## 496        8    5.01 40.13160 1.2973718
## 497        8    6.01 40.36455 1.2873263
## 498        8    7.01 40.59751 1.2786918
## 499        8    8.01 40.83046 1.2714971
## 500        8    9.01 41.06342 1.2657666
## 501        8   10.01 41.29637 1.2615204
## 502        8   11.01 41.52932 1.2587734
## 503        8   12.01 41.76228 1.2575356
## 504        8   13.01 41.99523 1.2578113
## 505        8   14.01 42.22819 1.2595995
## 506        8   15.01 42.46114 1.2628938
## 507        8   16.01 42.69410 1.2676826
## 508        8   17.01 42.92705 1.2739488
## 509        8   18.01 43.16000 1.2816710
## 510        8   19.01 43.39296 1.2908228
## 511        8   20.01 43.62591 1.3013742
## 512        8   21.01 43.85887 1.3132915
## 513        8   22.01 44.09182 1.3265378
## 514        8   23.01 44.32478 1.3410737
## 515        8   24.01 44.55773 1.3568578
## 516        8   25.01 44.79068 1.3738471
## 517        8   26.01 45.02364 1.3919975
## 518        8   27.01 45.25659 1.4112641
## 519        8   28.01 45.48955 1.4316018
## 520        8   29.01 45.72250 1.4529658
## 521        8   30.01 45.95545 1.4753114
## 522        8   31.01 46.18841 1.4985948
## 523        8   32.01 46.42136 1.5227728
## 524        8   33.01 46.65432 1.5478036
## 525        8   34.01 46.88727 1.5736465
## 526        8   35.01 47.12023 1.6002621
## 527        8   36.01 47.35318 1.6276125
## 528        8   37.01 47.58613 1.6556614
## 529        8   38.01 47.81909 1.6843738
## 530        8   39.01 48.05204 1.7137163
## 531        8   40.01 48.28500 1.7436572
## 532        8   41.01 48.51795 1.7741662
## 533        8   42.01 48.75091 1.8052144
## 534        8   43.01 48.98386 1.8367746
## 535        8   44.01 49.21681 1.8688207
## 536        8   45.01 49.44977 1.9013283
## 537        8   46.01 49.68272 1.9342740
## 538        8   47.01 49.91568 1.9676358
## 539        8   48.01 50.14863 2.0013930
## 540        8   49.01 50.38158 2.0355258
## 541        8   50.01 50.61454 2.0700157
## 542        8   51.01 50.84749 2.1048451
## 543        8   52.01 51.08045 2.1399975
## 544        8   53.01 51.31340 2.1754571
## 545        8   54.01 51.54636 2.2112093
## 546        8   55.01 51.77931 2.2472400
## 547        8   56.01 52.01226 2.2835360
## 548        8   57.01 52.24522 2.3200850
## 549        8   58.01 52.47817 2.3568751
## 550        8   59.01 52.71113 2.3938953
## 551        8   60.01 52.94408 2.4311349
## 552        8   61.01 53.17704 2.4685842
## 553        8   62.01 53.40999 2.5062336
## 554        8   63.01 53.64294 2.5440743
## 555        8   64.01 53.87590 2.5820979
## 556        8   65.01 54.10885 2.6202964
## 557        8   66.01 54.34181 2.6586624
## 558        8   67.01 54.57476 2.6971885
## 559        8   68.01 54.80771 2.7358682
## 560        8   69.01 55.04067 2.7746949
## 561        9    0.01 37.45870 1.5867361
## 562        9    1.01 37.69166 1.5714972
## 563        9    2.01 37.92461 1.5573323
## 564        9    3.01 38.15757 1.5442709
## 565        9    4.01 38.39052 1.5323412
## 566        9    5.01 38.62347 1.5215699
## 567        9    6.01 38.85643 1.5119816
## 568        9    7.01 39.08938 1.5035991
## 569        9    8.01 39.32234 1.4964426
## 570        9    9.01 39.55529 1.4905297
## 571        9   10.01 39.78824 1.4858753
## 572        9   11.01 40.02120 1.4824913
## 573        9   12.01 40.25415 1.4803863
## 574        9   13.01 40.48711 1.4795658
## 575        9   14.01 40.72006 1.4800320
## 576        9   15.01 40.95302 1.4817837
## 577        9   16.01 41.18597 1.4848162
## 578        9   17.01 41.41892 1.4891218
## 579        9   18.01 41.65188 1.4946895
## 580        9   19.01 41.88483 1.5015052
## 581        9   20.01 42.11779 1.5095520
## 582        9   21.01 42.35074 1.5188104
## 583        9   22.01 42.58370 1.5292583
## 584        9   23.01 42.81665 1.5408716
## 585        9   24.01 43.04960 1.5536241
## 586        9   25.01 43.28256 1.5674880
## 587        9   26.01 43.51551 1.5824341
## 588        9   27.01 43.74847 1.5984321
## 589        9   28.01 43.98142 1.6154506
## 590        9   29.01 44.21437 1.6334579
## 591        9   30.01 44.44733 1.6524215
## 592        9   31.01 44.68028 1.6723090
## 593        9   32.01 44.91324 1.6930877
## 594        9   33.01 45.14619 1.7147254
## 595        9   34.01 45.37915 1.7371898
## 596        9   35.01 45.61210 1.7604494
## 597        9   36.01 45.84505 1.7844730
## 598        9   37.01 46.07801 1.8092302
## 599        9   38.01 46.31096 1.8346914
## 600        9   39.01 46.54392 1.8608276
## 601        9   40.01 46.77687 1.8876107
## 602        9   41.01 47.00983 1.9150137
## 603        9   42.01 47.24278 1.9430103
## 604        9   43.01 47.47573 1.9715752
## 605        9   44.01 47.70869 2.0006841
## 606        9   45.01 47.94164 2.0303136
## 607        9   46.01 48.17460 2.0604412
## 608        9   47.01 48.40755 2.0910454
## 609        9   48.01 48.64050 2.1221056
## 610        9   49.01 48.87346 2.1536020
## 611        9   50.01 49.10641 2.1855158
## 612        9   51.01 49.33937 2.2178289
## 613        9   52.01 49.57232 2.2505243
## 614        9   53.01 49.80528 2.2835854
## 615        9   54.01 50.03823 2.3169965
## 616        9   55.01 50.27118 2.3507429
## 617        9   56.01 50.50414 2.3848102
## 618        9   57.01 50.73709 2.4191848
## 619        9   58.01 50.97005 2.4538539
## 620        9   59.01 51.20300 2.4888052
## 621        9   60.01 51.43596 2.5240269
## 622        9   61.01 51.66891 2.5595079
## 623        9   62.01 51.90186 2.5952375
## 624        9   63.01 52.13482 2.6312057
## 625        9   64.01 52.36777 2.6674026
## 626        9   65.01 52.60073 2.7038193
## 627        9   66.01 52.83368 2.7404468
## 628        9   67.01 53.06663 2.7772770
## 629        9   68.01 53.29959 2.8143017
## 630        9   69.01 53.53254 2.8515135
## 631       10    0.01 35.95058 1.8154678
## 632       10    1.01 36.18353 1.8012978
## 633       10    2.01 36.41649 1.7880806
## 634       10    3.01 36.64944 1.7758375
## 635       10    4.01 36.88239 1.7645889
## 636       10    5.01 37.11535 1.7543538
## 637       10    6.01 37.34830 1.7451501
## 638       10    7.01 37.58126 1.7369942
## 639       10    8.01 37.81421 1.7299009
## 640       10    9.01 38.04716 1.7238832
## 641       10   10.01 38.28012 1.7189526
## 642       10   11.01 38.51307 1.7151184
## 643       10   12.01 38.74603 1.7123879
## 644       10   13.01 38.97898 1.7107665
## 645       10   14.01 39.21194 1.7102572
## 646       10   15.01 39.44489 1.7108612
## 647       10   16.01 39.67784 1.7125771
## 648       10   17.01 39.91080 1.7154017
## 649       10   18.01 40.14375 1.7193295
## 650       10   19.01 40.37671 1.7243530
## 651       10   20.01 40.60966 1.7304625
## 652       10   21.01 40.84262 1.7376468
## 653       10   22.01 41.07557 1.7458924
## 654       10   23.01 41.30852 1.7551845
## 655       10   24.01 41.54148 1.7655065
## 656       10   25.01 41.77443 1.7768405
## 657       10   26.01 42.00739 1.7891672
## 658       10   27.01 42.24034 1.8024663
## 659       10   28.01 42.47329 1.8167164
## 660       10   29.01 42.70625 1.8318953
## 661       10   30.01 42.93920 1.8479802
## 662       10   31.01 43.17216 1.8649476
## 663       10   32.01 43.40511 1.8827735
## 664       10   33.01 43.63807 1.9014340
## 665       10   34.01 43.87102 1.9209046
## 666       10   35.01 44.10397 1.9411611
## 667       10   36.01 44.33693 1.9621790
## 668       10   37.01 44.56988 1.9839341
## 669       10   38.01 44.80284 2.0064025
## 670       10   39.01 45.03579 2.0295605
## 671       10   40.01 45.26875 2.0533847
## 672       10   41.01 45.50170 2.0778523
## 673       10   42.01 45.73465 2.1029407
## 674       10   43.01 45.96761 2.1286281
## 675       10   44.01 46.20056 2.1548930
## 676       10   45.01 46.43352 2.1817145
## 677       10   46.01 46.66647 2.2090724
## 678       10   47.01 46.89942 2.2369470
## 679       10   48.01 47.13238 2.2653192
## 680       10   49.01 47.36533 2.2941706
## 681       10   50.01 47.59829 2.3234833
## 682       10   51.01 47.83124 2.3532400
## 683       10   52.01 48.06420 2.3834242
## 684       10   53.01 48.29715 2.4140198
## 685       10   54.01 48.53010 2.4450113
## 686       10   55.01 48.76306 2.4763840
## 687       10   56.01 48.99601 2.5081234
## 688       10   57.01 49.22897 2.5402159
## 689       10   58.01 49.46192 2.5726482
## 690       10   59.01 49.69488 2.6054077
## 691       10   60.01 49.92783 2.6384821
## 692       10   61.01 50.16078 2.6718598
## 693       10   62.01 50.39374 2.7055295
## 694       10   63.01 50.62669 2.7394805
## 695       10   64.01 50.85965 2.7737024
## 696       10   65.01 51.09260 2.8081853
## 697       10   66.01 51.32555 2.8429198
## 698       10   67.01 51.55851 2.8778968
## 699       10   68.01 51.79146 2.9131074
## 700       10   69.01 52.02442 2.9485433
  • Add these to the data_space plot by using the fill aesthetic and geom_tile().
(data_space <- mario_kart %>% 
  ggplot(aes(x = duration,
             y = startPr)) +
  geom_point(aes(color = totalPr)))

# tile the plane
data_space + 
  geom_tile(data = price_hats, 
            aes(fill = .fitted), 
            alpha = 0.5  )

Models in 3D

An alternative way to visualize a multiple regression model with two numeric explanatory variables is as a plane in three dimensions. This is possible in R using the plotly package.

We have created three objects that you will need:

  • x: a vector of unique values of duration
  • y: a vector of unique values of startPr
  • plane: a matrix of the fitted values across all combinations of x and y

Much like ggplot(), the plot_ly() function will allow you to create a plot object with variables mapped to x, y, and z aesthetics. The add_markers() function is similar to geom_point() in that it allows you to add points to your 3D plot.

Note that plot_ly uses the pipe (%>%) operator to chain commands together.

# draw the 3D scatterplot
p <- plot_ly(data = mario_kart, 
             z = ~totalPr, 
             x = ~duration, 
             y = ~startPr, 
             opacity = 0.6) %>%
  add_markers() 
plane <- matrix(seq_range(price_hats$.fitted, n=70))
# draw the plane
p %>% 
  add_surface(x = ~x, 
              y = ~y,
              z = ~plane,
              showscale = FALSE)
# api_create(p, filename = "mario_kart_3D_scatter")

Coefficient magnitude

The coefficients from our model for the total auction price of MarioKarts as a function of auction duration and starting price are shown below.

mod
## 
## Call:
## lm(formula = totalPr ~ duration + startPr, data = mario_kart)
## 
## Coefficients:
## (Intercept)     duration      startPr  
##      51.030       -1.508        0.233

A colleague claims that these results imply that the duration of the auction is a more important determinant of final price than starting price, because the coefficient is larger. This interpretation is false because:

The coefficients have different units (dollars per day and dollars per dollar, respectively) and so they are not directly comparable.

Practicing interpretation

Fit a multiple regression model for the total auction price of an item in the mario_kart data set as a function of the starting price and the duration of the auction. Compute the coefficients and choose the correct interpretation of the duration variable.

mod <- lm(formula = totalPr ~ startPr + duration, data = mario_kart)
coef(mod)
## (Intercept)     startPr    duration 
##  51.0295070   0.2329542  -1.5081260

For each additional day the auction lasts, the expected final price declines by $1.51, after controlling for starting price.

Visualizing parallel planes

By including the duration, starting price, and condition variables in our model, we now have two explanatory variables and one categorical variable. Our model now takes the geometric form of two parallel planes!

The first plane corresponds to the model output when the condition of the item is new, while the second plane corresponds to the model output when the condition of the item is used. The planes have the same slopes along both the duration and starting price axes—it is the \(z\)-intercept that is different.

Once again we have stored the x and y vectors for you. Since we now have two planes, there are matrix objects plane0 and plane1 stored for you as well.

  • Use plot_ly to draw 3D scatterplot for totalPr as a function of duration, startPr, and cond by mapping the z variable to the response and the x and y variables to the explanatory variables. Duration should be on the x-axis and starting price should be on the y-axis. Use color to represent cond.
# draw the 3D scatterplot
p <- plot_ly(data = mario_kart, 
             z = ~totalPr, 
             x = ~duration, 
             y = ~startPr, 
             opacity = 0.6) %>%
  add_markers(color = ~cond) 
  • Use add_surface() (twice) to draw two planes through the cloud of points, one for new MarioKarts and another for used ones. Use the objects plane0 and plane1.
x0 <- mario_kart %>% 
  filter(cond == "new") %>% 
  select(duration, startPr)
plane0 <- as.matrix(table(x0$duration, x0$startPr))
dim(plane0)
## [1]  4 13
# draw two planes
p %>%
  add_surface(x = ~x, 
              y = ~y, 
              z = ~plane0, 
              showscale = FALSE) %>%
  add_surface(x = ~x, 
              y = ~y, 
              z = ~plane0, 
              showscale = FALSE)
sessionInfo()
## R version 3.4.4 (2018-03-15)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 17134)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252   
## [3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
## [5] LC_TIME=English_Canada.1252    
## 
## attached base packages:
## [1] methods   stats     graphics  grDevices utils     datasets  base     
## 
## other attached packages:
## [1] bindrcpp_0.2.2  plotly_4.7.1    modelr_0.1.2    ggplot2_2.2.1  
## [5] dplyr_0.7.5     broom_0.4.4     openintro_1.7.1
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_0.2.4  xfun_0.1          reshape2_1.4.3   
##  [4] purrr_0.2.5       lattice_0.20-35   colorspace_1.3-2 
##  [7] htmltools_0.3.6   viridisLite_0.3.0 yaml_2.1.19      
## [10] rlang_0.2.1       pillar_1.2.3      later_0.7.2      
## [13] foreign_0.8-70    glue_1.2.0        bindr_0.1.1      
## [16] plyr_1.8.4        stringr_1.3.1     munsell_0.4.3    
## [19] blogdown_0.6      gtable_0.2.0      htmlwidgets_1.2  
## [22] codetools_0.2-15  psych_1.8.4       evaluate_0.10.1  
## [25] knitr_1.20        httpuv_1.4.3      crosstalk_1.0.0  
## [28] parallel_3.4.4    Rcpp_0.12.17      xtable_1.8-2     
## [31] backports_1.1.2   scales_0.5.0      promises_1.0.1   
## [34] jsonlite_1.5      mime_0.5          mnormt_1.5-5     
## [37] digest_0.6.15     stringi_1.1.7     bookdown_0.7     
## [40] shiny_1.0.5       grid_3.4.4        rprojroot_1.3-2  
## [43] tools_3.4.4       magrittr_1.5      lazyeval_0.2.1   
## [46] tibble_1.4.2      tidyr_0.8.1       pkgconfig_2.0.1  
## [49] data.table_1.11.4 assertthat_0.2.0  rmarkdown_1.9    
## [52] httr_1.3.1        R6_2.2.2          nlme_3.1-137     
## [55] compiler_3.4.4
## Adding cites for R packages using knitr
knitr::write_bib(.packages(), "packages.bib")

References

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2018. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.