-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathSimple Linear Regression.R
49 lines (36 loc) · 1.4 KB
/
Simple Linear Regression.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
# Simple Linear Regression
# Importing the dataset
dataset = read.csv('Salary_Data.csv')
# Splitting the dataset into the Training set and Test set
library(caTools)
set.seed(123)
split = sample.split(dataset$Salary, SplitRatio = 2/3)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)
# Fitting Simple Linear Regression to the Training Set (Part 2)
regressor = lm(formula = Salary ~ YearsExperience,
data=training_set)
summary(regressor)
# Predictiing the Test Set Results (Part 3)
y_pred = predict(regressor, newdata = test_set)
y_pred
# Visualization (Part 4)
library(ggplot2)
### Training Set
ggplot() +
geom_point(aes(x = training_set$YearsExperience, y = training_set$Salary),
color = 'green') +
geom_line(aes(x = training_set$YearsExperience, y = predict(regressor, newdata = training_set)),
color = 'darkred') +
ggtitle('Salary vs Experience (Training Set)') +
xlab('Years of Experience') +
ylab('Salary')
# Visualising the Test set results
ggplot() +
geom_point(aes(x = test_set$YearsExperience, y = test_set$Salary),
color = 'maroon') +
geom_line(aes(x = training_set$YearsExperience, y = predict(regressor, newdata = training_set)),
color = 'navy') +
ggtitle('Salary vs Experience (Test Set)') +
xlab('Years of Experience') +
ylab('Salary')