require(prospectr)
data(NIRsoil)
# Filter the data using the Savitzky and Golay smoothing filter with
# a window size of 11 spectral variables and a polynomial order of 3
# (no differentiation).
sg <- savitzkyGolay(NIRsoil$spc, p = 3, w = 11, m = 0)
# Replace the original spectra with the filtered ones
NIRsoil$spc <- sg
Xu <- NIRsoil$spc[!as.logical(NIRsoil$train),]
Yu <- NIRsoil$CEC[!as.logical(NIRsoil$train)]
Yr <- NIRsoil$CEC[as.logical(NIRsoil$train)]
Xr <- NIRsoil$spc[as.logical(NIRsoil$train),]
Xu <- Xu[!is.na(Yu),]
Xr <- Xr[!is.na(Yr),]
Yu <- Yu[!is.na(Yu)]
Yr <- Yr[!is.na(Yr)]
# Example 1
# A mbl implemented in Ramirez-Lopez et al. (2013,
# the spectrum-based learner)
# Example 1.1
# An exmaple where Yu is supposed to be unknown, but the Xu
# (spectral variables) are known
ctrl1 <- mblControl(sm = "pc", pcSelection = list("opc", 40),
valMethod = "NNv",
scaled = FALSE, center = TRUE)
sbl.u <- mbl(Yr = Yr, Xr = Xr, Yu = NULL, Xu = Xu,
mblCtrl = ctrl1,
dissUsage = "predictors",
k = seq(40, 150, by = 10),
method = "gpr")
sbl.u
plot(sbl.u)
# Example 1.2
# If Yu is actually known...
sbl.u2 <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl1,
dissUsage = "predictors",
k = seq(40, 150, by = 10),
method = "gpr")
sbl.u2
# Example 1.3
# A variation of the spectrum-based learner implemented in
# Ramirez-Lopez et al. (2013) where the dissimilarity matrices are
# recomputed based on partial least squares scores
ctrl_1.3 <- mblControl(sm = "pls", pcSelection = list("opc", 40),
valMethod = "NNv",
scaled = FALSE, center = TRUE)
sbl_1.3 <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl_1.3,
dissUsage = "predictors",
k = seq(40, 150, by = 10),
method = "gpr")
sbl_1.3
# Example 2
# A mbl similar to the ones implemented in
# Ramirez-Lopez et al. (2013)
# and Fernandez Pierna and Dardenne (2008)
ctrl.mbl <- mblControl(sm = "cor",
pcSelection = list("cumvar", 0.999),
valMethod = "NNv",
scaled = FALSE, center = TRUE)
local.mbl <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl.mbl,
dissUsage = "none",
k = seq(40, 150, by = 10),
pls.c = c(5, 15),
method = "wapls1")
local.mbl
# Example 3
# A variation of the previous example (using the optimized pc
# dissmilarity matrix) using the control list of the example 1
local.mbl2 <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl1,
dissUsage = "none",
k = seq(40, 150, by = 10),
pls.c = c(5, 15),
method = "wapls1")
local.mbl2
# Example 4
# Using the function with user-defined dissimilarities:
# Examples 4.1 - 4.2: Compute a square symetric matrix of
# dissimilarities between
# all the elements in Xr and Xu (dissimilarities will be used as
# additional predictor variables later in the mbl function)
# Examples 4.3 - 4.4: Derive a dissimilarity value of each element
# in Xu to each element in Xr (in this case dissimilarities will
# not be used as additional predictor variables later in the
# mbl function)
# Example 4.1
# the manhattan distance
manhattanD <- dist(rbind(Xr, Xu), method = "manhattan")
manhattanD <- as.matrix(manhattanD)
ctrl.udd <- mblControl(sm = "none",
pcSelection = list("cumvar", 0.999),
valMethod = c("NNv", "loc_crossval"),
resampling = 10, p = 0.75,
scaled = FALSE, center = TRUE)
mbl.udd1 <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl.udd,
dissimilarityM = manhattanD,
dissUsage = "predictors",
k = seq(40, 150, by = 10),
method = "gpr")
mbl.udd1
#Example 4.2
# first derivative spectra
Xr.der.sp <- t(diff(t(rbind(Xr, Xu)), lag = 7, differences = 1))
Xu.der.sp <- t(diff(t(Xu), lag = 7, differences = 1))
# The principal components dissimilarity on the derivative spectra
der.ortho <- orthoDiss(Xr = Xr.der.sp, X2 = Xu.der.sp,
Yr = Yr,
pcSelection = list("opc", 40),
method = "pls",
center = FALSE, scale = FALSE)
der.ortho.diss <- der.ortho$dissimilarity
# mbl applied to the absorbance spectra
mbl.udd2 <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl.udd,
dissimilarityM = der.ortho.diss,
dissUsage = "none",
k = seq(40, 150, by = 10),
method = "gpr")
#Example 4.3
# first derivative spectra
der.Xr <- t(diff(t(Xr), lag = 1, differences = 1))
der.Xu <- t(diff(t(Xu), lag = 1, differences = 1))
# the sid on the derivative spectra
der.sid <- sid(Xr = der.Xr, X2 = der.Xu, mode = "density",
center = TRUE, scaled = FALSE)
der.sid <- der.sid$sid
mbl.udd3 <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl.udd,
dissimilarityM = der.sid,
dissUsage = "none",
k = seq(40, 150, by = 10),
method = "gpr")
mbl.udd3
# Example 5
# For running the mbl function in parallel
n.cores <- detectCores() - 1
if(n.cores == 0) n.cores <- 1
# Set the number of cores according to the OS
if (.Platform$OS.type == "windows") {
require(doParallel)
clust <- makeCluster(n.cores)
registerDoParallel(clust)
}else{
require(doSNOW)
clust <- makeCluster(n.cores, type = "SOCK")
registerDoSNOW(clust)
ncores <- getDoParWorkers()
}
ctrl <- mblControl(sm = "pc", pcSelection = list("opc", 40),
valMethod = "NNv",
scaled = FALSE, center = TRUE)
mbl.p <- mbl(Yr = Yr, Xr = Xr, Yu = Yu, Xu = Xu,
mblCtrl = ctrl,
dissUsage = "none",
k = seq(40, 150, by = 10),
method = "gpr")
registerDoSEQ()
try(stopCluster(clust))
mbl.p
Run the code above in your browser using DataLab