2025-11-09

Introduction

These slides demonstrates simple linear regression using the built in trees data set in R.
I will analyze how a tree, its circumference in inches, relates to its volume in cubic feet.
The slides and graphs used to visualize, model, and interpret this linear relationship using ggplot2 and plotly. The model we’ll explore has the form: \[ \text{Volume}=\beta_0+\beta_1\cdot\text{Girth}+\varepsilon,\quad\mathcal{N}(0,\sigma^2) \]

My plan is to estimate \(\beta_0\) and\(\beta_1\) using least squares: \[ S(\beta_0,\beta_1)=\sum_{i=1}^n(\text{Volume}-\beta_0-\beta_1\text{Girth})^2 \]

Each observation should represents a tree, with its Girth, Height, and Volume, with a focus on Girth and Volume for estimation.

## Warning: package 'ggplot2' was built under R version 4.5.2
## Warning: package 'plotly' was built under R version 4.5.2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
##   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
## 6  10.8     83   19.7
##      Girth           Height       Volume     
##  Min.   : 8.30   Min.   :63   Min.   :10.20  
##  1st Qu.:11.05   1st Qu.:72   1st Qu.:19.40  
##  Median :12.90   Median :76   Median :24.20  
##  Mean   :13.25   Mean   :76   Mean   :30.17  
##  3rd Qu.:15.25   3rd Qu.:80   3rd Qu.:37.30  
##  Max.   :20.60   Max.   :87   Max.   :77.00

Scatterplot with Fitted Line (ggplot2)

The plot shows a strong upward trend the larger girths generally correspond to larger tree volumes

Residual Behavior(ggplot2)

The points are randomly scattered around zero, suggesting a good linear fit.

Interactive Plot(Plotly)

Hover over any point to see its exact values an easier way to explore the mode and data.

Estimating Model Coefficients

These coefficients define the fitted regression line

Estimated Coeffecients for Trees Data
Parameter Estimate
Intercept (b0) -36.943
Slope (b1) 5.066

Intervals for parameters

The coefficients define the fitted regression line.
i WILL estimate the intercept \(\beta_0\) and slope \(\beta_1\) of the model

\[ \widehat{y}_i=\widehat{\beta}_0+\widehat{\beta}_1 x_i \]

using the least squares formulas:

\[ \widehat{\beta}_1=\frac{\sum_{i=1}^{n}(x_i- \bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad \widehat{\beta}_0=\bar{y}-\widehat{\beta}_1\bar{x} \]

Intervals for parameters

Both intervals are narrow, showing a good relationship in the estimated linear parameters.

90% Confidence Intervals
Term Estimate Lower90 Upper90
Intercept (b0) -36.943 -42.661 -31.226
Slope (b1) 5.066 4.646 5.486

R Code Example: Creating the Prediction Plot

Below is the R code I used to generate the 90% prediction interval plot shown on the next slide(formatted to fit)

new<- data.frame(Girth=c(8, 12, 16, 20))
pred<- predict(mod, newdata=new, interval="prediction",level=0.9)
pred_tbl<- cbind(new, round(pred, 2))
#kable(pred_tbl, caption = paste0("Predicted Tree Volumes"))
ggplot(pred_tbl,aes(x= Girth, y = fit))+
  geom_ribbon(aes(ymin = lwr,ymax = upr),
              alpha = 0.2, fill= "darkgreen")+
  geom_line(color = "darkgreen")+geom_point(size =2,color= "brown")+
  labs(title="Predicted Volume vs.Girth(90%)",x="Girth (inches)",
       y="Predicted Volume (ft³)")+
  theme_minimal(base_family = "serif")

Predicting Tree Volumes

Predictions show how estimated volume grows as girth increases, with wider intervals for larger trees.