-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplelinearRegression.R
More file actions
45 lines (37 loc) · 1.25 KB
/
SimplelinearRegression.R
File metadata and controls
45 lines (37 loc) · 1.25 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
#Simple Linear Regresseion
Salary = read.csv(file.choose())
#Splitting the data into training and test sets
library(caTools)
set.seed(123)
split = sample.split(Salary$Salary,SplitRatio = 2/3)
training_set = subset(Salary, split==T)
test_set = subset(Salary, split==F)
#Fitting simple linear regression to the Training Set
regressor = lm(formula = Salary ~ YearsExperience,
data = training_set)
#predecting the test set results
Y_pred = predict(regressor,newdata = test_set)
Y_pred
?predict
#Visualising Traing set
library(ggplot2)
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")+
geom_smooth()
#Visualising Test set
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")