Predict cluster membership for new data
pred_clusters.RdAssigns cluster labels to new observations using a previously fitted
clustering model (e.g. from find_cluster). The function
applies the same feature structure as the training data and appends
predicted cluster states to the input data frame.
Arguments
- clusters
A fitted clustering model object (e.g. `kcca` object) returned by
adaptNP::find_cluster. Must supportpredict()and contain training feature ranges in `@xrange`.- newdata
A data frame containing new observations to classify. Must include all variables used to train the clustering model.
- option
Character string specifying how predictions are applied. Options are:
`"same"` (default): predict cluster for each row in `newdata`
`"daily"`: aggregate data to daily means before prediction
`"daily9am"`: placeholder (not yet implemented)
Value
A data frame equal to `newdata` with an additional column:
`state`: predicted cluster label for each observation
Details
The function ensures that `newdata` contains all variables used to train the clustering model. Only these variables are used in prediction.
For `option = "same"`, cluster labels are predicted directly for each observation.
For `option = "daily"`, the data are aggregated to daily resolution (using mean values of numeric columns) before prediction. The resulting cluster labels are then mapped back to the original data.
Input variables are scaled using scale() before prediction.
Currently, automatic detection of timestamp columns is limited and assumes a column named `TIMESTAMP` when using `"daily"` aggregation.
Warning
The function assumes `newdata` contains a `TIMESTAMP` column for daily aggregation.
The `"daily9am"` option is not yet implemented and will result in an error.
No validation is performed on scaling consistency with the original training data.
See also
find_cluster, predict
Examples
# Example: predict clusters for new data
if (FALSE) { # \dontrun{
# Fit clustering model
model <- adaptNP::find_cluster(training_data)
# Predict cluster states for new observations
newdata$TIMESTAMP <- Sys.time() + seq_len(nrow(newdata))
result <- pred_clusters(
clusters = model,
newdata = newdata,
option = "same"
)
head(result$state)
} # }