if (FALSE) {
library(prospectr)
data(NIRsoil)
# Preprocess
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
train_x <- NIRsoil$spc_pr[NIRsoil$train == 1 & !is.na(NIRsoil$Ciso), ]
train_y <- NIRsoil$Ciso[NIRsoil$train == 1 & !is.na(NIRsoil$Ciso)]
test_x <- NIRsoil$spc_pr[NIRsoil$train == 0 & !is.na(NIRsoil$Ciso), ]
test_y <- NIRsoil$Ciso[NIRsoil$train == 0 & !is.na(NIRsoil$Ciso)]
# Basic search with reconstruction and similarity optimizations
gs <- gesearch(
Xr = train_x, Yr = train_y,
Xu = test_x, Yu = test_y,
k = 50, b = 100, retain = 0.97,
target_size = 200,
fit_method = fit_pls(ncomp = 15, method = "mpls"),
optimization = c("reconstruction", "similarity"),
control = gesearch_control(retain_by = "probability"),
seed = 42
)
# Predict
preds <- predict(gs, test_x)
# Plot progress
plot(gs)
plot(gs, which = "removed")
# With reconstruction and response optimization (requires Yu)
gs_response <- gesearch(
Xr = train_x, Yr = train_y,
Xu = test_x, Yu = test_y,
k = 50, b = 100, retain = 0.97,
target_size = 200,
fit_method = fit_pls(ncomp = 15),
optimization = c("reconstruction", "response"),
seed = 42
)
# Parallel processing
library(doParallel)
n_cores <- min(2, parallel::detectCores() - 1)
cl <- makeCluster(n_cores)
registerDoParallel(cl)
gs_parallel <- gesearch(
Xr = train_x, Yr = train_y,
Xu = test_x,
k = 50, b = 100, retain = 0.97,
target_size = 200,
fit_method = fit_pls(ncomp = 15),
pchunks = 3,
seed = 42
)
stopCluster(cl)
registerDoSEQ()
}
Run the code above in your browser using DataLab