2024-10-31

What is Simple Linear Regression?

Simple linear regression is a technique used to calculate a “line of best fit” between two variables. An example of this could be finding if there is a relationship between the inches of rain and the number of car accidents.

Using linear regression will give a linear model of the relationship between the two variables, as well as a correlation coefficient to show how strongly the two variables are correlated. If the correlation is strong, the model can be used to predict what the value of a dependent variable will be, given a value for the independent variable.

The Formula of Simple Linear Regression Models

The formula of a linear regression is:

\(y = B_1x + B_0\)


This gives the equation of a line that best fits the data

  • \(B_1\) is the slope of the linear regression model
  • \(B_0\) is the intercept of the linear regression model
  • \(x\) is the given value of the independent variable
  • \(y\) is the predicted value of the dependent variable

Generating and Plotting Linear Regression in R

We will demonstrate a simple linear regression model on the included R data set Orange, which gives data about orange trees. We will try to find a relationship between the age of an orange tree and its circumference. For this example, we will be using plotly to construct and plot the model.

model = lm(Orange$circumference ~ Orange$age, data=Orange)
x = Orange$age
y = Orange$circumference
xax = list(title = "Age")
yax = list(title = "Circumference")
plot = plot_ly(x=x, y=y, type="scatter", mode="markers", 
               name="data", width=690, height=270) %>%
  add_lines(x = x, y = fitted(model), name="linear model") %>%
  layout(xaxis = xax, yaxis = yax)
config(plot)

Generating and Plotting Linear Regression in R

We can see in this plot that there seems to be a positive correlation in our data scatter plot. The plotted line is the linear model from out linear regression. The model of the linear regression also seems to indicate that there is a positive correlation between the age of an orange tree and its circumference, as it is increasing and seems to fit our data relatively well.

The Linear Model

## 
## Call:
## lm(formula = Orange$circumference ~ Orange$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 .  
## Orange$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

The Linear Model

The previous slide shows that the \(B_1\) value was found to be around 0.11 and the \(B_0\) value was around 17.4. This means that the equation of our linear model is:
\(y=0.11x+17.4\)


You can also see that the correlation coefficient, \(r^2\), was about 0.83. The closer this value is to 1, the better the model fits the data, meaning that the correlation is stronger. In this case, 0.83 shows there is a decent positive correlation between the age and circumference of orange trees.

Predictions Using the Model

Since our model is a good fit, we can use it to predict what an orange tree’s circumference is at 900 years old, since we don’t have data at that age.

When Not to Use Linear Regression

Next is an example of data that would be a bad choice for a linear regression model. This is a relationship between temperature and wind from R’s airquality data set. Here, there appears to be no correlation. If a linear model is constructed, it will have a very low \(r^2\) value, meaning it is a bad fit for the data.

Example of Weak Correlation

Possible Problems with Simple Linear Regression

  • Linear models may not fit some relationships well. Certain relationships may be better fit through a polynomial equation or another type of function.
  • Extrapolation can often lead to inaccurate results. It is best to use the model within the set of values that is used to construct it.