2024-02-02

Simple Linear Regression

Linear Regression is a useful tool for data analysis and for making predictions on the data. It does this by finding the line of best fit between the independent and dependent variables. A simple linear regression does this for only one independent variable corresponding to one dependent variable. We can create a linear model with the lm() function in r. The formula for the linear model is expressed as y ~ x.

data = Orange #Growth of Orange Trees Dataset
linear_model = lm(circumference ~ age, data= data)

Plotting our model in plotly

How to Evaluate Our Model

The linear model created holds an abundance of data beyond just the line created by our linear model. summary() is a workhorse of a function that, when passed our linear model, gives us an abundance of information on how our linear model fits the data.

summary(linear_model)

Output of summary(linear_model)

## 
## Call:
## lm(formula = circumference ~ age, data = data)
## 
## 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

Residuals:

Residuals are the difference between the data’s y value and the y value of our line. The summary of lm() gives us descriptive statistics about the residuals of our model.

Coefficients:

This section gives the weights that minimize the sum of the square of errors. Notably, The coefficients section provides the p values for each weight as well as the Std. Error(Residual Standard Error) and t-values which are used to calculate the p-value. Ideally we want the p-value of the weight to be statistically significant.

Residual Standard Error:

Residual Standard Error is a measure of how well our model fits the data. It is measured by the following formula:

\(\text{RSE} = \sqrt{\frac{\sum_{i=1}^n (y_i - \hat{y}_i)^2}{n - 2}}\)

*formula obtained from source 3

Multiple R-Squared and Adjusted R-Squared:

Multiple R-Squared is a measure of the correlation between our independent and dependent variable. Naturally, higher numbers of R mean a stronger correlation. R-squared is calculated with the formula: \(R^2 = 1 - \frac{\sum(y-\hat{y})}{\sum(y-\bar{y})}\)

*formula obtained from source 4

F-statistic and p-value:

The last line of the summary function gives us two values that measure the statistical significance of R-squared. The higher the F-statistic the better we can say our model is while a lower p-value means higher statistical significance.

Pitfalls of Linear Regression:

As powerful as they are, Simple Linear Regressions have their limitations, namely, they can only model linear functions. If your data is of a non linear shape, it is unlikely that a line of best fit will be able to make accurate predictions on the data. Always explore your data.

Sources: