-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path1_Live_Demo.Rmd
More file actions
executable file
·161 lines (102 loc) · 3.13 KB
/
1_Live_Demo.Rmd
File metadata and controls
executable file
·161 lines (102 loc) · 3.13 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: "Live ML Demo"
author: "Matthew Ross"
date: "2024-11-20"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse) #You. know
library(xgboost) # Gradient Boosting
library(randomForest) # random forest
library(sf)
library(mapview)
library(Metrics)
```
# Data Explore
```{r}
sdd <- read_csv('data/western_sdd.csv')
summary(sdd$harmonized_value)
ggplot(sdd, aes(x = harmonized_value, y = red_corr7)) +
geom_point() +
scale_y_log10() +
geom_smooth(method = 'lm', se = F)
ggplot(sdd, aes(x = harmonized_value, y = green_corr7)) +
geom_point() +
scale_y_log10() +
geom_smooth(method = 'lm', se = F)
ggplot(sdd, aes(x = harmonized_value, y = BR_G)) +
geom_point() +
scale_y_log10() +
geom_smooth(method = 'lm', se = F)
```
## Quick site map
```{r}
sdd_sites <- sdd %>%
distinct(part, lat = WGS84_Latitude,
long = WGS84_Longitude) %>%
st_as_sf(.,coords = c('long','lat'), crs = 4263)
mapview(sdd_sites, zcol = 'part')
```
## Quick naive LM
```{r}
simple_mod <- lm(harmonized_value ~ red_corr7*blue_corr7*green_corr7*BR_G,
data = sdd)
summary(simple_mod)
```
## Quick naive random_forest
### Truly random test splitting
This is ill advised!
```{r}
set.seed(221432)
sdd_prepped <- sdd %>%
select(harmonized_value, c('R_BS','R_BN','B_RG','BG','NmR',
'green_corr7','BR_G','GR_2','fai','red_corr7','G_BN','NmS'))
test_sdd <- sdd_prepped %>%
sample_frac(0.2)
train_sdd <- sdd_prepped %>%
anti_join(test_sdd)
rf_mod <- randomForest(harmonized_value ~ .,
data = train_sdd,
importance = F,
ntree = 250)
test_sdd$sdd_pred <- predict(rf_mod, test_sdd)
ggplot(test_sdd, aes(y = sdd_pred,
x = harmonized_value)) +
geom_point() +
xlab('Observed') +
ylab('Predicted') +
geom_smooth(method = 'lm', se = F) +
geom_abline(intercept = 0, slope = 1,
color = 'red')
library(Metrics)
mape(test_sdd$harmonized_value, test_sdd$sdd_pred)
rmse(test_sdd$harmonized_value, test_sdd$sdd_pred)
```
## Proper train test split
```{r}
sdd_prepped <- sdd
test_sdd <- sdd %>%
filter(part != 5) %>%
select(harmonized_value, c('R_BS','R_BN','B_RG','BG','NmR',
'green_corr7','BR_G','GR_2','fai','red_corr7','G_BN','NmS'))
train_sdd <- sdd %>%
filter(part == 5) %>%
select(harmonized_value, c('R_BS','R_BN','B_RG','BG','NmR',
'green_corr7','BR_G','GR_2','fai','red_corr7','G_BN','NmS'))
rf_mod <- randomForest(harmonized_value ~ .,
data = train_sdd,
importance = F,
ntree = 250)
test_sdd$sdd_pred <- predict(rf_mod, test_sdd)
ggplot(test_sdd, aes(y = sdd_pred,
x = harmonized_value)) +
geom_point() +
xlab('Observed') +
ylab('Predicted') +
geom_smooth(method = 'lm', se = F) +
geom_abline(intercept = 0, slope = 1,
color = 'red')
mape(test_sdd$harmonized_value, test_sdd$sdd_pred)
rmse(test_sdd$harmonized_value, test_sdd$sdd_pred)
```