---
title: "STAT 2450 Assignment 8, Winter 2020 - due Monday, April 6, by 2 P.M."
author: "Your name here"
date: 'Banner:  B00??????'
output:
  html_document: default
  pdf_document: default
  word_document: default
---


0. Evaluation

Question 1: 4 points

Question 2: 10 points

Quesion 3: 5 points

Question 4: 17 points

Question 5: 6 points

Question 6: 18 points

Total will be normalized to 50 points. 


1.  ISLR, chapter 5, problem 1.

Use the fact that

$$Var(aX+(1-a)Y)=a^2 V(X) + 2a(1-a)Cov(X,Y) + (1-a)^2 V(Y)$$

2. ISLR, chapter 5, problem 2.

   DO ALL PARTS.  For part (g), base your comments on the following
   figure.

```{r}
   n=c(1:100,100*c(1:10),500*(1:100))
   Pn=1-(1-1/n)^n
   plot(n,Pn,xlab="n",ylab="P_n",main="P(j'th observation IS IN bootstrap sample)",log="x",ylim=c(0,1))
   abline(1-1/exp(1),0)
```

3. ISLR, chapter 5, problem 3.

4. ISLR, chapter 5, problem 8. (Do all parts.)

(a)  use the following code to start.

```{r}
set.seed(1)
y=rnorm(100)
x=rnorm(100)
y=x-2*x^2+rnorm(100)
x2=x^2
x3=x^3
x4=x^4
data=data.frame(y=y,x=x,x2=x2,x3=x3,x4=x4)
```

The $p$ that is being asked for is the $p$ for "y=x-2*x^2+rnorm(100)".





(f) Asks about the statistical significance of the coefficient estimates
that result from fitting each of the models in part (c).  This is asking
about the p-values for the estimated coefficients in the "summary" output.For example, for the simple linear regression in part (c i):

```{r}
yi=lm(y~x,data=data)
syi=summary(yi)
print(syi)
coefficients(syi)[2,4]

```
This tells us that when testing $H_0:\beta_1=0$ against $H_A:\beta_1 \ne 0$,
in the linear regression model, we don't reject $H_0$.

For the quadratic model $=\beta_0 + \beta_1 X + \beta_2 X^2 + \epsilon$,


```{r}
yi=lm(y~x+x2,data=data)
syi=summary(yi)
print(syi)
pvalues=coefficients(syi)[,4]

```

In this case the p-values  associated with "x" and "x2", being less than, say, .05,  mean that we will reject the hypotheses $H_0: \beta_1=0$, 
and $H_0:\beta_2=0$.

5. ISLR, chaper 8, problem 5.  

6. ISLR, chapter 8, problem 8. DO ALL PARTS. 

For part (a), use the following split into training and test sets.

```{r}
set.seed(77191)
library(ISLR)
library(randomForest)
attach(Carseats)
n=nrow(Carseats)
indices=sample(1:n,n/2,replace=F)
cstrain=Carseats[indices,]
cstest=Carseats[-indices,]
```


