#obtaining dataset
advertising<-read.csv("advertising.csv")
subset <-subset(advertising, select=c(TV,radio,newspaper,sales))
#building correlation matrix
plot(subset)
#linear association numbers
cor(subset)
## TV radio newspaper sales
## TV 1.00000000 0.05480866 0.05664787 0.7822244
## radio 0.05480866 1.00000000 0.35410375 0.5762226
## newspaper 0.05664787 0.35410375 1.00000000 0.2282990
## sales 0.78222442 0.57622257 0.22829903 1.0000000
After visualizing and calculating the correlation between variables, we can say that the predictors radio and newspaper have the strongest linear relation. Specifically, the two variables have an r of .354. On the other hand, TV is the predictor with the strongest linear association to sales. (r = .782).
lm1<-lm(sales~newspaper + TV, data=advertising)
lm1
##
## Call:
## lm(formula = sales ~ newspaper + TV, data = advertising)
##
## Coefficients:
## (Intercept) newspaper TV
## 5.77495 0.04422 0.04690
lm2<-lm(sales~newspaper + radio + TV, data=advertising)
lm2
##
## Call:
## lm(formula = sales ~ newspaper + radio + TV, data = advertising)
##
## Coefficients:
## (Intercept) newspaper radio TV
## 2.938889 -0.001037 0.188530 0.045765
summary(lm2)
##
## Call:
## lm(formula = sales ~ newspaper + radio + TV, data = advertising)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.8277 -0.8908 0.2418 1.1893 2.8292
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.938889 0.311908 9.422 <2e-16 ***
## newspaper -0.001037 0.005871 -0.177 0.86
## radio 0.188530 0.008611 21.893 <2e-16 ***
## TV 0.045765 0.001395 32.809 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.686 on 196 degrees of freedom
## Multiple R-squared: 0.8972, Adjusted R-squared: 0.8956
## F-statistic: 570.3 on 3 and 196 DF, p-value: < 2.2e-16
In this question, we built 2 linear models. The first linear model is the effect of sales on newspaper and TV, while the second linear model is the effect of sales on newspaper, radio, and TV. It is worth noting that the signs of the partial coefficients of newspaper are different. In the first linear model, B(newspaper) is 0.04 while in the second, B(newspaper) is -0.001. This might because the effect of newspaper marketing on sales is not statistically significant.
After further analyzing linear model 2, we can see that the coefficient of newspaper does not have a statistically significant result. This confirms the theory that newspaper budget does not have a statistical significant effect on sales.
lm3<-lm(sales~TV, data=advertising)
lm4<-lm(radio~TV, data=advertising)
#lm3$residuals #on y axis, part of TV (Y) not accounted by sales
#lm4$residuals #on x axis, part of radio variable (X2) not accounted by TV
plot(lm3$residuals~lm4$residuals)
abline(lm(lm3$residuals~lm4$residuals))
summary(lm(lm3$residuals~lm4$residuals))
##
## Call:
## lm(formula = lm3$residuals ~ lm4$residuals)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.7977 -0.8752 0.2422 1.1708 2.8328
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.681e-16 1.186e-01 0.00 1
## lm4$residuals 1.880e-01 8.020e-03 23.44 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.677 on 198 degrees of freedom
## Multiple R-squared: 0.7351, Adjusted R-squared: 0.7338
## F-statistic: 549.5 on 1 and 198 DF, p-value: < 2.2e-16
In the following, a partial regression plot was created in order to attempt to understand the effect of adding an additional variable to the model (in this case TV). We can see in the chart that the linear models created with the residuals are linearly related. This can be confirmed with the p-value of the linear regression, which is < .05. This would indicate that the unexplained variability when regressing radio on sales can be explained by TV. For this reason, TV should be included as a predictor in the regression.
lm3 <- lm(sales~TV+radio, data = advertising)
lm4 <- lm(sales~radio, data = advertising)
summary(lm3)
##
## Call:
## lm(formula = sales ~ TV + radio, data = advertising)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.7977 -0.8752 0.2422 1.1708 2.8328
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.92110 0.29449 9.919 <2e-16 ***
## TV 0.04575 0.00139 32.909 <2e-16 ***
## radio 0.18799 0.00804 23.382 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.681 on 197 degrees of freedom
## Multiple R-squared: 0.8972, Adjusted R-squared: 0.8962
## F-statistic: 859.6 on 2 and 197 DF, p-value: < 2.2e-16
summary(lm4)
##
## Call:
## lm(formula = sales ~ radio, data = advertising)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.7305 -2.1324 0.7707 2.7775 8.1810
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.31164 0.56290 16.542 <2e-16 ***
## radio 0.20250 0.02041 9.921 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.275 on 198 degrees of freedom
## Multiple R-squared: 0.332, Adjusted R-squared: 0.3287
## F-statistic: 98.42 on 1 and 198 DF, p-value: < 2.2e-16
For linear model 1 (sales on radio) the R-squared is 0.8972 and the adjusted R-squared 0.8962. On the other hand, for linear model 2 (sales on TV and radio) the R-squared is 0.332 and the adjusted R-squared 0.3287. This confirms the belief that TV data should be added to the linear model to make a better prediction.
lm5 <- lm(sales~TV+radio, data = advertising)
summary(lm5)
##
## Call:
## lm(formula = sales ~ TV + radio, data = advertising)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.7977 -0.8752 0.2422 1.1708 2.8328
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.92110 0.29449 9.919 <2e-16 ***
## TV 0.04575 0.00139 32.909 <2e-16 ***
## radio 0.18799 0.00804 23.382 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.681 on 197 degrees of freedom
## Multiple R-squared: 0.8972, Adjusted R-squared: 0.8962
## F-statistic: 859.6 on 2 and 197 DF, p-value: < 2.2e-16
Looking at the summary output, the predictor variables TV and radio are significant (their p-value < 0.5). We can interpret the partial regression coefficient of radio as the following: As radio budget increases 1 unit, sales go up 0.188 units, holding TV budget constant.
#Compare R2 and adjusted R2 for two models:
lm.full <- lm(sales~TV+radio+TV:radio, data = advertising)
lm.reduced <-lm(sales~TV+radio, data = advertising)
summary(lm.reduced)
##
## Call:
## lm(formula = sales ~ TV + radio, data = advertising)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.7977 -0.8752 0.2422 1.1708 2.8328
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.92110 0.29449 9.919 <2e-16 ***
## TV 0.04575 0.00139 32.909 <2e-16 ***
## radio 0.18799 0.00804 23.382 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.681 on 197 degrees of freedom
## Multiple R-squared: 0.8972, Adjusted R-squared: 0.8962
## F-statistic: 859.6 on 2 and 197 DF, p-value: < 2.2e-16
summary(lm.full)
##
## Call:
## lm(formula = sales ~ TV + radio + TV:radio, data = advertising)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.3366 -0.4028 0.1831 0.5948 1.5246
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.750e+00 2.479e-01 27.233 <2e-16 ***
## TV 1.910e-02 1.504e-03 12.699 <2e-16 ***
## radio 2.886e-02 8.905e-03 3.241 0.0014 **
## TV:radio 1.086e-03 5.242e-05 20.727 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9435 on 196 degrees of freedom
## Multiple R-squared: 0.9678, Adjusted R-squared: 0.9673
## F-statistic: 1963 on 3 and 196 DF, p-value: < 2.2e-16
#Conducting Partial F-test
#Ho B(TV:radio) = 0, Ha B(TV:radio) /= 0
anova(lm.reduced, lm.full)
## Analysis of Variance Table
##
## Model 1: sales ~ TV + radio
## Model 2: sales ~ TV + radio + TV:radio
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 197 556.91
## 2 196 174.48 1 382.43 429.59 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
For linear model 1 (sales on TV and radio) the R-squared is 0.8972 and the adjusted R-squared 0.8962. On the other hand, for linear model 2 (sales on TV, radio, and TV:radio) the R-squared is 0.9678 and the adjusted R-squared 0.9673. This leads us to believe that the interaction data data should be added to the linear model to make a better prediction.
This can be confirmed looking at the Partial F-test results, we can see that the interaction term (TV:radio) is a statistically significant predictor in the model, so it should be included. This is because p < .05.