View source: R/multilevel.descript.R
multilevel.descript | R Documentation |
This function computes descriptive statistics for two-level and three-level multilevel data, e.g. average cluster size, variance components, intraclass correlation coefficient, design effect, and effective sample size.
multilevel.descript(..., data = NULL, cluster, type = c("1a", "1b"),
method = c("aov", "lme4", "nlme"),
print = c("all", "var", "sd"), REML = TRUE, digits = 2,
icc.digits = 3, as.na = NULL, write = NULL, append = TRUE,
check = TRUE, output = TRUE)
... |
a numeric vector, matrix, or data frame. Alternatively, an
expression indicating the variable names in |
data |
a data frame when specifying one or more variables in the
argument |
cluster |
a character string indicating the name of the cluster
variable in |
type |
a character string indicating the type of intraclass
correlation coefficient, i.e., |
method |
a character string indicating the method used to estimate
intraclass correlation coefficients, i.e., |
print |
a character string or character vector indicating which results to
show on the console, i.e. |
REML |
logical: if |
digits |
an integer value indicating the number of decimal places to be used. |
icc.digits |
an integer indicating the number of decimal places to be used for displaying intraclass correlation coefficients. |
as.na |
a numeric vector indicating user-defined missing values,
i.e. these values are converted to |
write |
a character string naming a file for writing the output into
either a text file with file extension |
append |
logical: if |
check |
logical: if |
output |
logical: if |
In a two-level model, the intraclass correlation coefficients, design effect, and the effective sample size are computed based on the random intercept-only model:
Y_{ij} = \gamma_{00} + u_{0j} + r_{ij}
where the variance in Y
is decomposed into two independent components:
\sigma^2_{u_{0}}
, which represents the variance at Level 2, and
\sigma^2_{r}
, which represents the variance at Level 1 (Hox et al.,
2018). For the computation of the intraclass correlation coefficients, see
'Details' in the multilevel.icc
function. The design effect
represents the effect of cluster sampling on the variance of parameter
estimation and is defined by the equation
deff = (\frac{SE_{Cluster}}{SE_{Simple}})^2 = 1 + \rho(J - 1)
where SE_{Cluster}
is the standard error under cluster sampling,
SE_{Simple}
is the standard error under simple random sampling,
\rho
is the intraclass correlation coefficient, ICC(1), and
J
is the average cluster size. The effective sample size is defined
by the equation:
N_{effective} = \frac{N{total}}{deff}
The effective sample size N_{effective}
represents the equivalent total
sample size that we should use in estimating the standard error (Snijders &
Bosker, 2012).
In a three-level model, the intraclass correlation coefficients, design effect, and the effective sample size are computed based on the random intercept-only model:
Y_{ijk} = \gamma_{000} + v_{0k} + u_{0jk} + r_{ijk}
where the variance in Y
is decomposed into three independent components:
\sigma^2_{v_{0}}
, which represents the variance at Level 3,
\sigma^2_{u_{0}}
, which represents the variance at Level 2, and
\sigma^2_{r}
, which represents the variance at Level 1 (Hox et al., 2018).
For the computation of the intraclass correlation coefficients, see 'Details'
in the multilevel.icc
function. The design effect
represents the effect of cluster sampling on the variance of parameter
estimation and is defined by the equation
deff = (\frac{SE_{Cluster}}{SE_{Simple}})^2 = 1 + \rho_{L2}(J - 1) + \rho_{L3}(JK - 1)
where \rho_{L2}
is the ICC(1) at Level 2, \rho_{L3}
is the ICC(1) at Level 3,
J
is the average cluster size at Level 2, and K
is the average
cluster size at Level 3.
Returns an object of class misty.object
, which is a list with following
entries:
call |
function call |
type |
type of analysis |
data |
data frame specified in |
args |
specification of function arguments |
model.fit |
fitted lavaan object ( |
result |
list with result tables, i.e.,
|
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.
write.result
, multilevel.icc
, descript
## Not run:
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
#----------------------------------------------------------------------------
# Two-Level Data
#..........
# Cluster variable specification
# Example 1a: Cluster variable 'cluster'
multilevel.descript(Demo.twolevel[, c("y1", "cluster")], cluster = "cluster")
# Example 1b: Cluster variable 'cluster' not in '...'
multilevel.descript(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 1c: Alternative specification using the 'data' argument
multilevel.descript(y1, data = Demo.twolevel, cluster = "cluster")
#---------------------------
# Example 2: Multilevel descriptive statistics for 'y1'
multilevel.descript(Demo.twolevel$y1, cluster = Demo.twolevel$cluster)
# Example 3: Multilevel descriptive statistics, print variance and standard deviation
multilevel.descript(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, print = "all")
# Example 4: Multilevel descriptive statistics, print ICC with 5 digits
multilevel.descript(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, icc.digits = 5)
# Example 5: Multilevel descriptive statistics
# use lme() function in the nlme package to estimate ICC
multilevel.descript(Demo.twolevel$y1, cluster = Demo.twolevel$cluster, method = "nlme")
# Example 6a: Multilevel descriptive statistics for 'y1', 'y2', 'y3', 'w1', and 'w2'
multilevel.descript(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster)
# Example 6b: Alternative specification using the 'data' argument
multilevel.descript(y1:y3, w1, w2, data = Demo.twolevel, cluster = "cluster")
#----------------------------------------------------------------------------
# Three-Level Data
# Create arbitrary three-level data
Demo.threelevel <- data.frame(Demo.twolevel, cluster2 = Demo.twolevel$cluster,
cluster3 = rep(1:10, each = 250))
#..........
# Cluster variable specification
# Example 7a: Cluster variables 'cluster' in '...'
multilevel.descript(Demo.threelevel[, c("y1", "cluster3", "cluster2")],
cluster = c("cluster3", "cluster2"))
# Example 7b: Cluster variables 'cluster' not in '...'
multilevel.descript(Demo.threelevel$y1, cluster = Demo.threelevel[, c("cluster3", "cluster2")])
# Example 7c: Alternative specification using the 'data' argument
multilevel.descript(y1, data = Demo.threelevel, cluster = c("cluster3", "cluster2"))
#----------------------------------------------------------------------------
# Example 8: Multilevel descriptive statistics for 'y1', 'y2', 'y3', 'w1', and 'w2'
multilevel.descript(y1:y3, w1, w2, data = Demo.threelevel, cluster = c("cluster3", "cluster2"))
#----------------------------------------------------------------------------
# Write Results
# Example 9a: Write results into a Excel file
multilevel.descript(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster, write = "Multilevel_Descript.txt")
# Example 9b: Write results into a Excel file
multilevel.descript(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster, write = "Multilevel_Descript.xlsx")
result <- multilevel.descript(Demo.twolevel[, c("y1", "y2", "y3", "w1", "w2")],
cluster = Demo.twolevel$cluster, output = FALSE)
write.result(result, "Multilevel_Descript.xlsx")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.