compute_apar: Compute adiposity peak (AP) and adiposity rebound (AR).

View source: R/compute_apar.R

compute_aparR Documentation

Compute adiposity peak (AP) and adiposity rebound (AR).

Description

Compute adiposity peak (AP) and adiposity rebound (AR).

Usage

compute_apar(
  fit,
  from = c("predicted", "observed"),
  start = 0.25,
  end = 10,
  step = 0.01,
  filter = NULL
)

Arguments

fit

A model object from a statistical model such as from a call nlme::lme(), time_model() or egg_model().

from

A string indicating the type of data to be used for the AP and AR computation, either "predicted" or "observed". Default is "predicted".

start

The start of the time window to compute AP and AR.

end

The end of the time window to compute AP and AR.

step

The step to increment the sequence.

filter

A string following data.table syntax for filtering on "i" (i.e., row elements), e.g., filter = "source == 'A'". Argument pass through compute_apar() (see predict_bmi()). Default is NULL.

Value

A data.table object.

Examples

library(eggla)
data("bmigrowth")
res <- egg_model(
  formula = log(bmi) ~ age,
  data = bmigrowth[bmigrowth[["sex"]] == 0, ],
  id_var = "ID",
  random_complexity = 1
)

head(compute_apar(fit = res, from = "predicted")[AP | AR])

# Comparing observed and predicted values
library(data.table)
library(ggplot2)
library(patchwork)
list_gg <- melt(
  data = rbindlist(
    l = lapply(
      X = (function(.x) `names<-`(.x, .x))(c("predicted", "observed")),
      FUN = compute_apar,
      fit = res
    ),
    idcol = "from"
  )[
    AP | AR
  ][
    j = what := fifelse(paste(AP, AR) %in% paste(FALSE, TRUE), "AR", "AP")
  ],
  id.vars = c("from", "egg_id", "what"),
  measure.vars = c("egg_ageyears", "egg_bmi")
)[
  j = list(gg = list({
    dt <- dcast(data = .SD, formula = egg_id + what ~ from)
    range_xy <- range(dt[, c("observed", "predicted")], na.rm = TRUE)
    ggplot(data = dt) +
      aes(x = observed, y = predicted, colour = what) +
      geom_abline(intercept = 0, slope = 1) +
      geom_segment(aes(xend = observed, yend = observed), alpha = 0.5) +
      geom_point() +
      scale_colour_manual(values = c("#E69F00FF", "#56B4E9FF")) +
      labs(
        x = sprintf("Observed: %s", sub(".*_", "", toupper(variable))),
        y = sprintf("Predicted: %s", sub(".*_", "", toupper(variable))),
        colour = NULL,
        title = sub(".*_", "", toupper(variable))
      ) +
      coord_cartesian(xlim = range_xy, ylim = range_xy)
  })),
  by = "variable"
]
wrap_plots(list_gg[["gg"]], guides = "collect")

mcanouil/eggla documentation built on April 5, 2025, 3:06 a.m.