2024-03-22

What is Linear Regression?

  • Linear regression is a way in which we can estimate the values of datapoints beyond as well as in the bounds of the dataset we train the model (in this case, the line) off of.
  • \[y(x) = y_i + (\frac{(y_{i+1}-y_i)(x-x_i)}{(x_{i+1}-x_i)})\]
  • above, where x_i….x_n are the x values and correspondingly for y
  • So let’s see the dataset we will be working on and then the implementation of linear regression.

Setup and Code

# including library and datasets
library(ggplot2)
data("Orange")

# defining the plot's attributes
pl <- ggplot(
  Orange,
  aes(x=age, y=circumference, color=factor(Tree))
)

pl <- pl + geom_point()
pl <- pl + ggtitle("Age vs Circumference of Oranges for Distinct Trees")
pl <- pl + xlab("Age of Oranges")
pl <- pl + ylab("Circumference of Oranges")

# displaying the plot
pl

The Plot

Linear Regression Code and Summary

## 
## Call:
## lm(formula = circumference ~ age, data = Orange)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -46.310 -14.946  -0.076  19.697  45.111 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 17.399650   8.622660   2.018   0.0518 .  
## age          0.106770   0.008277  12.900 1.93e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 23.74 on 33 degrees of freedom
## Multiple R-squared:  0.8345, Adjusted R-squared:  0.8295 
## F-statistic: 166.4 on 1 and 33 DF,  p-value: 1.931e-14

Linear Regression Plot

pl <- pl + geom_abline(intercept = 17.3996, slope = 0.106)