LASSO Model

Column

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

Column

Model Performance: Area Under the ROC Curve (AUC)

Top Predictors

Variable Overview

Row

Variables

Data summary
Name Piped data
Number of rows 9687
Number of columns 17
_______________________
Column type frequency:
factor 1
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

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Easting 0 1 766133.65 111.82 765805.15 766053.49 766138.97 766231.51 766328.96 ▁▃▇▇▇
Northing 0 1 4334196.58 167.38 4333797.12 4334068.02 4334214.16 4334345.06 4334487.31 ▂▅▇▇▇
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 ▇▁▁▁▁
SDI_20th 0 1 26.35 12.72 0.33 17.20 25.59 34.45 92.86 ▅▇▂▁▁
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 ▇▁▁▁▁

Top Predictors

Row

Relationship Among the Top Predictors in the Model

---
title: "Predicting Lake Tahoe Basin Jeffrey Pine Beetle Outbreak"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    theme: journal
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(tidymodels)
library(corrr)
library(readxl)
library(car)
library(performance)
library(vip)
library(RColorBrewer)
library(GGally)
```

```{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)
pine_dat <- pine_dat %>% 
  mutate(Response = fct_recode(as_factor(Response),
                               "Alive" = "0", "JPB-attacked" = "1"))
# Variables 
pine_tbl <- pine_dat %>%
  select(Easting, Northing,
         Response, DeadDist, TreeDiam, Infest_Serv1, SDI_20th, 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
===================================== 

Column {data-width=600}
-----------------------------------------------------------------------

### 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.5, 1), guide="none")+
  scale_color_manual("",values=c("darkseagreen3", "brown")) + 
  theme_light() + xlab("UTM X") + ylab("UTM Y")+
  theme(legend.position = c(0,0.95), legend.text=element_text(size=12))
```

Column {data-width=400}
-----------------------------------------------------------------------

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

```{r Lasso}
# Create training/testing data
set.seed(1234)
pine_split <- initial_split(pine_tbl)
pine_train <- training(pine_split)
pine_test <- testing(pine_split)

pine_rec <- pine_train %>% 
  recipe(Response ~ DeadDist + TreeDiam + Infest_Serv1 + SDI_20th + 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()) 

pine_boot <- bootstraps(pine_train)

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

# model engine
lasso_mod <-
  logistic_reg(mixture = 1, penalty = tune()) %>% 
  set_engine("glmnet")

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

lasso_grid <- tune_grid(
  pine_lasso_wflow,
  resamples = pine_boot,
  grid = lambda_grid
)

# select the penalty with highest AUC
max_auc <- lasso_grid %>%
  select_best("roc_auc")

# update final model with highest AUC
final_lasso <- finalize_workflow(
  pine_lasso_wflow,
  max_auc
)

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

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

```

### Top Predictors

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

Variable Overview {data-orientation=rows}
===================================== 

Row {data-height=1000}
-----------------------------------------------------------------------

### Variables

```{r skim, out.width='100%'}
pine_tbl %>% 
  skimr::skim()
```


Top Predictors {data-orientation=rows}
===================================== 

Row {data-height=1000}
-----------------------------------------------------------------------

### Relationship Among the Top Predictors in the Model

```{r ggpairs, out.width='100%'}
pine_tbl %>% 
  select(DeadDist, SDI_20th, BA_Infest_1.5, TreeDiam, Neigh_1.5, Infest_Serv1) %>% 
  ggpairs(diag = list(continuous ="barDiag", discrete = "barDiag", na = "naDiag")) +
  ggthemes::theme_few()

```