Compute standard error for a variable, for all variables of a data frame, for joint random and fixed effects coefficients of (non-/linear) mixed models, the adjusted standard errors for generalized linear (mixed) models, or for intraclass correlation coefficients (ICC).
se(x, ...) # S3 method for icc.lme4 se(x, nsim = 100, ...)
x | (Numeric) vector, a data frame, an |
---|---|
... | Currently not used. |
nsim | Numeric, the number of simulations for calculating the
standard error for intraclass correlation coefficients, as
obtained by the |
The standard error of x
.
Standard error for variables
For variables and data frames, the standard error is the square root of the
variance divided by the number of observations (length of vector).
Standard error for mixed models
For linear mixed models, and generalized linear mixed models, this
function computes the standard errors for joint (sums of) random and fixed
effects coefficients (unlike se.coef
, which returns the
standard error for fixed and random effects separately). Hence, se()
returns the appropriate standard errors for coef.merMod
.
Standard error for generalized linear models
For generalized linear models, approximated standard errors, using the delta
method for transformed regression parameters are returned (Oehlert 1992).
Standard error for Intraclass Correlation Coefficient (ICC)
The standard error for the icc
is based on bootstrapping,
thus, the nsim
-argument is required. See 'Examples'.
Standard error for proportions and mean value
To compute the standard error for relative frequencies (i.e. proportions, or
mean value if x
has only two categories), this vector must be supplied
as table, e.g. se(table(iris$Species))
. se()
than computes the
relative frequencies (proportions) for each value and the related standard
error for each value. This might be useful to add standard errors or confidence
intervals to descriptive statistics. If standard errors for weighted variables
are required, use xtabs()
, e.g. se(xtabs(weights ~ variable))
.
Standard error for regression coefficient and p-value
se()
also returns the standard error of an estimate (regression
coefficient) and p-value, assuming a normal distribution to compute
the z-score from the p-value (formula in short: b / qnorm(p / 2)
).
See 'Examples'.
Computation of standard errors for coefficients of mixed models
is based on this code.
Standard errors for generalized linear (mixed) models, if
type = "re"
, are approximations based on the delta
method (Oehlert 1992).
A remark on standard errors:
“Standard error represents variation in the point estimate, but
confidence interval has usual Bayesian interpretation only with flat prior.”
(Gelman 2017)
Oehlert GW. 1992. A note on the delta method. American Statistician 46(1).
Gelman A 2017. How to interpret confidence intervals? http://andrewgelman.com/2017/03/04/interpret-confidence-intervals/
library(lme4) library(sjmisc) # compute standard error for vector se(rnorm(n = 100, mean = 3))#> [1] 0.1074613# compute standard error for each variable in a data frame data(efc) se(efc[, 1:3])#> c12hour e15relat e16sex #> 1.69162290 0.06942207 0.01565588# compute standard error for merMod-coefficients library(lme4) fit <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy) se(fit)#> $Subject #> (Intercept) Days #> 1 13.86649 2.7752 #> 2 13.86649 2.7752 #> 3 13.86649 2.7752 #> 4 13.86649 2.7752 #> 5 13.86649 2.7752 #> 6 13.86649 2.7752 #> 7 13.86649 2.7752 #> 8 13.86649 2.7752 #> 9 13.86649 2.7752 #> 10 13.86649 2.7752 #> 11 13.86649 2.7752 #> 12 13.86649 2.7752 #> 13 13.86649 2.7752 #> 14 13.86649 2.7752 #> 15 13.86649 2.7752 #> 16 13.86649 2.7752 #> 17 13.86649 2.7752 #> 18 13.86649 2.7752 #># compute odds-ratio adjusted standard errors, based on delta method # with first-order Taylor approximation. data(efc) efc$services <- sjmisc::dicho(efc$tot_sc_e, dich.by = 0) fit <- glm( services ~ neg_c_7 + c161sex + e42dep, data = efc, family = binomial(link = "logit") ) se(fit)#> # A tibble: 4 x 3 #> term estimate std.error #> <chr> <dbl> <dbl> #> 1 (Intercept) 0.595 0.224 #> 2 neg_c_7 1.04 0.0204 #> 3 c161sex 0.803 0.130 #> 4 e42dep 1.24 0.0972# compute odds-ratio adjusted standard errors for generalized # linear mixed model, also based on delta method # create binary response sleepstudy$Reaction.dicho <- dicho(sleepstudy$Reaction, dich.by = "median") fit <- glmer( Reaction.dicho ~ Days + (Days | Subject), data = sleepstudy, family = binomial("logit") ) se(fit)#> $Subject #> (Intercept) Days #> 1 1.859251 0.4700877 #> 2 2.622321 0.4115004 #> 3 2.622321 0.4115004 #> 4 1.633616 0.3289753 #> 5 1.745522 0.4826391 #> 6 1.714059 0.3560052 #> 7 1.715195 0.4646524 #> 8 2.219966 0.4133501 #> 9 2.622321 0.4115004 #> 10 1.806673 0.5077360 #> 11 2.314690 0.3959941 #> 12 2.111495 0.4335945 #> 13 1.830037 0.3388438 #> 14 1.686571 0.4933028 #> 15 1.988285 0.4534415 #> 16 2.167228 0.4013762 #> 17 2.314690 0.3959941 #> 18 1.778644 0.4321839 #>#> value proportion std.error #> 1 independent 0.07325194 0.008680166 #> 2 slightly dependent 0.24972253 0.014420404 #> 3 moderately dependent 0.33962264 0.015777276 #> 4 severely dependent 0.33740289 0.015752039# including weights efc$weights <- rnorm(nrow(efc), 1, .25) se(xtabs(efc$weights ~ efc$e42dep))#> value proportion std.error #> 1 independent 0.07898613 0.00900256 #> 2 slightly dependent 0.24649213 0.01438477 #> 3 moderately dependent 0.33359206 0.01573749 #> 4 severely dependent 0.34092968 0.01582180# compute standard error from regression coefficient and p-value se(list(estimate = .3, p.value = .002))#> [1] 0.09708008# NOT RUN { # compute standard error of ICC for the linear mixed model icc(fit) se(icc(fit)) # the standard error for the ICC can be computed manually in this way, # taking the fitted model example from above library(dplyr) library(purrr) dummy <- sleepstudy %>% # generate 100 bootstrap replicates of dataset bootstrap(100) %>% # run mixed effects regression on each bootstrap replicate # and compute ICC for each "bootstrapped" regression mutate( models = map(strap, ~lmer(Reaction ~ Days + (Days | Subject), data = .x)), icc = map_dbl(models, ~icc(.x)) ) # now compute SE and p-values for the bootstrapped ICC, values # may differ from above example due to random seed boot_se(dummy, icc) boot_p(dummy, icc) # }