Title: | Power Analysis and Data Simulation for Multilevel Models |
---|---|
Description: | A declarative language for specifying multilevel models, solving for population parameters based on specified variance-explained effect size measures, generating data, and conducting power analyses to determine sample size recommendations. The specification allows for any number of within-cluster effects, between-cluster effects, covariate effects at either level, and random coefficients. Moreover, the models do not assume orthogonal effects, and predictors can correlate at either level and accommodate models with multiple interaction effects. |
Authors: | Brian T. Keller [aut, cre, cph] |
Maintainer: | Brian T. Keller <[email protected]> |
License: | GPL-3 |
Version: | 1.0.9 |
Built: | 2025-02-17 02:44:19 UTC |
Source: | https://github.com/bkeller2/mlmpower |
mp_data
using lme4::lmer
Analyzes a single mp_data
based on the data generating model.
analyze(data, alpha = 0.05, no_lrt = FALSE, ...)
analyze(data, alpha = 0.05, no_lrt = FALSE, ...)
data |
a |
alpha |
the significance level to determine if an effect is statistically significant.
If |
no_lrt |
do not perform additional likelihood ratio tests.
Setting to |
... |
other arguments passed to |
A list
that with the following named elements:
estimates
: The estimates from fitting the model.
sig_test
: The logical if the estimates were statistically siginificant based on alpha
.
parameters
: The mp_parameters
extracted from data
.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(198723) # Create data set and analyze model |> generate(5, 50) |> analyze() -> results
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(198723) # Create data set and analyze model |> generate(5, 50) |> analyze() -> results
mp_power
to a Data FrameOutputs mp_power
as a data frame.
## S3 method for class 'mp_power' as.data.frame(x, row.names = NULL, optional = FALSE, power = TRUE, ...)
## S3 method for class 'mp_power' as.data.frame(x, row.names = NULL, optional = FALSE, power = TRUE, ...)
x |
a |
row.names |
passed to |
optional |
passed to |
power |
logical: do you want the power or the estimates |
... |
other arguments not used by this method. |
returns a data frame
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) -> powersim # Obtain Results as a data frame as.data.frame(powersim)
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) -> powersim # Obtain Results as a data frame as.data.frame(powersim)
mp_parameters
to a list
A wrapper to coerce mp_parameters
to a list
.
## S3 method for class 'mp_parameters' as.list(x, ...)
## S3 method for class 'mp_parameters' as.list(x, ...)
x |
the |
... |
additional arguments passed to |
a list
# Specify model model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # Obtain parameters and convert to a list model |> summary() |> as.list() -> param_list
# Specify model model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # Obtain parameters and convert to a list model |> summary() |> as.list() -> param_list
mp_data
Provides multilevel centering of a mp_data
data set.
center(data, all = FALSE, ...)
center(data, all = FALSE, ...)
data |
|
all |
a logical value to center all variables based on model defaults |
... |
see details below |
The ...
needs to be the variable's name followed by equals and the centering
strategy requested. There are three different strategies available:
cwc
= centering within cluster
cgm
= centering with group mean
none
= no centering
If all
is set to TRUE
then the default centering will be used unless
overwritten by specifying a specific centering strategy.
For ndata = 1
a single data.frame
is returned.
If a list of data sets are included then they will be contained in a list
.
Each data.frame
has an additional center
attribute which denotes
the centering strategy used.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(198723) # Create data set with default centering model |> generate(5, 50) |> center(all = TRUE) -> mydata # Create data centering X with cwc model |> generate(5, 50) |> center(X = cwc) -> mydata # See centering strategy attr(mydata, 'center')
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(198723) # Create data set with default centering model |> generate(5, 50) |> center(all = TRUE) -> mydata # Create data centering X with cwc model |> generate(5, 50) |> center(X = cwc) -> mydata # See centering strategy attr(mydata, 'center')
Creates a list of correlations to be added to a mp_model
.
correlations(within, between, randeff)
correlations(within, between, randeff)
within |
a single numeric value or |
between |
a single numeric value or |
randeff |
a single numeric value or |
The default values are random(0.1, 0.3)
.
Currently randeff
are required to be zero if more than one random slope is in the model.
A list that corresponds to each correlation value.
( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) # Defaults + correlations( within = random(0.1, 0.3), between = random(0.1, 0.3), randeff = random(0.1, 0.3) ) )
( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) # Defaults + correlations( within = random(0.1, 0.3), between = random(0.1, 0.3), randeff = random(0.1, 0.3) ) )
Creates a list of effect sizes to be added to a mp_model
.
Returns suggested ICC's for cross-sectional studies (0.05, 0.15, and 0.25).
Returns suggested ICC ranges for longitudinal studies (0.40, 0.50, and 0.60).
effect_size(icc, within, between, random_slope, product) cross_sectional() longitudinal()
effect_size(icc, within, between, random_slope, product) cross_sectional() longitudinal()
icc |
a numeric vector of global ICC values for |
within |
a single numeric value that corresponds to the proportion of variance explained by the within variables. |
between |
a single numeric value that corresponds to the incremental proportion of variance explained by the between variables. |
random_slope |
a single numeric value that corresponds to the proportion of variance explained by the random slopes. |
product |
a single numeric value that corresponds to the proportion of variance explained by the product terms. |
A list that corresponds to each R2 value.
# Set ICCs ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # With cross-sectional ICC ( outcome('Y') + within_predictor('X') + effect_size( icc = cross_sectional, within = 0.3 ) ) # With longitudinal ICC ( outcome('Y') + within_predictor('X') + effect_size( icc = longitudinal, within = 0.3 ) )
# Set ICCs ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # With cross-sectional ICC ( outcome('Y') + within_predictor('X') + effect_size( icc = cross_sectional, within = 0.3 ) ) # With longitudinal ICC ( outcome('Y') + within_predictor('X') + effect_size( icc = longitudinal, within = 0.3 ) )
mp_model
Generates data sets based on a mp_model
.
These data sets will be returned as a data.frame
and include the
solved parameters as an attribute to the data.frame
.
generate(model, n_within, n_between, ndata = 1, mechanism = NULL)
generate(model, n_within, n_between, ndata = 1, mechanism = NULL)
model |
a |
n_within |
a single positive integer of the desired within cluster observations. |
n_between |
a single positive integer of the desired between cluster observations. |
ndata |
a single positive integer of the number of desired data sets. |
mechanism |
a function for inducing missing data to the data set. If NULL it is ignored. See details below. |
Note that there must only be one global ICC in mp_model
.
Use the mechanism
argument to specify missing data mechanisms. See mechanisms
for predefined missing data mechanisms for the outcome and examples using them.
When creating custom mechanisms care needs to be taken because it is considered for
advanced usage. This a argument expects a function with the mp_data
as the input,
and the function should return the modified mp_data
. Be careful when
using this because it allows you to modify the population parameters, which will
be incorrect. You should only induce missing data values on variables. Missing data
on the predictors will cause listwise deletion to be used, but missing data on the
outcome will be appropriate for MAR-based mechanisms. See examples below for an
example that generates MCAR data on the outcome. See parameters
to obtain the population parameters from each data set.
For ndata = 1
a single data.frame
is returned.
The first variable is the cluster identifier labeled _id
.
This object is also of class mp_data
which means that it was generated
based on a specified model.
If multiple data sets are requested then they will be contained in a list
.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(198723) # Create data set model |> generate(5, 50) -> mydata # Induce missing data with built-in function model |> generate(50, 5, mechanism = MCAR(0.25)) -> mydata_mcar # Induce missing data with custom function model |> generate(50, 5, mechanism = \(data) { # `data` will be the `mp_data` used within(data, { # MCAR Process Y <- ifelse(runif(NROW(data)) < 0.5, Y, NA) }) }) -> mydata_mcar_custom
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(198723) # Create data set model |> generate(5, 50) -> mydata # Induce missing data with built-in function model |> generate(50, 5, mechanism = MCAR(0.25)) -> mydata_mcar # Induce missing data with custom function model |> generate(50, 5, mechanism = \(data) { # `data` will be the `mp_data` used within(data, { # MCAR Process Y <- ifelse(runif(NROW(data)) < 0.5, Y, NA) }) }) -> mydata_mcar_custom
This function is used to validate if a mp_model
is correct.
If the model is incorrect an appopriate error message describing while will be supplied
is_valid(x)
is_valid(x)
x |
a |
Invisibly returns the original model.
# Create Model model <- outcome('Y') + within_predictor('X') # Throws error tryCatch( is_valid(model), error = print ) # Succeeds is_valid(model + effect_size(icc = 0.1))
# Create Model model <- outcome('Y') + within_predictor('X') # Throws error tryCatch( is_valid(model), error = print ) # Succeeds is_valid(model + effect_size(icc = 0.1))
Returns which level a variable is observed at in the multilevel model.
## S3 method for class 'mp_variable' levels(x)
## S3 method for class 'mp_variable' levels(x)
x |
a |
Returns a single integer of the level of observation
# Returns 1 levels( within_predictor( 'X', weight = 1, mean = 5, sd = 10, icc = 0.1 ) )
# Returns 1 levels( within_predictor( 'X', weight = 1, mean = 5, sd = 10, icc = 0.1 ) )
Functions to generate data that always follows a specific mechanism in accordance to a single-level model.
# Generate MCAR data on outcome MCAR(mis.rate) # Generate MAR data on outcome due to `cause` MAR(mis.rate, cause, r2, lower = TRUE)
# Generate MCAR data on outcome MCAR(mis.rate) # Generate MAR data on outcome due to `cause` MAR(mis.rate, cause, r2, lower = TRUE)
mis.rate |
A proportion for the missing data rate at population level |
cause |
A character for a variable name that is the cause of missingness |
r2 |
A proportion of variance explained by the cause in the missing data indicator's latent propensity |
lower |
A logical for the lower or upper tail being more likely to be missing |
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Induce MCAR data on outcome set.seed(19723) model |> power_analysis(50, 5, 50, mechanism = MCAR(0.25)) -> powersim_mcar # Induce MAR data on outcome set.seed(19723) model |> power_analysis( 50, 5, 50, mechanism = MAR(0.25, 'X', 0.6) ) -> powersim_mar
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Induce MCAR data on outcome set.seed(19723) model |> power_analysis(50, 5, 50, mechanism = MCAR(0.25)) -> powersim_mcar # Induce MAR data on outcome set.seed(19723) model |> power_analysis( 50, 5, 50, mechanism = MAR(0.25, 'X', 0.6) ) -> powersim_mar
mlmpower
Modeling Frameworkmlmpower
constructs models by adding different features of the model using the plus sign (+
).
Every model requires an outcome
and an ICC specified in effect_size
to be valid.
model <- outcome('y') + effect_size(icc = 0.1)
Once a model is constructed, we can add additional features to build the model out more. For example, we may want to include a level-1 predictor that is centered within cluster.
model <- model + within_predictor('x', icc = 0.0)
The additions can be chained together to produce the entire model object. For example, the previous two code blocks can be combined into one.
model <- ( outcome('y') + effect_size(icc = 0.1) + within_predictor('x', icc = 0.0) )
Finally, we can also wrap multiple variables into a list and add that. This feature can be useful when programmatically generating a model.
model <- ( outcome('y') + effect_size(icc = 0.1) + lapply(1:10, \(i) within_predictor(paste0('x', i), icc = 0.0)) )
For more detailed information see the help vignette by running the following:
vignette('mlmpower')
Variables effect_size()
correlations()
random_slope()
product()
Specify a random correlation that is uniform between lower
and upper
random(lower, upper) fixed(value)
random(lower, upper) fixed(value)
lower |
the lower bound of the distribution. |
upper |
the upper bound of the distribution. |
value |
the fixed value for the correlation. |
A mp_corr_func
that generates the desired correlation
# Create Model with random and fixed correlations ( outcome('Y') + within_predictor('X') + between_predictor('Z') + effect_size(icc = 0.1) # Defaults + correlations( within = random(0.1, 0.3), between = fixed(0.2) ) )
# Create Model with random and fixed correlations ( outcome('Y') + within_predictor('X') + between_predictor('Z') + effect_size(icc = 0.1) # Defaults + correlations( within = random(0.1, 0.3), between = fixed(0.2) ) )
mp_parameters
Object for mlmpower
An S3 class that contains an base::environment
with the following objects:
r2
: The population proportion of variance explained
phi_b
: The population predictor covariance matrix for between
phi_p
: The population within cluster covariance matrix for products
phi_w
: The population predictor covariance matrix for within
mean_Z
: The population mean for level-2 predictors
mean_X
: The population mean for level-1 predictors
var_e
: The population within residual variance
tau
: The population level-2 covariance matrix
gammas
: The regression coefficients
mean_Y
: The population mean of the outcome
mp_parameters
from objectsA generic function to obtain mp_parameters
from defined models and data sets.
parameters(object) ## S3 method for class 'mp_model' parameters(object) ## S3 method for class 'mp_data' parameters(object) ## S3 method for class 'mp_power' parameters(object)
parameters(object) ## S3 method for class 'mp_model' parameters(object) ## S3 method for class 'mp_data' parameters(object) ## S3 method for class 'mp_power' parameters(object)
object |
an object which the |
Currently object can be:
If using on a mp_model
and the model has random correlations
then the average is used.
A mp_parameters
object
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Create data set and obtain population parameters model |> parameters() # Set seed set.seed(198723) # Create data set and obtain population parameters model |> generate(5, 50) |> parameters() # Set seed set.seed(198723) # Create data set and obtain population parameters model |> power_analysis(50, 5, 50) |> parameters()
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Create data set and obtain population parameters model |> parameters() # Set seed set.seed(198723) # Create data set and obtain population parameters model |> generate(5, 50) |> parameters() # Set seed set.seed(198723) # Create data set and obtain population parameters model |> power_analysis(50, 5, 50) |> parameters()
mp_model
This function will construct a multilevel power analysis via a Monte Carlo Simulation
based on a constructed mp_model
.
power_analysis(model, replications, n_within, n_between, ...)
power_analysis(model, replications, n_within, n_between, ...)
model |
a |
replications |
a single positive integer of the number of replications per condition. |
n_within |
an integer vector of the desired within cluster observations. |
n_between |
an integer vector of the desired between cluster observations. |
... |
other arguments passed to |
Specifying multiple n_within
and n_between
will produce a full factorial simulation design.
Specify a mechanism
argument to pass down to the generate
function.
See the details of generate
for more information about specifying missing data mechanisms.
See mechanisms
for predefined missing data mechanisms.
Specify an analyze
argument to use custom analysis functions. These functions should map onto
analyze
's structure, but can allow for things like specifying multiple imputations etc.
This is considered an advance usage that requires extreme care and caution.
A mp_power
object that contains the results.
See print.mp_power
for more information.
The object has the following slots:
sim
: The information about the simulation
power
: The power power results per condition.
estimates
: The simulation summaries of the parameter estimates per condition.
mean_parameters
: The average population parameter per condition.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) # Induce missing data on outcome with built in mechanisms set.seed(19723) model |> power_analysis(50, 5, 50, mechanism = MCAR(0.25)) -> powersim_mcar
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) # Induce missing data on outcome with built in mechanisms set.seed(19723) model |> power_analysis(50, 5, 50, mechanism = MCAR(0.25)) -> powersim_mcar
mp_correlations
Prints a mp_correlations
in a human readable format.
## S3 method for class 'mp_correlations' print(x, ...)
## S3 method for class 'mp_correlations' print(x, ...)
x |
|
... |
other arguments not used by this method. |
Invisibly returns the original variable.
model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # Print correlations only print(model$corrs)
model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # Print correlations only print(model$corrs)
mp_effsize
Prints a mp_effsize
in a human readable format.
## S3 method for class 'mp_effsize' print(x, ...)
## S3 method for class 'mp_effsize' print(x, ...)
x |
a |
... |
other arguments not used by this method. |
Invisibly returns the original variable.
model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # Print effect size only print(model$effect_size)
model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = c(0.1, 0.2), within = 0.3 ) ) # Print effect size only print(model$effect_size)
mp_model
Prints a mp_variable
in a human readable format.
## S3 method for class 'mp_model' print(x, ...)
## S3 method for class 'mp_model' print(x, ...)
x |
a |
... |
other arguments not used by this method. |
Invisibly returns the original variable.
print( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) )
print( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) )
mp_parameters
Prints a mp_parameters
in a human readable format.
## S3 method for class 'mp_parameters' print(x, ...)
## S3 method for class 'mp_parameters' print(x, ...)
x |
|
... |
arguments passed to |
Invisibly returns the original variable.
print( summary( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) ) )
print( summary( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) ) )
mp_power
Prints a mp_power
in a human readable format.
## S3 method for class 'mp_power' print(x, ...)
## S3 method for class 'mp_power' print(x, ...)
x |
a |
... |
other arguments not used by this method. |
Invisibly returns the original variable.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) -> powersim # Print results print(powersim)
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) -> powersim # Print results print(powersim)
mp_variable
Prints a mp_variable
in a human readable format.
## S3 method for class 'mp_variable' print(x, ...)
## S3 method for class 'mp_variable' print(x, ...)
x |
a |
... |
other arguments not used by this method. |
Invisibly returns the original variable.
print( within_predictor( 'X', weight = 1, mean = 5, sd = 10, icc = 0.1 ) )
print( within_predictor( 'X', weight = 1, mean = 5, sd = 10, icc = 0.1 ) )
Creates a product term between two variables that can be added to a mp_model
.
product(name1, name2, weight = 1)
product(name1, name2, weight = 1)
name1 |
a character string that references the first variable's name |
name2 |
a character string that references the second variable's name |
weight |
a single numeric value specifying the variable's contribution to the variance explained metric. Weights are normalized across all variables of the same level. |
Currently the product term is only limited to cross-level
interactions between a level-1 centered within cluster variable (icc = 0
)
and level-2 variable.
A mp_action
that can be added to a mp_model
.
# Create Model model <- ( outcome('Y') + within_predictor('X', icc = 0.0) + between_predictor('Z') ) # Add random slope to the model model + product('X', 'Z')
# Create Model model <- ( outcome('Y') + within_predictor('X', icc = 0.0) + between_predictor('Z') ) # Add random slope to the model model + product('X', 'Z')
Creates a random slope that can be added to a mp_model
.
random_slope(name, weight = 1)
random_slope(name, weight = 1)
name |
a character string that references a variable's name |
weight |
a single numeric value specifying the variable's contribution to the variance explained metric. Weights are normalized across all variables of the same level. |
A mp_action
that can be added to a mp_model
.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = 0.1, within = 0.1, random_slope = 0.03 ) ) # Add random slope to the model model + random_slope('X')
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = 0.1, within = 0.1, random_slope = 0.03 ) ) # Add random slope to the model model + random_slope('X')
mp_model
by Global ICCSubsets a mp_model
with multiple ICC values specified in effect_size
into a model with only the single ICC value.
## S3 method for class 'mp_model' subset(x, icc, ...)
## S3 method for class 'mp_model' subset(x, icc, ...)
x |
a |
icc |
a single numeric value to subset out of |
... |
other arguments not used by this method. |
A new mp_model
with only the subset ICC
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) ) # Obtain Model with only 0.15 ICC model |> subset(icc = 0.15)
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) ) # Obtain Model with only 0.15 ICC model |> subset(icc = 0.15)
mp_model
Provide the summarized parameter estimates for a mp_model
,
including the variance explained break downs.
## S3 method for class 'mp_model' summary(object, ...)
## S3 method for class 'mp_model' summary(object, ...)
object |
a |
... |
other arguments not used by this method. |
A mp_parameters
object that contains the population parameters based on the model.
If random correlations are used the average correlation is used to compute the parameters.
If multiple ICC's are specified then a named base::list
is
returned containing the parameter value for each ICC value.
summary( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) )
summary( outcome('Y') + within_predictor('X') + effect_size(icc = cross_sectional) )
mp_power
Summarizes a mp_power
in a human readable format.
This is a simple wrapper for print.mp_power
.
## S3 method for class 'mp_power' summary(object, ...)
## S3 method for class 'mp_power' summary(object, ...)
object |
a |
... |
other arguments not used by this method. |
Invisibly returns the original variable.
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) -> powersim # Summarizes results summary(powersim)
# Create Model model <- ( outcome('Y') + within_predictor('X') + effect_size(icc = 0.1) ) # Set seed set.seed(19723) # Create data set and analyze # Note: Generally Use more than 50 replications model |> power_analysis(50, 5, 50) -> powersim # Summarizes results summary(powersim)
mp_data
to a stats::formula
to be used for lme4::lmer
Produces the formula including the centering functions based on a data set generated with generate
.
to_formula(data, nested = FALSE)
to_formula(data, nested = FALSE)
data |
the |
nested |
logical value, if true then produce the nested restricted model |
# Specify model model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = 0.2, within = 0.3 ) ) # Set seed set.seed(198723) # Create formula based on data set model |> generate(5, 50) |> to_formula()
# Specify model model <- ( outcome('Y') + within_predictor('X') + effect_size( icc = 0.2, within = 0.3 ) ) # Set seed set.seed(198723) # Create formula based on data set model |> generate(5, 50) |> to_formula()
These functions are the building blocks used to create the multilevel model and are used to specify the names, properties, and variable types.
outcome(name, mean = 10, sd = 5, icc = NULL) within_predictor(name, weight = 1, mean = 0, sd = 1, icc = NULL) within_time_predictor(name, values, weight = 1) between_predictor(name, weight = 1, mean = 0, sd = 1) between_binary_predictor(name, proportion = 0.5, weight = 1)
outcome(name, mean = 10, sd = 5, icc = NULL) within_predictor(name, weight = 1, mean = 0, sd = 1, icc = NULL) within_time_predictor(name, values, weight = 1) between_predictor(name, weight = 1, mean = 0, sd = 1) between_binary_predictor(name, proportion = 0.5, weight = 1)
name |
a character string for the specific variable's name |
mean |
a single numeric value that specifies the variable's mean |
sd |
a single numeric value that specifies the variable's standard deviation |
icc |
a single numeric value between 0 and 1 that specifies the variable's intraclass correlation.
If |
weight |
a single numeric value specifying the variable's contribution to the variance explained metric. Weights are normalized across all variables of the same level. |
values |
a numeric vector specifying the time scores that will be repeated within each cluster. |
proportion |
a single numeric value between 0 and 1 that specifies the proportion of 1's at the population. |
Note that specifying an icc = 0
in within_predictor()
will result in a centered within cluster (CWC) predictor.
See vignettes for more details.
vignette(package = 'mlmpower')
Returns a mp_variable
object based on the variable's type.