Feature-based clustering.
The name of the response variable.
A function
with signature function(method, data)
that computes the representation per strata, returned as a matrix
.
Alternatively, representationStep
is a pre-computed representation matrix
.
A function
with signature function(repdata)
that outputs a lcModel
.
A function
to standardize the output matrix
of the representation step. By default, the output is shifted and rescaled to ensure zero mean and unit variance.
The function
for computing the longitudinal cluster centers, used for representing the cluster trajectories.
The name of the time variable.
The name of the trajectory identification variable.
Additional arguments.
In this example we define a feature-based approach where each trajectory is represented using a linear regression model. The coefficients of the trajectories are then clustered using k-means.
Note that this method is already implemented as lcMethodLMKM()
.
Representation step:
repStep <- function(method, data, verbose) {
library(data.table)
library(magrittr)
xdata = as.data.table(data)
coefdata <- xdata[,
lm(method$formula, .SD)
keyby = c(method$id)
]
# exclude the id column
coefmat <- subset(coefdata, select = -1)
rownames(coefmat) <- coefdata[[method$id]]
return(coefmat)
}
Cluster step:
clusStep <- function(method, data, repMat, envir, verbose) {
km <- kmeans(repMat, centers = method$nClusters)
lcModelPartition(
response = method$response,
data = data,
trajectoryAssignments = km$cluster
)
}
Now specify the method and fit the model:
data(latrendData)
method <- lcMethodFeature(
formula = Y ~ Time,
response = "Y",
id = "Id",
time = "Time",
representationStep = repStep,
clusterStep = clusStep
model <- latrend(method, data = latrendData)
)
Other lcMethod implementations:
getArgumentDefaults()
,
getArgumentExclusions()
,
lcMethod-class
,
lcMethodAkmedoids
,
lcMethodCrimCV
,
lcMethodDtwclust
,
lcMethodFunFEM
,
lcMethodFunction
,
lcMethodGCKM
,
lcMethodKML
,
lcMethodLMKM
,
lcMethodLcmmGBTM
,
lcMethodLcmmGMM
,
lcMethodMclustLLPA
,
lcMethodMixAK_GLMM
,
lcMethodMixtoolsGMM
,
lcMethodMixtoolsNPRM
,
lcMethodRandom
,
lcMethodStratify