Predicts the expected trajectory observations at the given time for each cluster.
# S3 method for class 'lcModel'
predict(object, newdata = NULL, what = "mu", ..., useCluster = NA)
The lcModel
object.
Optional data.frame
for which to compute the model predictions. If omitted, the model training data is used.
Cluster trajectory predictions are made when ids are not specified.
The distributional parameter to predict. By default, the mean response 'mu' is predicted. The cluster membership predictions can be obtained by specifying what = 'mb'
.
Additional arguments.
Whether to use the "Cluster" column in the newdata argument for computing predictions conditional on the respective cluster.
For useCluster = NA
(the default), the feature is enabled if newdata contains the "Cluster" column.
If newdata
specifies the cluster membership; a data.frame
of cluster-specific predictions. Otherwise, a list
of data.frame
of cluster-specific predictions is returned.
Note: Subclasses of lcModel
should preferably implement predictForCluster()
instead of overriding predict.lcModel
as that function is designed to be easier to implement because it is single-purpose.
The predict.lcModelExt
function should be able to handle the case where newdata = NULL
by returning the fitted values.
After post-processing the non-NULL newdata input, the observation- and cluster-specific predictions can be computed.
Lastly, the output logic is handled by the transformPredict()
function. It converts the computed predictions (e.g., matrix
or data.frame
) to the appropriate output format.
predict.lcModelExt <- function(object, newdata = NULL, what = "mu", ...) {
if (is.null(newdata)) {
newdata = model.data(object)
if (hasName(newdata, 'Cluster')) {
# allowing the Cluster column to remain would break the fitted() output.
newdata[['Cluster']] = NULL
}
}
# compute cluster-specific predictions for the given newdata
pred <- NEWDATA_COMPUTATIONS_HERE
transformPredict(pred = pred, model = object, newdata = newdata)
})
predictForCluster stats::predict fitted.lcModel clusterTrajectories trajectories predictPostprob predictAssignments
Other lcModel functions:
clusterNames()
,
clusterProportions()
,
clusterSizes()
,
clusterTrajectories()
,
coef.lcModel()
,
converged()
,
deviance.lcModel()
,
df.residual.lcModel()
,
estimationTime()
,
externalMetric()
,
fitted.lcModel()
,
fittedTrajectories()
,
getCall.lcModel()
,
getLcMethod()
,
ids()
,
lcModel-class
,
metric()
,
model.frame.lcModel()
,
nClusters()
,
nIds()
,
nobs.lcModel()
,
plot-lcModel-method
,
plotClusterTrajectories()
,
plotFittedTrajectories()
,
postprob()
,
predictAssignments()
,
predictForCluster()
,
predictPostprob()
,
qqPlot()
,
residuals.lcModel()
,
sigma.lcModel()
,
strip()
,
time.lcModel()
,
trajectoryAssignments()
data(latrendData)
method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time")
model <- latrend(method, latrendData)
predFitted <- predict(model) # same result as fitted(model)
# Cluster trajectory of cluster A
predCluster <- predict(model, newdata = data.frame(Cluster = "A", Time = time(model)))
# Prediction for id S1 given cluster A membership
predId <- predict(model, newdata = data.frame(Cluster = "A", Id = "S1", Time = time(model)))
# Prediction matrix for id S1 for all clusters
predIdAll <- predict(model, newdata = data.frame(Id = "S1", Time = time(model)))