Learn R Programming

netCoin (version 2.1.49)

addClusters: Add cluster columns to a netCoin object from surScat.

Description

addClusters adds one or several cluster columns to a netCoin object from surScat.

Usage

addClusters(scatObj, clusters, name=NULL, sort=TRUE,
  weight=NULL, sourceData=NULL, which=NULL, maxGroups=25, rest=NULL)

Value

A 'netCoin' object.

Arguments

scatObj

A 'netCoin' object.

clusters

vector, factor, data.frame holding one clusterization per column, or clustering object (kmeans, poLCA, tidyLPA, looking4clusters).

name

name of the new cluster column. 'clusters' by default. When several clusterizations are added at once, each column keeps its own name, and 'name' becomes a prefix.

sort

sort clusters if requested (by mean coordinate on first layout axis).

weight

a vector of weights.

sourceData

optional data.frame for validation that clusters are in correct order.

which

clusterization to take when 'clusters' is a 'looking4clusters' object, given as a name or an index. Methods run for several numbers of groups yield their optimal solution, unless a specific number is asked for with the "name:k" form, as in which="kmeans:4". When it is left empty, every clusterization held by the object is added as its own column, one per number of groups, named after the method and that number, as in "kmeans(4)".

maxGroups

clusterizations with more groups than this are skipped with a warning, which keeps a continuous variable passed in by mistake from becoming a column of hundreds of groups. NULL adds them all.

rest

the group standing for a residual one, gathering what was left over rather than found. It is pinned after every real group instead of taking a place among them by sorting, and it is labelled with a word ("rest", "resto", "resta") where the others carry a number, so that a reader cannot take it for one more group of the series. addCommunities sets it; a clusterization with no residual group leaves it NULL.

Author

Modesto Escobar, Department of Sociology and Communication, University of Salamanca.

Details

Each column is an ordered factor whose levels follow the group number. Their labels are prefixed with the name of the clusterization they come from, capitalized and without its trailing number of groups, so a column named "hclust(10)" is labelled "Hclust: 01" ... "Hclust: 10". Group numbers are padded with zeros to the width of the largest one, which keeps them in numeric order wherever they are sorted as text.

Each clusterization is named after where it comes from and how many groups it holds, as in "kmeans(4)" for a method of a 'looking4clusters' object. The solutions of a 'tidyLPA' object are named "LPA(4)", and labelled "LPA: 1" ... "LPA: 4", since a latent profile analysis estimated for several numbers of profiles is one analysis. A 'tidyLPA' object estimated for several model specifications at once is no longer a single analysis, so its solutions carry the number tidyLPA gives each specification, as in "LPA1(4)" and "LPA6(4)".

Several clusterizations can be added in one call, either as the columns of a data.frame or as a 'looking4clusters' object. They must all come from the same data, in the same order, as the one given to surScat: only their length is checked, so keeping the order is up to the caller.

A clusterization must hold one assignment per node, or one per case when surScat collapsed the cases into patterns through vPatterns, in which case the cases of each pattern are reduced to their most frequent group. Anything else is an error, which is also what happens when surScat capped the nodes through its maxN argument: the node table then holds a random sample of the cases, or of the patterns, so a clusterization computed on the whole data no longer lines up with it and cannot be matched to it. Compute the clusters on the node table of the object, which is that sample, or raise maxN so that every case is drawn.

A 'looking4clusters' object usually holds many clusterizations at once: those added through its own addcluster function, plus one solution per number of groups for each method it has run. Leaving which empty brings all of them in, whereas which picks a single one. The 'looking4clusters' package needs not be installed: the object is read through its list structure.

Each clusterization added gets, in $clusters, one entry per plane the object holds (see surScat and addAxes), named by joining the clusterization and the plane with a dot, as in scatObj$clusters[["kmeans(3).petals"]]; each entry is a matrix with one row per group and its centroid coordinates. Where surScat collapsed the cases into patterns, a pattern spanning several groups keeps in its column the compound value (as in "Group: 1|Group: 2"), but there is still one centroid per group: the pattern takes part in the centroid of each group it spans with the (weighted) cases it holds of that group. These counts, one matrix per clusterization with a row per node and a column per group, are kept in the attribute "clusterShares" of the object, since the group of every case is no longer known once the cases are collapsed. $clusterMeans is updated as well, with the mean of every group of every clusterization on every numeric variable of the node table (see surScat). Printing $clusters, on its own or as part of the object, only states how many clusterizations and planes it covers.