| transformPredict | R Documentation |
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.frameA 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.
matrixAn 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).
vectorA 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 signature 'NULL,lcModel'
transformPredict(pred, model, newdata)
## S4 method for signature 'vector,lcModel'
transformPredict(pred, model, newdata)
## S4 method for signature 'matrix,lcModel'
transformPredict(pred, model, newdata)
## S4 method for signature 'data.frame,lcModel'
transformPredict(pred, model, newdata)
pred |
The (per-cluster) predictions for |
model |
The |
newdata |
A |
A data.frame with the predictions, or a list of cluster-specific prediction data.frames.
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.