2024-03-19

What is Simple Linear Regression?

In statistics, linear regression is used to predict unknown values by using the values that are known to make that prediction. Businesses can use linear regression to make future decisions. It takes raw data and uses it instead of relying on intuition.

Example Graph of Linear Regression

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

Equations of Linear Regression

\[ \hat{y} = \beta_{0} + \beta_{1}x_{1} + \beta_{2}x_{2} + \dots + \beta_{n}x_{n} \] - \(\hat{y}\) is the predicted value of the dependent variable.

  • \(\beta_0\) is the intercept.

  • \(\beta_1,\beta_2,...\beta_n\) are the coefficients to \(x_1, x_2,...x_n\) respectively.

Creating a Scatter Plot

sd <- data.frame(
  x <- -10:10, ## Creating Sample Data
  y <- x^2 + 3 + rnorm(21, sd = 3))
scatter <- plot_ly(sd, x = x, y = y, type = "scatter", mode = "marker", 
              marker = list(size = 5, color = "red"))
trace <- lm(y~x + I(x^2))
y_pred <- predict(trace)
combined_plot <- scatter %>% add_trace(x = x, y = y_pred, 
                                       type = "scatter", mode = "lines")

A Scatter Plot

## A marker object has been specified, but markers is not in the mode
## Adding markers to the mode...
## A marker object has been specified, but markers is not in the mode
## Adding markers to the mode...

The Equation of the Line Regression

\[ \hat{y} = 1.09x^2 + 0.131x + 2.34 \] - Using Desmos, finding the the line of best fit for the scatter plot was solved

Using a Data Set Example

data(trees)
plotTrees <- ggplot(trees, aes(x = Girth, y = Volume)) +
  geom_point(col = "green") + geom_smooth(method = "lm", se = FALSE, 
                                          col = "brown") + 
  labs(title = "Volume by Girth", x = "Girth", y = "Volume")

Girth by Volume

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