2024-03-17

Defining Simple Linear Regression

Simple linear regression is a model that attempts to display the correlation between a dependent and independent variable as a non-vertical straight line using the following formula:

\(y = \alpha + \beta x\)


\(\beta\) = slope

\(\alpha\) = y-intercept

\(y\) = y-coordinate

\(x\) = x-coordinate

Dataset trees

We will use the trees data set to visualize the correlation between variables Height and Volume. According to the formula from the previous slide:

\(x = Height\)

\(y = Volume\)

While the values for slope (\(\beta\)) and y-intercept (\(\alpha\)) will be calculated using code on a later slide.

##   Girth Height Volume
## 1   8.3     70   10.3
## 2   8.6     65   10.3
## 3   8.8     63   10.2
## 4  10.5     72   16.4
## 5  10.7     81   18.8

Plotly plot Step 1

The first visualization of the correlation between tree height and tree volume will be represented as a plotly scattter plot.

The first step is to fit a linear regression model to the data using the following code:

mod <- lm(Volume ~ Height, data = trees)
summary(mod)
## 
## Call:
## lm(formula = Volume ~ Height, data = trees)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -21.274  -9.894  -2.894  12.068  29.852 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -87.1236    29.2731  -2.976 0.005835 ** 
## Height        1.5433     0.3839   4.021 0.000378 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.4 on 29 degrees of freedom
## Multiple R-squared:  0.3579, Adjusted R-squared:  0.3358 
## F-statistic: 16.16 on 1 and 29 DF,  p-value: 0.0003784

Plotly plot Step 2

Next, we must create a scatterplot of the tree data points and add the regression line to the plot using the following code:

x = trees$Height
y = trees$Volume

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

yax <- list(title = "Volume", 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 = predict(mod), mode = "lines", name = "Regression Line") %>%
    layout(xaxis = xax, yaxis = yax) %>%
    layout(margin = list(l = 150, r = 50, b = 20, t = 40))

Plotly plot Displayed

Displaying the code for the scatterplot from the following slide results in the model below:

Ggplot

The simple linear regression model is not limited to just plotly plots. The correlation between tree Height and Volume can be displayed as a ggplot as well using the following code:

fig2 <- ggplot(aes(x = Height, y = Volume), data = trees) + geom_point() +
    geom_smooth(method = "lm", se = FALSE)

Ggplot Displayed

The code from the previous slide displays the following figure:

Analysis

The simple linear regression models that we created show the evidence of a loose positive correlation between the Height and Volume variables of the ‘trees’ data set. The model shows that Volume increases as Height increases.

Further Exploration

Since we have determined that height and volume appear to have a positive correlation, we can explore the data further by visualizing the correlation between other variables in the ‘trees’ data set. Using the following code, we can analyze the correlation between tree Girth and tree Volume.

fig3 <- ggplot(aes(x = Girth, y = Volume), data = trees) + geom_point() +
    geom_smooth(method = "lm", se = FALSE)

Ggplot 2

The simple linear regression model displays evidence of a strong positive correlation between tree Girth and Volume.

End of Presentation








Thank You