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.
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.
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.