# 2 Linear Regression

## 2.1 Seminar

You will need to load the package from the course textbook:

library(ISLR)

### 2.1.1 Exercise

This question involves the use of multiple linear regression on the Auto dataset.

1. Produce a scatterplot matrix which includes all of the variables in the dataset.

2. Compute the matrix of correlations between the variables using the function cor(). You will need to exclude the name variable, which is qualitative.

3. Use the lm() function to perform a multiple linear regression with mpg as the response and all other variables except name as the predictors. Use the summary() function to print the results. Comment on the output. For instance:

1. Is there a relationship between the predictors and the response?
2. Which predictors appear to have a statistically significant relationship to the response?
3. What does the coefficient for the year variable suggest?
4. Use the plot() function to produce diagnostic plots of the linear regression fit. Comment on any problems you see with the fit. Do the residual plots suggest any unusually large outliers? Does the leverage plot identify any observations with unusually high leverage?

5. Use the * and : symbols to fit linear regression models with interaction effects. Do any interactions appear to be statistically significant?

6. Try a few different transformations of the variables, such as $$log(X)$$, $$\sqrt{X}$$, $$X^2$$. Comment on your findings.

### 2.1.2 Exercise

This question should be answered using the Carseats dataset from the ISLR package.

1. Fit a multiple regression model to predict Sales using Price, Urban, and US.

2. Provide an interpretation of each coefficient in the model. Be careful – some of the variables in the model are qualitative!

3. Write out the model in equation form, being careful to handle the qualitative variables properly.

4. For which of the predictors can you reject the null hypothesis $$H_0 : \beta_j =0$$?

5. On the basis of your response to the previous question, fit a smaller model that only uses the predictors for which there is evidence of association with the outcome.

6. How well do the models in (a) and (e) fit the data?

7. Using the model from (e), obtain 95% confidence intervals for the coefficient(s).

8. Is there evidence of outliers or high leverage observations in the model from (e)?

### 2.1.3 Exercise

In this exercise you will create some simulated data and will fit simple linear regression models to it. Make sure to use set.seed() prior to starting part (a) to ensure consistent results.

1. Using the rnorm() function, create a vector, x, containing 100 observations drawn from a $$N(0,1)$$ distribution. This represents a feature, X.

2. Using the rnorm() function, create a vector, eps, containing 100 observations drawn from a $$N(0,0.25)$$ distribution i.e. a normal distribution with mean zero and variance 0.25.

3. Using x and eps, generate a vector y according to the model $Y = -1 + 0.5X + \epsilon$ What is the length of the vector y? What are the values of $$\beta_0$$ and $$\beta_1$$ in this linear model?

4. Create a scatterplot displaying the relationship between x and y. Comment on what you observe.

5. Fit a least squares linear model to predict y using x. Comment on the model obtained. How do $$\hat{\beta}_0$$ and $$\hat{\beta}_1$$ compare to $$\beta_0$$ and $$\beta_1$$?

6. Display the least squares line on the scatterplot obtained in (d). Draw the population regression line on the plot, in a different color. Use the legend() command to create an appropriate legend.

7. Now fit a polynomial regression model that predicts $$y$$ using $$x$$ and $$x^2$$. Is there evidence that the quadratic term improves the model fit? Explain your answer.

8. Repeat (a)-(f) after modifying the data generation process in such a way that there is less noise in the data. The model should remain the same. You can do this by decreasing the variance of the normal distribution used to generate the error term $$\epsilon$$ in (b). Describe your results.

9. Repeat (a)-(f) after modifying the data generation process in such a way that there is more noise in the data. The model should remain the same. You can do this by increasing the variance of the normal distribution used to generate the error term $$\epsilon$$ in (b). Describe your results.

10. What are the confidence intervals for $$\beta_0$$ and $$\beta_1$$ based on the original dataset, the noisier dataset, and the less noisy dataset? Comment on your results.

### 2.1.4 Optional Exercise

1. Install the regsim package.
2. For any of the linear models estimated in previous exercises, use regsim to run simulations and plot confidence interval plots.