-
Notifications
You must be signed in to change notification settings - Fork 0
/
myplsr.R
467 lines (377 loc) · 20.7 KB
/
myplsr.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#### Based on {pls}
###FIt model and CHoose ncomp
fich <- function(mydata){
library(pls)
###fit models
## https://stat.ethz.ch/pipermail/r-help/2004-February/045457.html #centering/scaling
###p12 of {pls}: predictor variables centered as part of fit alg.
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="LOO") ##LOO
#summary(train)
#summary(tenfold) ###confirm that doing tenfold "10 consecutive segments" ##assumes data are random; do so to ensure reproducibility when calculating RMSE and MAE
#summary(loo) ###confirm that doing LOO "nrow(mydata) random segments"
###CrossVal plots to choose ncomp
par(bg="white",mfrow=c(3,1),ann=F)
plot(RMSEP(train))
title(main="Training",xlab="ncomp",ylab="RMSEP")
plot(RMSEP(tenfold))
title(main="10-fold",xlab="ncomp",ylab="RMSEP")
plot(RMSEP(loo))
title(main="LOO",xlab="ncomp",ylab="RMSEP")
#################################
########script to select the number of components with lowest RMSE
########in practice, results cannot be trusted because local minimum not always selected
######## human must judge what local min is!!##########
#################################
###find (cross-validated) no. of components to use; USE LOCAL MINIMUM
#delta.train <- diff(RMSEP(train)$val)
#ncomp.train <- which(delta.train==min(delta.train))
#ncomp.train <- 6 ###is this the local minimum or is 2?
#print(ncomp.train)
#for tenfold and loo, use adjCV (bias-corrected)
#ind1 <- seq(from=2,to=length(RMSEP(tenfold)$val),by=2)
#print(RMSEP(tenfold)$val)
#delta.tenfold <- diff(RMSEP(tenfold)$val[ind1])
#print(delta.tenfold)
#ncomp.tenfold <- which(delta.tenfold==min(delta.tenfold))
#print(ncomp.tenfold)
#ind2 <- seq(from=2,to=length(RMSEP(loo)$val),by=2)
#delta.loo <- diff(RMSEP(loo)$val[ind2])
#ncomp.loo <- which(delta.loo==min(delta.loo))
#print(ncomp.loo)
#ncomp.train <- #gives index of minimum;-1 to account for intercept
#ncomp.tenfold <- which.min(RMSEP(tenfold,estimate="CV")$val)-1 #gives index of minimum;-1 to account for intercept
#ncomp.loo <- which.min(RMSEP(loo,estimate="CV")$val)-1 #gives index of minimum;-1 to account for intercept
#################################
#################################
}
###PRedict and EXtract, with user-specified ncomps
prex <- function(mydata,ncomp.train,ncomp.tenfold,ncomp.loo){
###fit models again
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="LOO") ##LOO
###pred vs. measured plots of tenfold and LOO
par(bg="white",mfrow=c(2,1),ann=F)
predplot(tenfold,which="validation",ncomp=ncomp.tenfold,line=T)
points(predict(tenfold,ncomp=ncomp.tenfold)~mydata$logDT50,col="red") ##plots the training points
title(main=paste("10-fold CV of logDT50,",ncomp.tenfold,"comps"),xlab="measured",ylab="predicted")
#
predplot(loo,which="validation",ncomp=ncomp.loo,line=T)
points(predict(loo,ncomp=ncomp.loo)~mydata$logDT50,col="red") ##plots the training points
title(main=paste("LOOCV of logDT50,",ncomp.loo,"comps"),xlab="measured",ylab="predicted")
###extract R2,RMSE,MAE of the chosen ncomp
mae_train <- mean(abs(predict(train,comp=ncomp.train)-train$model[,1]))
Training <- c(pls::R2(train)$val[[ncomp.train+1]],
RMSEP(train)$val[[2*ncomp.train+2]],mae_train)
#print(tenfold$model[,1])
#print(loo$model[,1])
#mae_tenfold <- mean(abs(predict(tenfold,comp=ncomp.tenfold)-tenfold$model[,1])) ###gives training set somehow
mae_tenfold <- mean(abs(tenfold$validation$pred[,,ncomp.tenfold,drop=F]-tenfold$model[,1])) ##see line 405 https://github.com/cran/pls/blob/master/R/plots.R
Tenfold <- c(pls::R2(tenfold)$val[[ncomp.tenfold+1]],
RMSEP(tenfold)$val[[2*ncomp.tenfold+2]],mae_tenfold)
#mae_loo <- mean(abs(predict(loo,comp=ncomp.loo)-loo$model[,1])) ###gives training set somehow
mae_loo <- mean(abs(loo$validation$pred[,,ncomp.loo,drop=F]-loo$model[,1]))
LOO <- c(pls::R2(loo)$val[[ncomp.loo+1]],
RMSEP(loo)$val[[2*ncomp.loo+2]],mae_loo)
table <- rbind(Training,Tenfold,LOO)
colnames(table) <- c("R2", "RMSE", "MAE")
print(signif(as.matrix(table),2))
###Score plot to diagnose outliers, groups, patterns; same for training, tenfold, LOO
par(bg='white')
plot(train,plottype="scores",comps=1:3,main="Training")
#plot(tenfold,plottype="scores",comps=1:3,main="Tenfold")
#plot(loo,plottype="scores",comps=1:3,main="LOO")
}
#################################
#################################
###Find VAriance explained and plot COrrelation loadings plot
vaco<- function(mydata){
###fit models again
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="LOO") ##LOO
###Correlation loadings plot - to see correlations between each var and selected components - same for training, tenfold, loo
par(bg="white")
corrplot(train,labels="names",main="Training")
#corrplot(tenfold$validation,labels="names")
#corrplot(loo$validation,labels="names")
####extract the correlations between variables and components
###from line 257 https://github.com/cran/pls/blob/master/R/plots.R
S <- scores(train)[,1:2, drop = FALSE]
cl <- round(cor(model.matrix(train), S),2)
print(cl)
###Extract explained variance
vartable <- rbind(explvar(train),explvar(tenfold),explvar(loo))
vartable <- signif(as.matrix(vartable),2)
rownames(vartable) <- c("Training","Tenfold", "LOO")
print(vartable)
}
###output the reg. coef#################regression coefficients not very important!
#TrCoef <- coef(train,ncomp=ncomp.train,intercept=TRUE)[1:(ncomp.train+1)] ###+1 to add intercept
#TeCoef <- coef(tenfold,ncomp=ncomp.tenfold,intercept=TRUE)[1:(ncomp.tenfold+1)] ###+1 to add intercept
#LoCoef <- coef(loo,ncomp=ncomp.loo,intercept=TRUE)[1:(ncomp.loo+1)] ###+1 to add intercept
#coefftable <- rbind(TrCoef,TeCoef,LoCoef)
#colnames(coefftable) <- c("Intercept","Comp1","Comp2",) ##no way of knowing how many comps I will have!
#print(signif(as.matrix(coefftable),2))
###plot(train,plottype="coef",ncomp=1:3) ###coefficients plot
#print(Training)
################################
###############################
################################
###############################
################################
###############################
################################
###############################
### Same as above, but with no Moisture
fich.nomo <- function(mydata){ ##fit model and choose ncomp
library(pls)
###fit models
## https://stat.ethz.ch/pipermail/r-help/2004-February/045457.html #centering/scaling
###p12 of {pls}: predictor variables centered as part of fit alg.
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2+ cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="LOO") ##LOO
summary(train)
summary(tenfold) ###confirm that doing tenfold "10 consecutive segments" ##assumes data are random; do so to ensure reproducibility when calculating RMSE and MAE
summary(loo) ###confirm that doing LOO "nrow(mydata) random segments"
###CrossVal plots to choose ncomp
par(bg="white",mfrow=c(3,1),ann=F)
plot(RMSEP(train))
title(main="Training",xlab="ncomp",ylab="RMSEP")
plot(RMSEP(tenfold))
title(main="10-fold",xlab="ncomp",ylab="RMSEP")
plot(RMSEP(loo))
title(main="LOO",xlab="ncomp",ylab="RMSEP")
#################################
########cannot be trusted, human must judge what local min is!!##########
#################################
###find (cross-validated) no. of components to use; USE LOCAL MINIMUM
#delta.train <- diff(RMSEP(train)$val)
#ncomp.train <- which(delta.train==min(delta.train))
#ncomp.train <- 6 ###is this the local minimum or is 2?
#print(ncomp.train)
#for tenfold and loo, use adjCV (bias-corrected)
#ind1 <- seq(from=2,to=length(RMSEP(tenfold)$val),by=2)
#print(RMSEP(tenfold)$val)
#delta.tenfold <- diff(RMSEP(tenfold)$val[ind1])
#print(delta.tenfold)
#ncomp.tenfold <- which(delta.tenfold==min(delta.tenfold))
#print(ncomp.tenfold)
#ind2 <- seq(from=2,to=length(RMSEP(loo)$val),by=2)
#delta.loo <- diff(RMSEP(loo)$val[ind2])
#ncomp.loo <- which(delta.loo==min(delta.loo))
#print(ncomp.loo)
#ncomp.train <- #gives index of minimum;-1 to account for intercept
#ncomp.tenfold <- which.min(RMSEP(tenfold,estimate="CV")$val)-1 #gives index of minimum;-1 to account for intercept
#ncomp.loo <- which.min(RMSEP(loo,estimate="CV")$val)-1 #gives index of minimum;-1 to account for intercept
#################################
#################################
}
prex.nomo <- function(mydata,ncomp.train,ncomp.tenfold,ncomp.loo){ ##predict and extract, with user-specified ncomps
###fit models again
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="LOO") ##LOO
###pred vs. measured plots of tenfold and LOO
par(bg="white",mfrow=c(2,1),ann=F)
predplot(tenfold,which="validation",ncomp=ncomp.tenfold,line=T)
points(predict(tenfold,ncomp=ncomp.tenfold)~mydata$logDT50,col="red") ##plots the training points
title(main=paste("10-fold CV of logDT50,",ncomp.tenfold,"comps"),xlab="measured",ylab="predicted")
#
predplot(loo,which="validation",ncomp=ncomp.loo,line=T)
points(predict(loo,ncomp=ncomp.loo)~mydata$logDT50,col="red") ##plots the training points
title(main=paste("LOOCV of logDT50,",ncomp.loo,"comps"),xlab="measured",ylab="predicted")
###extract R2,RMSE,MAE of the chosen ncomp
mae_train <- mean(abs(predict(train,comp=ncomp.train)-train$model[,1]))
Training <- c(pls::R2(train)$val[[ncomp.train+1]],
RMSEP(train)$val[[2*ncomp.train+2]],mae_train)
#print(tenfold$model[,1])
#print(loo$model[,1])
#mae_tenfold <- mean(abs(predict(tenfold,comp=ncomp.tenfold)-tenfold$model[,1])) ###gives training set somehow
mae_tenfold <- mean(abs(tenfold$validation$pred[,,ncomp.tenfold,drop=F]-tenfold$model[,1])) ##see line 405 https://github.com/cran/pls/blob/master/R/plots.R
Tenfold <- c(pls::R2(tenfold)$val[[ncomp.tenfold+1]],
RMSEP(tenfold)$val[[2*ncomp.tenfold+2]],mae_tenfold)
#mae_loo <- mean(abs(predict(loo,comp=ncomp.loo)-loo$model[,1])) ###gives training set somehow
mae_loo <- mean(abs(loo$validation$pred[,,ncomp.loo,drop=F]-loo$model[,1]))
LOO <- c(pls::R2(loo)$val[[ncomp.loo+1]],
RMSEP(loo)$val[[2*ncomp.loo+2]],mae_loo)
table <- rbind(Training,Tenfold,LOO)
colnames(table) <- c("R2", "RMSE", "MAE")
print(signif(as.matrix(table),2))
###Score plot to diagnose outliers, groups, patterns
###they all look the same??
#par(bg="white",mfrow=c(2,2))
par(bg='white')
plot(train,plottype="scores",comps=1:3,main="Training")
#plot(tenfold,plottype="scores",comps=1:3,main="Tenfold")
#plot(loo,plottype="scores",comps=1:3,main="LOO")
}
#################################
#################################
vaco.nomo<- function(mydata){ ###correlation plot,variance explained
###fit models again
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + cec + OrgCont + Biomass_start,data=mydata,scale=T,validation="LOO") ##LOO
###Correlation loadings plot - to see correlations between each var and selected components
###they all look the same??
par(bg="white")
corrplot(train,labels="names",main="Training")
#corrplot(tenfold,labels="names")
#corrplot(loo,labels="names")
####extract the correlations between variables and components
###from line 257 https://github.com/cran/pls/blob/master/R/plots.R
S <- scores(train)[,1:2, drop = FALSE]
cl <- as.data.frame(round(cor(model.matrix(train), S),2))
print(cl)
###Extract explained variance
vartable <- rbind(explvar(train),explvar(tenfold),explvar(loo))
vartable <- signif(as.matrix(vartable),2)
rownames(vartable) <- c("Training","Tenfold", "LOO")
print(vartable)
}
################################
###############################
################################
###############################
###### Same as above, but with no Biomass_start
fich.nost <- function(mydata){ ##fit model and choose ncomp
library(pls)
###fit models
## https://stat.ethz.ch/pipermail/r-help/2004-February/045457.html #centering/scaling
###p12 of {pls}: predictor variables centered as part of fit alg.
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="LOO") ##LOO
summary(train)
summary(tenfold) ###confirm that doing tenfold "10 consecutive segments" ##assumes data are random; do so to ensure reproducibility when calculating RMSE and MAE
summary(loo) ###confirm that doing LOO "nrow(mydata) random segments"
###CrossVal plots to choose ncomp
par(bg="white",mfrow=c(3,1),ann=F)
plot(RMSEP(train))
title(main="Training",xlab="ncomp",ylab="RMSEP")
plot(RMSEP(tenfold))
title(main="10-fold",xlab="ncomp",ylab="RMSEP")
plot(RMSEP(loo))
title(main="LOO",xlab="ncomp",ylab="RMSEP")
#################################
########cannot be trusted, human must judge what local min is!!##########
#################################
###find (cross-validated) no. of components to use; USE LOCAL MINIMUM
#delta.train <- diff(RMSEP(train)$val)
#ncomp.train <- which(delta.train==min(delta.train))
#ncomp.train <- 6 ###is this the local minimum or is 2?
#print(ncomp.train)
#for tenfold and loo, use adjCV (bias-corrected)
#ind1 <- seq(from=2,to=length(RMSEP(tenfold)$val),by=2)
#print(RMSEP(tenfold)$val)
#delta.tenfold <- diff(RMSEP(tenfold)$val[ind1])
#print(delta.tenfold)
#ncomp.tenfold <- which(delta.tenfold==min(delta.tenfold))
#print(ncomp.tenfold)
#ind2 <- seq(from=2,to=length(RMSEP(loo)$val),by=2)
#delta.loo <- diff(RMSEP(loo)$val[ind2])
#ncomp.loo <- which(delta.loo==min(delta.loo))
#print(ncomp.loo)
#ncomp.train <- #gives index of minimum;-1 to account for intercept
#ncomp.tenfold <- which.min(RMSEP(tenfold,estimate="CV")$val)-1 #gives index of minimum;-1 to account for intercept
#ncomp.loo <- which.min(RMSEP(loo,estimate="CV")$val)-1 #gives index of minimum;-1 to account for intercept
#################################
#################################
}
prex.nost <- function(mydata,ncomp.train,ncomp.tenfold,ncomp.loo){ ##predict and extract, with user-specified ncomps
###fit models again
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="LOO") ##LOO
###pred vs. measured plots of tenfold and LOO
par(bg="white",mfrow=c(2,1),ann=F)
predplot(tenfold,which="validation",ncomp=ncomp.tenfold,line=T)
points(predict(tenfold,ncomp=ncomp.tenfold)~mydata$logDT50,col="red") ##plots the training points
title(main=paste("10-fold CV of logDT50,",ncomp.tenfold,"comps"),xlab="measured",ylab="predicted")
#
predplot(loo,which="validation",ncomp=ncomp.loo,line=T)
points(predict(loo,ncomp=ncomp.loo)~mydata$logDT50,col="red") ##plots the training points
title(main=paste("LOOCV of logDT50,",ncomp.loo,"comps"),xlab="measured",ylab="predicted")
###extract R2,RMSE,MAE of the chosen ncomp
mae_train <- mean(abs(predict(train,comp=ncomp.train)-train$model[,1]))
Training <- c(pls::R2(train)$val[[ncomp.train+1]],
RMSEP(train)$val[[2*ncomp.train+2]],mae_train)
#print(tenfold$model[,1])
#print(loo$model[,1])
#mae_tenfold <- mean(abs(predict(tenfold,comp=ncomp.tenfold)-tenfold$model[,1])) ###gives training set somehow
mae_tenfold <- mean(abs(tenfold$validation$pred[,,ncomp.tenfold,drop=F]-tenfold$model[,1])) ##see line 405 https://github.com/cran/pls/blob/master/R/plots.R
Tenfold <- c(pls::R2(tenfold)$val[[ncomp.tenfold+1]],
RMSEP(tenfold)$val[[2*ncomp.tenfold+2]],mae_tenfold)
#mae_loo <- mean(abs(predict(loo,comp=ncomp.loo)-loo$model[,1])) ###gives training set somehow
mae_loo <- mean(abs(loo$validation$pred[,,ncomp.loo,drop=F]-loo$model[,1]))
LOO <- c(pls::R2(loo)$val[[ncomp.loo+1]],
RMSEP(loo)$val[[2*ncomp.loo+2]],mae_loo)
table <- rbind(Training,Tenfold,LOO)
colnames(table) <- c("R2", "RMSE", "MAE")
print(signif(as.matrix(table),2))
###Score plot to diagnose outliers, groups, patterns
###they all look the same??
#par(bg="white",mfrow=c(2,2))
par(bg='white')
plot(train,plottype="scores",comps=1:3,main="Training")
#plot(tenfold,plottype="scores",comps=1:3,main="Tenfold")
#plot(loo,plottype="scores",comps=1:3,main="LOO")
}
#################################
#################################
vaco.nost<- function(mydata){ ###correlation plot,variance explained
###fit models again
train <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="none") ##training
tenfold <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="CV",segment.type="consecutive") ##default 10-fold
loo <- plsr(logDT50~Silt + Clay + Temp + pHCaCl2 + Moisture + cec + OrgCont,data=mydata,scale=T,validation="LOO") ##LOO
###Correlation loadings plot - to see correlations between each var and selected components
###they all look the same??
par(bg="white")
corrplot(train,labels="names",main="Training")
#corrplot(tenfold$validation,labels="names")
#corrplot(loo$validation,labels="names")
####extract the correlations between variables and components
###from line 257 https://github.com/cran/pls/blob/master/R/plots.R
S <- scores(train)[,1:2, drop = FALSE]
cl <- round(cor(model.matrix(train), S),2)
print(cl)
###Extract explained variance
vartable <- rbind(explvar(train),explvar(tenfold),explvar(loo))
vartable <- signif(as.matrix(vartable),2)
rownames(vartable) <- c("Training","Tenfold", "LOO")
print(vartable)
}
#################################
#################################
### (*) As described in Chong, Il-Gyo & Jun, Chi-Hyuck, 2005, Performance of
### some variable selection methods when multicollinearity is present,
### Chemometrics and Intelligent Laboratory Systems 78, 103--112.
## VIP returns all VIP values for all variables and all number of components,
## as a ncomp x nvars matrix.
VIP <- function(object) {
if (object$method != "oscorespls")
stop("Only implemented for orthogonal scores algorithm. Refit with 'method = \"oscorespls\"'")
if (nrow(object$Yloadings) > 1)
stop("Only implemented for single-response models")
SS <- c(object$Yloadings)^2 * colSums(object$scores^2)
Wnorm2 <- colSums(object$loading.weights^2)
SSW <- sweep(object$loading.weights^2, 2, SS / Wnorm2, "*")
sqrt(nrow(SSW) * apply(SSW, 1, cumsum) / cumsum(SS))
}
## VIPjh returns the VIP of variable j with h components
VIPjh <- function(object, j, h) {
if (object$method != "oscorespls")
stop("Only implemented for orthogonal scores algorithm. Refit with 'method = \"oscorespls\"'")
if (nrow(object$Yloadings) > 1)
stop("Only implemented for single-response models")
b <- c(object$Yloadings)[1:h]
T <- object$scores[,1:h, drop = FALSE]
SS <- b^2 * colSums(T^2)
W <- object$loading.weights[,1:h, drop = FALSE]
Wnorm2 <- colSums(W^2)
sqrt(nrow(W) * sum(SS * W[j,]^2 / Wnorm2) / sum(SS))
}