-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathMultiple Linear Regression Lecture and HW.R
More file actions
64 lines (41 loc) · 1.63 KB
/
Multiple Linear Regression Lecture and HW.R
File metadata and controls
64 lines (41 loc) · 1.63 KB
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
58
59
60
61
62
63
64
# Multiple Linear Regression
setwd('./Machine Learning A-Z/Part 2 - Regression/Section 5 - Multiple Linear Regression')
# Importing the dataset
dataset = read.csv('50_Startups.csv')
# dataset = dataset[, 2:3]
# Encoding categorical data
dataset$State = factor(dataset$State,
levels = c('New York', 'California', 'Florida'),
labels = c(1, 2, 3))
dataset
# Splitting the dataset into the Training set and Test set
# install.packages('caTools')
library(caTools)
set.seed(123)
split = sample.split(dataset$Profit, SplitRatio = 0.8)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)
## Part 2
# Feature Scaling
# training_set[, 2:3] = scale(training_set[, 2:3])
# test_set[, 2:3] = scale(test_set[, 2:3])
# Fitting Multiple Linear Regression Models
regressor = lm(formula = Profit ~ ., data = training_set)
summary(regressor)
## Part 3 - Predicting the Test Set Results
y_pred = predict(regressor, newdata = test_set)
y_pred
## Building the Optimal Model Using Backward Elimination
regressor = lm(formula = Profit ~ R.D.Spend + Administration +
Marketing.Spend + State, data = dataset)
summary(regressor)
#### Iteration 1
regressor = lm(formula = Profit ~ R.D.Spend + Administration +
Marketing.Spend, data = dataset)
summary(regressor)
#### Iteration 2 (Solutions)
regressor = lm(formula = Profit ~ R.D.Spend + Marketing.Spend, data = dataset)
summary(regressor)
#### Iteration 3
regressor = lm(formula = Profit ~ R.D.Spend, data = dataset)
summary(regressor)