Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active December 5, 2020 13:40
Show Gist options
  • Save mGalarnyk/efe529945102dc538c7962ab14c508ac to your computer and use it in GitHub Desktop.
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
# 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)
@mas-dse-jejarret
Copy link

So clear and concise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment