Libraries

library(MASS)
library(ISLR)
attach(Boston)

Simple Linear Regression

names(Boston)
##  [1] "crim"    "zn"      "indus"   "chas"    "nox"     "rm"      "age"    
##  [8] "dis"     "rad"     "tax"     "ptratio" "black"   "lstat"   "medv"

Multiple Linear Regression

lm.fit = lm(medv~lstat+age, data=Boston)    # Two predictors.
lm.fit = lm(medv~., data=Boston)            # All predictors.
lm.fit = lm(medv~.-age, data=Boston)        # All EXCEPT age, method 1. 
lm.fit1 = update(lm.fit, ~.-age)            # All EXCEPT age, method 2. 
summary(lm.fit)

Interaction Terms

Non-Linear Transformations of the Predictors

lm.fit = lm(medv ~ lstat)
lm.fit2 = lm(medv ~ lstat + I(lstat^2))
anova(lm.fit, lm.fit2)
## Analysis of Variance Table
## 
## Model 1: medv ~ lstat
## Model 2: medv ~ lstat + I(lstat^2)
##   Res.Df   RSS Df Sum of Sq     F    Pr(>F)    
## 1    504 19472                                 
## 2    503 15347  1    4125.1 135.2 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Qualitative Predictors

Writing Functions

Conceptual Exercises

  1. Describe the null hypotheses to which the p-values given in Table 3.4 correspond. Explain what conclusions you can draw based on these p-values. Your explanation should be phrased in terms of sales, TV, radio, and newspaper, rather than in terms of the coefficients of the linear model.
    • Table 3.4 shows p-values corresponding to the least-squares coefficient estimates of the multiple linear regression of number of units sold on radio, TV, and newspaper advertising budgets.
    • Intercept: The low p-value of \(< 0.0001\) for the intercept means that, in the absence of any advertising via TV, radio, and newspaper, the \(Pr(t \geq 9.42) < 0.0001\). Therefore, we can reject the null hypothesis \(H_0 : \beta_0 = 0\).
    • TV/Radio: Similarly, both TV and Radio have p < 0.0001. Therefore, we have strong evidence that there exists some relationship between both (and independently) sales-TV, sales-Radio.
    • Newspaper: Here, \(p = 0.8599\). In other words, there is a high probability of obtaining our observation of \(t = -0.18\), assuming that there is no relationship between newspaper and sales. Therefore, we don’t have strong enough evidence to conclude there is any relationship between sales and radio. Correction: Should have emphasized that we are rejecting the hypothesis that there is no relationship between newspaper and sales in the presence of TV and radio.
  2. Carefully explain the differences between the KNN classifier and KNN regression methods.