Feature-based clustering.

lcMethodFeature(
  response,
  representationStep,
  clusterStep,
  standardize = scale,
  center = meanNA,
  time = getOption("latrend.time"),
  id = getOption("latrend.id"),
  ...
)

Arguments

response

The name of the response variable.

representationStep

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.

clusterStep

A function with signature function(repdata) that outputs a lcModel.

standardize

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.

center

The function for computing the longitudinal cluster centers, used for representing the cluster trajectories.

time

The name of the time variable.

id

The name of the trajectory identification variable.

...

Additional arguments.

Linear regresion & k-means example

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)
)