Skip to contents

You need model coefficients to simulate multilevel data, and can get them from data simulated from parameters using sim_design() or rmulti().

Usage

get_coefs(data, formula = NULL, fun = c("lm", "glm", "lmer", "glmer"), ...)

Arguments

data

A dataset in long format

formula

A formula (can be extracted from datasets created by sim_design)

fun

the model function (one of "lm", "glm", "lmer", or "glmer")

...

Further arguments to the model function

Value

a list of the model coefficients

Examples

# simulate some data
data <- sim_design(within = 2, between = 2, 
                   mu = c(1, 0, 1, 1), 
                   long = TRUE, empirical = TRUE)


# get coefs for the full factorial model
get_coefs(data)
#> Warning: unable to evaluate scaled gradient
#> Warning: Model failed to converge: degenerate  Hessian with 1 negative eigenvalues
#>   (Intercept)         W1W1b         B1B1b   W1W1b:B1B1b 
#>  1.000000e+00 -1.000000e+00  3.552714e-15  1.000000e+00 

# a reduced model
get_coefs(data, y ~ B1 + W1)
#> (Intercept)       B1B1b       W1W1b 
#>        0.75        0.50       -0.50 

# specify a different model function
data$y <- norm2binom(data$y)
get_coefs(data, fun = "glm", family = binomial)
#> (Intercept)       W1W1b       B1B1b W1W1b:B1B1b 
#>   0.3432843  -1.7398645   0.2215453   1.5615874