Learn R Programming

misty (version 0.4.1)

group.scores: Group Scores

Description

This function computes group means by default.

Usage

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

Arguments

x

a numeric vector.

group

a integer vector, character vector, or factor representing the grouping structure (i.e., group variable).

fun

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

expand

logical: if TRUE, vector of group 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 group.

check

logical: if TRUE, argument specification is checked.

Value

Returns a numeric vector containing group scores with the same length as x if expand = TRUE or with the length length(unique(group)) 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),
                     group = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
                     x = c(4, 2, 5, 6, 3, 4, 1, 3, 4))

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

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

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

Run the code above in your browser using DataLab