cluster.scores: Cluster Scores

View source: R/cluster.scores.R

cluster.scoresR Documentation

Cluster Scores

Description

This function is used to compute group means by default.

Usage

cluster.scores(x, cluster, fun = c("mean", "sum", "median", "var", "sd", "min", "max"),
              expand = TRUE, names = ".a", as.na = NULL, check = TRUE)

Arguments

x

a numeric vector for computing cluster scores for a variable, matrix or data frame for computing cluster scores for more than one variable.

cluster

a vector representing the nested grouping structure (i.e., group or cluster variable).

fun

character string indicating the function used to compute group scores, default: "mean".

expand

logical: if TRUE, vector of cluster scores is expanded to match the input vector x.

names

a character string or character vector indicating the names of the computed variables when specifying more than one variable. By default, variables are named with the ending ".a" resulting in e.g. "x1.a" and "x2.a". Variable names can also be specified using a character vector matching the number of variables specified in x (e.g., names = c("cluster.x1", "cluster.x2")).

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis. Note that as.na() function is only applied to the argument x, but not to cluster.

check

logical: if TRUE, argument specification is checked.

Value

Returns a numeric vector or data frame containing cluster scores with the same length or same number of rows as x if expand = TRUE or with the length or number of rows as length(unique(cluster)) if expand = FALSE.

Author(s)

Takuya Yanagida takuya.yanagida@univie.ac.at

References

Hox, J., Moerbeek, M., & van de Schoot, R. (2018). Multilevel analysis: Techniques and applications (3rd. ed.). Routledge.

Snijders, T. A. B., & Bosker, R. J. (2012). Multilevel analysis: An introduction to basic and advanced multilevel modeling (2nd ed.). Sage Publishers.

See Also

item.scores, multilevel.descript, multilevel.icc

Examples

dat.ml <- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                     cluster = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
                     x1 = c(4, 2, 5, 6, 3, 4, 1, 3, 4),
                     x2 = c(2, 5, 3, 1, 2, 7, 4, 5, 3))

# Compute cluster means and expand to match the input x
cluster.scores(dat.ml$x1, cluster = dat.ml$cluster)

# Compute standard deviation for each cluster and expand to match the input x
cluster.scores(dat.ml$x1, cluster = dat.ml$cluster, fun = "sd")

# Compute cluster means without expanding the vector
cluster.scores(dat.ml$x1, cluster = dat.ml$cluster, expand = FALSE)

# Compute cluster means and attach to 'dat.ml'
dat.ml <- cbind(dat.ml,
                cluster.scores(dat.ml[, c("x1", "x2")], cluster = dat.ml$cluster))

misty documentation built on Nov. 15, 2023, 1:06 a.m.

Related to cluster.scores in misty...