Package 'MR.Corr2'

Title: MR.Corr2
Description: More details about what the package does. See <http://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-DESCRIPTION-file> for details on how to write this part.
Authors: Your Name
Maintainer: Your Name <[email protected]>
License: GPL (>= 2)
Version: 1.0
Built: 2026-06-03 06:34:44 UTC
Source: https://github.com/remlapmot/MR.Corr2

Help Index


MR.Corr2: Two-Sample Mendelian Randomization with Correlated Pleiotropy

Description

MR.Corr2 implements a Bayesian two-sample Mendelian randomization method that simultaneously accounts for linkage disequilibrium (LD) among instrumental variants and correlated horizontal pleiotropy. Inference is performed via a block-parallel Gibbs sampler implemented in C++ using RcppArmadillo.

Main analysis functions:

MRcorr

MR analysis assuming independent SNPs (no LD).

MRCorr2Sim

MR analysis with LD using a pre-computed LD matrix (e.g. from simulated data).

MRCorr2Real

MR analysis with LD computed on-the-fly from a reference panel (for real GWAS data).

Data preparation functions:

matchscreen

Match and p-value screen SNPs across exposure, outcome, and reference panel files.

summaryQC

QC filters: remove MHC region SNPs and SNPs with extreme chi-square statistics.

ReadSNPinfo

Read SNP annotation from reference panel.

Read_summarystat

Read GWAS summary statistics.

matchsnp

Match SNPs between files.

getLineNum

Count lines in a file.

LD computation functions:

Cal_blockR

Compute block LD matrices from a reference panel (returns field of matrices).

Cal_block_Rmatrix

Compute block LD as a single full matrix from a reference panel.

Cal_blockinf

Determine block structure for a set of SNPs.

Cal_block_SimR

Compute block LD from simulated genotypes.

LDclump

LD-based clumping within a block.

load_block_file

Load LD block definitions from file.

test_blocks

Check block assignments for SNPs.

Simulation and visualisation:

genRawGeno

Generate simulated genotype data with AR1 LD structure.

traceplot

Trace plot of Gibbs sampler output.

fastSigLm

Fast simple linear regression for SNP screening.

Author(s)

Qing Cheng [email protected]

References

Cheng, Q., Qiu, T., Chai, X., Sun, B., Xia, Y., Shi, X., and Liu, J. (2021). MR-Corr2: a two-sample Mendelian randomization method that accounts for correlated horizontal pleiotropy using correlated instrumental variants. Bioinformatics. doi:10.1093/bioinformatics/btab646.

See Also

The package vignette provides worked examples for both simulation and real data analyses: vignette("MR.Corr2").


Compute Full Block LD Matrix from a Reference Panel

Description

Similar to Cal_blockR, but returns the block LD matrices assembled into a single full sparse-block LD matrix rather than an Armadillo field. Useful when the full matrix representation is needed.

Usage

Cal_block_Rmatrix(bp, chr, avbIndex, idx4panel, block_file, stringname3,
                  ld_r2_thresh, coreNum, lam)

Arguments

bp

Integer vector of base pair positions for the analysis SNPs.

chr

Integer vector of chromosome numbers for the analysis SNPs.

avbIndex

Integer vector of 0-based indices of the analysis SNPs within the reference panel.

idx4panel

Integer vector of 0-based indices of reference panel SNPs used for LD estimation.

block_file

Character string giving the path to the LD block definition file.

stringname3

Character string giving the path prefix for PLINK binary reference panel files (.bed/.bim/.fam).

ld_r2_thresh

Numeric r2r^2 threshold for LD clumping.

coreNum

Integer number of CPU cores for parallel computation.

lam

Numeric regularisation parameter for positive-definite correction of LD matrices.

Value

A named list containing:

R

Numeric matrix of full block-diagonal LD correlations (p x p).

block_inf

Integer matrix of 0-based block start/end indices.

nblocks

Integer number of LD blocks.

Indpid

Integer vector of SNP indices retained after LD clumping.

See Also

Cal_blockR, Cal_block_SimR, MRCorr2Sim


Compute Block LD Matrix from Simulated Genotypes

Description

Computes a block-diagonal LD correlation matrix from a simulated genotype matrix. Each block's correlation matrix is regularised to ensure positive definiteness.

Usage

Cal_block_SimR(block_inf, X, lam)

Arguments

block_inf

Integer matrix defining LD blocks. Each row gives the 0-based start and end column indices of one block within X. Typically obtained from Cal_blockinf.

X

Integer matrix of simulated genotypes (individuals x SNPs), with values 0, 1, or 2. Can be generated using genRawGeno.

lam

Numeric regularisation parameter. Values above 0.5 apply linear shrinkage towards the identity; values at or below 0.5 use the pdsoft positive-definite projection algorithm.

Value

A numeric matrix of block-diagonal LD correlations (p x p), where p is the number of SNPs. Off-diagonal entries between different blocks are zero.

See Also

genRawGeno, Cal_blockinf, MRCorr2Sim


Compute Block Information for a Set of SNPs

Description

Given chromosomal positions of SNPs and a block definition file, determines which LD block each SNP belongs to and returns a matrix of block start/end indices into the SNP vectors. This block structure is used by MRCorr2Sim and Cal_block_SimR.

Usage

Cal_blockinf(bp, chr, block_file)

Arguments

bp

Integer vector of base pair positions for each SNP.

chr

Integer vector of chromosome numbers for each SNP.

block_file

Character string giving the path to the LD block definition file. See load_block_file.

Value

An integer matrix with one row per non-empty LD block. Each row contains the 0-based start and end indices of the SNPs in that block within the input vectors.

See Also

load_block_file, Cal_blockR, Cal_block_SimR, MRCorr2Sim


Compute Block LD Matrices from a Reference Panel

Description

Reads genotype data from a reference panel (PLINK binary format) and computes regularised LD correlation matrices for each LD block. The result includes block LD matrices, block boundary information, and a clumped set of independent SNP indices.

Usage

Cal_blockR(bp, chr, avbIndex, idx4panel, block_file, stringname3,
           ld_r2_thresh, coreNum, lam)

Arguments

bp

Integer vector of base pair positions for the analysis SNPs.

chr

Integer vector of chromosome numbers for the analysis SNPs.

avbIndex

Integer vector of 0-based indices of the analysis SNPs within the reference panel.

idx4panel

Integer vector of 0-based indices of reference panel SNPs used for LD estimation.

block_file

Character string giving the path to the LD block definition file.

stringname3

Character string giving the path prefix for PLINK binary reference panel files (.bed/.bim/.fam).

ld_r2_thresh

Numeric r2r^2 threshold for LD clumping within blocks.

coreNum

Integer number of CPU cores for parallel computation.

lam

Numeric regularisation parameter for positive-definite correction of LD matrices. Values above 0.5 use linear shrinkage; values at or below 0.5 use the pdsoft algorithm.

Value

A named list containing:

F4Rblock

An Armadillo field of block LD matrices, one per block.

block_inf

Integer matrix of 0-based block start/end indices.

nblocks

Integer number of LD blocks.

Indpid

Integer vector of SNP indices retained after LD clumping.

See Also

Cal_block_Rmatrix, Cal_blockinf, LDclump, MRCorr2Real


Fast Simple Linear Regression for Multiple Predictors

Description

Performs simple linear regression of a response vector y on each column of a matrix X separately, returning coefficient estimates and their standard errors. This is a vectorised implementation intended for rapid screening of SNP-trait associations.

Usage

fastSigLm(y, X)

Arguments

y

Numeric vector of the response variable (n observations).

X

Numeric matrix of predictors (n observations x p SNPs). Each column is regressed against y independently.

Value

A named list with:

coef

Numeric vector of slope estimates (one per column of X).

std

Numeric vector of standard errors for the slope estimates.


Generate Simulated Genotype Data with LD Structure

Description

Generates a simulated genotype matrix for n individuals at L * M SNPs arranged in L independent blocks each containing M SNPs. Within each block, SNPs are correlated according to a first-order autoregressive (AR1) correlation structure with parameter rho. Genotypes (0, 1, 2) are generated by thresholding a multivariate normal distribution according to the specified minor allele frequencies.

Usage

genRawGeno(maf, L, M, rho, n)

Arguments

maf

Numeric vector of minor allele frequencies, one per SNP (length L * M).

L

Integer number of independent LD blocks.

M

Integer number of SNPs per block.

rho

Numeric AR1 correlation parameter controlling LD within each block. Must be in [0,1)[0, 1).

n

Integer number of individuals (sample size).

Details

Requires the mvtnorm package for multivariate normal sampling (rmvnorm).

Value

An integer matrix of dimensions n x L * M with genotype values 0, 1, or 2.

See Also

Cal_block_SimR, fastSigLm, MRCorr2Sim

Examples

## Not run: 
library(mvtnorm)
set.seed(1)
L <- 10; M <- 5; n <- 500
maf <- runif(L * M, 0.1, 0.5)
X <- genRawGeno(maf, L, M, rho = 0.5, n = n)
dim(X)  # 500 x 50

## End(Not run)

Count Lines in a File

Description

Returns the number of lines in a text file. Used to determine the number of SNPs or samples before allocating vectors for ReadSNPinfo or Read_summarystat.

Usage

getLineNum(filename)

Arguments

filename

Character string giving the path to the file.

Value

An integer giving the number of lines in the file.

See Also

ReadSNPinfo, Read_summarystat


Hello World Placeholder

Description

A placeholder function that prints a greeting. Not used in analyses.

Usage

hello(txt = "world")

Arguments

txt

Character string to print. Default is "world".

Value

Called for its side effect of printing to the console. Returns NULL invisibly.

Examples

hello()
hello("MR.Corr2")

LD Clumping of SNPs

Description

Performs LD-based clumping to select a set of approximately independent SNPs from a block LD correlation matrix. Starting from the top SNP in each block, removes SNPs with r2r^2 above the threshold with any retained SNP.

Usage

LDclump(R, ld_r2_thresh)

Arguments

R

Numeric matrix of LD correlations (p x p) for SNPs within a block.

ld_r2_thresh

Numeric r2r^2 threshold. Pairs of SNPs with r2>r^2 > ld_r2_thresh are considered in LD; the lower-ranked SNP is removed.

Value

An integer vector of 0-based indices of the retained SNPs after clumping.

See Also

Cal_blockR, Cal_block_Rmatrix


Load LD Block Definitions from File

Description

Reads an LD block definition file (e.g., the EUR block file from Berisa and Pickrell 2016) into memory as a matrix of chromosomal start and end positions for each block.

Usage

load_block_file(block_file)

Arguments

block_file

Character string giving the path to the LD block file. The file should have columns for chromosome, start position, and end position.

Value

A matrix with one row per LD block and columns for chromosome number, start base pair position, and end base pair position.

References

Berisa, T. and Pickrell, J.K. (2016). Approximately independent linkage disequilibrium blocks in human populations. Bioinformatics, 32(2), 283–285.

See Also

Cal_blockinf, test_blocks, Cal_blockR


Match and Screen SNPs Using a Screening File

Description

Matches SNPs from two GWAS summary statistics files and a reference panel, and then screens them by p-value using a separate screening summary statistics file. Only SNPs that pass the p-value threshold and are present in all three files are retained.

Usage

matchscreen(screenname, stringname1, stringname2, stringname3,
            pva_cutoff, matchExp = FALSE)

Arguments

screenname

Character string giving the path to the GWAS summary statistics file used for screening (typically the exposure GWAS used to select instrumental variables).

stringname1

Character string giving the path to the first GWAS summary statistics file (exposure).

stringname2

Character string giving the path to the second GWAS summary statistics file (outcome).

stringname3

Character string giving the path prefix for the reference panel genotype files (PLINK binary format).

pva_cutoff

Numeric p-value threshold for selecting instrumental variables from screenname.

matchExp

Logical. If TRUE, applies additional allele-alignment checking. Default is FALSE.

Value

A named list containing:

bh1

Numeric vector of SNP-exposure effect estimates for matched SNPs.

bh2

Numeric vector of SNP-outcome effect estimates for matched SNPs.

se1

Numeric vector of standard errors for bh1.

se2

Numeric vector of standard errors for bh2.

bp

Integer vector of base pair positions.

chr

Integer vector of chromosome numbers.

rsname

Character vector of rs IDs.

avbIndex

Integer vector of 0-based SNP indices in the reference panel.

idx4panel

Integer vector of 0-based indices for the reference panel LD calculation subset.

See Also

matchsnp, summaryQC, MRCorr2Real


Match SNPs Between Reference Panel and Summary Statistics Files

Description

Matches SNPs present in two GWAS summary statistics files against a reference panel, identifying the common set of SNPs available for Mendelian randomization analysis.

Usage

matchsnp(stringname1, stringname2, stringname3, matchExp)

Arguments

stringname1

Character string giving the path to the first GWAS summary statistics file (typically the exposure GWAS).

stringname2

Character string giving the path to the second GWAS summary statistics file (typically the outcome GWAS).

stringname3

Character string giving the path prefix for the reference panel genotype files (PLINK binary format).

matchExp

Logical. If TRUE, applies additional allele-alignment checking between the files.

Value

A named list containing integer vectors of indices identifying the matched SNPs in the reference panel and each summary statistics file.

See Also

matchscreen, ReadSNPinfo, Read_summarystat


Mendelian Randomization with Correlated Pleiotropy (Independent SNPs)

Description

Performs two-sample Mendelian randomization using a Bayesian Gibbs sampling approach that accounts for correlated horizontal pleiotropy. This function assumes independent instrumental variants (no LD between SNPs). For analyses with correlated SNPs, see MRCorr2Sim and MRCorr2Real.

Usage

MRcorr(gammah, Gammah, se1, se2, opts = NULL)

Arguments

gammah

Numeric vector of SNP-exposure effect estimates (GWAS summary statistics for the exposure trait).

Gammah

Numeric vector of SNP-outcome effect estimates (GWAS summary statistics for the outcome trait). Must be the same length as gammah.

se1

Numeric vector of standard errors for the SNP-exposure effects. Must be the same length as gammah.

se2

Numeric vector of standard errors for the SNP-outcome effects. Must be the same length as Gammah.

opts

Optional named list of Gibbs sampler hyperparameters and MCMC settings. If NULL, default values are used. See Details.

Details

The model assumes that each SNP either acts as a valid instrument (η=1\eta = 1) or exhibits horizontal pleiotropy (η=0\eta = 0). The causal effect β0\beta_0 is estimated from valid instruments, while β1\beta_1 captures the pleiotropic contribution.

The opts list may contain the following named elements:

agm

Shape hyperparameter for the inverse-gamma prior on instrument effect variance σγ2\sigma_\gamma^2. Default: 1.

bgm

Scale hyperparameter for the inverse-gamma prior on σγ2\sigma_\gamma^2. Default: 0.1.

aal

Shape hyperparameter for the inverse-gamma prior on pleiotropic effect variance σα2\sigma_\alpha^2. Default: 1.

bal

Scale hyperparameter for the inverse-gamma prior on σα2\sigma_\alpha^2. Default: 0.1.

a

First shape parameter for the Beta prior on the mixture weight ω\omega. Default: 1.

b

Second shape parameter for the Beta prior on ω\omega. Default: 1.

maxIter

Number of Gibbs sampling iterations after burn-in. Default: 10000.

thin

Thinning interval for saving Gibbs samples. Default: 1.

burnin

Number of burn-in iterations discarded before saving. Default: 2000.

Value

A named list containing the following components:

Eta

Integer vector of final indicator values for each SNP (1 = valid instrument, 0 = pleiotropic).

Etares

Numeric vector of saved posterior proportion of valid instruments across MCMC iterations.

EtaAll

Integer matrix (SNPs x iterations) of saved indicator values across all MCMC iterations.

Beta0res

Numeric vector of saved posterior samples of the causal effect β0\beta_0.

Beta1res

Numeric vector of saved posterior samples of the pleiotropic effect β1\beta_1.

Sgga2Res

Numeric vector of saved posterior samples of instrument effect variance σγ2\sigma_\gamma^2.

Sgal2Res

Numeric vector of saved posterior samples of pleiotropic effect variance σα2\sigma_\alpha^2.

References

Cheng, Q., Qiu, T., Chai, X., Sun, B., Xia, Y., Shi, X., and Liu, J. (2021). MR-Corr2: a two-sample Mendelian randomization method that accounts for correlated horizontal pleiotropy using correlated instrumental variants. Bioinformatics. doi:10.1093/bioinformatics/btab646.

See Also

MRCorr2Sim, MRCorr2Real, traceplot

Examples

## Not run: 
# Simulated example with independent SNPs
p <- 100
gammah <- rnorm(p, mean = 0.1, sd = 0.05)
Gammah <- 0.3 * gammah + rnorm(p, sd = 0.05)
se1 <- rep(0.05, p)
se2 <- rep(0.05, p)

result <- MRcorr(gammah, Gammah, se1, se2)

# Posterior mean of causal effect
mean(result$Beta0res)

# Trace plot of causal effect
traceplot(result$Beta0res)

## End(Not run)

Mendelian Randomization with Correlated SNPs (Real GWAS Data)

Description

Performs two-sample Mendelian randomization using a block-parallel Bayesian Gibbs sampling approach that accounts for both linkage disequilibrium (LD) and correlated horizontal pleiotropy. This function is intended for real GWAS summary statistics, computing LD matrices on-the-fly from a reference panel. For pre-computed LD matrices (e.g. simulation studies), see MRCorr2Sim.

Usage

MRCorr2Real(bp, chr, avbIndex, idx4panel, block_file, stringname3,
            ld_r2_thresh, bh1, bh2, se1, se2, lam, coreNum, opts = NULL)

Arguments

bp

Integer vector of base pair positions for each SNP.

chr

Integer vector of chromosome numbers for each SNP.

avbIndex

Integer vector of 0-based indices identifying the SNPs available for the analysis within the reference panel.

idx4panel

Integer vector of 0-based indices of SNPs in the reference panel used for LD calculation.

block_file

Character string giving the path to a file defining LD blocks (e.g., the EUR LD block file from Berisa and Pickrell 2016).

stringname3

Character string giving the path prefix for the reference panel genotype files (PLINK binary format: .bed/.bim/.fam).

ld_r2_thresh

Numeric threshold for LD r2r^2 clumping of SNPs within blocks.

bh1

Numeric vector of SNP-exposure effect estimates (γ^\hat\gamma).

bh2

Numeric vector of SNP-outcome effect estimates (Γ^\hat\Gamma).

se1

Numeric vector of standard errors for bh1.

se2

Numeric vector of standard errors for bh2.

lam

Numeric regularisation parameter for positive-definite correction of block LD matrices. Values above 0.5 use linear shrinkage; values at or below 0.5 use the pdsoft algorithm.

coreNum

Integer number of CPU cores for parallel computation.

opts

Optional named list of Gibbs sampler settings. See MRcorr Details section for available options.

Value

A named list containing:

nblocks

Integer number of LD blocks used in the analysis.

Indpid

Integer vector of SNP indices after clumping, indicating which SNPs were retained.

Eta

Integer vector of final block-level indicator values (1 = valid instrument block, 0 = pleiotropic block).

Etares

Numeric vector of saved posterior proportion of valid instrument blocks across MCMC iterations.

Beta0res

Numeric vector of saved posterior samples of the causal effect β0\beta_0.

Beta1res

Numeric vector of saved posterior samples of the pleiotropic effect β1\beta_1.

Sgga2Res

Numeric vector of saved posterior samples of instrument effect variance σγ2\sigma_\gamma^2.

Sgal2Res

Numeric vector of saved posterior samples of pleiotropic effect variance σα2\sigma_\alpha^2.

References

Cheng, Q., Qiu, T., Chai, X., Sun, B., Xia, Y., Shi, X., and Liu, J. (2021). MR-Corr2: a two-sample Mendelian randomization method that accounts for correlated horizontal pleiotropy using correlated instrumental variants. Bioinformatics. doi:10.1093/bioinformatics/btab646.

Berisa, T. and Pickrell, J.K. (2016). Approximately independent linkage disequilibrium blocks in human populations. Bioinformatics, 32(2), 283–285.

See Also

MRcorr, MRCorr2Sim, matchscreen, summaryQC, traceplot

Examples

## Not run: 
# See the package vignette for a complete real data example.
# vignette("MR.Corr2")

## End(Not run)

Mendelian Randomization with Correlated SNPs (Simulation Data)

Description

Performs two-sample Mendelian randomization using a block-parallel Bayesian Gibbs sampling approach that accounts for both linkage disequilibrium (LD) among instrumental variants and correlated horizontal pleiotropy. This function is intended for use with simulated data where the full LD matrix R is available. For real GWAS data, see MRCorr2Real.

Usage

MRCorr2Sim(gammah, Gammah, se1, se2, R, block_inf, coreNum, opts = NULL)

Arguments

gammah

Numeric vector of SNP-exposure effect estimates.

Gammah

Numeric vector of SNP-outcome effect estimates. Must be the same length as gammah.

se1

Numeric vector of standard errors for SNP-exposure effects.

se2

Numeric vector of standard errors for SNP-outcome effects.

R

Numeric matrix of pairwise LD correlations among SNPs (p x p). Can be computed using Cal_block_SimR.

block_inf

Integer matrix defining LD blocks. Each row gives the 0-based start and end indices (columns) of one block within R. Can be obtained from Cal_blockinf.

coreNum

Integer specifying the number of CPU cores for parallel computation.

opts

Optional named list of Gibbs sampler settings. See MRcorr Details section for available options.

Details

This function partitions SNPs into LD blocks and performs block-parallel Gibbs sampling. Parallelism is applied across blocks within each iteration, enabling efficient computation for large numbers of SNPs.

The LD matrix R and block structure block_inf are typically computed from reference panel genotypes using Cal_block_SimR and Cal_blockinf.

Value

A named list containing:

Eta

Integer vector of final block-level indicator values (1 = valid instrument block, 0 = pleiotropic block).

Etares

Numeric vector of saved posterior proportion of valid instrument blocks across MCMC iterations.

Beta0res

Numeric vector of saved posterior samples of the causal effect β0\beta_0.

Beta1res

Numeric vector of saved posterior samples of the pleiotropic effect β1\beta_1.

References

Cheng, Q., Qiu, T., Chai, X., Sun, B., Xia, Y., Shi, X., and Liu, J. (2021). MR-Corr2: a two-sample Mendelian randomization method that accounts for correlated horizontal pleiotropy using correlated instrumental variants. Bioinformatics. doi:10.1093/bioinformatics/btab646.

See Also

MRcorr, MRCorr2Real, Cal_block_SimR, Cal_blockinf, traceplot

Examples

## Not run: 
# See the package vignette for a complete simulation example.
# vignette("MR.Corr2")

## End(Not run)

Read GWAS Summary Statistics from File

Description

Reads GWAS summary statistics from a text file and populates pre-allocated vectors with allele codes, rs IDs, effect estimates, standard errors, p-values, chromosomal positions, and sample sizes.

Usage

Read_summarystat(stringname, SA1, SA2, rsname, betah, s2, pvalue, chr, bp, N)

Arguments

stringname

Character string giving the path to the summary statistics file.

SA1

Character vector to be filled with effect allele codes.

SA2

Character vector to be filled with other allele codes.

rsname

Character vector to be filled with rs IDs.

betah

Numeric vector to be filled with effect estimates.

s2

Numeric vector to be filled with standard errors.

pvalue

Numeric vector to be filled with p-values.

chr

Integer vector to be filled with chromosome numbers.

bp

Integer vector to be filled with base pair positions.

N

Integer specifying the number of SNPs to read.

Value

Called for its side effect of populating the supplied vectors. Returns NULL invisibly.

See Also

ReadSNPinfo, matchsnp, matchscreen, getLineNum


Read SNP Information from a Reference Panel File

Description

Reads SNP annotation information from a reference panel file (typically a PLINK .bim file or similar format) and populates pre-allocated vectors with allele codes, rs IDs, chromosomes, base pair positions, genetic distances, and sample sizes.

Usage

ReadSNPinfo(stringname, A1, A2, rsname, chr, bp, morgan, N)

Arguments

stringname

Character string giving the path to the SNP information file.

A1

Character vector to be filled with effect allele codes.

A2

Character vector to be filled with other allele codes.

rsname

Character vector to be filled with rs IDs.

chr

Integer vector to be filled with chromosome numbers.

bp

Integer vector to be filled with base pair positions.

morgan

Numeric vector to be filled with genetic distances (cM).

N

Integer specifying the number of SNPs to read.

Value

A named list with the populated vectors:

A1

Character vector of effect alleles.

A2

Character vector of other alleles.

rsname

Character vector of rs IDs.

chr

Integer vector of chromosome numbers.

bp

Integer vector of base pair positions.

morgan

Numeric vector of genetic distances.

See Also

Read_summarystat, matchsnp, matchscreen, getLineNum


Subset a Vector by Index

Description

Returns the elements of a vector at specified indices. A thin C++ wrapper around element selection, provided for use in Rcpp-based workflows.

Usage

select(vec_, idx_)

Arguments

vec_

A numeric or integer vector.

idx_

An integer vector of 0-based indices specifying which elements to extract.

Value

A vector containing the elements of vec_ at positions idx_.


Set Difference of Two Integer Vectors

Description

Returns the elements of integer vector x that are not present in integer vector y. Implemented via C++ std::set_difference for efficiency.

Usage

std_setdiff(x, y)

Arguments

x

Integer vector.

y

Integer vector. Elements in y are removed from x.

Value

An integer vector of elements in x but not in y, in sorted order.


Quality Control for GWAS Summary Statistics

Description

Applies quality control filters to GWAS summary statistics prior to Mendelian randomization analysis. Removes SNPs located in the MHC region (chromosome 6, positions 28,477,797–33,448,354) and SNPs whose chi-square statistics exceed specified bounds for either the exposure or outcome GWAS.

Usage

summaryQC(mhcstart = 28477797, mhcend = 33448354, bh1, bh2, s12, s22, bp, chr,
          rsname, avbIndex, idx4panel, xbound, ybound)

Arguments

mhcstart

Integer giving the start base pair position of the MHC region to exclude (default 28,477,797).

mhcend

Integer giving the end base pair position of the MHC region to exclude (default 33,448,354).

bh1

Numeric vector of SNP-exposure effect estimates.

bh2

Numeric vector of SNP-outcome effect estimates.

s12

Numeric vector of standard errors for bh1.

s22

Numeric vector of standard errors for bh2.

bp

Integer vector of base pair positions.

chr

Integer vector of chromosome numbers.

rsname

Character vector of rs IDs.

avbIndex

Integer vector of SNP indices in the reference panel.

idx4panel

Integer vector of LD panel SNP indices.

xbound

Numeric upper chi-square bound for the exposure GWAS. SNPs with (γ^/se1)2>(\hat\gamma / se_1)^2 > xbound are removed.

ybound

Numeric upper chi-square bound for the outcome GWAS. SNPs with (Γ^/se2)2>(\hat\Gamma / se_2)^2 > ybound are removed.

Value

A named list containing the QC-filtered data:

bh1new

Filtered exposure effect estimates.

bh2new

Filtered outcome effect estimates.

s12new

Filtered exposure standard errors.

s22new

Filtered outcome standard errors.

bpnew

Filtered base pair positions.

chrnew

Filtered chromosome numbers.

rsnamenew

Filtered rs IDs.

avbIndexnew

Filtered reference panel indices.

idx4panelnew

Filtered LD panel indices.

pmhc

Integer number of SNPs removed due to MHC region.

px

Integer number of SNPs removed due to exposure chi-square bound.

py

Integer number of SNPs removed due to outcome chi-square bound.

See Also

matchscreen, MRCorr2Real

Examples

## Not run: 
qc <- summaryQC(mhcstart = 28477797, mhcend = 33448354,
                bh1 = bh1, bh2 = bh2,
                s12 = se1, s22 = se2,
                bp = bp, chr = chr,
                rsname = rsname,
                avbIndex = avbIndex,
                idx4panel = idx4panel,
                xbound = 80, ybound = 80)

## End(Not run)

Test Block Assignments for SNPs

Description

Given SNP positions and a block definition file, returns the block index assigned to each SNP. Useful for verifying that SNPs are correctly assigned to LD blocks before running the main analysis.

Usage

test_blocks(bp, chr, block_file)

Arguments

bp

Integer vector of base pair positions for each SNP.

chr

Integer vector of chromosome numbers for each SNP.

block_file

Character string giving the path to the LD block definition file.

Value

An integer vector of block indices (1-based) for each SNP. SNPs that do not fall within any defined block are assigned NA or 0.

See Also

load_block_file, Cal_blockinf


Trace Plot of Gibbs Sampler Output

Description

Produces a trace plot of posterior samples from the Gibbs sampler, typically used to assess convergence of the causal effect estimate β^0\hat\beta_0. Returns a ggplot2 plot object.

Usage

traceplot(bhatpoint)

Arguments

bhatpoint

Numeric vector of posterior samples, e.g. the Beta0res component returned by MRcorr, MRCorr2Sim, or MRCorr2Real.

Details

Requires the ggplot2 package.

Value

A ggplot object displaying the sample index on the x-axis and the parameter value on the y-axis.

See Also

MRcorr, MRCorr2Sim, MRCorr2Real

Examples

## Not run: 
result <- MRcorr(gammah, Gammah, se1, se2)
p <- traceplot(result$Beta0res)
print(p)

## End(Not run)