twdtw_knn1: Train a KNN-1 TWDTW model

View source: R/train.R

twdtw_knn1R Documentation

Train a KNN-1 TWDTW model

Description

This function prepares a KNN-1 model with the Time Warp Dynamic Time Warping (TWDTW) algorithm.

Usage

twdtw_knn1(
  x,
  y,
  resampling_fun = NULL,
  resampling_freq = NULL,
  time_weight,
  cycle_length,
  time_scale,
  start_column = "start_date",
  end_column = "end_date",
  label_colum = "label",
  ...
)

Arguments

x

A three-dimensional stars object (x, y, time) with bands as attributes.

y

An sf object with the coordinates of the training points.

resampling_fun

a function specifying how to create temporal patterns using the samples. If not defined, it will keep all samples. Note that reducing the samples to patterns can significantly improve computational time of predictions. The resampling function must receive a single data frame as argument an return a model. See details.

resampling_freq

The time for sampling the time series if resampling_fun is given. If NULL, the function will infer the frequency of observations in x.

time_weight

A numeric vector with length two (steepness and midpoint of logistic weight) or a function. See details in twdtw.

cycle_length

The length of the cycle, e.g. phenological cycles. Details in twdtw.

time_scale

Specifies the time scale for the observations. Details in twdtw.

start_column

Name of the column in y that indicates the start date. Default is 'start_date'.

end_column

Name of the column in y that indicates the end date. Default is 'end_date'.

label_colum

Name of the column in y containing land use labels. Default is 'label'.

...

Additional arguments passed to twdtw.

Details

If resampling_fun not informed, the KNN-1 model will retain all training samples.

If a custom smoothing function is passed to resampling_fun, the function will be used to resample values of samples sharing the same label (land cover class).

The custom smoothing function takes a single data frame and returns a model. The data frame has two named columns:

  • x is the first column representing the independent variable (typically time).

  • y is the second column representing the dependent variable (e.g., band values) corresponding to each coordinate in x.

Smooting the samples can significantly reduce the processing time for prediction using twdtw_knn1 model.

See the examples section for further clarity.

Value

A 'twdtw_knn1' model containing the trained model information and the data used.

Examples

## Not run: 

# Read training samples
samples_path <-

samples <- st_read(samples_path, quiet = TRUE)

# Get satellite image time sereis files
tif_path <- system.file("mato_grosso_brazil", package = "dtwSat")
tif_files <- dir(tif_path, pattern = "\\.tif$", full.names = TRUE)

# Get acquisition dates
acquisition_date <- regmatches(tif_files, regexpr("[0-9]{8}", tif_files))
acquisition_date <- as.Date(acquisition_date, format = "%Y%m%d")

# Create a 3D datacube
dc <- read_stars(tif_files,
                 proxy = FALSE,
                 along = list(time = acquisition_date),
                 RasterIO = list(bands = 1:6))
dc <- st_set_dimensions(dc, 3, c("EVI", "NDVI", "RED", "BLUE", "NIR", "MIR"))
dc <- split(dc, c("band"))

# Create a knn1-twdtw model
m <- twdtw_knn1(
 x = dc,
 y = samples,
 resampling_fun = function(data) mgcv::gam(y ~ s(x), data = data),
 cycle_length = 'year',
 time_scale = 'day',
 time_weight = c(steepness = 0.1, midpoint = 50))

print(m)

# Visualize model patterns
plot(m)

# Classify satellite images
system.time(lu <- predict(dc, model = m))

# Visualise land use classification
ggplot() +
  geom_stars(data = lu) +
  theme_minimal()


# Create a knn1-twdtw model with custom smoothing function

m <- twdtw_knn1(x = dc,
 y = samples,
 resampling_fun = function(data) lm(y ~ factor(x), data = data),
 cycle_length = 'year',
 time_scale = 'day',
 time_weight = c(steepness = 0.1, midpoint = 50))

plot(m)


## End(Not run)

vwmaus/dtwSat documentation built on Jan. 28, 2024, 9 a.m.