Learn R Programming

misty (version 0.6.7)

cluster.scores: Cluster Scores

Description

This function computes group means by default.

Usage

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

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.

Arguments

...

a numeric vector for computing cluster scores for a variable, matrix or data frame for computing cluster scores for more than one variable. Alternatively, an expression indicating the variable names in data e.g., ci.mean(x1, x2, data = dat). Note that the operators ., +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

data

a data frame when specifying one or more variables in the argument .... Note that the argument is NULL when specifying a numeric vector, matrix, or data frame for the argument ....

cluster

either a character string indicating the variable name of the cluster variable in ... or data, or 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 (default), vector of cluster scores is expanded to match the input vector x.

append

logical: if TRUE (default), cluster scores are appended to the data frame specified in the argument data.

name

a character string or character vector indicating the names of the computed variables. 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., name = 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 (default), argument specification is checked.

Author

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

Run this code
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")

# Example 1a: Compute cluster means for 'y1' and expand to match the input 'y1'
cluster.scores(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)

# Example 1b: Alternative specification using the 'data' argument
cluster.scores(y1, data = Demo.twolevel, cluster = "cluster")

# Example 2: Compute standard deviation for each cluster
# and expand to match the input x
cluster.scores(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, fun = "sd")

# Example 3: Compute cluster means without expanding the vector
cluster.scores(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, expand = FALSE)

# Example 4a: Compute cluster means for 'y1' and 'y2' and append to 'Demo.twolevel'
cbind(Demo.twolevel,
      cluster.scores(Demo.twolevel[, c("y1", "y2")], cluster = Demo.twolevel$cluster))

# Example 4b: Alternative specification using the 'data' argument
cluster.scores(y1, y2, data = Demo.twolevel, cluster = "cluster")

Run the code above in your browser using DataLab