-
Notifications
You must be signed in to change notification settings - Fork 1
/
Homwork Excercise week 3.R
50 lines (35 loc) · 1.39 KB
/
Homwork Excercise week 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
#Statistics One, Week 3 Exercise R
#Multiple regression analysis
#load library
library(psych)
#read data into data frame called "endur"
endur <- read.table("supplemental_STATS1.EX.04.txt", header=T)
#examine scatterplot
plot(endur$endurance ~ endur$age, main = "Scatterplot", ylab = "Endurance", xlab = "Age")
abline(lm(endur$endurance ~ endur$age), col-"blue")
#Simple regression
model1 = lm(endur$endurance ~ endur$age)
print(summary(model1))
model2 = lm(endur$endurance ~ endur$activeyears)
print(summary(model2))
#Multiple regression
model3 = lm(endur$endurance ~ endur$age + endur$activeyears)
print(summary(model3))
#Check the homoscedasticity assumption
#plot a residual versus predicted value
model3.res = resid(model3)
plot(endur$endurance, model3.res, ylab="Residuals", xlab="Endurance", main="Residual analysis")
plot(endur$age, model3.res, ylab="Residuals", xlab="Age", main="Residual analysis")
#Statistics One, Week 3 exercise
#Multiple regression analysis with standardized regression coefficients
model1.z = lm(scale(endur$endurance) ~ scale(endur$age))
print(summary(model1.z))
model2.z = lm(scale(endur$endurance) ~ scale(endur$activeyears))
print(summary(model2.z))
model3.z = lm(scale(endur$endurance) ~ scale(endur$age) + scale(endur$activeyears))
print(summary(model3.z))
#model comparisons
comp1 = anova(model1.z, model3.z)
print(comp1)
comp2 = anova(model2.z, model3.z)
print(comp2)