predictHyperspec: Prediction based on train-object and Speclib

predictHyperspecR Documentation

Prediction based on train-object and Speclib

Description

Perform predictions based on a train-object from the caret-package and a hyperspectral dataset from hsdar. See help file to function predict.train of the caret-package for general information on prediction with caret.

Usage

## S4 method for signature 'train,.CaretHyperspectral,missing'
predictHyperspec(object, newdata, preProcess, ...)  
  
## S4 method for signature 'train,.CaretHyperspectral,function'
predictHyperspec(object, newdata, preProcess, ...)

Arguments

object

Object of class train from caret-package

newdata

Object of class Speclib or Nri to predict on.

preProcess

Optional function to be applied on newdata prior to the prediction.

...

Further arguments passed to original train function and/or to the preProcess-function.

Value

Depending on the settings either a vector of predictions if type = "raw" or a data frame of class probabilities for type = "prob". In the latter case, there are columns for each class. For predict.list, a list results. Each element is produced by predict.train. See details in predict.train in the caret-package.

Author(s)

Lukas Lehnert

See Also

predict.train, Speclib

Examples

## Not run: 
## The following example is taken from the journal paper 
## "Hyperspectral Data Analysis in R: the hsdar Package"
## under review at the "Journal of Statistical Software"
  
data(spectral_data)

spectral_data <- noiseFiltering(spectral_data, method = "sgolay", p = 15)

## Convert the chlorophyll measurements stored in the SI dataframe 
## from SPAD-values into mg.
SI(spectral_data)$chlorophyll <- 
  (117.1 * SI(spectral_data)$chlorophyll) /
  (148.84 - SI(spectral_data)$chlorophyll)
  
## Mask spectra  
spectral_data <- spectral_data[, wavelength(spectral_data) >= 310 & 
  wavelength(spectral_data) <= 1000]

## Transform reflectance values into band depth applying a segmented upper hull
## continuum removal.  
spec_bd <- transformSpeclib(spectral_data, method = "sh", out = "bd")

## Select the chlorophyll absorption features at 460 nm and 670 nm for further
## processing
featureSpace <- specfeat(spec_bd, c(460, 670))

## Calculate all parameters from both selected features such as area, distance
## to Gauss curve etc.
featureSpace <- feature_properties(featureSpace)

## Set response and additional predictor variables for random forest model
featureSpace <- setResponse(featureSpace, "chlorophyll")
featureSpace <- setPredictor(featureSpace,
  names(SI(featureSpace))[4:ncol(SI(featureSpace))])

## Define training and cross validation for random forest model tuning
ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 5)

## Partition data set for training and validation
training_validation <- createDataPartition(featureSpace)

## Train random forest model based on training-subset
rfe_trained <- train(featureSpace[training_validation$Resample1,], 
                     trainControl = ctrl, method = "rf")  

## Predict on the validation data set
pred <- predictHyperspec(rfe_trained, featureSpace[-training_validation$Resample1,])

## Plot result for visual interpretation
lim <- range(c(SI(featureSpace,i = -training_validation$Resample1)$chlorophyll,
               pred))
plot(SI(featureSpace,i = -training_validation$Resample1)$chlorophyll, pred,
     ylab = "Predicted chlorophyll content",
     xlab = "Estimated chlorophyll content", 
     xlim = lim, ylim = lim)
lines(par()$usr[c(1,2)],par()$usr[c(3,4)], lty = "dotted")

## End(Not run)

hsdar documentation built on March 18, 2022, 6:35 p.m.