-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathModule-4-Example-6.R
More file actions
40 lines (22 loc) · 818 Bytes
/
Module-4-Example-6.R
File metadata and controls
40 lines (22 loc) · 818 Bytes
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
# Matrix Operations in R https://www.statmethods.net/advstats/matrix.html
# Some good example https://datascienceplus.com/linear-regression-from-scratch-in-r/
data <- read.csv("Datasets/CEO_salary.csv")
salary1 <- data$salary/1000
y <- salary1
X <- as.matrix(cbind(data$age, data$height))
# vector of ones with same length as rows in y
int <- rep(1, length(y))
# Add intercept column to X
X <- cbind(int, X)
# Implement closed-form solution
betas <- solve(t(X) %*% X) %*% t(X) %*% y
# Round for easier viewing
betas <- round(betas, 2)
print(betas)
# Linear regression model
lm.mod <- lm(salary1~ data$age + data$height)
# Round for easier viewing
lm.betas <- round(lm.mod$coefficients, 2)
# Create data.frame of results
results <- data.frame(our.results=betas, lm.results=lm.betas)
print(results)