Skip to main content

Section 8.7 Linear Regression Model

We have identified that there is a significant negative correlation between the two variables. Since we want to be able to predict the amount (volume) of pizza sold using price, our next step is to run a linear regression model.
With a linear regression model, we are trying to build the line of best fit and figure out the equation for the line.
Where:
  • y is the predicted value (in this case, volume)
  • m is the slope
  • x is the given value (in this case, price)
  • b is the y intercept (the y value when x is 0)
When we are able to get the slope and the intercept, we can then begin to predict values.
To run a linear regression model, we can use the lm command. It follows the structure:
  • lm(y ~ x, data = dataset)
  • lm(what we want to predict ~ the predictor, data = our dataset)
pizza_lm <- lm(volume ~ price, data = pizza_sales)
# The code above is saving our linear regression model as pizza_lm

summary(pizza_lm)
# When we call summary on our model, we can find out our key insights
Call:
lm(formula = volume ~ price, data = pizza_sales)

Residuals:
   Min     1Q Median     3Q    Max 
-28602 -11685  -1824   8379 110857 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   385442      27575   13.98   <2e-16 ***
price        -111669      10340  -10.80   <2e-16 ***
---
Signif. codes:  0 β€˜***’ 0.001 β€˜**’ 0.01 β€˜*’ 0.05 β€˜.’ 0.1 β€˜ ’ 1

Residual standard error: 17300 on 154 degrees of freedom
Multiple R-squared:  0.431,	Adjusted R-squared:  0.4273 
F-statistic: 116.6 on 1 and 154 DF,  p-value: < 2.2e-16
Congratulations! We have just run our first linear regression model! We have some key insights, including:
  • Intercept: 385,442. This is our b value on our line equation. This means that when the price of pizza is 0 (free pizza would be nice, but really just a dream), the y value would be 385442.
  • price: -111,669. This is our slope, or our m value on our line equation. This tells us that for every one-unit increase in price, sales volume decreased by approximately 111,669 units. This makes sense, since we have a negative correlation.
  • Multiple R-squared: As we have seen before, this tells us how much of the variability our model explains. For our example, about 43% of the variance in pizza sales volume is explained by price alone. Warning: Multiple R-squared in linear regression models always increases with each new predictor added to a model, regardless of how potent it is.
  • Adjusted R-squared: This is almost identical to the Multiple R-squared value, except that it takes into account each new predictor added, and does not go up just because a new predictor was added to the model.
  • p-value: There are three p-values here. The first two, on the intercept and price lines, indicate if the relationships are statistically significant, which they are. The bottom one is if the model itself is significant, which it is.
  • F-statistic: This tells us how strong our model is. 116.6 is a high value.

Putting this all together, our line formula is:.

y = -111,669x + 385,442
All of these insights together mean that this is a very good model to predict volume of pizza sold.
In R, we always want to explore different packages. The broom package in R is very helpful when making statistical objects into nicer tibbles, using the tidy() function.
# If we want to get the same information from our lm model using a different package

library(broom)
tidy(pizza_lm)
# A tibble: 2 Γ— 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  385442.    27575.      14.0 3.44e-29
2 price       -111669.    10340.     -10.8 1.36e-20
glance(pizza_lm)
# A tibble: 1 Γ— 12
  r.squared adj.r.squared  sigma statistic  p.value    df logLik   AIC   BIC
      <dbl>         <dbl>  <dbl>     <dbl>    <dbl> <dbl>  <dbl> <dbl> <dbl>
1     0.431         0.427 17303.      117. 1.36e-20     1 -1743. 3491. 3501.
# β„Ή 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
Our finalized equation is y (-111,669 * price) + 385,442. All we need to do is plug in any price value, and we can predict the volume of pizza sold.