### 6.1.1 Create your equation. Equation creation follows the same pattern as with classification, and that section should be reviewed. The response, of course, will never be categorical, but some predictor variables might be. The following are two ways to set your equation.
### If the data were imported using the commands above for shapefiles or externally prepared data were imported, all bands will be used as potential predictors, and none of the variables are categorical.
equation <- amount ~ .
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 <- amount ~ B1 + B2 + factor(B3) + B4
equation <- as.formula(equation)
### 6.1.2 Compare continuous models.
### Define which methods to analyze. Add and/or subtract methods as desired.
mthd <- c("rf", "rpart", "svmLinear", "svmRadial", "svmPoly", "pls", "nnet", "kknn", "foba", "cubist", "cforest", "glmStepAIC", "earth")
### Step through each method, saving the results in Word file.
for (x in mthd) {
train.model <- train(equation, method = x, data=train.full)
predicted <- predict (train.model, newdata = valid.full)
observed <- valid.full$amount
results <- t.test(observed, predicted, paired=TRUE)
capture.output(x, results, file = "results.txt", append = TRUE)
}
### 6.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 your 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="FLT4S", overwrite=TRUE, na.action=na.omit)
endCluster()