---
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()
```