lmerPredictionPlot: lmerPredictionPlot

View source: R/lmerPredictionPlot.R

lmerPredictionPlotR Documentation

lmerPredictionPlot

Description

lmerPredictionPlot is a function for creating plots of predicted intervals from lme4 models using ggplot2. It provides flexibility in specifying the variables for the x-axis, y-axis, and grouping, and allows customization of plot appearance through additional parameters.

Usage

lmerPredictionPlot(model, x = NULL, y = NULL, grp = NULL, size = NULL, ...)

Arguments

model

A lme4 model object.

x

The name of the x variable (independent variable). Defaults to NULL. If not provided, the function attempts to guess the name by examining the formula of the model.

y

The name of the y variable (dependent variable). Defaults to NULL. If not provided, the function isolates the first variable from the second formula term.

grp

A categorical variable of the model. Defaults to NULL. If not provided, the function isolates the second variable from the second formula term.

size

The size of the data. By default, the size is set to the number of rows in the model frame.

...

Additional parameters for the function merTools::predictInterval(); see the reference documentation for the function.

Value

A ggplot object.

See Also

merTools::predictInterval, ggplot2

Examples

  d <- data.frame(
    Type = rep(c("Forest", "Regeneration", "Restoration"), each = 12),
    Landscape = rep(paste0("L", 1:12), times = 3),
    Mean_NDVI_SD_500 = rnorm(36, mean = 0.2, sd = 0.02),
    hill_q0 = sample(5:14, 36, replace = TRUE),
    Abundance = sample(5:50, 36, replace = TRUE)
  )
  modelX <- lme4::lmer(formula = hill_q0 ~ Mean_NDVI_SD_500 * Type+(1|Landscape), data=d)
  
  lmerPredictionPlot(model = modelX, type = "linear.prediction")
  lmerPredictionPlot(model = modelX) +
    scico::scale_color_scico_d(palette = "batlow", begin = 0.1, end = 0.7) +
    scico::scale_fill_scico_d(palette = "batlow", begin = 0.1, end = 0.7)
  lmerPredictionPlot(model = modelX) +
    scico::scale_color_scico_d(palette = "batlow", begin = 0.1, end = 0.7) +
    scico::scale_fill_scico_d(palette = "batlow", begin = 0.1, end = 0.7) +
    ggplot2::facet_wrap(~Type)
    
  modelY <- lme4::glmer.nb(formula = Abundance ~ Mean_NDVI_SD_500 * Type+(1|Landscape), data=d)
  
  lmerPredictionPlot(model = modelY, type = "probability")
  lmerPredictionPlot(model = modelY) +
    scico::scale_color_scico_d(palette = "batlow", begin = 0.1, end = 0.7) +
    scico::scale_fill_scico_d(palette = "batlow", begin = 0.1, end = 0.7)
  lmerPredictionPlot(model = modelY) +
    scico::scale_color_scico_d(palette = "batlow", begin = 0.1, end = 0.7) +
    scico::scale_fill_scico_d(palette = "batlow", begin = 0.1, end = 0.7) +
    ggplot2::facet_wrap(~Type)

wilsonfrantine/R4eco documentation built on Jan. 30, 2024, 4:55 p.m.