Learn R Programming

misty (version 0.4.5)

cluster.scores: Cluster Scores

Description

This function computes cluster means by default.

Usage

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

Arguments

x

a numeric vector.

cluster

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

fun

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

expand

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

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 containing cluster scores with the same length as x if expand = TRUE or with the length length(unique(cluster)) if expand = FALSE.

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
# NOT RUN {
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),
                     x = c(4, 2, 5, 6, 3, 4, 1, 3, 4))

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

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

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

Run the code above in your browser using DataLab