2024-03-12

{=html}

Background

What is Simple Linear regression?

  • Simple linear regression aims to find a linear relationship to describe the correlation between an independent and possibly dependent variable.

  • The regression line can be used to predict or estimate missing values, this is known as interpolation.

(Reference: Newcastle University)

Plotly Plot Code

# Plotly is a graphical package that we can use in R 
# we use the function lm (linear model). As we saw, mod is a list about various 
# information for the model. 
mod = lm(mpg ~ wt, data=mtcars)
x = mtcars$wt; y = mtcars$mpg
xax <- list(
  title = "Weight", titlefont = list(family="Modern Computer Roman"))
yax <- list(
  title = "Miles Per Gallon", titlefont = list(family="Modern Computer Roman"),
  range = c(0,40))

# The first 2 inputs are the wt column and the mpg column.
# type = "scatter" means that our plot will be scatter plot. 
# mode = "markers means that we only plot the points.
# The name of the first trace (we have two traces in this example) is data.
# We can also control the width and the height.
# Then we use the pipe operator %>%.
# After that, we add lines by using the command add lines. 
# We name the second trace "fitted".
# Furthermore,  we use the command layout and we add names to x and y axis. 
# Finally, we specify the margins. l stands for left, r stands for right, 
# b for bottom and t for top.

fig <- plot_ly(x=x, y=y, type="scatter", mode="markers", name="data", 
               width=690, height=300) %>%
  add_lines(x = x, y = fitted(mod), name="fitted") %>%
  layout(xaxis = xax, yaxis = yax) %>%
  layout(margin=list(l=150,r=50,b=20,t=40))

Plotly Plot Output

  • Dataset: mtcars
  • X-axis: Weight
  • Y-axis: Miles Per Gallon
  • 3D Interaction

ggplot 1 code

  • This one uses three variables
    • x axis: Weight
    • y axis: Miles per Gallon
    • Color: Cylinder count (6, 4, 8)
# Apart from weight and miles per gallon represented on the x and y axes, 
# we represent the categorical variable cycle by color.

hw3gg <- ggplot(data=mtcars, aes(x=wt,y=mpg,color=cyl)) +
  geom_smooth(method="lm", se=T, color="blue") + #we include the regression line
  geom_point(alpha=1) + coord_cartesian(ylim=c(0,50))

# se (standard error) is true which means the gray area appears in our plot.
# alpha controls the thickness of the points. 

ggplot 1 output

## `geom_smooth()` using formula = 'y ~ x'

ggplot 2 code

  • This one uses four variables
    • x axis: Weight
    • y axis: Miles per Gallon
    • Color: Cylinder count (6, 4, 8)
    • Size: Displacement
# Everything is the same as ggplot 1 code except we add size to aes based on the displacement. 
hw3gg2 <- ggplot(mtcars, aes(x=wt,y=mpg,color=cyl, size=disp)) + geom_point(alpha=1) +
  geom_smooth(method="lm", se=T, color="blue") 

ggplot 2 output

## `geom_smooth()` using formula = 'y ~ x'

Relevant statistics for linear regression

  • x axis: Weight
  • y axis: Miles per Gallon
coeff <- lm(mpg~ wt, data=mtcars)
summary(coeff)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

Linear Regression Equation + Coefficients of the model

  • Linear regression model: \(\hat{y}\) = \({\hat{\beta}}_0\) + \({\hat{\beta}}_1x\)

    • \(\hat{y}\) = dependent variable (Miles per gallon)
    • \({\hat{\beta}}_0\) = y-intercept
    • \({\hat{\beta}}_1\) = slope
    • \(x\) = independent variable (Weight)
  • R-squared (coefficient of determination): The proportion of the sample variation in y that can be explained by x.

Interpreting Linear Regression Equation + Coefficients

  • Linear regression model for our example:

\(mpg\) = \(37.2851\) + \(-5.3445\)(wt)

  • Interpretation:
    • y-intercept: 37.2851 is what we should expect the we should expect the miles per gallon to be when the weight (wt) is 0.
    • Slope: For every 1 unit increase in weight, the miles per gallon should decrease by 5.3445.
    • R-square: Approximately 75.28% of the sample variation in the miles per gallon can be explained by weight (wt).