This article outlines how to create a widget to perform cluster analysis using a finite mixture of normal variables.
Method
1. Create a calculation by clicking Calculation > Custom Code from Anything
in the toolbar or + in the Report tree in Displayr or Create > R Output in Q.
2. In the R Code editor (in Q) or the Code panel (in Displayr), paste the following:
library(flipTransformations)
library(mclust)
variables <- flipTransformations::AsNumeric(QDataFrame(clusteringVariables), binary = binaryCat)
missing.cases = is.na(apply(variables,1,sum))
variables = variables[!missing.cases,]
if (is.null(QCalibratedWeight)) {
mod = Mclust(data = variables[QFilter,])
} else {
stop("This model must be estimated with unweighted data.")
}
`cluster analysis` = summary(mod, parameters = TRUE)3. Under Data > Inputs Javascript in the object inspector (Q) or the Code panel, ensuring Inputs Javascript is selected from the code selector dropdown (in Displayr), paste the following:
form.setHeading("Cluster Analysis")
form.dropBox({label: "Variables",
types:["Question: Number, Pick One, Date", "Variable: Numeric, Date, Money, Categorical, OrderedCategorical"],
name: "clusteringVariables",
multi:true})
form.checkBox({ name: "binaryCat", label: "Categorical as binary", default_value: false });4. Select the variables that you would like to cluster.
5. OPTIONAL: Tick Categorical as binary if you wish to treat unordered categorical variables as binary variables. Otherwise, they are represented as sequential integers (i.e., 1 for the first category, 2 for the second, etc.).
6. OPTIONAL: Apply a filter or weight.