2025-04-14

Simple Linear Regression

Simple linear regression is a model which estimates the relationship between one dependent variable and one independent variable. \[ y = \alpha + \beta x \]

where \(\beta\) represents slope and \(\alpha\) represents y-intercept

Simple Linear Regression Formulas

If mean of x (\(\bar x\)) and mean of y (\(\bar y\)) are known at time of calculation where n is number of elements: \[\hat\beta\ = \frac{\sum_{i=1}^{n}(x_i - \bar x)(y_i - \bar y)} {\sum_{i=1}^{n}(x_i - \bar x)^2}\] \[\hat\alpha\ = \bar y - \hat\beta\ \bar x\]

Example 1 - Plotly with R dataset longley

Example 2 - Plotly with R dataset longley

Plotly Code

plot_ly(x=longley$Population, y=longley$GNP,
        type="scatter", mode="markers", 
        name="data") %>%
  add_lines(x=longley$Population, 
            y=fitted(lm(longley$GNP ~ longley$Population)),
            name="fitted") %>%
  layout(title="GNP by Population", 
         xaxis=list(title="Population"), 
         yaxis=list(title="GNP"))

plot_ly(x=longley$Population, y=longley$GNP.deflator, 
        type="scatter", mode="markers", 
        name="data") %>%
  add_lines(x=longley$Population, 
            y=fitted(lm(longley$GNP.deflator ~ longley$Population)),
            name="fitted") %>%
  layout(title="GNP Deflator by Population", 
         xaxis=list(title="Population"), yaxis=list(title="GNP Deflator"))

Example 3 - GGPlot with R dataset mtcars

Example 4 - GGPlot with R dataset mtcars

GGPlot Code

ggplot(data=mtcars, aes(x=wt,y=mpg)) + geom_point() + 
  geom_smooth(method="lm", se=FALSE) +
  ggtitle("Miles per Gallon by Weight of Car") + xlab("Weight") + ylab("MPG")
ggplot(data=mtcars, aes(x=hp,y=mpg)) + geom_point() + 
  geom_smooth(method="lm", se=FALSE) +
  ggtitle("Miles per Gallon by HP of Car") + xlab("HP") + ylab("MPG")