Learn R Programming

dtwclust (version 3.1.1)

tsclusters-methods: Methods for TSClusters

Description

Methods associated with TSClusters-class and derived objects.

Usage

"initialize"(.Object, ..., override.family = TRUE)
"show"(object)
"update"(object, ..., evaluate = TRUE)
"update"(object, ..., evaluate = TRUE)
"predict"(object, newdata = NULL, ...)
"predict"(object, newdata = NULL, ...)
"plot"(x, y, ..., clus = seq_len(x@k), labs.arg = NULL, series = NULL, time = NULL, plot = TRUE, type = NULL)
"plot"(x, y, ..., clus = seq_len(x@k), labs.arg = NULL, series = NULL, time = NULL, plot = TRUE, type = NULL)

Arguments

.Object
A TSClusters prototype. You shouldn't use this, see Initialize section and the examples.
...
For initialize, any valid slots. For plot, further arguments to pass to geom_line for the plotting of the cluster centroids, or to plot.hclust. See Plotting section. For update, any supported argument. Otherwise ignored.
override.family
Logical. Attempt to substitute the default family with one that conforms to the provided elements? See Initialize section.
object, x
An object that inherits from TSClusters-class as returned by tsclust.
evaluate
Logical. Defaults to TRUE and evaluates the updated call, which will result in a new TSClusters object. Otherwise, it returns the unevaluated call.
newdata
New data to be assigned to a cluster. It can take any of the supported formats of tsclust. Note that for multivariate series, this means that it must be a list of matrices, even if the list has only one element.
y
Ignored.
clus
A numeric vector indicating which clusters to plot.
labs.arg
Arguments to change the title and/or axis labels. See labs for more information
series
Optionally, the data in the same format as it was provided to tsclust.
time
Optional values for the time axis. If series have different lengths, provide the time values of the longest series.
plot
Logical flag. You can set this to FALSE in case you want to save the ggplot object without printing anything to screen
type
What to plot. NULL means default. See details.

Value

The plot method returns a gg object (or NULL for dendrogram plot) invisibly.

Initialize

The initialize method is used when calling new. The family slot can be substituted with an appropriate one if certain elements are provided by the user. The initialize methods of derived classes also inherit the family and can use it to calculate other slots. In order to get a fully functional object, at least the following slots should be provided:
  • type: "partitional", "hierarchical", "fuzzy" or "tadpole".
  • datalist: The data in one of the supported formats.
  • centroids: The time series centroids in one of the supported formats.
  • cluster: The cluster indices for each series in the datalist.
  • control*: A tsclust-controls object with the desired parameters.
  • distance*: A string indicating the distance that should be used.
  • centroid*: A string indicating the centroid to use (only necessary for partitional clustering).
*Necessary when overriding the default family for the calculation of other slots, CVIs or prediction. Maybe not always needed, e.g. for plotting.

Plotting

The plot method uses the ggplot2 plotting system (see ggplot). The default depends on whether a hierarchical method was used or not. In those cases, the dendrogram is plotted by default; you can pass any extra parameters to plot.hclust via .... Otherwise, the function plots the time series of each cluster along with the obtained centroid. The default values for cluster centroids are: linetype = "dashed", size = 1.5, colour = "black", alpha = 0.5. You can change this by means of .... You can choose what to plot with the type parameter. Possible options are:
  • "dendrogram": Only available for hierarchical clustering.
  • "series": Plot the time series divided into clusters without including centroids.
  • "centroids": Plot the obtained centroids only.
  • "sc": Plot both series and centroids
If created, the function returns the gg object invisibly, in case you want to modify it to your liking. You might want to look at ggplot_build if that's the case. If you want to free the scale of the X axis, you can do the following: plot(object, plot = FALSE) + facet_wrap(~cl, scales = "free")

Details

The update method takes the original function call, replaces any provided argument and optionally evaluates the call again. Use evaluate = FALSE if you want to get the unevaluated call.

The predict generic can take the usual newdata argument and it returns the cluster(s) to which the data belongs; if NULL, it simply returns the obtained cluster indices. It preprocesses the data with the corresponding function if available.

Examples

Run this code

data(uciCT)

# Assuming this was generated by some clustering procedure
centroids <- CharTraj[seq(1L, 100L, 5L)]
cluster <- unclass(CharTrajLabels)

pc_obj <- new("PartitionalTSClusters",
              type = "partitional", datalist = CharTraj,
              centroids = centroids, cluster = cluster,
              distance = "sbd", centroid = "dba",
              control = partitional_control(),
              args = tsclust_args(cent = list(window.size = 8L, norm = "L2")))

fc_obj <- new("FuzzyTSClusters",
              type = "fuzzy", datalist = CharTraj,
              centroids = centroids, cluster = cluster,
              distance = "sbd", centroid = "fcm",
              control = fuzzy_control())

fc_obj

Run the code above in your browser using DataLab