-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path99-testing-app-functions.Rmd
More file actions
160 lines (132 loc) · 3.69 KB
/
99-testing-app-functions.Rmd
File metadata and controls
160 lines (132 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
title: "Testing Functions for WhoseEgg Shiny App"
author: "Katherine Goode <br>"
date: 'Last Updated: `r format(Sys.time(), "%B %d, %Y")`'
output: rmarkdown::github_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
dpi = 300
)
```
This notebook contains the code and example data for testing the WhoseEgg functions
Load R packages:
```{r}
# Load packages
library(dplyr)
library(DT)
library(forcats)
library(ggplot2)
library(markdown)
library(plotly)
library(purrr)
library(randomForest)
library(shiny)
library(shinythemes)
library(stringr)
library(tidyr)
```
Source the helper functions used by the app:
```{r}
source("../helper-functions.R")
```
Load the WhoseEgg training data:
```{r}
eggdata <- read.csv("../data/eggdata_for_app.csv")
```
Load the WhoseEgg random forest models:
```{r}
rfs <- readRDS("../data/rfs_for_app.rds")
```
Select a file for testing the functions:
```{r}
input_data <- readxl::read_xlsx("../../example_data/warning_outside_ranges.xlsx")
```
Apply checks to input data to check for any issues:
```{r collapse = TRUE}
check_for_vars(input_data)
get_missing_vars(input_data)
check_fct_levels(input_data)
get_wrong_fct_levels(input_data)
check_dates(input_data)
get_na_dates(input_data)
check_for_historical_dates(input_data)
get_any_future_dates(input_data)
```
Process the inputs as needed for the random forest:
```{r}
processed_inputs <-
input_data %>%
compute_variables() %>%
adjust_variable_types() %>%
adjust_factor_levels() %>%
sort_vars()
```
Add a variable if some observations are outside of ranges:
```{r}
if (!check_var_ranges(processed_inputs, eggdata)) {
ids_outside = get_obs_outside_var_ranges(processed_inputs, eggdata)
processed_inputs <-
processed_inputs %>%
mutate(Warning = ifelse(Egg_ID %in% ids_outside, "var(s) out of range", "none")) %>%
select(Egg_ID, Warning, everything())
}
if (!check_var_ranges(processed_inputs, eggdata)) {
processed_inputs %>% select(Egg_ID, Warning) %>% head()
}
```
Prepare the inputs for the random forest:
```{r}
inputs_clean <- processed_inputs %>% select(all_of(rf_pred_vars))
```
Get the predictions and random forest probabilities:
```{r}
pred_list <-
list(
family_pred = as.character(predict(rfs$Family_IC, inputs_clean)),
genus_pred = as.character(predict(rfs$Genus_IC, inputs_clean)),
species_pred = as.character(predict(rfs$Common_Name_IC, inputs_clean)),
family_prob = data.frame(predict(rfs$Family_IC, inputs_clean, type = "prob")),
genus_prob = data.frame(predict(rfs$Genus_IC, inputs_clean, type = "prob")),
species_prob = data.frame(predict(
rfs$Common_Name_IC, inputs_clean, type = "prob"
))
)
```
Put the predictions in a data frame with the input values:
```{r}
data_and_preds <-
processed_inputs %>%
mutate(
Family_Pred = pred_list$family_pred,
Family_Prob = get_rf_prob(pred_list, "family"),
Genus_Pred = pred_list$genus_pred,
Genus_Prob = get_rf_prob(pred_list, "genus"),
Species_Pred = pred_list$species_pred,
Species_Prob = get_rf_prob(pred_list, "species")
) %>%
bind_cols(
pred_list$family_prob %>% rename_all(
.funs = function(x)
paste0("Family_Prob_", x)
),
pred_list$genus_prob %>% rename_all(
.funs = function(x)
paste0("Genus_Prob_", x)
),
pred_list$species_prob %>% rename_all(
.funs = function(x)
paste0("Species_Prob_", x)
)
)
```
Create plots summarizing the random forest predictions:
```{r fig.width = 16, fig.height = 7}
rf_pred_plot(data_and_preds)
```
Create plots with the random forest probabilities for all taxonomic levels:
```{r fig.width = 16, fig.height = 7}
rf_prob_plot(data_and_preds, 1)
```