Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Simple Linear Regression
- rm(list = ls())
- # Data Preprocessing Template
- # Importing the dataset
- dataset <- read.csv(file.path(getwd(),'Data/Salary_Data.csv'))
- # Splitting the dataset into the Training set and Test set
- # install.packages('caTools')
- 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)
- # Feature Scaling
- # No need to feature scale in Regression
- # training_set = scale(training_set)
- # test_set = scale(test_set)
- # Fittin Linear Regresion to the Training set
- regressor <- lm(formula = Salary ~ YearsExperience,
- data = training_set)
- # Predicting the Test set results
- y_pred <- predict(regressor, newdata = test_set) # we did not use this line!
- # Visualizing the Training set results
- 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 Experience (Training Set)") +
- xlab("Years of Experience") +
- ylab("Salary") +
- theme_bw() +
- theme(plot.title = element_text(hjust = 0.5))
- # the last line is used to center the title
- # if we change 0.5 to 0 or 1, then we go left or right of the plot
- # theme_bw() to change the theme of the plot
- # Visualizing the Test set results
- 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 Experience (Test Set)") +
- xlab("Years of Experience") +
- ylab("Salary") +
- theme_bw() +
- theme(plot.title = element_text(hjust = 0.5))
Add Comment
Please, Sign In to add comment