First of all, I had installed RStudio, this is the link for download:
https://leandro26.webnode.com/products/download-r-for-windows/
Once we have installed RStudio, load the script below:
--===============================================
--// R script
--===============================================
# Linear Regression predicts linear relationship between two variables
# Set path to Desktop
setwd("~/Desktop")
download.file(url = 'https://raw.githubusercontent.com/mGalarnyk/Python_Tutorials/master/Python_Basics/Linear_Regression/linear.csv'
, destfile = 'linear.csv')
rawData=read.csv("linear.csv", header=T)
# Show first n entries of data.frame, notice NA values
head(rawData, 10)
linModel <- lm(y~x, data = rawData)
# Show attributes of linModel
attributes(linModel)
# To show what happens with na.action, "omit" since data has NA
linModel$na.action
# Show coefficients of model
linModel$coefficients
# Predicting New Value based on our model
predict(linModel, data.frame(x = 3))
plot(y ~ x, data = rawData,
xlab = "This labels the x axis",
ylab = "This labels the y axis",
main = "Scatter Plot"
)
abline(linModel, col = "red", lwd = 3)
--=================================================
R is very simple to execute and work on it, you need to select the command or execute all.
On the line (but execute all the commands before as well):
plot(y ~ x, data = rawData,
xlab = "This labels the x axis",
ylab = "This labels the y axis",
main = "Scatter Plot"
)
You can see this output - Scatter Plot chart:
Also, you can include a trend line by this code:
abline(linModel, col = "red", lwd = 3)