-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter_3.R
79 lines (64 loc) · 1.6 KB
/
Chapter_3.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Exercise 3.2.R1
1-pnorm(0.9, 0.5, 0.2) # for 0.9 because the p value is 0.0227
1-pnorm(0.8, 0.5, 0.2) # the answer is 0.8 because the p value is 0.0668 > 0.05
# Exercise 3.2.R2
# Exercise 3.3
#The more the pertubations the better to test the model
# Exercise 3.5R2
#0.029+0.0011*50
#taken from page 89
# Linear Regression in R
library(MASS)
library(ISLR)
names(Boston)
plot(medv~lstat,Boston)
fit1 <- lm(medv~lstat,data=Boston)
fit1
summary(fit1)
abline(fit1, col ="green")
names(fit1)
confint(fit1)
predict(fit1,data.frame(lstat=c(5,10,15)),interval="confidence")
# # Multiple linear regression
fit2 <- lm(medv~lstat+age,data=Boston)
summary(fit2)
fit3 <- lm(medv~.,Boston)
summary(fit)
par(mfrow=c(2,2))
plot(fit3)
fit4 <- update(fit3,~.-age-indus)
summary(fit4)
# # Nonlinear terms and Interactions
fit5 <- lm(medv~lstat*age,Boston)
summary(fit5)
fit6 <- lm(medv~lstat + I(lstat^2) ,Boston); summary(fit6)
attach(Boston)
par(mfrow=c(1,1))
plot(medv~lstat)
points(lstat,fitted(fit6),col="red",pch=20)
fit7 <- lm(medv~poly(lstat,4))
points(lstat,fitted(fit7),col="blue",pch=20)
plot(1:20,1:20,pch = 1:20,cex=2)
# Qualitative Predictors
fix(Carseats)
names(Carseats)
summary(Carseats)
fitl <- lm(Sales~.+Income:Advertising+Age:Price,Carseats)
summary(fit1)
contrasts(Carseats$ShelveLoc)
#Writing R functions
regplot = function(x,y){
fit <- lm(y~x)
plot(x,y)
abline(fit,col="red")
}
attach(Carseats)
regplot(Price,Sales)
#Writing R functions
regplot = function(x,y,...){
fit <- lm(y~x)
plot(x,y,...)
abline(fit,col="red")
}
attach(Carseats)
regplot(Price,Sales,xlab="Price",ylab="Sales",col="blue",pch=10)