R/model-transform.R
transformFitted.Rd
A helper function for implementing the fitted.lcModel()
method as part of your own lcModel
class, ensuring the correct output type and format (see the Value section).
Note that this function has no use outside of implementing fitted.lcModel
.
The function makes it easier to implement fitted.lcModel
based on existing implementations that may output their results in different data formats. Furthermore, the function checks whether the input data is valid.
The prediction ordering depends on the ordering of the data observations that was used for fitting the lcModel
.
By default, transformFitted()
accepts one of the following inputs:
data.frame
A data.frame
in long format providing a cluster-specific prediction for each observation per row, with column names "Fit"
and "Cluster"
. This data.frame
therefore has nobs(object) * nClusters(object)
rows.
matrix
An N-by-K matrix
where each row provides the cluster-specific predictions for the respective observation. Here, N = nrow(model.data(object))
and K = nClusters(object)
.
list
A list
of cluster-specific prediction vector
s. Each prediction vector should be of length nrow(model.data(object))
. The overall (named) list of cluster-specific prediction vectors is of length nClusters(object)
.
Users can implement support for other prediction formats by defining the transformFitted
method with other signatures.
transformFitted(pred, model, clusters)
# S4 method for class 'NULL,lcModel'
transformFitted(pred, model, clusters = NULL)
# S4 method for class 'matrix,lcModel'
transformFitted(pred, model, clusters = NULL)
# S4 method for class 'list,lcModel'
transformFitted(pred, model, clusters = NULL)
# S4 method for class 'data.frame,lcModel'
transformFitted(pred, model, clusters = NULL)
If the clusters
argument was specified, a vector
of fitted values conditional on the given cluster assignment. Else, a matrix
with the fitted values per cluster per column.
A typical implementation of fitted.lcModel()
for your own lcModel
class would have the following format:
fitted.lcModelExample <- function(object,
clusters = trajectoryAssignments(object)) {
# computations of the fitted values per cluster here
predictionMatrix <- CODE_HERE
transformFitted(pred = predictionMatrix, model = object, clusters = clusters)
}
For a complete and runnable example, see the custom models vignette accessible via vignette("custom", package = "latrend")
.