-
Notifications
You must be signed in to change notification settings - Fork 2
/
4.02_PREDICTING_VARIABLE.R
252 lines (207 loc) · 5.98 KB
/
4.02_PREDICTING_VARIABLE.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
####################### VALE INSTITUTE OF TECHNOLOGY ##########################
######################## GEA-GPA TUTORIAL ##########################
###################### STEP 02: PILOCARPINE VALUES ########################
library(dplyr)
library(caret)
library(mgcv)
library(glmnet)
library(ranger)
library(gbm)
library(kernlab)
#### Load data ----
## Pilocarpine
piloc = read.table("./data_raw/jaborandi_database_piloc.csv", h=T, sep=";", dec=".") %>%
dplyr::filter(!is.na(pilocarpina)) %>%
dplyr::filter(!parcela %in% c("PCMA","LAMA", "PVNMA", "CPI", "PGPI"))
head(piloc)
nrow(piloc)
## Leaf data
leaf = read.table("./data_raw/jaborandi_database_folha.csv", h=T, sep=";", dec=".") %>%
rename(cod_folhas = amostra) ## change "amostra" column name as in piloc dataframe
head(leaf)
nrow(leaf)
#merge pilocarpine and leaf tables
nrow(piloc); nrow(leaf)
df = left_join(piloc, leaf, by="cod_folhas")
head(df)
nrow(df) ## OK
dfF <- df %>%
dplyr::select(-c("cod_folhas", "cod_solo", "UTM1", "UTM2",
"Numero.Amostra", "Data.da.OS", "Data.Prog.Fim")) %>%
dplyr::select(site, everything())
head(dfF)
nrow(dfF)
## Soil data
soil = read.table("./data_raw/jaborandi_database_solo.csv", h=T, sep=";", dec=".")
nrow(soil) ## Lower N - exclude!
#### Explore data----
str(dfF)
names(dfF)
## Check for outliers
names <- names(dfF[, 3:ncol(dfF)])
for(i in 1:length(names)){
dotchart(dfF[, names[i]], main=paste(names[i]))
}
## Outliers: in Cu_I, Fe_I, Zn_I
dfF[dfF$Cu_l>=80,] #53
dfF[dfF$Fe_l>=15000,] #53
dfF[dfF$Zn_l>=400,] #53
dfF <- dfF %>% dplyr::filter(Cu_l<=80) # Removing sample 53
##Normality
hist(dfF$pilocarpina) # OK
##### Model selection using machine learning and the caret package ----
## 10-fold validation with 5 repeats
## Pre-processing data by centering, scaling and using PCA axes to avoid collinearity
summary(dfF)
## Random Forest (no need to worry abour colinearity)
modelLookup("ranger")
set.seed(42)
model_rf <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + S_l + B_l + Zn_l + Fe_l
+ Mn_l + Cu_l,
dfF,
method = "ranger",
importance = "impurity",
trControl = trainControl(
method = "repeatedcv", p=0.8, number = 10, repeats=5, verbose = TRUE
),
tuneLength = 5
)
ggplot(model_rf)
plot(varImp(model_rf)) # Exclude B_l, Mn_l, Fe-L, S_l in subsequent models
## lm
modelLookup("lm")
set.seed(42)
model_lm <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + Zn_l + Cu_l,
dfF,
method = "lm",
trControl = trainControl(
method = "repeatedcv", p=0.8, number = 10, repeats=5, verbose = TRUE
),
preProcess = c( "center", "scale", "pca")
)
min(model_lm$results$RMSE) ## Try to minimize RMSE
print(model_lm)
## glmnet
modelLookup("glmnet")
set.seed(42)
model_glmnet <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + Zn_l + Cu_l,
dfF,
method = "glmnet",
#tuneLength=10,
tuneGrid = expand.grid(
lambda = 0:10 / 10,
alpha = seq(0, 1, 0.1)
),
trControl = trainControl(
method = "repeatedcv", p=0.8, number = 10, repeats=5, verbose = TRUE
),
preProcess = c( "center", "scale", "pca")
)
min(model_glmnet$results$RMSE) ## Try to minimize RMSE
ggplot(model_glmnet)
print(model_glmnet)
## gam
modelLookup("gam")
set.seed(42)
model_gam <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + Zn_l + Cu_l,
dfF,
method = "gam",
trControl = trainControl(
method = "repeatedcv", p=0.8, number = 10, repeats=5, verbose = TRUE
),
preProcess = c( "center", "scale", "pca"),
tuneLength = 5
)
min(model_gam$results$RMSE) ## Try to minimize RMSE
ggplot(model_gam)
print(model_gam)
## random forest
modelLookup("ranger")
set.seed(42)
model_rf <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + S_l + B_l + Zn_l + Fe_l
+ Mn_l + Cu_l,
dfF,
method = "ranger",
importance = "impurity",
trControl = trainControl(
method = "repeatedcv", p=0.8, number = 10, repeats=5, verbose = TRUE
),
#preProcess = c( "center", "scale"),
tuneLength = 5
)
min(model_rf$results$RMSE) ## Try to minimize RMSE
ggplot(model_rf)
plot(varImp(model_rf))
## gradient boosting machines
modelLookup("gbm")
set.seed(42)
model_gbm <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + S_l + B_l + Zn_l + Fe_l
+ Mn_l + Cu_l,
dfF,
method = "gbm",
trControl = trainControl(
method = "repeatedcv", number = 10, repeats=5, verbose = TRUE
),
#preProcess = c( "center", "scale", "pca"),
tuneLength = 10
)
min(model_rf$results$RMSE) ## Try to minimize RMSE
ggplot(model_gbm)
## Support vector machines
modelLookup("svmPoly")
set.seed(42)
model_svm <- train(
pilocarpina ~ N_l + P_l + K_l + Ca_l + Mg_l + S_l + B_l + Zn_l + Fe_l
+ Mn_l + Cu_l,
dfF,
method = "svmPoly",
trControl = trainControl(
method = "repeatedcv", p=0.8, number = 10, repeats=5, verbose = TRUE
),
#preProcess = c( "center", "scale", "pca"),
#tuneLength = 5
)
min(model_rf$results$RMSE) ## Try to minimize RMSE
ggplot(model_svm)
#### Compare models
model_list <- list(
lm = model_lm,
glmnet = model_glmnet,
gam = model_gam,
rf = model_rf,
gbm = model_gbm,
svm = model_svm
)
## Collect resamples from the CV folds
resamps <- resamples(model_list)
resamps
summary(resamps)
dotplot(resamps, metric = "RMSE") ## svm is best model
bwplot(resamps, metric = "RMSE")
xyplot(resamps, metric = "RMSE")
#### Predict new data using the best model ----
model_svm ## best model
#Load the genotyped samples:
new.data1 = read.csv(file = "./Results/Outputs/imputed_Leaf.csv", row.names = 1)
#Compare col names:
names(new.data1)
model_svm$call
#organized cols:
new.data = new.data1[,c(3:9,13,11,12,10)]
colnames(new.data) = c("N_l", "P_l", "K_l", "Ca_l", "Mg_l", "S_l", "B_l", "Zn_l", "Fe_l", "Mn_l", "Cu_l")
#verify
names(new.data)
model_svm$call
predicted.pilocarpine <- predict(model_svm, new.data) ## Predicted pilocarpina for new dataset using svm
predicted.pilocarpine ## These are the predicted values
#save results:
pilo_pred = cbind(new.data1[,1:2], predicted.pilocarpine)
head(pilo_pred)
write.csv(pilo_pred, file = "./Results/Outputs/GPA_pilo_pred.csv")
#END