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 the **=** sign above **Calculation** in the toolbar in Displayr or **Create > R Output** in Q.

2. Under **Properties > R CODE**, paste the below in:

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 **Properties > INPUTS JAVASCRIPT**, paste the below in:

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.