2025-10-19

Topic & Objectives

  • Perform Linear Regression
  • Use in-built ‘Trees’ data set
  • Create Interactive plots

Data Cleaning

  • Checking for missing values (na-values)
n_og = nrow(trees)
per_col = sapply(trees, function(x) sum(is.na(x)))
total_na = sum(per_col)

print(per_col)
##  Girth Height Volume 
##      0      0      0
cat("\nTotal missing values in dataset:", total_na, "\n\n")
## 
## Total missing values in dataset: 0
# There are Zero Missing Values in the data set

The two-predictor linear model:

\[ y_i = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \varepsilon_i,\qquad \varepsilon_i\sim\mathcal{N}(0,\sigma^2), \]

where y = Volume, x1 = Girth, x2 = Height.

Model matrix form and estimator

Matrix form and least squares estimator:

\[ \mathbf{Y} = \mathbf{X}\boldsymbol{\beta} + \boldsymbol{\varepsilon}, \qquad \hat{\boldsymbol{\beta}} = (\mathbf{X}^\top\mathbf{X})^{-1}\mathbf{X}^\top\mathbf{Y}. \]

These give the fitted coefficients used for prediction and inference.

Girth vs Volume (Plotly Plot)

Code for Girth vs Volume Plot

p_plotly <- plot_ly(data = trees, x = ~Girth, y = ~Volume,
        text = ~paste("Girth:", Girth, "<br>Volume:", 
                      Volume, "<br>Height:", Height),
        color = ~Height, colors = c("blue", "red")) %>%
  add_markers(marker = list(size = 7, opacity = 0.7), 
              hoverinfo = "text") %>%
  layout(title = "Girth vs Volume (color = Height)",
         xaxis = list(title = "Girth (in)"),
         yaxis = list(title = "Volume (cubic ft)"))

Scatter Plot: Volume vs Girth

Scatter Plot: Volume vs Height

Residuals vs Fitted