Learn R Programming

dtwSat (version 0.2.8)

twdtwAssess: Assess TWDTW classification

Description

Performs an accuracy assessment of the classified maps. The function returns Overall Accuracy, User's Accuracy, Produce's Accuracy, error matrix (confusion matrix), and estimated area according to Olofsson:2013,Olofsson:2014;textualdtwSat. The function returns the metrics for each time interval and a summary considering all classified intervals.

Usage

# S4 method for twdtwRaster
twdtwAssess(
  object,
  y,
  labels = NULL,
  id.labels = NULL,
  proj4string = NULL,
  conf.int = 0.95,
  rm.nosample = FALSE,
  start_date = NULL
)

# S4 method for data.frame twdtwAssess(object, area, conf.int = 0.95, rm.nosample = TRUE)

# S4 method for table twdtwAssess(object, area, conf.int = 0.95, rm.nosample = TRUE)

# S4 method for matrix twdtwAssess(object, area, conf.int = 0.95, rm.nosample = TRUE)

# S4 method for twdtwMatches twdtwAssess(object, area, conf.int = 0.95, rm.nosample = TRUE)

Arguments

object

An object of class twdtwRaster resulting from the classification, i.e. twdtwClassify. The argument can also receive an error matrix (confusion matrix) using the classes data.frame or table. In this case the user must provide the area for each class to the argument area.

y

A data.frame whose attributes are: longitude, latitude, the start ''from'' and the end ''to'' of the time interval for each sample. This can also be a SpatialPointsDataFrame whose attributes are the start ''from'' and the end ''to'' of the time interval. If missing ''from'' and/or ''to'', they are set to the time range of the object.

labels

Character vector with time series labels. For signature twdtwRaster this argument can be used to set the labels for each sample in y, or it can be combined with id.labels to select samples with a specific label.

id.labels

A numeric or character with an column name from y to be used as samples labels. Optional.

proj4string

Projection string, see CRS-class. Used if y is a data.frame.

conf.int

Specifies the confidence level (0-1).

rm.nosample

If sum of columns and sum of rows of the error matrix are zero then remove class. Default is TRUE.

start_date

A date. Required if there is only one map to be assessed. Usually this is the first date of the timeline from satellite images.

area

A numeric vector with the area for each class if the argument object is an error matrix (confusion matrix). If object is twdtwMatches area can be either a vector with the area of each classified object, or a single number if the objects are single pixels.

Author

Victor Maus, vwmaus1@gmail.com

References

Maus:2019dtwSat

Maus:2016dtwSat

See Also

twdtwClassify, twdtwAssessment, and twdtwXtable.

Examples

Run this code
if (FALSE) {
  
# Example of TWDTW analysis using raster files 
library(dtwSat)
library(caret) 

# Load raster data 
evi  <- brick(system.file("lucc_MT/data/evi.tif",  package = "dtwSat"))
ndvi <- brick(system.file("lucc_MT/data/ndvi.tif", package = "dtwSat"))
red  <- brick(system.file("lucc_MT/data/red.tif",  package = "dtwSat"))
blue <- brick(system.file("lucc_MT/data/blue.tif", package = "dtwSat"))
nir  <- brick(system.file("lucc_MT/data/nir.tif",  package = "dtwSat"))
mir  <- brick(system.file("lucc_MT/data/mir.tif",  package = "dtwSat"))
doy  <- brick(system.file("lucc_MT/data/doy.tif",  package = "dtwSat"))
timeline <- 
  scan(system.file("lucc_MT/data/timeline", package = "dtwSat"), what="date")

# Create raster time series 
rts <- twdtwRaster(evi, ndvi, red, blue, nir, mir, timeline = timeline, doy = doy)

# Load field samples and projection 
field_samples <- 
  read.csv(system.file("lucc_MT/data/samples.csv", package = "dtwSat"))
proj_str <- 
  scan(system.file("lucc_MT/data/samples_projection", package = "dtwSat"), 
       what = "character")

# Split samples for training (10%) and validation (90%) using stratified sampling 
set.seed(1)
I <- unlist(createDataPartition(field_samples$label, p = 0.1))
training_samples <- field_samples[I, ]
validation_samples <- field_samples[-I, ]

# Get time series form raster
training_ts <- getTimeSeries(rts, y = training_samples, proj4string = proj_str)
validation_ts <- getTimeSeries(rts, y = validation_samples, proj4string = proj_str)

# Create temporal patterns 
temporal_patterns <- createPatterns(training_ts, freq = 8, formula = y ~ s(x))

# Set TWDTW weight function 
log_fun <- logisticWeight(-0.1, 50)

# Run TWDTW analysis 
system.time(
  r_twdtw <-
    twdtwApply(x = rts, y = temporal_patterns, weight.fun = log_fun, progress = 'text') 
)

# Plot TWDTW distances for the first year 
plot(r_twdtw, type = "distance", time.levels = 1)

# Classify raster based on the TWDTW analysis 
r_lucc <- twdtwClassify(r_twdtw, progress = 'text')

# Plot TWDTW classification results 
plot(r_lucc, type = "map")

# Assess classification 
twdtw_assess <- 
  twdtwAssess(object = r_lucc, y = validation_samples, 
              proj4string = proj_str, conf.int = .95) 

# Plot map accuracy 
plot(twdtw_assess, type = "accuracy")

# Plot area uncertainty 
plot(twdtw_assess, type = "area")

# Plot misclassified samples  
plot(twdtw_assess, type = "map", samples = "incorrect") 

# Get latex table with error matrix 
twdtwXtable(twdtw_assess, table.type = "matrix")

# Get latex table with error accuracy 
twdtwXtable(twdtw_assess, table.type = "accuracy")

# Get latex table with area uncertainty 
twdtwXtable(twdtw_assess, table.type = "area")

}


# Total mapped area by class. Data from [1]
area = c(A = 22353, B = 1122543, C = 610228) 

# Error matrix, columns (Reference) rows (Map)
x = 
    rbind(
         c( 97,	 0,   3),
         c(  3, 279,  18),
         c(  2,   1,  97)
   )

table_assess = twdtwAssess(x, area, conf.int = .95)

table_assess

plot(table_assess, type="area", perc=FALSE)

plot(table_assess, type="accuracy")

if (FALSE) {
  
# Example of TWDTW analysis using raster files 
library(dtwSat)
library(caret) 

# Load raster data 
evi  <- brick(system.file("lucc_MT/data/evi.tif",  package = "dtwSat"))
ndvi <- brick(system.file("lucc_MT/data/ndvi.tif", package = "dtwSat"))
red  <- brick(system.file("lucc_MT/data/red.tif",  package = "dtwSat"))
blue <- brick(system.file("lucc_MT/data/blue.tif", package = "dtwSat"))
nir  <- brick(system.file("lucc_MT/data/nir.tif",  package = "dtwSat"))
mir  <- brick(system.file("lucc_MT/data/mir.tif",  package = "dtwSat"))
doy  <- brick(system.file("lucc_MT/data/doy.tif",  package = "dtwSat"))
timeline <- 
  scan(system.file("lucc_MT/data/timeline", package = "dtwSat"), what="date")

# Create raster time series 
rts <- twdtwRaster(evi, ndvi, red, blue, nir, mir, timeline = timeline, doy = doy)

# Load field samples and projection 
field_samples <- 
  read.csv(system.file("lucc_MT/data/samples.csv", package = "dtwSat"))
proj_str <- 
  scan(system.file("lucc_MT/data/samples_projection", package = "dtwSat"), 
       what = "character")

# Split samples for training (10%) and validation (90%) using stratified sampling 
set.seed(1)
I <- unlist(createDataPartition(field_samples$label, p = 0.1))
training_samples <- field_samples[I, ]
validation_samples <- field_samples[-I, ]

# Get time series form raster
training_ts <- getTimeSeries(rts, y = training_samples, proj4string = proj_str)
validation_ts <- getTimeSeries(rts, y = validation_samples, proj4string = proj_str)

# Create temporal patterns 
temporal_patterns <- createPatterns(training_ts, freq = 8, formula = y ~ s(x))

# Set TWDTW weight function 
log_fun <- logisticWeight(-0.1, 50)

# Run TWDTW analysis 
system.time(
  r_twdtw <-
    twdtwApply(x = rts, y = temporal_patterns, weight.fun = log_fun, progress = 'text') 
)

# Plot TWDTW distances for the first year 
plot(r_twdtw, type = "distance", time.levels = 1)

# Classify raster based on the TWDTW analysis 
r_lucc <- twdtwClassify(r_twdtw, progress = 'text')

# Plot TWDTW classification results 
plot(r_lucc, type = "map")

# Assess classification 
twdtw_assess <- 
  twdtwAssess(object = r_lucc, y = validation_samples, 
              proj4string = proj_str, conf.int = .95) 

# Plot map accuracy 
plot(twdtw_assess, type = "accuracy")

# Plot area uncertainty 
plot(twdtw_assess, type = "area")

# Plot misclassified samples  
plot(twdtw_assess, type = "map", samples = "incorrect") 

# Get latex table with error matrix 
twdtwXtable(twdtw_assess, table.type = "matrix")

# Get latex table with error accuracy 
twdtwXtable(twdtw_assess, table.type = "accuracy")

# Get latex table with area uncertainty 
twdtwXtable(twdtw_assess, table.type = "area")

}

Run the code above in your browser using DataLab