MODEL 1

Column

MODEL 1 :: BACKWARD STEPWISE REGRESSION LOOP FOR GAMMA LOG MODEL

Models 1-3 attempt to predict the minimum linear distance to the nearest brood tree using the total basal area of all infested trees within a given neighborhood. Model 1 is a gamma generalized linear model, with the log function as its link function. To achieve this, a loop was created in which one predictor was removed in each iteration. Since multiple predictors display high collinearity, model 1 removes the predictor with the highest variance inflation factor. If all predictors have a variance inflation factor less than four or if only two predictors remain, the loop then selects the predictor with the highest p-value to remove. Summary statistics, including estimates of coefficients and statistics, are reported for each iteration of the loop. Descriptions of the variables used are also provided in the flexdashboard.

DESCRIPTION OF VARIABLES

OUTCOME VARIABLE

  1. Dead Distance: Minimum linear distance to nearest brood tree

PREDICTOR VARIABLES

  1. 1/20th-Acre: Basal area total for all infested trees within 1/20th-acre neighborhood
  2. 1/4th-Acre: Basal area of infested trees within 1/4th-acre neighborhood of response tree
  3. 1/2th-Acre: Basal area of infested trees within 1/2-acre neighborhood of response tree
  4. 1-Acre: Basal area of infested trees within 1-acre neighborhood of response tree
  5. 1.5-Acre: Basal area of infested trees within 1.5-acre neighborhood of response tree

Column

SUMMARY OF DATA

Variable n_Obs Mean SD Median MAD Min Max Skewness Kurtosis percentage_Missing
6 Dead Distance 9687 24.08 18.84 19.84 17.35 0.11 124.95 1.29 2.25 0.00
1 1/20th-Acre 9687 0.60 1.91 0.00 0.00 0.00 21.88 4.91 29.65 0.00
4 1/4th-Acre 9687 10.30 16.49 3.13 4.64 0.00 106.67 2.63 7.49 0.00
3 1/2th-Acre 9687 5.26 9.77 0.89 1.33 0.00 92.40 3.43 15.85 0.00
5 1-Acre 9687 15.05 22.09 7.75 10.68 0.00 135.70 2.45 6.20 0.00
2 1.5-Acre 9687 2.69 6.04 0.00 0.00 0.00 72.30 4.18 24.12 0.00

CORRELATION PLOT OF OUTCOME AND PREDICTORS

Column {data-height=800, data-width = 500}

SUMMARY PLOT

Column {data-height=800, data-width = 500}

LOOP 1

LOOP 2

LOOP 3

LOOP 4

LOOP 5

Row {data-height=400, data-width = 1000}

ESTIMATED COEFFICIENT PLOT

VARIANCE INFLATION FACTOR (VIF) PLOT

Row {data-height=400, data-width = 1000}

LOOP 1 STATISTICS

loop predictor estimate std.error statistic p.value VIF
loop1 1/4th-Acre 0.001 0.002 0.527 0.598 31.326
loop1 1/2th-Acre -0.005 0.003 -1.450 0.147 20.831
loop1 1.5-Acre -0.023 0.004 -5.602 0.000 12.384
loop1 1-Acre -0.011 0.001 -7.939 0.000 16.893
loop1 1/20th-Acre -0.113 0.007 -17.354 0.000 3.022

LOOP 2 STATISTICS

loop predictor estimate std.error statistic p.value VIF
loop2 1/2th-Acre -0.004 0.003 -1.429 0.153 13.806
loop2 1.5-Acre -0.024 0.004 -5.774 0.000 12.062
loop2 1-Acre -0.010 0.001 -16.213 0.000 3.542
loop2 1/20th-Acre -0.114 0.007 -17.427 0.000 3.011

LOOP 3 STATISTICS

loop predictor estimate std.error statistic p.value VIF
loop3 1.5-Acre -0.028 0.002 -11.989 0 3.982
loop3 1/20th-Acre -0.111 0.006 -17.576 0 2.881
loop3 1-Acre -0.011 0.000 -25.009 0 1.717

LOOP 4 STATISTICS

loop predictor estimate std.error statistic p.value VIF
loop4 1-Acre -0.013 0.000 -36.909 0 1.182
loop4 1/20th-Acre -0.176 0.004 -43.858 0 1.182

LOOP 5 STATISTICS

loop predictor estimate std.error statistic p.value
loop5 1/20th-Acre -0.239 0.004 -57.797 0

GOODNESS-OF-FIT STATISTICS FOR EACH LOOP

loop 1 loop 2 loop 3 loop 4 loop 5
null.deviance 7290.621 7290.621 7290.621 7290.621 7290.621
df.null 9686 9686 9686 9686 9686
logLik -37920.06 -37920.96 -37920.38 -37992.21 -38687.17
AIC 75854.13 75853.93 75850.75 75992.43 77380.33
BIC 75904.38 75897.00 75886.64 76021.14 77401.87
deviance 4804.812 4805.637 4805.097 4871.458 5558.657
df.residual 9681 9682 9683 9684 9685
nobs 9687 9687 9687 9687 9687
chisq NA 1.798 -1.177 143.677 1389.902
p_value NA 0.18 1.00 0.00 0.00

Row {data-height=400, data-width = 1000}

FIT STATISTICS FOR EACH LOOP

MODEL 2 & 3

Column

MODEL 2 & 3 :: LASSO AND RANDOM FOREST REGRESSION

Models 2 and 3 use the same predictors to predict the minimum linear distance to the nearest brood tree. However, model two is a Lasso regression, while model three is a random forest regression. Unlike model one, both models were trained and tested on a 75/25 split. Summary statistics were reported for the cross-validation, indicating how well the models fit the test data.

Row {data-height=1000, data-width = 500}

PLOTS OF ACTUAL VERSUS FITTED VALUES

Column

GOODNESS-OF-FIT METRICS FOR LASSO AND RANDOM FOREST MODELS

rmse rsq mae
Glmnet 29.150 0.204 22.649
Forest 0.112 0.932 0.068

R-Squared for Lasso Regression

R-Squared for Random Forest

---
title: "Project 2 Flexdashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    theme: 
      version: 4
      bootswatch: minty
    source_code: embed
---


```{r echo = FALSE, results='axis', include=FALSE}

# PACKAGES ----------------------------------------------------------------

library(tidyverse)
library(tidymodels)  
library(readr) 
library(easystats)
library(randomForest)
library(vip)
library(car)
library(knitr)
library(ggplot2)
library(corrplot)

# DOWNLOAD DATA -----------------------------------------------------------
data <- read.csv("/Users/stephenvieno/Projects/hgen-612/data/Data_1993.csv") # Download data 

### CLEAN DATA ###
data <- na.omit(data) # Remove missing values from data 
# Remove single column with NA values 
data$TreeNum <- as.numeric(data$TreeNum, na.rm = FALSE) # Convert TreeNum to numeric 

```

MODEL 1
===============================================================================

Column 
----------------------------------------------------------------------

### MODEL 1 :: BACKWARD STEPWISE REGRESSION LOOP FOR GAMMA LOG MODEL 

Models 1-3 attempt to predict the minimum linear distance to the nearest brood tree using the total basal area of all infested trees within a given neighborhood. Model 1 is a gamma generalized linear model, with the log function as its link function. To achieve this, a loop was created in which one predictor was removed in each iteration. Since multiple predictors display high collinearity, model 1 removes the predictor with the highest variance inflation factor. If all predictors have a variance inflation factor less than four or if only two predictors remain, the loop then selects the predictor with the highest p-value to remove. Summary statistics, including estimates of coefficients and statistics, are reported for each iteration of the loop. Descriptions of the variables used are also provided in the flexdashboard.

### DESCRIPTION OF VARIABLES

OUTCOME VARIABLE

1. Dead Distance: Minimum linear distance to nearest brood tree

PREDICTOR VARIABLES

1. 1/20th-Acre: Basal area total for all infested trees within 1/20th-acre neighborhood 
2. 1/4th-Acre: Basal area of infested trees within 1/4th-acre neighborhood of response tree
3. 1/2th-Acre: Basal area of infested trees within 1/2-acre neighborhood of response tree
4. 1-Acre: Basal area of infested trees within 1-acre neighborhood of response tree
5. 1.5-Acre: Basal area of infested trees within 1.5-acre neighborhood of response tree

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### SUMMARY OF DATA

```{r}
# -------------------------------------------------------------------------
### SUMMMARY OF VARIABLES USED IN GLM ### 
var <- c("DeadDist", # List of variables used in the following models 
         "BA_Inf_20th", 
         "bsadead1new", 
         "bsadead5new", 
         "bsadead15new", 
         "bsadead25new")
data |> 
  dplyr::select(var) |> # Select variables used in the following models 
  setNames(c("Dead Distance", # Rename variables to original names 
           "1/20th-Acre", 
           "1/4th-Acre", 
           "1/2th-Acre",
           "1-Acre", 
           "1.5-Acre")) |> 
  report() |>  # Create report of variables 
  kable() # Create table for report of variables 

```

### CORRELATION PLOT OF OUTCOME AND PREDICTORS 

```{r}
col <- colorRampPalette(c("#0072B2", "#56B4E9",  "#FFFFFF","#E69F00","#D55E00")) # Set colors for correlation matrix plot 
cor_data <- data |> 
  dplyr::select(var) |> # Select variables used in the following models 
  setNames(c("Dead Distance", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre")) |> 
  cor() # Create correlation matrix 

corrplot::corrplot(cor_data, # Set data as correlation matrix 
                   method="color", # Set correlation plot style to color 
                   col=col(200), # Set number of possible colors for correlation plot 
                   type="upper", # Display only upper section of matrix 
                   order="original", # Use original order of variables for correlation matrix 
                   addCoef.col = "black", # Color of coefficients on the graph 
                   tl.col="black", # Color of text label 
                   tl.srt=45, # Rotation of labels 
                   diag=FALSE) # Hide diagonal in correlation matrix 



```

Column {data-height=800, data-width = 500} 
-----------------------------------------------------------------------

### SUMMARY PLOT 
```{r, fig.height=7, fig.width=12}
data_test_1 <- data |> 
  dplyr::select(var) |> # Select variables in the model 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot predictors into column 
               names_to = "variable", # Name of predictors variables
               values_to = "values") |> # Values of predictor variables
  mutate(variable = factor(variable, # Convert predictors variables to factor 
                           levels = c("1/20th-Acre", # Order the predictor variables 
                                      "1/4th-Acre", 
                                      "1/2th-Acre",
                                      "1-Acre", 
                                      "1.5-Acre")))

data_test_2 <- data |> 
  dplyr::select(var) |> # Select variables in the model 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot predictors into column 
               names_to = "variable",  # Values of predictor variables
               values_to = "values") |> # Values of predictor variables
  dplyr::select(-variable) # Remove predictor name assignments 

cbPalette <- c("#0072B2", "#56B4E9", "#009E73", "#F0E442","#E69F00", "#D55E00", "#CC79A7") # Use colorblind friendly colors 

ggplot(data_test_1, aes(values, DeadDist)) + # Create scatter plot of DeadDist and predictors 
    geom_point(data = data_test_2, # Plot all data points for predictors  
               colour = "#999999", # Set color as grey for plots 
               alpha = 0.5) + # Set alpha to 0.5 
    geom_point(aes(colour = variable), # Set color to predictor 
               alpha = 0.5) +  # Set alpha to 0.5 
    facet_wrap(~variable) + # Facet wrap on the basis of predictor variable 
    scale_color_manual(values=cbPalette) + # Set colors to colorblind friendly colors 
    theme_bw() + # Set theme to black and white theme 
    xlab("Basal area of total for all infested trees within neighborhood") + # Label x-axis 
    ylab("Minimum linear distance to nearest brood tree") + # Label y-axis 
    ggtitle("Scatter Plot of Dead Distance Variable and Predictor Variables") + # Label title
    theme(legend.title = element_text(face = "bold"), # Bold legend theme 
          axis.title=element_text(size=15), # Size axis text 
          plot.title = element_text(size = 20)) +  # Size plot title 
    labs(colour = "Basal Area") # Change legend title 

```

Column {data-height=800, data-width = 500} {.tabset .tabset-fade}
-----------------------------------------------------------------------

```{r setup, include=FALSE}

### CREATE MODEL FOR EXPOTNETIAL DECAY :: GAMMA LOG GLM ### 
log_mod <- linear_reg(mode = "regression") |>  #Set model to linear regression model 
  set_engine("glm", # Set engine to generalized linear model 
             family = Gamma(link = "log")) # Set family to Gamma log model 

rec <- recipe(DeadDist ~ BA_Inf_20th + bsadead1new + bsadead5new + bsadead15new + bsadead25new, # Set recipe for model 
              data = data) # Set data to data 

vif_loop <- NULL  # Set to NULL :: Remove any previous assignments to vector/data frame 
loop_data <- NULL # Set to NULL :: Remove any previous assignments to vector/data frame 
glance_lm <- NULL # Set to NULL :: Remove any previous assignments to vector/data frame 
pred <- NULL # Set to NULL :: Remove any previous assignments to vector/data frame 

loop_data <- list() # Create list to store coefficients and statistics for each loop 
glance_lm <- list() # Create list to store fit statistics for each loop 
vif_loop <- list() # Create list to store predictor VIF for each loop 
pred <- list()  # Create list to store predictions for each loop 

# BACKWARD STEPWISE REGRESSION LOOP FOR GAMMA LOG MODEL 
# IF VIF > 4 FOR ANY PREDICTOR AND NUMBER OF PREDICTORS > 2, THEN REMOVE VARIABLE WITH HIGHEST VIF 
# IF VIF < 4 FOR ALL PREDICTORS OR ALL NUMBER OF PREDICTORS < 3, THEN REMOVE PREDICTOR WTH HIGHEST P-VALUE 
for (i in 1:5) { 
  
  lm_workflow <- workflow() |> # Create workflow 
    add_model(log_mod) |>  # Add gamma log glm model 
    add_recipe(rec) # Add recipe 
  
  glance_lm[[i]] <- lm_workflow |> # Create list to store fit statistics for each loop 
    fit(data = data) |> # Fit the data to the model 
    glance() # Use glance to create fit statistics for loop 
  
  fit_lm <- lm_workflow |> # Create summary statistics for coefficients in each loop 
    fit(data = data) |> # Fit the data to the model 
    tidy() |> # Create data frame of summary statistics for coefficients in each loop 
    dplyr::filter(term != "(Intercept)") |> # Remove intercept from data frame  
    arrange(desc(p.value)) # Arrange in order of descending p-values 
  
  loop <- print(rep( paste0("loop",i), 6-i)) # Create vector of loop 1-5 labels 
  loop_data[[i]] <- as.data.frame(bind_cols(loop, fit_lm)) # Label loop number for summary statistics for coefficients in each loop 
  
  lm_workflow <- lm_workflow |> fit(data = data) # Fit data to gamma log glm model
 
  pred[[i]] <- predict(lm_workflow, new_data = data) # Create predictions using gamma log glm model
  
# If else statement:: 
# Create VIF for first loop or if more than two predictors are used 
  if (i == 1 || nrow(vif_lm) > 2) {
    vif_lm <- lm_workflow |> 
      extract_recipe() |> # Extract recipe from workflow 
      lm(data = data) |>  # Fit data to linear model 
      vif() |>  # Extract VIF for predictors 
      as.data.frame() |> # Convert to data frame 
      rownames_to_column("Variables") |> # Convert row names to a column 
      rename(VIF = `vif(lm(extract_recipe(lm_workflow), data = data))`) |> # Rename column to VIF 
      arrange(desc(VIF)) # Arrage in descending order of VIF 
    
    vif_loop[[i]] <- as.data.frame(bind_cols(loop, vif_lm)) # Label loop number for VIF in each loop  
  
  } else {
    print("no vif") # If unable to use VIF to analyse multi-collinearity, print no VIF
  }
  
  if(nrow(vif_lm) > 2 & vif_lm[1,2] > 5){ # Create 
    rec <- remove_role(rec, as.character(vif_lm[1,1]), old_role = "predictor") # If VIF available, remove predictor with highest VIF 
  } else {
    rec <- remove_role(rec, as.character(fit_lm[1,1]), old_role = "predictor") # If no VIF available, removed predictor with highest p-value 
  }
  
}

lm_loop <- do.call(bind_rows, loop_data) # Create data frame to store coefficients and statistics for each loop 
colnames(lm_loop)[1] <- "loop" # Rename column with loop assignment 

glance_loop <- do.call(bind_rows, glance_lm) # Create data frame to store fit statistics for each loop 

vif_loop <- do.call(bind_rows, vif_loop) # Create data frame to store predictor VIF for each loop 
colnames(vif_loop)[1] <- "loop" # Rename column with loop assignment 

pred_loop <- do.call(bind_cols, pred) # Create data frame to store predictions for each loop 
colnames(pred_loop)  <- c("loop1", "loop2", "loop3", "loop4", "loop5") # Rename columns by loop assignment 
plot_data <- bind_cols(data, pred_loop) # Bind loop predictions to predictors 





```

### LOOP 1 
```{r, fig.height=7, fig.width=12}

### PLOT THE FITTED VALUES FOR THE LOOP 1
plot_data |>  # Plot predicted values 
  dplyr::select(var, loop1:loop5) |> # Select predictors and prediction values 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre", 
             "loop1", 
             "loop2",
             "loop3",
             "loop4", 
             "loop5")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot longer predictors 
               names_to = "predictors", # Convert column names to predictors 
               values_to = "variables_values") |>
  mutate(predictors = factor(predictors, # Convert predictors variables to factor 
                           levels = c("1/20th-Acre", # Order the predictor variables 
                                      "1/4th-Acre", 
                                      "1/2th-Acre",
                                      "1-Acre", 
                                      "1.5-Acre"))) |> # Convert variables values to variables_values
  ggplot(aes(y = DeadDist, # Plot actual values (DeadDist) vs Predictors 
             x = variables_values # Plot actual values (DeadDist) vs Predictors 
             )) + 
  geom_point(color = "#999999") +  # Set color of actual values to grey 
  geom_point(aes(y = loop1, # Plot predicted values (DeadDist) vs Predictors 
                 x = variables_values), # Plot predicted values (DeadDist) vs Predictors 
                 alpha = 0.3, # Set alpha to 0.3
                 color = "#0072B2") + # Set color of predicted values to blue  
  facet_wrap(vars(predictors)) +  # Facet wrap on predictors
  theme_bw(base_size = 15) + # Set theme to black and white 
  xlab("Basal area of total for all infested trees within neighborhood") + # Title x-axis 
  ylab("Minimum linear distance to nearest brood tree") + # Title y-axis 
  ggtitle("Loop 1: Scatter plot of Fitted and Actual Values for the Dead Distance Variable",  # Title 
          subtitle = "Fitted Values in Dark Blue & Actual Values in Grey") # Subtitle 

```

### LOOP 2
```{r, fig.height=7, fig.width=12}

### PLOT THE FITTED VALUES FOR THE LOOP 2
plot_data |>  # Plot predicted values 
  dplyr::select(var, loop1:loop5) |> # Select predictors and prediction values 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre", 
             "loop1", 
             "loop2",
             "loop3",
             "loop4", 
             "loop5")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot longer predictors 
               names_to = "predictors", # Convert column names to predictors 
               values_to = "variables_values") |>
  mutate(predictors = factor(predictors, # Convert predictors variables to factor 
                           levels = c("1/20th-Acre", # Order the predictor variables 
                                      "1/4th-Acre", 
                                      "1/2th-Acre",
                                      "1-Acre", 
                                      "1.5-Acre"))) |> # Convert variables values to variables_values
  ggplot(aes(y = DeadDist, # Plot actual values (DeadDist) vs Predictors 
             x = variables_values # Plot actual values (DeadDist) vs Predictors 
            )) + 
  geom_point(color = "#999999") +  # Set color of actual values to grey 
  geom_point(aes(y = loop2, # Plot predicted values (DeadDist) vs Predictors 
                 x = variables_values), # Plot predicted values (DeadDist) vs Predictors 
                 alpha = 0.3, # Set alpha to 0.3
                 color = "#56B4E9") + # Set color of predicted values to light blue  
  facet_wrap(vars(predictors)) +  # Facet wrap on predictors
  theme_bw(base_size = 15) + # Set theme to black and white 
  xlab("Basal area of total for all infested trees within neighborhood") + # Title x-axis 
  ylab("Minimum linear distance to nearest brood tree") + # Title y-axis 
  ggtitle("Loop 2: Scatter plot of Fitted and Actual Values for the Dead Distance Variable",  # Title 
          subtitle = "Fitted Values in Light Blue & Actual Values in Grey") # Subtitle 
```

### LOOP 3
```{r, fig.height=7, fig.width=12}

### PLOT THE FITTED VALUES FOR THE LOOP 3 
plot_data |>  # Plot predicted values 
  dplyr::select(var, loop1:loop5) |> # Select predictors and prediction values 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre", 
             "loop1", 
             "loop2",
             "loop3",
             "loop4", 
             "loop5")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot longer predictors 
               names_to = "predictors", # Convert column names to predictors 
               values_to = "variables_values") |>
  mutate(predictors = factor(predictors, # Convert predictors variables to factor 
                           levels = c("1/20th-Acre", # Order the predictor variables 
                                      "1/4th-Acre", 
                                      "1/2th-Acre",
                                      "1-Acre", 
                                      "1.5-Acre"))) |> # Convert variables values to variables_values
  ggplot(aes(y = DeadDist, # Plot actual values (DeadDist) vs Predictors 
             x = variables_values # Plot actual values (DeadDist) vs Predictors 
             )) + 
  geom_point(color = "#999999") +  # Set color of actual values to grey 
  geom_point(aes(y = loop3, # Plot predicted values (DeadDist) vs Predictors 
                 x = variables_values # Plot predicted values (DeadDist) vs Predictors 
                 ), alpha = 0.3, color =  "#009E73") + # Set color of predicted values to green 
  facet_wrap(vars(predictors)) +  # Facet wrap on predictors
  theme_bw(base_size = 15) + # Set theme to black and white 
  xlab("Basal area of total for all infested trees within neighborhood") + # Title x-axis 
  ylab("Minimum linear distance to nearest brood tree") + # Title y-axis 
  ggtitle("Loop 3: Scatter plot of Fitted and Actual Values for the Dead Distance Variable",  # Title 
          subtitle = "Fitted Values in Green & Actual Values in Grey") # Subtitle 

```

### LOOP 4
```{r, fig.height=7, fig.width=12}

### PLOT THE FITTED VALUES FOR THE LOOP 4
plot_data |>  # Plot predicted values 
  dplyr::select(var, loop1:loop5) |> # Select predictors and prediction values 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre", 
             "loop1", 
             "loop2",
             "loop3",
             "loop4", 
             "loop5")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot longer predictors 
               names_to = "predictors", # Convert column names to predictors 
               values_to = "variables_values") |>
  mutate(predictors = factor(predictors, # Convert predictors variables to factor 
                           levels = c("1/20th-Acre", # Order the predictor variables 
                                      "1/4th-Acre", 
                                      "1/2th-Acre",
                                      "1-Acre", 
                                      "1.5-Acre"))) |> # Convert variables values to variables_values
  ggplot(aes(y = DeadDist, # Plot actual values (DeadDist) vs Predictors 
             x = variables_values # Plot actual values (DeadDist) vs Predictors 
             )) +
  geom_point(color = "#999999") +  # Set color of actual values to grey 
  geom_point(aes(y = loop4, # Plot predicted values (DeadDist) vs Predictors 
                 x = variables_values), # Plot predicted values (DeadDist) vs Predictors 
                 alpha = 0.3, # Set alpha to 0.3
                 color = "#F0E442") + # Set color of predicted values to yellow
  facet_wrap(vars(predictors)) +  # Facet wrap on predictors
  theme_bw(base_size = 15) + # Set theme to black and white 
  xlab("Basal area of total for all infested trees within neighborhood") + # Title x-axis 
  ylab("Minimum linear distance to nearest brood tree") + # Title y-axis 
  ggtitle("Loop 4: Scatter plot of Fitted and Actual Values for the Dead Distance Variable",  # Title 
          subtitle = "Fitted Values in Yellow & Actual Values in Grey") # Subtitle 
```

### LOOP 5
```{r, fig.height=7, fig.width=12}

### PLOT THE FITTED VALUES FOR THE LOOP 5
plot_data |>  # Plot predicted values 
  dplyr::select(var, loop1:loop5) |> # Select predictors and prediction values 
  setNames(c("DeadDist", # Rename variables to original names 
             "1/20th-Acre", 
             "1/4th-Acre", 
             "1/2th-Acre",
             "1-Acre", 
             "1.5-Acre", 
             "loop1", 
             "loop2",
             "loop3",
             "loop4", 
             "loop5")) |> 
  pivot_longer(cols = "1/20th-Acre":"1.5-Acre", # Pivot longer predictors 
               names_to = "predictors", # Convert column names to predictors 
               values_to = "variables_values") |>
  mutate(predictors = factor(predictors, # Convert predictors variables to factor 
                           levels = c("1/20th-Acre", # Order the predictor variables 
                                      "1/4th-Acre", 
                                      "1/2th-Acre",
                                      "1-Acre", 
                                      "1.5-Acre"))) |> # Convert variables values to variables_values
  ggplot(aes(y = DeadDist, # Plot actual values (DeadDist) vs Predictors 
             x = variables_values # Plot actual values (DeadDist) vs Predictors 
              )) +
  geom_point(color = "#999999") +  # Set color of actual values to grey 
  geom_point(aes(y = loop5, # Plot predicted values (DeadDist) vs Predictors 
                 x = variables_values), # Plot predicted values (DeadDist) vs Predictors 
                 alpha = 0.3, # Set alpha to 0.3 
                 color = "#E69F00") + # Set color of predicted values to orange
  facet_wrap(vars(predictors)) +  # Facet wrap on predictors
  theme_bw(base_size = 15) + # Set theme to black and white 
  xlab("Basal area of total for all infested trees within neighborhood") + # Title x-axis 
  ylab("Minimum linear distance to nearest brood tree") + # Title y-axis 
  ggtitle("Loop 5: Scatter plot of Fitted and Actual Values for the Dead Distance Variable",  # Title 
          subtitle = "Fitted Values in Orange & Actual Values in Grey") # Subtitle 

  
```

Row {data-height=400, data-width = 1000}
-----------------------------------------------------------------------
### ESTIMATED COEFFICIENT PLOT
```{r}
# Colorblind friendly colors 
cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") 

### CREATE PLOT OF COEFFICENTS FOR EACH LOOP ###
lm_loop |> 
  mutate(loop = as.numeric(gsub("loop", "", lm_loop$loop)), # Create vector of number of loop 
         num_var = 6 - loop) |> # Create vector of number of predictors 
  ggplot(aes(x = num_var, # X-axis as number of predictors 
             y = estimate, # Y-axis as estimate of coefficient 
             group = term, # Group by predictors
             color = term)) + # Color by predictors 
  geom_point(size = 3) + # Size of point 
  geom_line(alpha = 0.5, # Set alpha to 0.5 
            lwd = 2)  + # Set line width to 2 
  geom_hline(yintercept = 0, # Set y-intercept to 0
             color = "black", # Set line color to black 
             linetype = "dashed", # Set line type to dashed 
              size=1) + # Set line size to 1 
  geom_errorbar(width=0.1, # Set line width for error bars 
                aes(ymin=estimate-std.error, # Set lower error bar 
                    ymax=estimate+std.error), # Set upper error bar 
                colour="black") + # Set color of error bars 
  theme_bw(base_size = 12) + # Set theme to black and white 
  labs(title = "Estimate of Coefficent for Each Loop", # Title 
       col = "Predictors") + # Title of legend 
  xlab("Number of Predictors") + # Title of of x-axis
  ylab("Estimate of Coefficent") + # Title of y-axis
  scale_colour_manual(values=cbPalette, # Set colors to colorblind friendly colors 
                      labels =  c("1/20th-Acre", # Labels for predictors 
                                  "1/4th-Acre", 
                                  "1/2th-Acre",
                                  "1-Acre", 
                                  "1.5-Acre")) 
 


```

### VARIANCE INFLATION FACTOR (VIF) PLOT
```{r}
### CREAT VIF PLOT FOR EACH LOOP ###
vif_loop |> 
  mutate(loop = as.numeric(gsub("loop", "", vif_loop$loop)), # Create vector of number of loop 
         num_var = 6 - loop) |>  # Create vector of number of predictors 
  ggplot(aes(x = num_var, # Set x-axis as number of variables 
             y = VIF, # Set y-axis as VIF 
             group = Variables, # Group by predictors  
             col = Variables)) + # Group by predictors  
  geom_point(size = 3) + # Size points to 3 
  geom_line(alpha = 0.5, # Set alpha to 0.5 
            lwd = 2)  + # 
  geom_hline(yintercept = 5, 
             color = "black",
             linetype = "dashed", # Set line type to dashed 
             size=1) + # Set size of line to 1
  theme(legend.title = element_text(face = "bold")) + # Create bold label 
  theme_bw(base_size = 12) + # Set size of text to 12 
  labs(title = "Variance Inflation Factor (VIF) for Each Loop", # Title 
       subtitle = "Dashed Line: Variance Inflation Factor = 4") + # Subtitle 
  xlab("Number of Variables") + # Title of x-axis 
  ylab("Variance Inflation Factor") +  # Title of y-axis 
  scale_colour_manual(values=cbPalette, # Set colors to colorblind friendly colors 
                      labels =  c("1/20th-Acre", # Labels for predictors 
                                  "1/4th-Acre", 
                                  "1/2th-Acre",
                                  "1-Acre", 
                                  "1.5-Acre")) 





```

Row {data-height=400, data-width = 1000} {.tabset .tabset-fade}
-----------------------------------------------------------------------
### LOOP 1 STATISTICS 
```{r}
colnames(vif_loop)[2] <- "term" # Rename column to term 
loop_stat <- lm_loop |> 
  dplyr::inner_join(vif_loop, # Inner join summary statistics with VIF 
                    by = c("loop", "term")) |>  # Inner join by term and loop 
  dplyr::filter(loop == "loop1") # Filter loop 1 
colnames(loop_stat)[2] <- "predictor" # Reassign name of term to predictor 

loop_stat |> 
  mutate(predictor = dplyr::recode(predictor, # Rename the predictor to correct names 
                            "BA_Inf_20th" = "1/20th-Acre", 
                            "bsadead1new"="1/4th-Acre", 
                            "bsadead5new"="1/2th-Acre",
                            "bsadead15new"="1-Acre", 
                            "bsadead25new"="1.5-Acre"),
         estimate = round(estimate,3), # Round to 3 decimals 
         std.error = round(std.error,3), # Round to 3 decimals 
         statistic = round(statistic,3), # Round to 3 decimals 
         p.value = round(p.value,3), # Round to 3 decimals 
         VIF = round(VIF,3)) |>  # Round to 3 decimals 
kable() # Create table 
```

### LOOP 2 STATISTICS 
```{r}
colnames(vif_loop)[2] <- "term" # Rename column to term 
loop_stat <- lm_loop |> 
  dplyr::inner_join(vif_loop, # Inner join summary statistics with VIF 
                    by = c("loop", "term")) |>  # Inner join by term and loop 
  dplyr::filter(loop == "loop2") # Filter loop 2 
colnames(loop_stat)[2] <- "predictor" # Reassign name of term to predictor 

loop_stat |> 
  mutate(predictor = dplyr::recode(predictor, # Rename the predictor to correct names 
                            "BA_Inf_20th" = "1/20th-Acre", 
                            "bsadead1new"="1/4th-Acre", 
                            "bsadead5new"="1/2th-Acre",
                            "bsadead15new"="1-Acre", 
                            "bsadead25new"="1.5-Acre"),
         estimate = round(estimate,3), # Round to 3 decimals 
         std.error = round(std.error,3), # Round to 3 decimals 
         statistic = round(statistic,3), # Round to 3 decimals 
         p.value = round(p.value,3), # Round to 3 decimals 
         VIF = round(VIF,3)) |>  # Round to 3 decimals 
kable() # Create table 
```

### LOOP 3 STATISTICS 
```{r}
colnames(vif_loop)[2] <- "term" # Rename column to term 
loop_stat <- lm_loop |> 
  dplyr::inner_join(vif_loop, # Inner join summary statistics with VIF 
                    by = c("loop", "term")) |>  # Inner join by term and loop 
  dplyr::filter(loop == "loop3") # Filter loop 3 
colnames(loop_stat)[2] <- "predictor" # Reassign name of term to predictor 

loop_stat |> 
  mutate(predictor = dplyr::recode(predictor, # Rename the predictor to correct names 
                            "BA_Inf_20th" = "1/20th-Acre", 
                            "bsadead1new"="1/4th-Acre", 
                            "bsadead5new"="1/2th-Acre",
                            "bsadead15new"="1-Acre", 
                            "bsadead25new"="1.5-Acre"),
         estimate = round(estimate,3), # Round to 3 decimals 
         std.error = round(std.error,3), # Round to 3 decimals 
         statistic = round(statistic,3), # Round to 3 decimals 
         p.value = round(p.value,3), # Round to 3 decimals 
         VIF = round(VIF,3)) |>  # Round to 3 decimals 
kable() # Create table 
```

### LOOP 4 STATISTICS 
```{r}
colnames(vif_loop)[2] <- "term" # Rename column to term 
loop_stat <- lm_loop |> 
  dplyr::inner_join(vif_loop, # Inner join summary statistics with VIF 
                    by = c("loop", "term")) |>  # Inner join by term and loop 
  dplyr::filter(loop == "loop4") # Filter loop 4
colnames(loop_stat)[2] <- "predictor" # Reassign name of term to predictor 

loop_stat |> 
  mutate(predictor = dplyr::recode(predictor, # Rename the predictor to correct names 
                            "BA_Inf_20th" = "1/20th-Acre", 
                            "bsadead1new"="1/4th-Acre", 
                            "bsadead5new"="1/2th-Acre",
                            "bsadead15new"="1-Acre", 
                            "bsadead25new"="1.5-Acre"),
         estimate = round(estimate,3), # Round to 3 decimals 
         std.error = round(std.error,3), # Round to 3 decimals 
         statistic = round(statistic,3), # Round to 3 decimals 
         p.value = round(p.value,3), # Round to 3 decimals 
         VIF = round(VIF,3)) |>  # Round to 3 decimals 
kable() # Create table 
```

### LOOP 5 STATISTICS 
```{r}
colnames(vif_loop)[2] <- "term" # Rename column to term 
loop_stat <- lm_loop |> 
  dplyr::filter(loop == "loop5") # Filter loop 5
colnames(loop_stat)[2] <- "predictor" # Reassign name of term to predictor 

loop_stat |> 
  mutate(predictor = dplyr::recode(predictor, # Rename the predictor to correct names 
                            "BA_Inf_20th" = "1/20th-Acre", 
                            "bsadead1new"="1/4th-Acre", 
                            "bsadead5new"="1/2th-Acre",
                            "bsadead15new"="1-Acre", 
                            "bsadead25new"="1.5-Acre"),
         estimate = round(estimate,3), # Round to 3 decimals 
         std.error = round(std.error,3), # Round to 3 decimals 
         statistic = round(statistic,3), # Round to 3 decimals 
         p.value = round(p.value,3)) |>  # Round to 3 decimals 
kable() # Create table 
```

### GOODNESS-OF-FIT STATISTICS FOR EACH LOOP 
```{r}

### LOG LIKELIHOOD TEST ### 
# LOG LIKELIHOOD TEST FOR REMOVAL OF ONE PREDICTOR COMPARED TO THE PREVIOUS MODEL 
for (i in 1:4){  
    glance_loop$chisq[1] <- NA # If last loop, set as NA 
  glance_loop$p_value[1] <- NA  # If last loop, set as NA 
  nested <- glance_loop$logLik[i+1] # Log Likelihood for Nested Model 
  complex <- glance_loop$logLik[i] # Log Likelihood for Complex Model 
  glance_loop$chisq[i+1] <- (-2 * (nested-complex)) # 2 Log Likelihood 
  glance_loop$p_value[i+1] <- pchisq(glance_loop$chisq[i+1], df = 1, lower.tail = FALSE) # Calculate p-value for Log Likelihood Test 
} 

glance_loop$loop <- c("loop 1", "loop 2", "loop 3", "loop 4", "loop 5") # Label the loop 
glance_loop |> 
  mutate(chisq = round(chisq,3), # Round to 3 decimal points 
         p_value = round(p_value, 3)) |>  # Round to 3 decimal points 
  as.data.frame() |> # Covert to data frame 
  t() |> # Transpose data frame 
  as.data.frame() |> # Covert to data frame 
  row_to_colnames(11) |> # Convert loop to column names 
  kable() # Table for fit statistics 

```

Row {data-height=400, data-width = 1000}
-----------------------------------------------------------------------
### FIT STATISTICS FOR EACH LOOP
```{r, fig.width=10}
cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") # Use colorblind friendly colors 
glance_loop$var_num <- c(5:1) # Create List of number of variables 
  
glance_loop |> 
  pivot_longer(cols = c("AIC", "BIC", "logLik", "p_value"), # Pivot longer fit statistics 
               names_to = "Statistic", # Name of statistics  
               values_to = "values") |> # Values of statistics
  ggplot(aes(x = var_num, # Set x-axis as number of variables 
             y = values,  # Set y-axis as statistics values 
             group = Statistic, # Group by Statistic
             col = Statistic)) + # Color by Statistic
  geom_line(alpha = 0.5, # Set alpha to 0.5 
            lwd = 1.2) + # Set line width to 1.2 
  geom_point(size = 3) + # Set point size to 3 
  theme_bw() + # Set theme to black and white 
  facet_wrap(vars(Statistic), scales ="free") + # Facet wrap on Statistic with free scales 
  scale_color_manual(values=cbPalette,  # Set colors to colorblind friendly colors 
                      labels =  c("AIC", # Label statistics in legend 
                                  "BIC", 
                                  "Log Likelihood",
                                  "p-value ")) +
  xlab("Number of Variables in Each Loop") +  # Title of of x-axis
  ylab("Value of Fit Statistic") + # Title of y-axis
    theme_bw(base_size = 12) + # Set theme to black and white 
  theme(legend.title = element_text(face = "bold")) # Change legend to bold 
 

  


```

MODEL 2 & 3
================================================================================

Column 
----------------------------------------------------------------------

### MODEL 2 & 3 :: LASSO AND RANDOM FOREST REGRESSION 

Models 2 and 3 use the same predictors to predict the minimum linear distance to the nearest brood tree. However, model two is a Lasso regression, while model three is a random forest regression. Unlike model one, both models were trained and tested on a 75/25 split. Summary statistics were reported for the cross-validation, indicating how well the models fit the test data.

```{r}
# Download data 
data <- readxl::read_excel("/Users/stephenvieno/Projects/hgen-612/data/Data_1993.xlsx") |> na.omit() # Remove NAs 
data <- data |> 
  dplyr::select(DeadDist, # Select outcome and predictor variables 
                "BA_Inf_20th", 
                "BA_Infest_1/4th", 
                "BA_Infest_1/2th", 
                "BA_Infest_1", 
                "BA_Infest_1.5") 

names(data)[2:6] <- c("BA0.05", "BA0.25", "BA0.5", "BA1", "BA1.5") # Rename variables 

```

```{r}

set.seed(4595) # Set seed 
data_split <- initial_split(data, strata = "DeadDist",# Split data 
                            prop = 0.75) # Split data with 0.75/0.25 split 

data_train <- training(data_split) # Training data 
data_test  <- testing(data_split) # Testing data 

### LASSO MODEL ### REGULARIZATION REGRESSION ###
norm_recipe <- recipe(DeadDist ~ .,  # Set recipe with outcome and predictors 
                      data = data_train ) |> # Use training data 
  step_center(all_predictors()) |> # Center all predictors 
  step_scale(all_predictors()) |> # Scale all predictors 
  step_log(DeadDist, base = 10) |> # Log transform DeadDist 
  prep(training = data_train, # Prep with training data 
       retain = TRUE) 

glmn_fit <- linear_reg(penalty = 0.001, # Set regularization penalty 
                       mixture = 1) |>  # Set as Lasso Model 
  set_engine("glmnet") |>  # Set engine as glmnet 
  fit(DeadDist ~ ., # Fit with outcome and all predictors 
      data = bake(norm_recipe, new_data = NULL)) # Bake with training data 

# First, get the processed version of the test set predictors:
test_normalized <- bake(norm_recipe, # Use normalized (centered and scaled) recipe  
                        new_data = data_test, # Bake with test data 
                        all_predictors()) # Use all predictors 

test_results_glmn_fit <- predict(glmn_fit, # Used glmnet model to predict the 
                                 new_data = test_normalized) |> # Predict with normalized test data 
                         rename(glmnet = .pred) |> # Rename prediction with glmnet 
                         bind_cols(data_test) # Bind predictions with testing data 

```

```{r, echo = FALSE, warning=FALSE, include=FALSE}
### RANDOM FORREST MODEL ###
set.seed(123) # Set seed 
rf_defaults <- rand_forest(mode = "regression") # Set model as random forest regression model 

predictors <- data |>  
  dplyr::select(-DeadDist) |> # Remove DeadDist 
  colnames() # Get predictors names from column names 

rf_defaults <- 
  rand_forest(mode = "regression", # Regression model 
              mtry = 3, # Number of predictors that will be sampled 
              trees = 1000) |> # Number of trees 
  set_engine("randomForest") |>  # Set engine as Random Forest 
  fit_xy(
    x = data_train[, predictors], # Used predictors from training data 
    y = log10(data_train$DeadDist)) # Log Transform DeadDist 

test_results <- data_test |> 
  dplyr::select(DeadDist) |> # Select DeadDist from test data 
  mutate(DeadDist = log10(DeadDist)) |> # Log Transform DeadDist 
  bind_cols(predict(rf_defaults, # Bind prediction to actual values 
                    new_data = model.matrix(~., data_test))) # Create predictions with test data 
```

Row {data-height=1000, data-width = 500} 
-------------------------------------


```{r}

glmnet <- metrics(test_results_glmn_fit, truth = DeadDist, estimate = glmnet)  # Use prediction from glmnet
          
forest <- metrics(test_results, truth = DeadDist, estimate = .pred) # Use prediction from random forest
  

table_fit <- bind_rows(glmnet,forest) |> # Bind together prediction fit statistics 
  dplyr::select(.metric, .estimate) |> # Select columns of interest 
  setNames(c("Statistic", "Estimate")) |> # Rename columns 
  mutate(Estimate= round(Estimate, 3), # Round statistics 
         Model = c(rep("Glmnet", 3), rep("Forest", 3))) |> # Create labels for model type 
  pivot_wider(names_from = Statistic, values_from = Estimate) |> # Pivot wider 
  column_to_rownames("Model") # Model to rownames 

```

### PLOTS OF ACTUAL VERSUS FITTED VALUES 
```{r, echo=FALSE, fig.width=10, fig.height=7}

test_results <- 
  test_results |> 
  rename("random forest" = .pred) |> # Rename prediction from random forest 
  bind_cols(test_results_glmn_fit) # Bind to other prediction 
colnames(test_results)[1] <- "DeadDist_Log10" # Rename variables 
colnames(test_results)[2] <- "forest" # Rename variables 
colnames(test_results)[4] <- "DeadDist" # Rename variables 

test_results |> 
  mutate(forest = 10^forest, # Log Transform predictions
         glmnet = 10^glmnet) |> # Log Transform predictions 
  dplyr::select(-DeadDist_Log10) |> # Remove Log to actual values 
  pivot_longer(cols = forest:glmnet, # Pivot longer model predictions 
               names_to = "model", # Names to model type 
               values_to = "prediction") |> # Values to prediction values 
  ggplot(aes(x = prediction, # Plot predictions versus actual values 
             y = DeadDist)) + 
  geom_point(alpha = 0.4) + # Set alpha to 0.4 
  geom_abline(col = "#56B4E9", # Set color to colorblind friendly colors 
              lty = 2, # Set line to dashed line 
              size =1) + # Set line size to 1 
  facet_wrap(~model, scales = "free") + # Facet wrap based on model 
  ylab("Actual minimum linear distance to nearest brood tree") + # Title y-axis 
  xlab("Predicted minimum linear distance to nearest brood tree") + # Tile x-axis 
  ggtitle("Scatter Plot of Actual versus Predicted Values for Random Forest and Lasso Models") + # Title 
  theme_bw() # Set black and white theme 
  

```

Column 
-------------------------------------

### GOODNESS-OF-FIT METRICS FOR LASSO AND RANDOM FOREST MODELS

```{r}
knitr::kable(table_fit) # Create table 
```

### R-Squared for Lasso Regression

```{r}
library(flexdashboard)
rate <- as.vector(glmnet[2,3]) # R-Squared for Lasso Model 
gauge(rate$.estimate * 100, # R-Squared for Lasso Model 
      min = 0, # Minimum of 0
      max = 100, # Maximum of 100
      symbol = '%', # Percentage symbol 
      gaugeSectors(success = c(80, 100), # Set success, warning, danger thresholds 
                   warning = c(40, 79), 
                   danger = c(0, 39)))
```

### R-Squared for Random Forest 

```{r}
rate <- as.vector(forest[2,3]) # R-Squared for Random Forest 
gauge(rate$.estimate * 100,  # R-Squared for Random Forest 
      min = 0, # Minimum of 0
      max = 100,  # Maximum of 100
      symbol = '%',  # Percentage symbol 
      gaugeSectors(success = c(80, 100),  # Set success, warning, danger thresholds 
                   warning = c(40, 79), 
                   danger = c(0, 39)))
```