# \donttest{
library(terra)
library(caret)
## Create fake input images
agg.level <- 9
modis <- terra::aggregate(rlogo, agg.level)
## Perform an exemplary classification
lc <- unsuperClass(rlogo, nClass=2)
## Calculate the true cover, which is of course only possible in this example,
## because the fake corse resolution imagery is exactly res(rlogo)*9
trueCover <- terra::aggregate(lc$map, agg.level,
fun = function(x, ...){sum(x == 1, ...)/sum(!is.na(x))})
## Run with randomForest and support vector machine (radial basis kernel)
## Of course the SVM is handicapped in this example,
## due to poor tuning (tuneLength)
par(mfrow=c(2,3))
for(model in c("rf", "svmRadial")){
fc <- fCover(
classImage = lc$map ,
predImage = modis,
classes=1,
trControl = trainControl(method = "cv", number = 3),
model=model,
nSample = 50,
tuneLength=2
)
## How close is it to the truth?
compare.rf <- trueCover - fc$map
plot(fc$map, main = paste("Fractional Cover: Class 1\nModel:", model))
plot(compare.rf, main = "Diffence\n true vs. predicted")
plot(trueCover[],fc$map[], xlim = c(0,1), ylim =c(0,1),
xlab = "True Cover", ylab = "Predicted Cover" )
abline(coef=c(0,1))
rmse <- sqrt(global(compare.rf^2, "sum", na.rm = TRUE))/ncell(compare.rf)
r2 <- cor(trueCover[], fc$map[], "complete.obs")
text(0.9,0.1, adj=1, labels =
paste(c("RMSE:","\nR2:"), round(unlist(c(rmse, r2)),3), collapse=""))
}
## Reset par
par(mfrow=c(1,1))
# }
Run the code above in your browser using DataLab