```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```



## Species diversity on the Galapagos Islands

```{r}
#install.packages("faraway")
library(faraway)
data(gala)
gala
galaNew <- gala[, -2]
galaNew
```


## Diagnostics in Multiple Linear Regression

### Leverage

```{r}
full <- lm(Species ~ ., data = galaNew)
step_gala <- step(full)

X <- model.matrix(step_gala)
H <- X %*% solve((t(X) %*% X)) %*% t(X)
lev <- hat(X)
high_lev <- which(lev >= 2 * 3 / 30)
attach(gala)

par(las = 1)
plot(Elevation, Adjacent,
     cex = sqrt(5 * lev),
     col = "blue", ylim = c(0, 5000))
points(Elevation[high_lev],
       Adjacent[high_lev], col = "red",
       pch = 16,
       cex = sqrt(5 *lev[high_lev]))
```

### Studentized Residuals

```{r}
gs <- summary(step_gala)
gs$sig

studRes <- gs$res / (gs$sig * sqrt(1 - lev))

par(las = 1)
plot(studRes, pch = 16,
     cex = 0.8, col = "blue",
     ylab = expression(r[i]), main = "Studentized Residuals", xlab = "")
abline(h = 0, lty = 2, col = "gray")

par(las = 1)
plot(step_gala$fitted.values, studRes, pch = 16,
     cex = 0.8, col = "blue",
     ylab = expression(r[i]), main = "Studentized Residuals", xlab = "")
abline(h = 0, lty = 2, col = "gray")
```


### Jackknife Residuals

```{r}
jack <- rstudent(step_gala)

par(las = 1)
plot(jack, pch = 16,
     cex = 0.8, col = "blue", main =" Jacknife Residuals ", xlab = "",
     ylab = "")
abline(h = 0, lty = 2, col = "gray")
```


### Identifying Influential Observations: DFFITS
```{r, message=F}
library(olsrr)
ols_plot_dffits(step_gala)
```


### Residual Plot

```{r}
par(las = 1)
plot(step_gala$fitted.values,
     step_gala$residuals, 
     pch = 16, cex = 0.8, col = "blue", main =" Residuals ",
     xlab = expression(hat(Y)), ylab = expression(e))
abline(h = 0, lty = 2, col = "gray")

sqrt_fit <- lm(sqrt(Species) ~ Elevation + Adjacent)

par(las = 1)
plot(sqrt_fit$fitted.values,
     sqrt_fit$residuals, 
     pch = 16, cex = 0.8, col = "blue", main =" Residuals ",
     xlab = expression(hat(Y)), ylab = expression(e))
abline(h = 0, lty = 2, col = "gray")
```

## Regression with Both Quantitative and Qualitative Predictors: Salaries for Professors Data Set

The 2008-09 nine-month academic salary for Assistant Professors, Associate Professors and Professors in a college in the U.S. The data were collected as part of the on-going effort of the college's administration to monitor salary differences between male and female faculty members.


```{r}
library(carData)
data("Salaries")
head(Salaries)
summary(Salaries)
boxplot(salary ~ sex, data = Salaries, las = 1)
boxplot(salary ~ discipline, data = Salaries, las = 1)
plot(Salaries$yrs.service, Salaries$salary, las = 1, pch = 16, cex = 0.75)
grid()

model <- lm(salary ~ discipline + rank + sex + yrs.service, data = Salaries)
X <- model.matrix(model)

attach(Salaries)
sex.col <- ifelse(sex == "Male", "blue", "red")
plot(yrs.service, salary, pch = 16, cex = 0.4,
     col = sex.col, las = 1)
grid()

m1 <- lm(salary ~ sex * yrs.since.phd)
summary(m1)
coeff <- m1$coefficients
abline(coeff[1], coeff[3], col = "red")
abline(coeff[1] + coeff[2], coeff[3] + coeff[4],
       col = "blue")
legend("toplef", legend = c("Female", "Male"),
       pch = 16, col = c("red", "blue"),
       bty = "n")
```

## Polynomial regression: Housing Values in Suburbs of Boston

```{r}
library(MASS)
data(Boston)

plot(Boston$lstat, Boston$medv, col = "gray", pch = 16,
     cex = 0.6, las = 1, xlab = "lower status of the population (percent)", ylab = "median value of owner-occupied homes")

m1 <- lm(medv ~ lstat, data = Boston)
abline(m1)

m2 <- lm(medv ~ lstat + I(lstat^2), data = Boston)
lines(sort(Boston$lstat), m2$fitted.values[order(Boston$lstat)], col = "red")

m3 <- lm(medv ~ lstat + I(lstat^2)+ I(lstat^3), data = Boston)
lines(sort(Boston$lstat), m3$fitted.values[order(Boston$lstat)], col = "blue")

m3new <- lm(medv ~ poly(lstat, 3), data = Boston)
summary(m3new)
```



