2024-03-17

Topic: Simple Linear Regression

What is Simple Linear Regression?

  • Simple linear regression is used to find a straight line that fits a set of data points
  • Regression must include an independent variable and a dependent variable

Simple Linear Regression Prediction Line Equation

  • equation: \(\hat{y} = mx + b\)
  • Where:
  • \(\hat{y}\) represents the estimated/predicted value
  • \(m\) represents the regression slope estimate
  • \(x\) represents the value of x for the observation
  • \(b\) represents the y-intercept value

dataset flowers

The dataset I will be working with to illustrate simple linear regression has information regarding flowers

head(flowers)
##   treat nitrogen block height weight leafarea shootarea flowers
## 1   tip   medium     1    7.5   7.62     11.7      31.9       1
## 2   tip   medium     1   10.7  12.14     14.1      46.0      10
## 3   tip   medium     1   11.2  12.76      7.1      66.7      10
## 4   tip   medium     1   10.4   8.78     11.9      20.3       1
## 5   tip   medium     1   10.4  13.58     14.5      26.9       4
## 6   tip   medium     1    9.8  10.08     12.2      72.7       9

Visualizing The Data

  • In order to ensure the relationship between the two variables is roughly linear before proceeding
  • The two variables from the dataset I am looking at is weight and shoot area
x <- flowers$weight
y <- flowers$shootarea
ggplot(flowers, aes(x = x, y = y)) +
  geom_point() +  # Add points
  labs(x = "Weight of Flower", y = "Shoot Area", title = "Flower Weight vs. Shoot Area")

  • Based on the above plot, we can see that the data seems to be roughly linear

Creating Box Plot

  • The next step is to check if there are any outliers in the data
  • If there is an outlier, a tiny circle will appear in the boxplot
x <- flowers$weight
y <- flowers$shootarea
ggplot(flowers, aes(x = x, y = y)) +
  geom_boxplot() +  
  labs(title = "Boxplot")  # Add axis labels and title
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

  • There don’t appear to be any outliers

Peforming Simple Linear Regression

  • Now that we see that there are no outliers, and that the data seems roughly linear, we can perform linear regression
  • flower weight is the explanatory variable and leaf area is the response variable
model <- lm(flowers$shootarea~flowers$weight) #simple linear regression model
summary(model)
## 
## Call:
## lm(formula = flowers$shootarea ~ flowers$weight)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -78.352 -29.694  -3.447  28.143  68.884 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    -18.4346    12.2048  -1.510    0.134    
## flowers$weight   8.0807     0.9546   8.465 3.34e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 37.1 on 94 degrees of freedom
## Multiple R-squared:  0.4326, Adjusted R-squared:  0.4265 
## F-statistic: 71.66 on 1 and 94 DF,  p-value: 3.342e-13
  • Based on the summary we can see that the fitted regression equation is:
  • \(\hat{y} = 8.0807x - 18.4346\)

Plotting Data With Regression Equation

Here is a plot with the data and the regression equation line:

x <- flowers$weight
y <- flowers$shootarea

xax <- list(title = "Flower Weight",
            titlefont = list(family = "Modern Computer Roman"))

yax <- list(title = "Shoot Area",
            titlefont = list(family = "Modern Computer Roman"),
            range = c(0, 80))


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

config(fig, displaylogo = T)

Conclusion

  • As flower weight increases, Shoot Area increases
  • Simple linear regression is a powerful tool that produces a linear function that can accurately predict the relationship between two variables