- Perform Linear Regression
- Use in-built ‘Trees’ data set
- Create Interactive plots
2025-10-19
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
\[ 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.
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.
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)"))