-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISLR Ch3 q8.R
50 lines (33 loc) · 1.27 KB
/
ISLR Ch3 q8.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ISL - Ch3 Q8
LoadLibraries=function()
{
library(MASS)
library(ISLR)
library(car)
print("Libraries have been loaded")
}
LoadLibraries()
# a)
lm.fit = lm(mpg~horsepower, data=Auto)
summary(lm.fit)
# i) There is a relationship between predictor and response - suggested by p-value of <2e-16
summary(lm.fit)$r.sq
# ii) ~60% of variance is explained by horsepower alone, suggesting a strong relationship
# iii) value is -0.16, therefore negative
predict(lm.fit, data.frame(horsepower = c(98)), interval="confidence", level = 0.95)
predict(lm.fit, data.frame(horsepower = c(98)), interval="prediction", level = 0.95)
# iv) as calculated above, predicted mpg is 24.47, with
# 95% confidence interval of [23.97, 24.96]
# 95% prediction interval of [14.81, 34.12]
# b)
plot(Auto$horsepower, Auto$mpg)
abline(lm.fit, lwd=3, col="red")
# c)
par(mfrow=c(2,2))
plot(lm.fit)
# Residuals look suspisciously curved
# qqplot looks as if it has some deviation espeically in the tails
# spread-location - clear non-horizontal line
# hints at heteroscedasticity (unequal variance)
# Leverage - visually, no large values - all within cook's distance lines
# Summarily, suggests underlying relation is not really linear