-
Notifications
You must be signed in to change notification settings - Fork 0
/
Machine_Learning_script.R
177 lines (140 loc) · 6.28 KB
/
Machine_Learning_script.R
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
161
162
163
164
###############################
# AAUTC Machine Learning Script
# Author: Thomas Estabrook
# 1/17/2022
# Updated: 1/17/2022
###############################
library(sf)
library(randomForest)
library(tidyverse)
library(e1071)
library(caret)
library(nnet)
# # Initial information
# seg_method <- "pixel" # segmentation method (e.g. LiDAR, pixel, buffered pixel)
# location <- "BPW" # location (e.g. BPW, full_city)
# genus_threshold <- 40 # number below which genera are lumped into "other" category
# today <- Sys.Date() # current date
# set.seed(1) # set a random seed for reproducibility
# setwd('G:/Shared drives/A2_UTC Drive/R_scripts/Model_Inputs') # set working directory
#
# # Load in the csv with tree data
# # Should have a column with Genus, and columns for each predictor variable
# trees <- read_sf("training_testing_Clip_resample5ft_withPixelVal.shp") %>%
# mutate(tree_id = row_number()) %>% # add a new unique ID for the dataset
# st_drop_geometry() %>%
# drop_na()
# Use this to list all columns not being used as predictor variables
# dropcols <- c("tree_id", "Join_Count", "TARGET_FID", "Plnt_Cd", "Cmmn_Nm", "Source", "Prjct_C", "train")
# # Initial information
# seg_method <- "lidRvarsm2" # segmentation method (e.g. LiDAR, pixel, buffered pixel)
# location <- "BPW" # location (e.g. BPW, full_city)
# genus_threshold <- 40 # number below which genera are lumped into "other" category
# today <- Sys.Date() # current date
# set.seed(1) # set a random seed for reproducibility
# setwd('G:/Shared drives/A2_UTC Drive/R_scripts/Model_Inputs') # set working directory
#
# # Load in the csv with tree data
# # Should have a column with Genus, and columns for each predictor variable
# trees <- read.csv("Les_BPW_LidR_seg_var_smooth2_finalZS.csv") %>%
# mutate(tree_id = row_number()) %>% # add a new unique ID for the dataset
# drop_na() %>%
# filter(Genus != "")
#
# head(trees)
# colnames(trees)
#
# #Use this to list all columns not being used as predictor variables
# dropcols <- c("X", "Id", "MAJORITY", "MINORITY", "VARIETY", "tree_id")
# # Initial information
# seg_method <- "lidR30sm" # segmentation method (e.g. LiDAR, pixel, buffered pixel)
# location <- "BPW" # location (e.g. BPW, full_city)
# genus_threshold <- 40 # number below which genera are lumped into "other" category
# today <- Sys.Date() # current date
# set.seed(1) # set a random seed for reproducibility
# setwd('G:/Shared drives/A2_UTC Drive/R_scripts/Model_Inputs') # set working directory
#
# # Load in the csv with tree data
# # Should have a column with Genus, and columns for each predictor variable
# trees <- read.csv("Les_BPW_LidR_seg_30_smooth_finalZS.csv") %>%
# mutate(tree_id = row_number()) %>% # add a new unique ID for the dataset
# drop_na() %>%
# filter(Genus != "")
#
# head(trees)
# colnames(trees)
#
# #Use this to list all columns not being used as predictor variables
# dropcols <- c("X", "Id", "MAJORITY", "MINORITY", "VARIETY", "tree_id")
# Initial information
seg_method <- "lidR30" # segmentation method (e.g. LiDAR, pixel, buffered pixel)
location <- "BPW" # location (e.g. BPW, full_city)
genus_threshold <- 40 # number below which genera are lumped into "other" category
today <- Sys.Date() # current date
set.seed(1) # set a random seed for reproducibility
setwd('G:/Shared drives/A2_UTC Drive/R_scripts/Model_Inputs') # set working directory
# Load in the csv with tree data
# Should have a column with Genus, and columns for each predictor variable
trees <- read.csv("Les_BPW_LidR_seg_30_finalZS.csv") %>%
mutate(tree_id = row_number()) %>% # add a new unique ID for the dataset
drop_na() %>%
filter(Genus != "")
head(trees)
colnames(trees)
#Use this to list all columns not being used as predictor variables
dropcols <- c("X", "Id", "MAJORITY", "MINORITY", "VARIETY", "tree_id")
############################################################################################################
# Get counts of tree genera
genuscounts <- trees %>%
group_by(Genus) %>%
summarise(count = n())
# Filter out underrepresented genuses and recode to "Other"
other_genera <- genuscounts$Genus[genuscounts$count < genus_threshold]
trees$Genus[trees$Genus %in% other_genera] <- "Other"
# Randomly take half of each genus for training and testing
training_trees <- trees %>%
group_by(Genus) %>%
slice_sample(prop = 0.5)
testing_trees <- trees[!(trees$tree_id %in% training_trees$tree_id),]
# Remove unneeded columns
training_trees <- training_trees %>%
select(!all_of(dropcols))
testing_trees <- testing_trees %>%
select(!all_of(dropcols))
# Turn the Genus column into a factor
training_trees$Genus <- as.factor(training_trees$Genus)
testing_trees$Genus <- as.factor(testing_trees$Genus)
# Run a random forest model
rf_mod <- randomForest(Genus ~ ., data = training_trees,
xtest = testing_trees[,!names(testing_trees) == "Genus"],
ytest = testing_trees$Genus)
rf_pred <- rf_mod$test$predicted
rf_conf <- confusionMatrix(data = rf_pred, reference = testing_trees$Genus)
# Run a SVR model
svm_mod <- svm(Genus ~ ., data = training_trees)
svm_pred <- predict(svm_mod, testing_trees)
svm_conf <- confusionMatrix(data = svm_pred, reference = testing_trees$Genus)
# Run multinomial logistic regression
reg_mod <- multinom(Genus ~ ., data = training_trees)
reg_pred <- predict(reg_mod, testing_trees)
reg_conf <- confusionMatrix(data = reg_pred, reference = testing_trees$Genus)
# Save outputs
setwd('G:/Shared drives/A2_UTC Drive/R_scripts/Model_Outputs')
filename <- paste0(seg_method, "_", location, "_", genus_threshold, "_", today)
save(rf_mod, file = paste0("rf_mod_", filename))
save(svm_mod, file = paste0("svm_mod_", filename))
save(reg_mod, file = paste0("reg_mod_", filename))
sink(file = paste0("summary_", filename, ".txt"))
print("Random Forest:")
rf_conf$overall[1:2]
print("Support Vector:")
svm_conf$overall[1:2]
print("Multinomial Regression:")
reg_conf$overall[1:2]
print("Random Forest:")
rf_conf
print("Support Vector:")
svm_conf
print("Multinomial Regression:")
reg_conf
sink(file=NULL)