LASSO Model

Column

Clusters of Jeffrey Pine Beetle (JPB) Infestation in Lake Tahoe Basin in 1993

Column

Model Performance: R-squared (\(R^2\))

A LASSO model predicting an index tree’s linear distance from its nearest JBP-infested tree.

Predictors Retained by the LASSO Model (out of 15 Variables)

Relationship with the Predictors

Column

Bivariate relationship with each of the Model-selected Predictors

Classifying by Distance

Column

Clusters of Trees with Proximity to other Jeffrey Pine Beetle (JPB)-Infested Trees

Column

Model Performance: Area Under the ROC Curve (AUC)

A Random-Forest Model predicting whether a tree is near another JBP-infested tree, i.e., within the 25% quantile of the linear distance from the nearest infested tree.

Model Performance: Accuracy

Classification by the Random-Forest Model


  • No False Positives.


True Class Predicted Class N Result
Near JBP-Attacked Tree Near JBP-Attacked Tree 592 True Positive
Near JBP-Attacked Tree Distant from JBP-Attacked Tree 22 False Negative
Distant from JBP-Attacked Tree Distant from JBP-Attacked Tree 1808 True Negative

Data Overview

Variables Used in the Models

Data summary
Name Piped data
Number of rows 9687
Number of columns 18
_______________________
Column type frequency:
factor 2
numeric 16
________________________
Group variables None

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
Response 0 1 FALSE 2 Ali: 8452, JPB: 1235
NearBrood 0 1 FALSE 2 Dis: 7265, Nea: 2422

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
DeadDist 0 1 24.08 18.84 0.11 9.52 19.84 34.72 124.95 ▇▃▁▁▁
TreeDiam 0 1 13.11 6.05 3.00 9.00 12.00 16.00 78.00 ▇▂▁▁▁
Infest_Serv1 0 1 10.70 18.26 1.00 1.00 3.00 11.00 85.00 ▇▁▁▁▁
Infest_Serv2 0 1 10.93 21.47 0.14 0.55 1.58 10.65 92.40 ▇▁▁▁▁
SDI_20th 0 1 26.35 12.72 0.33 17.20 25.59 34.45 92.86 ▅▇▂▁▁
Neigh_SDI_1/4th 0 1 104.60 40.54 4.04 75.51 107.09 133.26 221.05 ▂▅▇▅▁
BA_20th 0 1 16.54 8.01 0.14 10.97 16.00 21.43 58.38 ▅▇▂▁▁
Neigh_1/4th 0 1 66.17 25.02 1.95 49.24 66.61 83.13 140.46 ▂▅▇▃▁
Neigh_1/2th 0 1 122.79 40.98 4.54 97.55 125.02 150.25 230.42 ▁▃▇▆▁
Neigh_1 0 1 228.42 67.85 13.03 188.71 236.04 273.86 391.42 ▁▃▇▇▂
Neigh_1.5 0 1 328.16 93.54 22.24 270.60 338.88 393.72 541.62 ▁▂▆▇▂
BA_Inf_20th 0 1 0.60 1.91 0.00 0.00 0.00 0.00 21.88 ▇▁▁▁▁
BA_Infest_1/4th 0 1 2.69 6.04 0.00 0.00 0.00 2.63 72.30 ▇▁▁▁▁
BA_Infest_1/2th 0 1 5.26 9.77 0.00 0.00 0.89 7.11 92.40 ▇▁▁▁▁
BA_Infest_1 0 1 10.30 16.49 0.00 0.27 3.13 12.78 106.67 ▇▁▁▁▁
BA_Infest_1.5 0 1 15.05 22.09 0.00 1.37 7.75 17.07 135.70 ▇▁▁▁▁
---
title: "Predicting the Distance from a Jeffrey Pine Beetle-Infested Tree."
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    theme: sandstone
    logo: JPBlogo.jpg
    source_code: embed
    
---


```{r setup, include=FALSE}
library(tidyverse)
library(tidymodels)
library(readxl)
library(performance)
library(flexdashboard)
library(vip)
library(RColorBrewer)
library(patchwork)
library(ggthemes)
```


```{r data}
setwd("~/HGEN/2022-1 Spring/612 - Data Science II/hgen-612/assignments/project-1")
pine_dat <- read_excel("Data/Data_1993.xlsx", sheet = 1)

# Variable Transformation
pine_dat <- pine_dat %>% 
  mutate(Response = fct_recode(as_factor(Response),
                               "Alive" = "0", "JPB-attacked" = "1"),
         NearBrood = ifelse(DeadDist <= quantile(DeadDist, probs = 0.25), 1, 0),
         NearBrood = fct_recode(as_factor(NearBrood),
                                "Distant from JBP-Attacked Tree" = "0",
                                "Near JBP-Attacked Tree" = "1"),
         NearBrood = fct_relevel(NearBrood, "Near JBP-Attacked Tree"))


# Variables
pine_tbl <- pine_dat %>%
  select(Easting, Northing,
         Response, DeadDist, NearBrood, TreeDiam, Infest_Serv1, 
         Infest_Serv2, SDI_20th, `Neigh_SDI_1/4th`, BA_20th,
         `Neigh_1/4th`, `Neigh_1/2th`, Neigh_1, Neigh_1.5, BA_Inf_20th,
         `BA_Infest_1/4th`, `BA_Infest_1/2th`, BA_Infest_1, BA_Infest_1.5) 

```


LASSO Model {data-navmenu="Predicting the Distance"}
===================================== 


Column {data-width=550}
-----------------------------------------------------------------------

### Clusters of Jeffrey Pine Beetle (JPB) Infestation in Lake Tahoe Basin in 1993

```{r jpb_plot, out.width='100%'}
ggplot(pine_tbl, aes(x = Easting, y = Northing)) +
  geom_point(aes(color = Response, alpha = Response)) +
  scale_alpha_discrete(range = c(0.25, 1), guide="none") +
  scale_color_brewer("", palette = "Dark2") +
  theme_minimal() + xlab("UTM X") + ylab("UTM Y")+
  theme(legend.position = c(0,0.95), legend.text=element_text(size=10))
```


Column {data-width=450}
-----------------------------------------------------------------------

### Model Performance: R-squared ($R^2$)

A LASSO model predicting an index tree's linear distance from its nearest JBP-infested tree.

```{r lasso}
# Predicting the linear distance from the near infected tree

# Create training/testing data
set.seed(1234)
pine_split <- initial_split(pine_tbl)
pine_train <- training(pine_split)
pine_test  <- testing(pine_split)

# bootstrap samples for resampling and tuning the penalty parameter
pine_boot <- bootstraps(pine_train)

# a grid of tuning parameters
lambda_grid <- grid_regular(penalty(), levels = 50)

lasso_mod <- linear_reg(mixture = 1, penalty = tune()) %>% 
  set_engine("glmnet")

# recipe
pine_rec <- pine_train %>% 
  recipe(DeadDist ~ TreeDiam + Infest_Serv1 + Infest_Serv2 + SDI_20th + 
           `Neigh_SDI_1/4th` + BA_20th + `Neigh_1/4th` + `Neigh_1/2th` + 
           Neigh_1 + Neigh_1.5 + BA_Inf_20th + `BA_Infest_1/4th` +
           `BA_Infest_1/2th` + BA_Infest_1 + BA_Infest_1.5) %>% 
  step_corr(all_predictors()) %>% 
  step_normalize(all_numeric()) %>% 
  step_zv(all_numeric(), -all_outcomes()) 

# workflow
pine_lasso_wflow <- 
  workflow() %>% 
  add_model(lasso_mod) %>% 
  add_recipe(pine_rec)

# penalty parameter

set.seed(2020)
lasso_grid <- tune_grid(
  pine_lasso_wflow,
  resamples = pine_boot,
  grid = lambda_grid
)

min_rmse <- lasso_grid %>%
  select_best("rmse")

# update the final model with lowest rmse
final_lasso <- finalize_workflow(
  pine_lasso_wflow,
  min_rmse
)

# R-sq

lasso_rsq <- last_fit(
  final_lasso,
  pine_split
) %>%
  collect_metrics() %>% 
  filter(.metric == "rsq") %>% 
  select(.estimate)

gauge(round(as.numeric(lasso_rsq*100),2), min = 0, max = 100, symbol = "%")


```

### Predictors Retained by the LASSO Model (out of 15 Variables)


```{r vip}
final_lasso %>%
  fit(pine_train) %>%
  extract_fit_parsnip() %>%
  vi(lambda = min_rmse$penalty) %>%
  mutate(
    Importance = abs(Importance),
    Variable = fct_reorder(Variable, Importance),
    Sign = fct_relevel(Sign, "POS")
  ) %>%
  ggplot(aes(x = Importance, y = Variable, fill = Sign)) +
  geom_col() +
  scale_x_continuous(expand = c(0, 0)) +
  scale_fill_brewer(palette = "Dark2") +
  labs(title = "", y = NULL) +
  theme_minimal(15) + 
  theme(legend.position = c(1,0),
        legend.justification = c(1,0))

```



Relationship with the Predictors {data-navmenu="Predicting the Distance"}
===================================== 


Column {data-width=1000}
-----------------------------------------------------------------------

### Bivariate relationship with each of the Model-selected Predictors

```{r predictors, out.width='120%'}

pred1 <- ggplot(pine_tbl, aes(Neigh_1.5, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Basal Area of All Trees Within 1.5 Acre") +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred2 <- ggplot(pine_tbl, aes(BA_Infest_1.5, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Basal Area of Infested Trees Within 1.5 Acre")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred3 <- ggplot(pine_tbl, aes(`BA_Infest_1/4th`, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Basal Area of Infested Trees Within 1/4th Acre")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred4 <- ggplot(pine_tbl, aes(`Neigh_SDI_1/4th`, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Stand Density Index Within 1/4th Acre")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred5 <- ggplot(pine_tbl, aes(BA_Inf_20th, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Basal Area of Infested Trees Within 1/20th Acre")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred6 <- ggplot(pine_tbl, aes(Infest_Serv1, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Infestation Severity Nearest to Index Tree")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred7 <- ggplot(pine_tbl, aes(BA_20th, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Basal Area of All Trees Within 1/20th Acre")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

pred8 <- ggplot(pine_tbl, aes(TreeDiam, DeadDist)) +
  geom_smooth(method = "gam", color = "forestgreen") +
  labs(title = "Tree Diameter/Size")  +
  theme_minimal(10) +
  theme(plot.title.position = "plot",
        plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_text(size = 8))

(pred1 + pred2 + pred3 + pred4) / (pred5 + pred6 + pred7 + pred8)

```




Classifying by Distance
===================================== 


Column {data-width=550}
-----------------------------------------------------------------------

### Clusters of Trees with Proximity to other Jeffrey Pine Beetle (JPB)-Infested Trees

```{r NearBrood, out.width='100%'}

ggplot(pine_tbl, aes(x = Easting, y = Northing)) +
  geom_point(aes(color = NearBrood, alpha = NearBrood)) +
  scale_alpha_discrete(range = c(0.5, 1), guide="none") +
  scale_color_brewer("", palette = "Reds") +
  labs(caption = "'Near' = Within the 25% Quantile of the Linear Distance\nFrom the Nearest JBP-Attacked Tree in this Sample.") +
  theme_minimal() + xlab("UTM X") + ylab("UTM Y") +
  theme(legend.position = c(0.1,0.95), legend.text=element_text(size=10))

```


Column {data-width=450}
-----------------------------------------------------------------------

### Model Performance: Area Under the ROC Curve (AUC)

A Random-Forest Model predicting whether a tree is near another JBP-infested tree, i.e., within the 25% quantile of the linear distance from the nearest infested tree.

```{r randfor}
# Predicting whether a tree is near or distant from an infested tree.
# Near = within 25% quantile of the distance from the nearest infested tree.

# recipe
dichot_recipe <- pine_train %>%
  recipe(NearBrood ~ TreeDiam + Infest_Serv1 + Infest_Serv2 + SDI_20th + 
           `Neigh_SDI_1/4th` + BA_20th + `Neigh_1/4th` + `Neigh_1/2th` + 
           Neigh_1 + Neigh_1.5 + BA_Inf_20th + `BA_Infest_1/4th` +
           `BA_Infest_1/2th` + BA_Infest_1 + BA_Infest_1.5) %>% 
  step_corr(all_predictors()) %>% 
  step_normalize(all_numeric()) %>% 
  step_zv(all_numeric(), -all_outcomes()) %>%
  prep()

# model
rf_mod <- rand_forest(trees = 100, mode = "classification") %>%
  set_engine("ranger")

# workflow

pine_rf_wflow <- 
  workflow() %>% 
  add_model(rf_mod) %>% 
  add_recipe(dichot_recipe)

# train
pine_rf <- pine_rf_wflow %>% 
  fit(pine_test)

# test
pine_rf_auc <- predict(pine_rf, pine_test, type = "prob") %>%
  bind_cols(predict(pine_rf, pine_test)) %>%
  bind_cols(select(pine_test, NearBrood)) %>%
  metrics(truth = NearBrood, `.pred_Near JBP-Attacked Tree`, 
          estimate = .pred_class) %>% 
  filter(.metric == "roc_auc") %>% 
  select(.estimate)

gauge(round(as.numeric(pine_rf_auc*100),3), min = 0, max = 100, symbol = "%")


```


### Model Performance: Accuracy

```{r accuracy}
pine_rf_acc <- pine_rf %>% 
  predict(pine_test) %>%
  bind_cols(pine_test) %>%
  metrics(truth = NearBrood, estimate = .pred_class) %>% 
  filter(.metric == "accuracy") %>% 
  select(.estimate)

gauge(round(as.numeric(pine_rf_acc*100),3), min = 0, max = 100, symbol = "%")
```


### Classification by the Random-Forest Model


- ***No False Positives.***
```{r RFtable} predict(pine_rf, pine_test) %>% bind_cols(pine_test) %>% count(NearBrood, .pred_class) %>% rename(`True Class` = NearBrood, `Predicted Class` = .pred_class, N = n) %>% mutate(Result = c("True Positive","False Negative","True Negative")) %>% knitr::kable(align = 'llrr') ``` Data Overview ===================================== ### Variables Used in the Models ```{r skim, out.width='100%'} pine_tbl %>% select(-c(Easting, Northing)) %>% skimr::skim() ```