transformFitted {latrend} | R Documentation |
Helper function for custom lcModel classes implementing fitted.lcModel()
Description
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"
. Thisdata.frame
therefore hasnobs(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))
andK = nClusters(object)
.list
A
list
of cluster-specific predictionvector
s. Each prediction vector should be of lengthnrow(model.data(object))
. The overall (named) list of cluster-specific prediction vectors is of lengthnClusters(object)
.
Users can implement support for other prediction formats by defining the transformFitted
method with other signatures.
Usage
transformFitted(pred, model, clusters)
## S4 method for signature 'NULL,lcModel'
transformFitted(pred, model, clusters = NULL)
## S4 method for signature 'matrix,lcModel'
transformFitted(pred, model, clusters = NULL)
## S4 method for signature 'list,lcModel'
transformFitted(pred, model, clusters = NULL)
## S4 method for signature 'data.frame,lcModel'
transformFitted(pred, model, clusters = NULL)
Arguments
pred |
The cluster-specific predictions for each observation |
model |
The |
clusters |
The trajectory cluster assignment per observation. Optional. |
Value
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.
Example implementation
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")
.