Type: Package
Title: Multi-Factor Curve Analysis for Grouped Data in 'R'
Version: 1.0.2
Maintainer: Maximilian Frank <maximilian.frank@psy.lmu.de>
Description: Implements multi-factor curve analysis for grouped data in 'R', replicating and extending the functionality of the the 'Stata' ado 'mfcurve' (Krähmer, 2023) https://ideas.repec.org/c/boc/bocode/s459224.html. Related to the idea of specification curve analysis (Simonsohn, Simmons, and Nelson, 2020) <doi:10.1038/s41562-020-0912-z>. Includes data preprocessing, statistical testing, and visualization of results with confidence intervals.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.2
Imports: dplyr, stats, plotly, tidyr, tidyselect, magrittr, rlang
Suggests: knitr, rmarkdown, testthat (≥ 3.0.0), withr
VignetteBuilder: knitr
URL: https://github.com/XAM12/mfcurve_R
BugReports: https://github.com/XAM12/mfcurve_R/issues
NeedsCompilation: no
Packaged: 2026-01-19 22:10:32 UTC; ra35riq
Author: Maximilian Frank [aut, cre], Daniel Krähmer [aut], Claudia Weileder [aut]
Repository: CRAN
Date/Publication: 2026-01-23 14:40:07 UTC

mfcurve: Multi-Factor Curve Analysis for Grouped Data in 'R'

Description

Implements multi-factor curve analysis for grouped data in 'R', replicating and extending the functionality of the the 'Stata' ado 'mfcurve' (Krähmer, 2023) https://ideas.repec.org/c/boc/bocode/s459224.html. Related to the idea of specification curve analysis (Simonsohn, Simmons, and Nelson, 2020) doi:10.1038/s41562-020-0912-z. Includes data preprocessing, statistical testing, and visualization of results with confidence intervals.

Author(s)

Maintainer: Maximilian Frank maximilian.frank@psy.lmu.de

Authors:

See Also

Useful links:


Pipe operator

Description

See magrittr::%>% for details.

Usage

lhs %>% rhs

Arguments

lhs

A value or the magrittr placeholder.

rhs

A function call using the magrittr semantics.

Value

The result of calling rhs(lhs).


Wrapper for mfcurve preprocessing and plotting

Description

Calls mfcurve_preprocessing() and mfcurve_plotting() in sequence to generate a two-panel interactive mfcurve plot.

Usage

mfcurve(
  data,
  outcome,
  factors,
  test = "mean",
  alpha = 0.05,
  showTitle = TRUE,
  SaveProcessedData = FALSE,
  mode = "collapsed",
  rounding = 2,
  plotOrigin = FALSE,
  CI = TRUE,
  showGrandMean = TRUE,
  showSigStars = TRUE
)

Arguments

data

A data frame containing the variables.

outcome

Name of the numeric outcome variable (string).

factors

Character vector of factor variable names for grouping.

test

Reference for t-tests: "mean", "zero", or "leave-one-out". Passed to preprocessing. Default is "mean".

alpha

Significance level for t-tests and confidence intervals. Default is 0.05.

showTitle

Logical. Show the plot title? Default is TRUE.

SaveProcessedData

Logical. If TRUE, writes group-level statistics to the session temporary directory (tempdir()) as timestamped files: group_stats_*.csv and group_stats_*.rds. Default is FALSE.

mode

Factor labeling mode: "collapsed" (default) or "expanded".

rounding

Number of digits to round outcome statistics. Default is 2.

plotOrigin

Logical. Force axes to include 0? Default is FALSE.

CI

Logical. Display confidence intervals? Default is TRUE.

showGrandMean

Logical. Show the grand mean line? Default is TRUE.

showSigStars

Logical. Show markers for significant values? Default is TRUE.

Details

mfcurve() plots the mean of an outcome variable across all combinations of multiple grouping factors, producing a two-panel interactive plot.

The upper panel shows group means (and confidence intervals, if requested); the lower panel marks which factor levels are present in each group. In the lower panel, factor labels can be displayed in two modes:

While collapsed mode saves space when many factors or levels are present, expanded mode may be more intuitive (especially for readers familiar with specification curves).

mfcurve() allows optional significance testing (t-tests). Group-level statistics can be saved if needed.

Value

Invisibly returns the plotly object representing the two-panel plot. If SaveProcessedData = TRUE, also writes group_stats to CSV and RDS in tempdir() and prints the file paths.

See Also

mfcurve_preprocessing, mfcurve_plotting

Examples

# Simulate data for a 3 x 2 experimental design: 3 treatments (A, B, C), 2 doses (low, high)
set.seed(123)
df <- data.frame(
  treatment = sample(c("A", "B", "C"), 1000, replace = TRUE),
  dose      = sample(c("low", "high"), 1000, replace = TRUE)
)

# Generate self-rated health (scale 1–10) with small group differences
df$self_rated_health <- 6 +
  ifelse(df$treatment == "B", 0.5, ifelse(df$treatment == "C", -0.5, 0)) +
  ifelse(df$dose == "high", 0.3, 0) +
  rnorm(1000, 0, 1.5)

# Restrict health scores to valid range
df$self_rated_health <- pmin(pmax(df$self_rated_health, 1), 10)

# Create mfcurve plot
mfcurve(
  data = df,
  outcome = "self_rated_health",
  factors = c("treatment", "dose"),
  test = "mean"
)


Create a two-panel mfcurve plot from processed statistics

Description

Generates an interactive two-panel plot showing group means (with optional confidence intervals) and corresponding factor combinations.

Usage

mfcurve_plotting(
  group_stats_vis,
  lower_data,
  grand_mean,
  outcome,
  factors,
  level,
  rounding = 2,
  showTitle = TRUE,
  plotOrigin = FALSE,
  CI = TRUE,
  mode = "collapsed",
  showGrandMean = TRUE,
  showSigStars = TRUE
)

Arguments

group_stats_vis

Data frame containing group-level summary statistics.

lower_data

Data frame defining the factor structure for the lower panel.

grand_mean

Numeric. The overall mean of the outcome variable.

outcome

Name of the outcome variable (string).

factors

Character vector of factor variable names.

level

Level for confidence intervals (e.g., 0.95).

rounding

Number of digits to round outcome values. Default is 2.

showTitle

Logical. Show the plot title? Default is TRUE.

plotOrigin

Logical. Force axes to include 0? Default is FALSE.

CI

Logical. Display confidence intervals? Default is TRUE.

mode

Labeling mode for the lower panel: "collapsed" (default) or "expanded".

showGrandMean

Logical. Show the grand mean overall groups. Default is TRUE.

showSigStars

Logical. Flag significant results. Default is TRUE.

Value

A plotly object (invisible).


Preprocess data and compute group statistics

Description

Prepares the data and computes descriptive statistics and t-tests for groups defined by combinations of categorical factors.

Usage

mfcurve_preprocessing(data, outcome, factors, alpha = 0.05, test = "mean")

Arguments

data

Data frame containing the variables.

outcome

Name of the numeric outcome variable (string).

factors

Character vector of factor variable names for grouping.

alpha

Significance level for the t-tests and confidence intervals. Default is 0.05.

test

Reference for t-tests: "mean" (grand mean), "leave-one-out" (mean of all other groups), or "zero" (testing against 0).

Value

A list with:

group_stats

Data frame with computed statistics and CI bounds.

group_stats_vis

Visualization-ready version with rounded values.

lower_data

Data for the lower panel (without y positions).

grand_mean

Overall mean of the outcome variable.

level

Confidence level used.