if (FALSE) {
library(prospectr)
data(NIRsoil)
# Preprocess: detrend + first derivative with Savitzky-Golay
sg_det <- savitzkyGolay(
detrend(NIRsoil$spc, wav = as.numeric(colnames(NIRsoil$spc))),
m = 1, p = 1, w = 7
)
NIRsoil$spc_pr <- sg_det
# Split data
test_x <- NIRsoil$spc_pr[NIRsoil$train == 0 & !is.na(NIRsoil$CEC), ]
test_y <- NIRsoil$CEC[NIRsoil$train == 0 & !is.na(NIRsoil$CEC)]
train_x <- NIRsoil$spc_pr[NIRsoil$train == 1 & !is.na(NIRsoil$CEC), ]
train_y <- NIRsoil$CEC[NIRsoil$train == 1 & !is.na(NIRsoil$CEC)]
# Example 1: Spectrum-based learner (Ramirez-Lopez et al., 2013)
ctrl <- mbl_control(validation_type = "NNv")
sbl <- mbl(
Xr = train_x,
Yr = train_y,
Xu = test_x,
neighbors = neighbors_k(seq(40, 140, by = 20)),
diss_method = diss_pca(ncomp = ncomp_by_opc(40)),
fit_method = fit_gpr(),
control = ctrl
)
sbl
plot(sbl)
get_predictions(sbl)
# Example 2: With known Yu
sbl_2 <- mbl(
Xr = train_x,
Yr = train_y,
Xu = test_x,
Yu = test_y,
neighbors = neighbors_k(seq(40, 140, by = 20)),
fit_method = fit_gpr(),
control = ctrl
)
plot(sbl_2)
# Example 3: LOCAL algorithm (Shenk et al., 1997)
local_algo <- mbl(
Xr = train_x,
Yr = train_y,
Xu = test_x,
Yu = test_y,
neighbors = neighbors_k(seq(40, 140, by = 20)),
diss_method = diss_correlation(),
diss_usage = "none",
fit_method = fit_wapls(min_ncomp = 3, max_ncomp = 15),
control = ctrl
)
plot(local_algo)
# Example 4: Using dissimilarity as predictors
local_algo_2 <- mbl(
Xr = train_x,
Yr = train_y,
Xu = test_x,
Yu = test_y,
neighbors = neighbors_k(seq(40, 140, by = 20)),
diss_method = diss_pca(ncomp = ncomp_by_opc(40)),
diss_usage = "predictors",
fit_method = fit_wapls(min_ncomp = 3, max_ncomp = 15),
control = ctrl
)
plot(local_algo_2)
# Example 5: Parallel execution
library(doParallel)
n_cores <- min(2, parallel::detectCores() - 1)
clust <- makeCluster(n_cores)
registerDoParallel(clust)
local_algo_par <- mbl(
Xr = train_x,
Yr = train_y,
Xu = test_x,
Yu = test_y,
neighbors = neighbors_k(seq(40, 140, by = 20)),
diss_method = diss_correlation(),
fit_method = fit_wapls(min_ncomp = 3, max_ncomp = 15),
control = ctrl
)
registerDoSEQ()
try(stopCluster(clust))
}
Run the code above in your browser using DataLab