-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMy_Simple_Linear_Regression_In_R.R
69 lines (36 loc) · 1.42 KB
/
My_Simple_Linear_Regression_In_R.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Linear Regression
# Import the dataset
dataset = read.csv('Salary_Data.csv')
#spliting the training set and the 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)
# spliting simple linear regression to the training set
regressor = lm(formula = Salary ~ YearsExperience, data = training_set)
summary(regressor)
#Prediction the test set result
y_predict = predict(regressor, newdata = test_set)
#visualising the training set
install.packages('ggplot2')
library(ggplot2)
ggplot() +
geom_point(aes(x=training_set$YearsExperience, y=training_set$Salary),
colour = 'red') +
geom_line(aes(x=training_set$YearsExperience, y=predict(regressor, newdata = training_set)),
colour = 'blue') +
ggtitle('Salary vs Yeares of Experience (training set)') +
xlab('Experience') +
ylab('Salary')
#visualising the test set
install.packages('ggplot2')
library(ggplot2)
ggplot() +
geom_point(aes(x=test_set$YearsExperience, y=test_set$Salary),
colour = 'red') +
geom_line(aes(x=training_set$YearsExperience, y=predict(regressor, newdata = training_set)),
colour = 'blue') +
ggtitle('Salary vs Yeares of Experience (training set)') +
xlab('Experience') +
ylab('Salary')