-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14. simple_linear_regression.R
38 lines (33 loc) · 1.12 KB
/
14. 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
#Importing dataset
dataset <- read.csv("datasets/Salary_Data.csv")
library(caTools)
set.seed(123)
split = sample.split(dataset$Salary, SplitRatio = 2/3)
training_set = dataset[split,]
test_set = dataset[!split,]
#Fitting simple linear regression
regressor = lm(formula = Salary ~ YearsExperience,
data = training_set)
summary(regressor)
#Prediction
y_pred = predict(regressor, newdata = test_set)
#Visualizing
library(ggplot2)
p <- ggplot() +
geom_point(aes(x=training_set$YearsExperience, y=training_set$Salary),
color = 'red') +
geom_line(aes(x=training_set$YearsExperience, y=predict(regressor, newdata = training_set)),
color = 'blue') +
ggtitle('Salary vs Experience (Training Set)') +
xlab('Years of experience') +
ylab('Salary')
p
q <- ggplot() +
geom_point(aes(x=test_set$YearsExperience, y=test_set$Salary),
color = 'red') +
geom_line(aes(x=training_set$YearsExperience, y=predict(regressor, newdata = training_set)),
color = 'blue') +
ggtitle('Salary vs Experience (Test Set)') +
xlab('Years of experience') +
ylab('Salary')
q