-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathLinearRegression
More file actions
30 lines (25 loc) · 743 Bytes
/
LinearRegression
File metadata and controls
30 lines (25 loc) · 743 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
# Read Data
vehicle <- read.csv("https://raw.githubusercontent.com/bkrai/Statistical-Modeling-and-Graphs-with-R/main/vehicle.csv", header = TRUE)
# Data Review and Data Preparation
vehicle$lh[vehicle$lh==0] <- mean(vehicle$lh)
vehicle$lc[vehicle$lc==0] <- mean(vehicle$lc)
# Data Partition
set.seed(1234)
ind <- sample(2, nrow(vehicle),
replace = TRUE,
prob = c(0.7, 0.3))
training <- vehicle[ind==1,]
testing <- vehicle[ind==2,]
# Multiple Linear Regression
model <- lm(lc~lh, data=training)
model
summary(model)
plot(lc~lh, training)
abline(model, col = "blue")
# Model Diagnostics
par(mfrow=c(2,2))
plot(model)
vehicle[1620,]
# Prediction
pred <- predict(model, testing)
predict(model, data.frame(lh=10))