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

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

#ILKER : 
# In part c Check  The regions you mentioned.



<br><hr><br>


#### An example from Regression Diagnostics: Identifying Influential Data and Sources of Collinearity (Belsley, Kuh and Welsch)

[,1]	sr	numeric	aggregate personal savings
[,2]	pop15	numeric	% of population under 15
[,3]	pop75	numeric	% of population over 75
[,4]	dpi	numeric	real per-capita disposable income
[,5]	ddpi	numeric	% growth rate of dpi


```{r}


data(LifeCycleSavings)
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
summary(inflm.SR <- influence.measures(lm.SR))
inflm.SR
which(apply(inflm.SR$is.inf, 1, any)) 
rstandard(lm.SR)
rstudent(lm.SR)

# dfbetas(lm.SR)
dffits(lm.SR)
covratio(lm.SR)


```

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


## Question-1 
Chapter 6, Problem 15. 

First check the following page from R project documentation (for various plots to visualize the influence measures): 

https://cran.r-project.org/web/packages/olsrr/vignettes/influence_measures.html

Note: You might need libraries such as olsrr for some of the plots below. 

(a) Plot :  Cook's D chart,  DFBETAs Panel, DFFITS Plot and Standardized Residual Chart that are shown in the above link. 

(b) Find the points with high leverage and Cook's distance. 

(c) Plot "Studentized Residuals vs Leverage Plot" that you see in the above link.  Which regions in this plot corresponds to leverage points,  pure leverage and influential regions. Detect the points in each region.

(d) What do you think are the most influential points? (You can use the stats shown above or plots in previous parts.)

(e) Comment about the normality assumption using probability plot. Remove the most influential points (that you suggested in part-d) and discuss the change/improvements on normality assumption (comparing probability plots).  



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

```{r}

library(olsrr)
library(MPV)
data(table.b14)
fitted=lm(y~x1+x2+x3+x4, data=table.b14)


cat ("Part a.")

# Cooks D 
ols_cooksd_chart(fitted)

#   DFBETAs Panel
ols_dfbetas_panel(fitted)

# DFFITS Plot  
ols_dffits_plot(fitted)
 
# Standardized Residuals Chart
ols_srsd_chart(fitted)


cat ("Part b.
     2,4,8 and 9 seems to have high Cook's D value. From the outlier and leverage diagnostics plot we see that point 4 has the highest leverage, and point 2 is the next one that can also be included as a leverage point since it seems just a little below the threshold. 2 and 4 are the two points with highest Cook's D.")

cat("Part c. 
    The points on the right side of the vertical line are  “leverage points” which denote the points that are at remote locations in x-space. 4 is in this region and 2 is very close to it.  The points with high leverage but consistent with the fitted model (thus low residual) are pure leverage points. This corresponds the middle section of the right side of the vertical line. The remaining upper and lower sections (on the right of the vertical line) are the regions for influential points. Observation 4 is in this region.  
There are two horizontal line let us denote as the upper and lower one. The regions above the upper horizontal line and below the lower horizontal line are outliers. 2,4,8 are in this region. 
")

ols_rsdlev_plot(fitted)



cat("Part d. 
    The points 2 and 4 are considered as leverage points and they are also outliers therefore we can conclude they are the influential points. It is possible to add more by considering different cut-off values. ")


cat("Part e.
    As we can see from the probability plot there is a violation of normality assumption. The plot improves slightly with the removal of outliers that we are studying.") 
rStuRes = rstudent(fitted)
qqnorm(rStuRes, datax = TRUE, main="Normal Probability Plot") 
qqline(rStuRes, datax = TRUE)

delTable=table.b14[-c(2,4),]
fitted=lm(y~x1+x2+x3+x4, data=delTable)
rStuRes = rstudent(fitted)
qqnorm(rStuRes, datax = TRUE, main="Normal Probability Plot (with 2,4 deleted)") 
qqline(rStuRes, datax = TRUE)


```






```
