Last active
December 5, 2020 13:40
-
-
Save mGalarnyk/efe529945102dc538c7962ab14c508ac to your computer and use it in GitHub Desktop.
Univariate Linear Regression (relationship between the dependent variable y and the independent variable x is linear) using R programming Language for the blog post https://medium.com/@GalarnykMichael/univariate-linear-regression-using-r-programming-3db499bdd1e3#.kcm3t9rl3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So clear and concise!