groupedstats (version 0.0.7)

grouped_lmer: Function to run linear mixed-effects model (lmer) across multiple grouping variables.

Description

Function to run linear mixed-effects model (lmer) across multiple grouping variables.

Usage

grouped_lmer(data, grouping.vars, formula, REML = TRUE,
  control = lme4::lmerControl(optimizer = "bobyqa", restart_edge = TRUE,
  boundary.tol = 1e-07, calc.derivs = FALSE, use.last.params = FALSE,
  optCtrl = list(maxfun = 2e+09)), p.kr = FALSE, output = "tidy")

Arguments

data

Dataframe from which variables are to be taken.

grouping.vars

List of grouping variables.

formula

a two-sided linear formula object describing both the fixed-effects and random-effects part of the model, with the response on the left of a ~ operator and the terms, separated by + operators, on the right. Random-effects terms are distinguished by vertical bars (|) separating expressions for design matrices from grouping factors. Two vertical bars (||) can be used to specify multiple uncorrelated random effects for the same grouping variable. (Because of the way it is implemented, the ||-syntax works only for design matrices containing numeric (continuous) predictors; to fit models with independent categorical effects, see dummy or the lmer_alt function from the afex package.)

REML

logical scalar - Should the estimates be chosen to optimize the REML criterion (as opposed to the log-likelihood)?

control

a list (of correct class, resulting from lmerControl() or glmerControl() respectively) containing control parameters, including the nonlinear optimizer to be used and parameters to be passed through to the nonlinear optimizer, see the *lmerControl documentation for details.

p.kr

Logical, if TRUE, the computation of p-values is based on conditional F-tests with Kenward-Roger approximation for the df (see 'Details').

output

A character describing what output is expected. Two possible options: "tidy" (default), which will return the results, or "glance", which will return model summaries.

Value

A tibble dataframe with tidy results from linear model or model summaries.

Examples

Run this code
# NOT RUN {
# loading libraries containing data
library(ggplot2)
library(gapminder)

# getting tidy output of results
# let's use only 50% data to speed it up
groupedstats::grouped_lmer(
  data = dplyr::sample_frac(gapminder, size = 0.5),
  formula = scale(lifeExp) ~ scale(gdpPercap) + (gdpPercap | continent),
  grouping.vars = year,
  output = "tidy"
)

# getting model summaries
# let's use only 50% data to speed it up
grouped_lmer(
  data = ggplot2::diamonds,
  formula = scale(price) ~ scale(carat) + (carat | color),
  REML = FALSE,
  grouping.vars = c(cut, clarity),
  output = "glance"
)
# }

Run the code above in your browser using DataLab