## ------------------------------------------------------------
## Classification tree example using non-spatial partitioning
## ------------------------------------------------------------
# Muenchow et al. (2012), see ?ecuador
fo <- slides ~ dem + slope + hcurv + vcurv + log.carea + cslope
library(rpart)
mypred_part <- function(object, newdata) predict(object, newdata)[, 2]
ctrl <- rpart.control(cp = 0.005) # show the effects of overfitting
# show the effects of overfitting
fit <- rpart(fo, data = ecuador, control = ctrl)
### Non-spatial cross-validation:
mypred_part <- function(object, newdata) predict(object, newdata)[, 2]
nsp_res <- sperrorest(
data = ecuador, formula = fo,
model_fun = rpart,
model_args = list(control = ctrl),
pred_fun = mypred_part,
progress = TRUE,
smp_fun = partition_cv,
smp_args = list(repetition = 1:2, nfold = 3)
)
summary(nsp_res$error_rep)
summary(nsp_res$error_fold)
summary(nsp_res$represampling)
# plot(nsp_res$represampling, ecuador)
### Spatial cross-validation:
sp_res <- sperrorest(
data = ecuador, formula = fo,
model_fun = rpart,
model_args = list(control = ctrl),
pred_fun = mypred_part,
progress = TRUE,
smp_fun = partition_kmeans,
smp_args = list(repetition = 1:2, nfold = 3)
)
summary(sp_res$error_rep)
summary(sp_res$error_fold)
summary(sp_res$represampling)
# plot(sp_res$represampling, ecuador)
smry <- data.frame(
nonspat_training = unlist(summary(nsp_res$error_rep,
level = 1
)$train_auroc),
nonspat_test = unlist(summary(nsp_res$error_rep,
level = 1
)$test_auroc),
spatial_training = unlist(summary(sp_res$error_rep,
level = 1
)$train_auroc),
spatial_test = unlist(summary(sp_res$error_rep,
level = 1
)$test_auroc)
)
boxplot(smry,
col = c("red", "red", "red", "green"),
main = "Training vs. test, nonspatial vs. spatial",
ylab = "Area under the ROC curve"
)
Run the code above in your browser using DataLab