2024-06-04

Simple Linear Regressions

Definition


Linear regressions are used to fit data that can be placed on a scatter plot and create a line that “best fits” the data.

Simple Linear Regression Equation

Linear Regression


Equation: \(\text{Y} = \beta_0 +\beta_1\cdot \text{X} + \varepsilon\)

Linear Regression with cars dataset

We will now illustrate an example of linear regression.
Listed below is some of the data set we will utilize for our linear regression.

  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10

Showing the Linear model

Here is the breakdown of our linear model

Call:
lm(formula = speed ~ dist, data = cars)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.5293 -2.1550  0.3615  2.4377  6.4179 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  8.28391    0.87438   9.474 1.44e-12 ***
dist         0.16557    0.01749   9.464 1.49e-12 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.156 on 48 degrees of freedom
Multiple R-squared:  0.6511,    Adjusted R-squared:  0.6438 
F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12

Scatter plot of cars

Here we illustrate the scatter plot of the speed and distance of the dataset.

ggplot(cars, aes(speed, dist)) + geom_point()

Scatter plot of cars with linear regression

Here we have the same scatter plot with the best fit line plotted with it.

ggplot(cars, aes(speed, dist)) + geom_point() + geom_smooth(method=lm)

Equation of previous linear regression


Equation: \(\text{dist} = \beta_0 +\beta_1\cdot \text{speed} + \varepsilon\) = 0

Code for CO2 dataset


Equation: \(\text{uptake} = \beta_0 +\beta_1\cdot \text{conc} + \varepsilon\) = 0

data("CO2")
comod = lm(conc ~ uptake, data=CO2)
X = CO2$uptake
Y= CO2$conc
post = plot_ly(x=X, y=Y, type="scatter", mode="markers", 
        name="data", width=700, height= 400) %>%
        add_lines(x=X, y=fitted(comod), name="bestfit") %>%
        layout(margin=list(
                          l=175,
                          r=60,
                          b=30,
                          t=40
        )
        )

Scatter plot of CO2 dataset with best fit line

post