---
title: "Math 50 Fall 2017, Homework #6"
---
<hr>
__NOTE: For your homework download and use the template__ (https://math.dartmouth.edu/~m50f17/HW6.Rmd)

__Read the green comments in the rmd file to see where your answers should go.__


<br><hr><br>




#### Lets first look at the scatter plot for the windmill data, and visually check the straight line fit.

```{r}
windmill <- read.table("https://math.dartmouth.edu/~m50f17/windmill.csv", header=T)
plot(windmill$velocity, windmill$DC, xlab = "wind velocity", ylab = "DC current")
fit <- lm(DC~velocity, data = windmill)
abline(fit$coefficients, col="red")

```

#### The summary statistics are below, $R^2$ is about 0.87. The residual plot below suggests that the relation might be non-linear. When you look at the above scatter diagram one might think the straight line model seems OK, however the residual plot below amplifies the nonlinearity. Why?  Can we also see this by carefully looking at the scatter plot above? 

```{r}

summary(fit)
plot(fitted.values(fit), rstudent(fit), xlab = "y", ylab = "R-Student residuals", main = "Windmill - Residual Plot")
abline(c(0,0), col="red")
```


####  Also note that it looks like that there is a potential outlier and however this might change when we fix the model. It seems consistent with the rest (visually). Start with fitting a quadratic model. 


```{r}

fit2 <- lm(DC~poly(velocity, degree = 2), data = windmill)
summary(fit2)
plot(windmill$velocity, windmill$DC, xlab = "wind velocity", ylab = "DC current")
 
lines(sort(windmill$velocity), fitted(fit2)[order(windmill$velocity)], col='red') 

```

#### This seems to fix the curved nature of the data, however the application domain suggests to use a model of the form $$ y = \beta_0 + \beta_1 \frac{1}{x} + \varepsilon $$ Note that there doesn't seem a potential outlier in the new model.

```{r}

velRep = 1/windmill$velocity
DC <- windmill$DC

plot(velRep, windmill$DC, xlab = "1/velocity", ylab = "DC current")
fit3 <- lm(DC~velRep)
abline(fit3$coefficients, col="red")

  
summary(fit3)
plot(fitted.values(fit), rstudent(fit3), xlab = "fitted values", ylab = "Studentized residuals", main = "Residuals - reciprocal model")
abline(c(0,0), col="red")



```




<br><hr><br>
<!-- ############################################################################################ --> 
<!-- ################################# NEW QUESTION ############################################# --> 
<!-- ############################################################################################ --> 


## Question-1

Recall the phytoplankton population data is given at : 
https://math.dartmouth.edu/~m50f17/phytoplankton.csv

where headers are 

- pop : population of phytoplankton ($y$)
- subs2 : concentration of substance-2  ($x$)


(a)  
Plot the scatter diagram for pop ~ subs2. Do you think a straight line model is adequate?  Fit a straight line model and support your argument with summary statistics. 

(b)
Do you suggest to use Box-Cox method? If not explain, if so apply the method and demonstrate the improvement. 

(c) An analyst suggests to use the following model 
$$ y = \beta_0 + \beta_1 (x-4.5)^2 $$
Using transformations, fit a simple linear regression model. Plot the scatter diagram and fitted curve (Note: it is not a straight line in this case). Compare $MS_{res}$, $R^2$ and the R-student residual plots with the model in part a. 

(d)  Construct the probability plot for part (c). Is there a problem with the normality assumption? If so determine the problem (heavy tailed, light tailed, or something else) 

### Answer: 
<!-- If question requires a written answer it goes here. R code goes to the chunk below.) -->


```{r}


```



<br><hr><br>
<!-- ############################################################################################ --> 
<!-- ################################# NEW QUESTION ############################################# --> 
<!-- ############################################################################################ --> 


## Question-2 
Chapter 5, Problem 2 all parts. 


### Answer: 
<!-- If question requires a written answer it goes here. R code goes to the chunk below.) -->

```{r}

```



<br><hr><br>
<!-- ############################################################################################ --> 
<!-- ################################# NEW QUESTION ############################################# --> 
<!-- ############################################################################################ --> 


## Question-2 
Chapter 5, Problem 3 all parts. 
Note: In part (c) consider natural log of the ~~minutes~~ number of bacteria. 


### Answer: 
<!-- If question requires a written answer it goes here. R code goes to the chunk below.) -->

```{r}

```

