2025-11-09

Simple Linear Regression with trees Dataset

3D scatter plot shows linear relationship between the independent variables (tree Girth and tree Height) and dependent variable (tree Volume).

Code for Previous Plot

model = lm(Volume ~ Girth + Height, data = trees);
xRange = seq(min(trees$Girth), max(trees$Girth), length.out = 60);
yRange = seq(min(trees$Height), max(trees$Height), length.out = 60);
dataGrid = expand.grid(Girth = xRange, Height = yRange);
dataGrid$zPred = predict(model, newdata = dataGrid);
zMatrix = matrix(dataGrid$zPred, nrow = length(xRange), ncol = length(yRange));

plot_ly(trees, x = ~Girth, y = ~Height, z = ~Volume, type = "scatter3d",
        mode = "markers", name = "Data Values",
        marker = list(size = 5, color = 'orange')) %>%
  add_surface(x = ~xRange, y = ~yRange, z = ~zMatrix,
              name = "Prediction", opacity = 0.5,
              colorscale = list(c(0, "lightblue"), c(1, "darkblue")),
              showscale = FALSE) %>%
  layout(title = "Trees", scene = list(xaxis = list(title = "Girth - X"),
                                  yaxis = list(title = "Height - Y"),
                                  zaxis = list(title = "Volume - Z")));

Simple Linear Regression with mtcars Dataset

Plot shows linear relationship between miles per gallon (mpg) and horsepower (hp) as well as how the variables of displacement (disp) and weight (wt) are related to miles per gallon and horsepower.

Code for Previous Plot

ggplot(data = mtcars, aes(x = mpg, y = hp, size = disp, color = wt)) +
  geom_point() + geom_smooth(method = "lm", linetype = "dashed", color = "orange",
  show.legend = FALSE) + scale_size_continuous("Displacement (cu.in.)") +
  scale_color_continuous("Weight (1000 lbs)") +
  labs(title = "Cars", x = "Miles Per Gallon", y = "Gross Horsepower",
       color = "Weight (1000 lbs)");

Simple Linear Regression with iris Dataset

\(\text{Petal Length to Sepal Length}\)

\(\text{model: Petal.Length} = \beta_0 + \beta_1\cdot \text{Sepal.Length} + \varepsilon; \hspace{1cm} \varepsilon \sim \mathcal{N}(0; \sigma^2)\)
\(\text{fitted: Petal.Length} = \hat{\beta}_0 + \hat{\beta}_1\cdot\text{Sepal.Length}\); \(\hspace{1.5cm} \hat{\beta}_0 = b_0 - \text{estimate of }\beta_0 = -7.10144\);   \(\hat{\beta}_1 = b_1 - \text{estimate of }\beta_1 = 1.85843\)
\(\hspace{1cm} \text{Petal.Length} = (-7.10144) + (1.85843)\cdot\text{Sepal.Length}\)

Simple Linear Regression with iris Dataset

\(\text{Petal Width to Sepal Width}\)

\(\text{model: Petal.Width} = \beta_0 + \beta_1\cdot \text{Sepal.Width} + \varepsilon; \hspace{1cm} \varepsilon \sim \mathcal{N}(0; \sigma^2)\)
\(\text{fitted: Petal.Width} = \hat{\beta}_0 + \hat{\beta}_1\cdot\text{Sepal.Width}\); \(\hspace{1.5cm} \hat{\beta}_0 = b_0 - \text{estimate of }\beta_0 = -7.10144\);   \(\hat{\beta}_1 = b_1 - \text{estimate of }\beta_1 = 1.85843\)
\(\hspace{1cm} \text{Petal.Width} = (3.1569) + (-0.6403)\cdot\text{Sepal.Width}\)

Simple Linear Regression with iris Dataset

\(\text{Sepal Width to Sepal Length}\)

\(\text{model: Sepal.Width} = \beta_0 + \beta_1\cdot \text{Sepal.Length} + \varepsilon; \hspace{1cm} \varepsilon \sim \mathcal{N}(0; \sigma^2)\)
\(\text{fitted: Sepal.Width} = \hat{\beta}_0 + \hat{\beta}_1\cdot\text{Sepal.Length}\); \(\hspace{1.5cm} \hat{\beta}_0 = b_0 - \text{estimate of }\beta_0 = -7.10144\);   \(\hat{\beta}_1 = b_1 - \text{estimate of }\beta_1 = 1.85843\)
\(\hspace{1cm} \text{Sepal.Width} = (3.41895) + (-0.06188)\cdot\text{Sepal.Length}\)

Simple Linear Regression with iris Dataset

\(\text{Petal Width to Petal Length}\)

\(\text{model: Petal.Width} = \beta_0 + \beta_1\cdot \text{Petal.Length} + \varepsilon; \hspace{1cm} \varepsilon \sim \mathcal{N}(0; \sigma^2)\)
\(\text{fitted: Petal.Width} = \hat{\beta}_0 + \hat{\beta}_1\cdot\text{Petal.Length}\); \(\hspace{1.5cm} \hat{\beta}_0 = b_0 - \text{estimate of }\beta_0 = -7.10144\);   \(\hat{\beta}_1 = b_1 - \text{estimate of }\beta_1 = 1.85843\)
\(\hspace{1cm} \text{Petal.Width} = (-0.363076) + (0.415755)\cdot\text{Petal.Length}\)

Code for Previous Four Plots

# Petal Length to Sepal Length
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(aes(color = Species)) + geom_smooth(method = "lm",
  linetype = "dashed") + labs(title = "Length of Sepal compared to Length of Petal",
                              x = "Length of Sepal", y = "Length of Petal");

# Petal Width to Sepal Width
ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width)) +
  geom_point(aes(color = Species)) + geom_smooth(method = "lm",
  linetype = "dashed") + labs(title = "Width of Sepal compared to Width of Petal",
                              x = "Width of Sepal", y = "Width of Petal");

 # Sepal Width to Sepal Length
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) + geom_smooth(method = "lm",
  linetype = "dashed") + labs(title = "Width of Sepal compared to Length of Sepal",
                              x = "Length of Sepal", y = "Width of Sepal");

# Petal Width to Petal Length
ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(color = Species)) + geom_smooth(method = "lm",
  linetype = "dashed") + labs(title = "Width of Petal compared to Length of Petal",
                              x = "Length of Petal", y = "Width of Petal");