### 5.1.1 Create your equation. The equation can be simple or a bit complex depending on how the data were imported and the nature of the variables. Important matters to remember are (1) categorical variables that are represented by numbers must be preceded by "factor" (e.g., if the classes are designated 1, 2, 3 . . ., the equation response variable should be "factor(types)") and (2) variable names (and everything else in R) are case sensitive. The following are several ways to set your equation.
### If the data were imported using the commands above for shapefiles, all bands will be used as potential predictors, and none of the variables are categorical.
equation <- factor(types) ~ .
equation <- as.formula(equation)
### If there are four bands, and band 3 is categorical. Use the "names" function to get the actual names of the variables and replace B1, B2, etc. with these names.
names(train.full)
equation <- factor(types) ~ B1 + B2 + factor(B3) + B4
equation <- as.formula(equation)
### If externally prepared data have been imported, the classes are designated by numbers, all bands will be used as potential predictors, and there are no categorical predictor variables.
equation <- factor(types) ~ .
equation <- as.formula(equation)
### If externally prepared data have been imported, the classes are designated by numbers, and there are four bands, with band 3 being categorical.
equation <- factor(types) ~ B1 + B2 + factor(B3) + B4
equation <- as.formula(equation)
### 5.1.2 Compare classification models.
### Define which methods you want to analyze. Add and/or subtract methods as desired.
mthd <- c("svmLinear", "svmRadial", "rf", "rpart", "C5.0", "kknn", "lda")
### Step through each method, saving the results in a text file.
for (x in mthd) {
train.model <- train(equation, method = x, data = train.full)
predict.model <- predict (train.model, newdata = valid.full)
results <- confusionMatrix(predict.model, valid.full$types)
capture.output(x, results, file = "results.txt", append = TRUE)
}
### 5.2 Output a final image
### Recreate the desired model, replacing x with the value for the desired method (in quotation marks).
train.model <- train(equation, method = "x", data = train.full)
### Create the output image, changing the filename as desired. This code uses the clusterR function to implement parallel processing and increase the speed of the predictions (the first line sets this up for four cores; change this as desired).
beginCluster(4)
### Use the following line only if you have a designated NA value; insert the NA value you are using in place of x.
NAvalue(img) <- x
clusterR(img,raster::predict, args=list(model=train.model), filename="output", format="HFA", datatype="INT1U" , overwrite=TRUE, na.action=na.omit)
endCluster()