View source: R/cluster.scores.R
cluster.scores | R Documentation |
This function is used to compute group means by default.
cluster.scores(x, cluster, fun = c("mean", "sum", "median", "var", "sd", "min", "max"),
expand = TRUE, names = ".a", as.na = NULL, check = TRUE)
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: |
expand |
logical: if |
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 |
as.na |
a numeric vector indicating user-defined missing values, i.e.
these values are converted to |
check |
logical: if |
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
.
Takuya Yanagida takuya.yanagida@univie.ac.at
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.
item.scores
, multilevel.descript
,
multilevel.icc
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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.