-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1.R
57 lines (37 loc) · 913 Bytes
/
problem1.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
## AMI22T Home Exercise 3, Problem 1
## Linear Regression Concepts
## Saumya Gupta, DS
# load required packages
library(ggplot2)
library(GGally)
# 2. ----
set.seed(1)
x1 = runif(100)
x2 = 0.5 * x1 + rnorm(100) / 10
y = 2 + 2 * x1 + 0.3 * x2 + rnorm(100)
# create a data frame for ease
df = data.frame(x1 = x1, x2 = x2, y = y)
# check randomness
plot(x1)
plot(x2)
plot(y)
# check distributions
ggplot(df, aes(x1)) + geom_density()
ggplot(df, aes(x2)) + geom_density()
ggplot(df, aes(y)) + geom_density()
# check correlations
ggcorr(df, nbreaks = 10)
cor(df)
# fit models
## Model a) ----
ols.fit.1 <- lm(y ~ x1,
data = df)
summary(ols.fit.1)
## Model b) ----
ols.fit.2 <- lm(y ~ x2,
data = df)
summary(ols.fit.2)
## Model c) ----
ols.fit.3 <- lm(y ~ x1 + x2,
data = df)
summary(ols.fit.3)