R/model-transform.R
transformPredict.Rd
A helper function for implementing the predict.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 ensuring valid output for predict.lcModel
.
For implementing lcModel
predictions from scratch, it is advisable to implement predictForCluster instead of predict.lcModel.
The prediction ordering corresponds to the observation ordering of the newdata
argument.
By default, transformPredict()
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 nrow(model.data(object)) * nClusters(object)
rows.
matrix
An N-by-K matrix
where each row provides the cluster-specific predictions for the respective observations in newdata
.
Here, N = nrow(newdata)
and K = nClusters(object)
.
vector
A vector
of length nrow(newdata)
with predictions corresponding to the rows of newdata
.
Users can implement support for other prediction formats by defining the transformPredict()
method with other signatures.
transformPredict(pred, model, newdata)
# S4 method for class 'NULL,lcModel'
transformPredict(pred, model, newdata)
# S4 method for class 'vector,lcModel'
transformPredict(pred, model, newdata)
# S4 method for class 'matrix,lcModel'
transformPredict(pred, model, newdata)
# S4 method for class 'data.frame,lcModel'
transformPredict(pred, model, newdata)
A data.frame
with the predictions, or a list of cluster-specific prediction data.frame
s.
In case we have a custom lcModel
class based on an existing internal model representation with a predict()
function,
we can use transformPredict()
to easily transform the internal model predictions to the right format.
A common output is a matrix
with the cluster-specific predictions.
predict.lcModelExample <- function(object, newdata) {
predictionMatrix <- predict(object@model, newdata)
transformPredict(
pred = predictionMatrix,
model = object,
newdata = newdata
)
}
However, for ease of implementation it is generally advisable to implement predictForCluster instead of predict.lcModel.
For a complete and runnable example, see the custom models vignette accessible via vignette("custom", package = "latrend")
.
predictForCluster, predict.lcModel