I am trying to use random forest model. My first problem is with my Landsat data where I am using 3 different tiles with the same projection. When I am using the rast function I get an error: (Error: [rast] extents do not match). The second issue is I am trying to use my Landsat data as reference data train and test my model. Finally, I want to compare the result of my model using my Land cover data.
library(terra)
library(randomForest)
lsat_path <- "D:/RF_model_data_2017_2019/lsat_data/"
lsat_files <- list.files(lsat_path, full.names = TRUE)
lsat <- rast(lsat_files)
MOD09GA_path <- "D:/RF_model_data_2017_2019/MOD09GA_7bands/"
MOD09GA_files <- list.files(MOD09GA_path, full.names = TRUE)
MOD09GA <- rast(MOD09GA_files)
NDWI_path <- "D:/RF_model_data_2017_2019/NDWI_data/"
NDWI_files <- list.files(NDSI_path, full.names = TRUE)
NDWI <- rast(NDSI_files)
NDVI_path <- "D:/RF_model_data_2017_2019/NDVI_data/"
NDVI_files <- list.files(NDVI_path, full.names = TRUE)
NDVI <- rast(NDVI_files)
DEM_path <- "D:/RF_model_data_2017_2019/DEM_data/"
DEM <- rast(DEM_path)
lc_path <- "D:/RF_model_data_2017_2019/Landcover/"
lc_files <- list.files(MOD10A1_path, full.names = TRUE)
lc <- rast(MOD10A1_files)
data <- c("lsat", "MOD09GA", "NDWI", "NDVI", "DEM")
set.seed(100)
pos_training <- sample(1:nrow(lsat), round(lsat * 0.8))
training <- data[pos_training,]
validation <- data[- pos_training]
rf_model <- randomForest(lsat ~ ., files=training, mtry=500,
importance=TRUE, na.action=na.omit)
rf_prediction <- predict(rf_model, validation[,lsat])
data.frame(obs = validation[,lsat], RF = rf_prediction)
varImpPlot(rf_model)