format_mr_input: Format vectors into a Mendelian Randomization input data...

View source: R/mr_transform.R

format_mr_inputR Documentation

Format vectors into a Mendelian Randomization input data frame

Description

Assembles raw vectors into a working data frame, aligns outcome alleles to the exposure strand (Step 1), then standardizes the sign of beta_exposure (Step 2). Returns both a full working data frame (check_df) and a slim, renamed input data frame (input_df), mirroring the output of harmonize_mr_data().

Usage

format_mr_input(
  Instrument,
  beta_exposure,
  se_exposure,
  beta_outcome,
  se_outcome,
  Outcome,
  Exposure,
  ALLELE1 = NULL,
  ALLELE0 = NULL,
  A1FREQ = NULL,
  ALLELE1_outcome = NULL,
  ALLELE0_outcome = NULL,
  A1FREQ_outcome = NULL,
  beta_sign = c("positive", "negative")
)

Arguments

Instrument

Character vector of instrument/SNP identifiers.

beta_exposure

Numeric vector of exposure effects.

se_exposure

Numeric vector of exposure standard errors.

beta_outcome

Numeric vector of outcome effects.

se_outcome

Numeric vector of outcome standard errors.

Outcome

Character string or vector for outcome names (Mandatory).

Exposure

Character string or vector for exposure names (Mandatory).

ALLELE1

Optional character vector for non-effect alleles (NEA) on the exposure strand (ALLELE1 = NEA_exposure).

ALLELE0

Optional character vector for effect alleles (EA) on the exposure strand (ALLELE0 = EA_exposure).

A1FREQ

Optional numeric vector for effect allele frequencies (exposure dataset).

ALLELE1_outcome

Optional character vector for non-effect alleles in the outcome dataset. If NULL, assumed identical to ALLELE1.

ALLELE0_outcome

Optional character vector for effect alleles in the outcome dataset. If NULL, assumed identical to ALLELE0.

A1FREQ_outcome

Optional numeric vector for effect allele frequencies in the outcome dataset.

beta_sign

Character string controlling the target sign for beta_exposure. One of "positive" (default, forces beta_exposure >= 0) or "negative" (forces beta_exposure <= 0). When a row is flipped, beta_outcome is negated as a side effect and all allele columns are swapped consistently. Ignored when no allele columns are supplied (strand identity is unknown without allele information).

Value

A named list with two elements:

check_df

Full working data frame retaining all allele columns (NEA_exposure, EA_exposure, NEA_outcome, EA_outcome, A1FREQ_exposure, A1FREQ_outcome) after harmonization. Useful for quality-checking the harmonization results.

input_df

Slim data frame ready for MR analysis, with columns: Instrument, beta_exposure, se_exposure, beta_outcome, se_outcome, Outcome, Exposure, ALLELE1, ALLELE0, A1FREQ.

Examples

data("fi_49item")

# Without allele columns
result1 <- format_mr_input(
  Instrument    = fi_49item$Instrument,
  beta_exposure = fi_49item$beta_exposure,
  se_exposure   = fi_49item$se_exposure,
  beta_outcome  = fi_49item$beta_outcome,
  se_outcome    = fi_49item$se_outcome,
  Outcome       = fi_49item$Outcome,
  Exposure      = fi_49item$Exposure
)
head(result1$input_df)

# With allele columns (enables alignment + sign standardization)
result2 <- format_mr_input(
  Instrument      = fi_49item$Instrument,
  beta_exposure   = fi_49item$beta_exposure,
  se_exposure     = fi_49item$se_exposure,
  beta_outcome    = fi_49item$beta_outcome,
  se_outcome      = fi_49item$se_outcome,
  Outcome         = fi_49item$Outcome,
  Exposure        = fi_49item$Exposure,
  ALLELE1         = fi_49item$NEA_exposure,
  ALLELE0         = fi_49item$EA_exposure,
  A1FREQ          = fi_49item$A1FREQ_exposure,
  ALLELE1_outcome = fi_49item$NEA_outcome,
  ALLELE0_outcome = fi_49item$EA_outcome,
  A1FREQ_outcome  = fi_49item$A1FREQ_outcome
)
head(result2$check_df)
head(result2$input_df)

# Force all exposure betas to be negative
result3 <- format_mr_input(
  Instrument    = fi_49item$Instrument,
  beta_exposure = fi_49item$beta_exposure,
  se_exposure   = fi_49item$se_exposure,
  beta_outcome  = fi_49item$beta_outcome,
  se_outcome    = fi_49item$se_outcome,
  Outcome       = fi_49item$Outcome,
  Exposure      = fi_49item$Exposure,
  ALLELE1       = fi_49item$NEA_exposure,
  ALLELE0       = fi_49item$EA_exposure,
  beta_sign     = "negative"
)
head(result3$input_df)

autoMR documentation built on June 12, 2026, 9:07 a.m.