Loop: use gbm.auto trycatch to only move to next number if it run successfully
loop runs whole gbm.auto. in gbm.auto it checks the outcome of assign(gbm.step.SD) which I can't do in loop. check the saved outputs?
either numbered folder only contains grv.csv ( which shouldn't exist, now removed) i.e. empty folder, or metrics are bad (can't see how to test, gbm.auto fails with error more than it succeeds with bad results.
Not sure if this will work given gbm.auto fails with error, stopping the loop, but starter for 10:
userdefined <- 10
maxattempts <- 10
for (i in 1:userdefined) {
success <- FALSE # Initialize success flag
attempts <- 0 # Initialize attempt counter
setwd(paste0("./", i)) # move to it
while (!success) {
attempts <- attempts + 1
# Simulate your code that might fail (replace with your actual code)
gbm.auto(grids = grids, # run i'th gbm.auto
samples = samples,
expvar = expvar,
resvar = resvar,
randomvar = randomvar,
tc = tc,
lr = lr,
bf = bf,
n.trees = n.trees,
ZI = ZI,
fam1 = fam1,
fam2 = fam2,
simp = simp,
gridslat = gridslat,
gridslon = gridslon,
multiplot = multiplot,
cols = cols,
linesfiles = linesfiles,
smooth = smooth,
savedir = "./", # current directory created & moved to above
savegbm = savegbm,
loadgbm = loadgbm,
varint = varint,
map = map,
shape = shape,
RSB = RSB,
BnW = BnW,
alerts = alerts,
pngtype = pngtype,
gaus = gaus,
MLEvaluate = MLEvaluate,
...) # accept other gbm.auto values than these basics
# test outcome: if folder empty or metrics shit (can't see how to test this) then success FALSE else TRUE
success <- ifelse(is.character(list.files()) & length(list.files()) == 0, FALSE, TRUE)
# result <- tryCatch({
# # Your code here
# if (runif(1) < 0.3) {
# stop("Simulated failure") # Simulate a failure 30% of the time
# } else {
# paste("Success on attempt", attempts, "for i =", i) # Simulate success
# }
# }, error = function(e) {
# print(paste("Attempt", attempts, "failed for i =", i, ":", e$message))
# NULL # Return NULL on error, so success remains FALSE
# })
#
# if (!is.null(result)) {
# print(result)
# success <- TRUE # Set success to TRUE if the code ran without error
# } # close if (!is.null(result)
# Optional: Add a maximum number of retries to prevent infinite loops
if (attempts > maxattempts) {
print(paste("Maximum retries reached for loop ", i, ". Skipping."))
break # Exit the while loop if max retries are exceeded
} # close if (attempts > maxattempts)
} # close while
setwd("../") # move back up to root folder
}
Report.csv for bad results:
Best Gaussian BRT Model combo: Gaus_BRT.tc5.lr0.5.bf0.9 trees: 1900 Training Data Correlation: 1 CV Mean Deviance: 0 CV Deviance SE: 0 CV D squared: 1 CV Mean Correlation: 1 CV Correlation SE: 0 CV RMSE: 0 Deviance% explained relative to null, training: 100 Deviance% explained relative to null, CV: 100