Read Me

Row

What are Pine Beetles? And Why Are You Reading This?

Read me before proceeding to the analysis!

Pine Beetles are insects that can cause devastation to forests by eating into tree bark and upsetting the flow of water and nutrients to infected trees. Pine Beetles cause economic and ecological harm, so it’s important to figure out how to protect forests from them.

There are five categories of variables shown on the Variable Chart page, which is accessed through the tabs at the top of this page: Tree diameter, infestation severity, nearest brood tree, forest density, and beetle population pressure.

Knowing how these variables relate to each other can help foresters figure out how to protect forests. For example, in this analysis, I will make two models for predicting the minimum linear distance to the nearest brood tree, or DeadDist. In other words, how far away from an infected tree can a given healthy tree be to be considered “safe”? This is important for foresters so that they don’t clear trees unnecessarily and know when to expect tree death.

Pine Beetle characteristics that might be relevant to our analysis

  • Female Pine Beetles use pheromones to attract other Pine Beetles nearby when they are ready to lay eggs1
    • Pine Beetles will tend to attack as a group and infected trees will cause more infection around them (a reason to include infestation severity and nearest brood tree variables)

  • Pine Beetles infestations in forests are usually directional1

  • Most Pine Beetles like attacking bigger trees (>10in), but when there is a lot of competition for trees, they will attack smaller trees2
    • May be wise to include TreeDiam as a variable!

  • Pine Beetles usually fly 300 feet or less at a time
    • 300 feet = ~.007 acres, which means that we will probably not want to use any of the beetle population pressure measures of over the smallest variable measure (1/20 acres = .05 acres)2

Row

Sources

  1. Basic Pine Beetle info
  2. Pine Beetle FAQ USDA
  3. Stand Density Index info

    Credit for a majority of the code used in these analyses comes from Dr. Timothy York from Virginia Commonwealth University

Data Exploration

Row

Data table, explore how the raw data looks!

Row

Things to Note

The panel to the left is a scatterplot matrix showing the relationships between variables.

IND_BA_Infest_20th is a variable that shows whether a given tree is or is not within 1/20th acres of an infested tree. Variables like DeadDist or Infest_Serv1 also relate to distance and Pine Beetle infestation. As such, I was interested to see whether dividing the each ggpair correlation into either Yes (an infested tree is within 1/20th acres) or No (an infested tree is not within 1/20th acres) to observe whether there was an observable difference between Yes and No. 

It looks like for single-variable distributions (e.g. TreeDiam by TreeDiam), the groups of points divided by IND_BA_Infest_20th varied in whether they were observably different or not. The TreeDiam plot did not, which makes sense: why would the presence of a Pine Beetle within 1/20th acres affect the diameter of an already grown tree? DeadDist, another variable, has two very distinct groups for Yes and No, which also makes sense as the closer to an infested tree a given tree is, the more likely it is within 1/20th acres of that given tree.

Ridge Model

Row

Ridge Model Evaluation

One of the two models I used to find how far away an infected tree can be from a given healthy tree be to be considered “safe” was a ridge regression model. This kind of model is good for when there are numerous predictors, like in this model. for this model I use DeadDist_sqrt (I took the square root of DeadDist to make the distribution more normal) as the outcome variable and TreeDiam, Infest_Serv1, SDI_20th, and IND_BA_Infest_20th as the predictor variables. In a ridge regression the coefficients of each predictor variable is optimized to create an accurate model, so some coefficients may be near zero if they are not very relevant to the outcome variable, or a coefficient may be much more than another variable’s coefficient if it is very influential to the outcome variable.

This model’s RMSE is 1.3785935, on which is a means that the average difference between this model’s predicted values and the actual values in the dataset is 1.3785935. To put that into the context of the scale of the DeadDist_sqrt, the total range of values in DeadDist_sqrt is 0.3277936 to 11.1783.

The graph below was made with predicted values from the model and the actual values from the testing data. There is a strange split at 3 where the data divides into two groups: one that seems slightly diagonal where the trees are nearer to an infested tree, and one horizontal, where the nearest infested tree is not within 1/20th acres. I know that a predicted vs actual graph for a regression model is supposed to be diagonal, however I tried using different evaluations and the base r function to no better of a graph. Additionally, the adjusted r squared seems good. I cannot see where the model is incorrect with my current time and skill, though I know it is somewhere. I am sorry about this but I have the code and know how to evaluate it, so I am proceeding with what I have.

Row

RMSE

Actual vs Predicted Values

Random Forest Model

Row

Random Forest Evaluation

The actual vs predicted graph here seems more diagonal, but is still split into two groups.

A random forest model uses a decision tree to narrow down what parts of the data indicate well for the outcome variable values.

On the next page of this flexdashboard is a comparison of the two models’ metrics

Row

Actual vs Predicted Graph for Random Forest Model

Conclusion

Row

Explaination

Overall, the random forest model had a lower rmse and a higher rsq. Both are indicators of a more functional model, so I will say the random forest model is more accurate.

Row

Model Results in Graph Form

Model Results in Table Form

Variable Chart

Picture of variables

---
title: "Predicting the Minimum Linear Distance from a Given Pine Tree to the Nearest Tree that is Infected by Pine Beetles"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    source_code: embed
    theme: yeti
---

```{r setup, include=FALSE}
library(flexdashboard) # base for presentation
library(tidyverse)
library(readxl) # reading in data
library(GGally) # ggpairs
library(tidymodels)
library(DT) # datatable
library(RColorBrewer) # colors for ggplots
library(randomForest) # model type
library(plotly) # guage
```

Read Me
==============================================================================

Row {data-height=700}
-----------------------------------------------------------------------

### What are Pine Beetles? And Why Are You Reading This?

__*Read me before proceeding to the analysis!*__

Pine Beetles are insects that can cause devastation to forests by eating into tree bark and upsetting the flow of water and nutrients to infected trees. Pine Beetles cause economic and ecological harm, so it's important to figure out how to protect forests from them.
<br>
</br>
There are five categories of variables shown on the Variable Chart page, which is accessed through the tabs at the top of this page: Tree diameter, infestation severity, nearest brood tree, forest density, and beetle population pressure.
<br>
</br>
Knowing how these variables relate to each other can help foresters figure out how to protect forests. For example, in this analysis, I will make two models for predicting the minimum linear distance to the nearest brood tree, or DeadDist. In other words, how far away from an infected tree can a given healthy tree be to be considered "safe"? This is important for foresters so that they don't clear trees unnecessarily and know when to expect tree death.

### Pine Beetle characteristics that might be relevant to our analysis

* Female Pine Beetles use pheromones to attract other Pine Beetles nearby when they are ready to lay eggs<sup>1</sup>
  + Pine Beetles will tend to attack as a group and infected trees will cause more infection around them (a reason to include infestation severity and nearest brood tree variables)
<br>
</br>
* Pine Beetles infestations in forests are usually directional<sup>1</sup>
<br>
</br>
* Most Pine Beetles like attacking bigger trees (>10in), but when there is a lot of competition for trees, they will attack smaller trees<sup>2</sup>
  + May be wise to include TreeDiam as a variable!
<br>
</br>
* Pine Beetles usually fly 300 feet or less at a time
  + 300 feet = ~.007 acres, which means that we will probably not want to use any of the beetle population pressure measures of over the smallest variable measure (1/20 acres = .05 acres)<sup>2</sup>



Row {data-height=200}
-----------------------------------------------------------------------

### Sources
1. [Basic Pine Beetle info](https://www.treefarmsystem.org/SPBwhenbeetlesattack)
2. [Pine Beetle FAQ USDA](https://www.fs.usda.gov/Internet/FSE_DOCUMENTS/stelprdb5304956.pdf)
3. [Stand Density Index info](https://fennerschool-associated.anu.edu.au/mensuration/density.html)
<br>
</br>
_Credit for a majority of the code used in these analyses comes from Dr. Timothy York from Virginia Commonwealth University_





Data Exploration
==============================================================================

Row {data-height=300}
-----------------------------------------------------------------------

### Data table, explore how the raw data looks!

```{r readindata}
pine_tbl <- read_excel("../../hgen-612_temp/p1/data/Data_1993.xlsx", sheet = 1)
```

```{r makeshowcasebones}
pine_tbl %>% 
  dplyr::select(DeadDist, TreeDiam, Infest_Serv1, SDI_20th, BA_Inf_20th, IND_BA_Infest_20th) %>% 
  DT::datatable()

```


Row {data-height=450}
-----------------------------------------------------------------------

### Factors related to Pine Beetle infestation in forests as they relate to each other when a tree within 1/20th acres is infested vs not

```{r ggpairsIND}
pine_fact_tbl <- pine_tbl %>% 
  mutate(IND_BA_Infest_20th_fac = factor(IND_BA_Infest_20th,
                              levels = 0:1,
                              labels = c("No", "Yes")))

ggpairs(pine_fact_tbl, columns = c("DeadDist", 
                                   "TreeDiam", 
                                   "Infest_Serv1", 
                                   "SDI_20th"), ggplot2::aes(color = IND_BA_Infest_20th_fac)) +
          scale_fill_brewer(palette = "Dark2") +
   scale_color_brewer(palette = "Dark2")
```

### Factors related to Pine Beetle infestation in forests as they relate to each other in general

```{r ggpairsregular}
ggpairs(pine_fact_tbl, columns = c("DeadDist", 
                                   "TreeDiam", 
                                   "Infest_Serv1", 
                                   "SDI_20th",
                                   "IND_BA_Infest_20th"))
```

### Things to Note

The panel to the left is a scatterplot matrix showing the relationships between variables.
<br>
</br>
`IND_BA_Infest_20th` is a variable that shows whether a given tree _is_ or _is not_ within 1/20th acres of an infested tree. Variables like `DeadDist` or `Infest_Serv1` also relate to distance and Pine Beetle infestation. As such, I was interested to see whether dividing the each ggpair correlation into either Yes (an infested tree is within 1/20th acres) or No (an infested tree is not within 1/20th acres) to observe whether there was an observable difference between Yes and No. 
<br>
</br>
It looks like for single-variable distributions (e.g. `TreeDiam` by `TreeDiam`), the groups of points divided by `IND_BA_Infest_20th` varied in whether they were observably different or not. The `TreeDiam` plot did not, which makes sense: why would the presence of a Pine Beetle within 1/20th acres affect the diameter of an already grown tree? `DeadDist`, another variable, has two very distinct groups for Yes and No, which also makes sense as the closer to an infested tree a given tree is, the more likely it is within 1/20th acres of that given tree. 



Ridge Model
==============================================================================

Row {data-height=350}
-----------------------------------------------------------------------

### Ridge Model Evaluation

One of the two models I used to find how far away an infected tree can be from a given healthy tree be to be considered "safe" was a ridge regression model. This kind of model is good for when there are numerous predictors, like in this model. for this model I use `DeadDist_sqrt` (I took the square root of DeadDist to make the distribution more normal) as the outcome variable and `TreeDiam`, `Infest_Serv1`, `SDI_20th`, and `IND_BA_Infest_20th` as the predictor variables. In a ridge regression the coefficients of each predictor variable is optimized to create an accurate model, so some coefficients may be near zero if they are not very relevant to the outcome variable, or a coefficient may be much more than another variable's coefficient if it is very influential to the outcome variable.
<br>
</br>
This model's RMSE is 1.3785935, on which is a means that the average difference between this model's predicted values and the actual values in the dataset is 1.3785935. To put that into the context of the scale of the `DeadDist_sqrt`, the total range of values in `DeadDist_sqrt` is 0.3277936 to 11.1783. 
<br>
</br>
The graph below was made with predicted values from the model and the actual values from the testing data. There is a strange split at 3 where the data divides into two groups: one that seems slightly diagonal where the trees are nearer to an infested tree, and one horizontal, where the nearest infested tree is not within 1/20th acres. I know that a predicted vs actual graph for a regression model is supposed to be diagonal, however I tried using different evaluations and the base r function to no better of a graph. Additionally, the adjusted r squared seems good. I cannot see where the model is incorrect with my current time and skill, though I know it is somewhere. I am sorry about this but I have the code and know how to evaluate it, so I am proceeding with what I have.

```{r sqrtdeaddist}
# I tried using step_sqrt(all_outcomes()) to normalize curve, but it kept giving me an error because predict() does not take models that have outcomes() used in the recipe. As such, I am transforming the data ahead of time outside of the recipe to avoid this error

pine_tbl_sqrt <- pine_fact_tbl %>% 
  mutate(DeadDist_sqrt = sqrt(DeadDist))
  
# source: https://stackoverflow.com/questions/75762005/error-in-step-log-when-trying-to-make-predictions-with-my-model
```

```{r datasplit}
# training/testing data
pine_split <- initial_split(pine_tbl_sqrt)
pine_train <- training(pine_split)
pine_test <- testing(pine_split)
```


```{r ridgerecipe, include=FALSE}
ridge_recipe <- pine_train %>% 
  recipe(DeadDist_sqrt ~ TreeDiam + Infest_Serv1 +  SDI_20th + IND_BA_Infest_20th) %>%
  step_corr(all_predictors()) %>% # gets rid of variables to get rid of multicollinearity 
  step_zv(-all_outcomes()) %>% 
  step_scale(all_predictors(), -all_outcomes()) # gets rid of variables of all one value
```

```{r ridgelambda, include=FALSE}
# credit for code used to tune lambda goes to Professor York!
# bootstrap samples for re-sampling and tuning the penalty parameter
set.seed(1234)
ridge_boot <- bootstraps(pine_train) # re-sampling

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

```{r ridgemodelandengine, include = FALSE}
ridge_model <- linear_reg(mixture = 0, penalty = tune()) %>% 
  set_engine("glmnet")

ridge_model %>% 
  translate()
```

```{r ridgeworkflow, include=FALSE}
# workflow
ridge_wflow <- 
  workflow() %>% 
  add_model(ridge_model) %>% 
  add_recipe(ridge_recipe)
```

```{r ridgetuning, include=FALSE}
set.seed(9870) # why do I need to set a seed here?
ridge_grid <- tune_grid(
  ridge_wflow,
  resamples = ridge_boot, # rset object (re-sample object)
  grid = lambda_grid # the lambda list we are tuning to
)


# bootstrap results
ridge_grid %>%
  collect_metrics()
```

```{r rmsechart, include=FALSE}
ridge_grid %>% 
  collect_metrics() %>%
  ggplot(aes(penalty, mean, color = .metric)) +
  geom_errorbar(aes(
    ymin = mean - std_err,
    ymax = mean + std_err
  ),
  alpha = 0.5
  ) +
  geom_line(linewidth = 1.5) +
  facet_wrap(~.metric, scales = "free", nrow = 2) +
  scale_x_log10() +
  theme(legend.position = "none")

# rmse low and rsq high is more desireable

lowest_rmse <- ridge_grid %>% # the algorithm selects the best lambda
  select_best("rmse")

# update final model with lowest rmse
final_ridge <- finalize_workflow(
  ridge_wflow,
  lowest_rmse
)

```


Row {data-height=450}
-----------------------------------------------------------------------

```{r ridgefit, include=FALSE}
final_ridge %>% 
  fit(data = pine_train)

final_ridge %>% 
  fit(pine_train) %>%
  extract_fit_parsnip() %>% 
  tidy()

final_ridge %>% 
  extract_spec_parsnip()

last_fit(
  final_ridge,
  pine_split
  ) %>%
  collect_metrics()
```


### RMSE

```{r ridgermse, include = FALSE}
ridge_predicted <- final_ridge %>% 
  fit(data = pine_train) %>% 
  predict(pine_test) %>%
  bind_cols(pine_test) 

ridge_metrics <- ridge_predicted %>%
  metrics(truth = DeadDist_sqrt, estimate = .pred)
```

```{r ridgeguage}
plot_ly(
    domain = list(x = c(0, 1), y = c(0, 1)),
    value = 1.3785935,
    title = list(text = "RMSE"),
    type = "indicator",
    mode = "gauge+number",
    gauge = list(
      axis = list(range = list(0, max(pine_tbl_sqrt$DeadDist_sqrt) - min(pine_tbl_sqrt$DeadDist_sqrt)), tickwidth = 1, tickcolor = "yellow"),
      bar = list(color = "yellow")))
# https://plotly.com/r/gauge-charts/
```

### Actual vs Predicted Values
```{r actualvspredictedridge}
ridge_predicted %>% 
  ggplot(aes(x = DeadDist_sqrt, y = .pred, color = factor(IND_BA_Infest_20th,
                                                          levels = 0:1,
                                                          labels = c("No", "Yes")))) + 
  geom_point(alpha = 0.5) + 
  labs(y = "Predicted Minimum Distance to an Infected Tree (sqrt)", x = "Actual Minimum Distance to an Infected Tree", color = "Infested tree within 1/20th acres?") +
  coord_obs_pred() +
  scale_fill_brewer(palette = "Dark2") +
  scale_color_brewer(palette = "Dark2") 
```





Random Forest Model
==============================================================================

Row {data-height=350}
-----------------------------------------------------------------------

### Random Forest Evaluation

The actual vs predicted graph here seems more diagonal, but is still split into two groups. 
<br>
</br>
A random forest model uses a decision tree to narrow down what parts of the data indicate well for the outcome variable values.
<br>
</br>
On the next page of this flexdashboard is a comparison of the two models' metrics

```{r forestrecipe}
forest_recipe <- pine_train %>% 
  recipe(DeadDist_sqrt ~ TreeDiam + Infest_Serv1 +  SDI_20th + IND_BA_Infest_20th) %>% 
  step_corr(all_predictors()) %>% # gets rid of variables to get rid of multicollinearity 
  step_zv(all_numeric(), -all_outcomes()) %>% 
  step_scale(all_predictors(), -all_outcomes()) # gets rid of variables of all one value
```

```{r randomforestengine}
forest_model <-  rand_forest(trees = 100, mode = "regression") %>%
  set_engine("randomForest") 

```

```{r forestworkflow}
# workflow
forest_wflow <- 
  workflow() %>% 
  add_model(forest_model) %>% 
  add_recipe(forest_recipe)
```

```{r forestfit, include = FALSE}
forest_wflow %>% 
  fit(data = pine_train)

lm(DeadDist_sqrt ~ TreeDiam + Infest_Serv1 +  SDI_20th + IND_BA_Infest_20th, data = pine_tbl_sqrt) %>% 
  glance()
```

Row {data-height=450}
-----------------------------------------------------------------------

### Actual vs Predicted Graph for Random Forest Model
```{r forestrmse, include = FALSE}
forest_predicted <- forest_wflow %>% 
  fit(data = pine_train) %>% 
  predict(pine_test) %>%
  bind_cols(pine_test) 

forest_predicted  %>%
  metrics(truth = DeadDist_sqrt, estimate = .pred)
```

```{r actualvspredicted}
forest_predicted  %>%
  ggplot(aes(x = DeadDist_sqrt, y = .pred, color = factor(IND_BA_Infest_20th,
                                                          levels = 0:1,
                                                          labels = c("No", "Yes")))) + 
  geom_point(alpha = 0.5) + 
  labs(y = "Predicted Minimum Distance to an Infected Tree (sqrt)", x = "Actual Minimum Distance to an Infected Tree", color = "Infested tree within 1/20th acres?") +
  coord_obs_pred() +
  scale_fill_brewer(palette = "Dark2") +
  scale_color_brewer(palette = "Dark2") 
```

Conclusion
==============================================================================

Row {data-height=250}
-----------------------------------------------------------------------
### Explaination

Overall, the random forest model had a lower rmse and a higher rsq. Both are indicators of a more functional model, so I will say the random forest model is more accurate.

Row {data-height=550}
-----------------------------------------------------------------------

### Model Results in Graph Form

```{r compare}
metric_table <- forest_predicted  %>%
  metrics(truth = DeadDist_sqrt, estimate = .pred) %>% 
  mutate(forest_estimate = .estimate) %>% 
  select(-.estimate, -.estimator) %>% 
  bind_cols(ridge_metrics) %>% 
  mutate(ridge_estimate = .estimate) %>% 
  select(-.estimate, -.estimator, -.metric...3)
metric_table %>% 
  pivot_longer(
    cols = !.metric...1, 
    names_to = "model_type", 
    values_to = "value") %>% 
  filter(.metric...1 == "rmse") %>% 
  ggplot(aes(model_type, value, fill = model_type)) +
  geom_bar(stat = "identity") +
  ylim(0, 11.1783) +
  scale_fill_brewer(palette = "Dark2")

```

### Model Results in Table Form

```{r resultstable}
metric_table %>% 
  mutate(metric = .metric...1) %>%
  select(metric, forest_estimate, ridge_estimate, -.metric...1) %>% 
  DT::datatable()
```

Variable Chart
==============================================================================

### Picture of variables

<img src="../img/variables.png" width="200" height="100">