forked from bquast/Regression-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz2.R
43 lines (36 loc) · 775 Bytes
/
Quiz2.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
# Regression Models
# Coursera
# Quiz 2
# Bastiaan Quast
# Question 1
x <- c(0.61, 0.93, 0.83, 0.35, 0.54, 0.16, 0.91, 0.62, 0.62)
y <- c(0.67, 0.84, 0.6, 0.18, 0.85, 0.47, 1.1, 0.65, 0.36)
fit <- lm(y~x)
summary(fit)
# Question 2
e <- resid(fit)
sqe <- e*e
res.var <- sum(sqe) / (length(e) - 2)
sqrt(res.var)
# Question 3
data(mtcars)
attach(mtcars)
fit <- lm(mpg ~ wt, mtcars)
summary(fit)
exp <- fit$coefficients[1] + mean(wt) * fit$coefficients[2]
exp - 2 * 0.5591
# Question 4
?mtcars
# Question 5
summary(fit)
fit[[1]][1] + 3 * fit[[1]][2]
# Question 6
summary(fit)
2 * (fit$coefficients[2] - 2 * 0.5591)
# Question 9
attributes(fit)
w.c <- fit$residuals ^ 2
fit.c <- lm(mpg ~ 1, mtcars)
fit.c.res <- fit.c$residuals ^ 2
sum(fit.c.res)
sum(w.c) /sum(fit.c.res)