### Install TwoSampleMR from Source Source: https://github.com/mrcieu/twosamplemr/blob/master/README.md Install the TwoSampleMR package directly from its GitHub repository. Ensure 'remotes' package is installed first. This method is useful for installing the latest development version. ```r install.packages("remotes") remotes::install_github("MRCIEU/TwoSampleMR") ``` -------------------------------- ### Example: Split Exposure Data Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md An example demonstrating how to use the `split_exposure` function to split exposure data. ```r exposure_list <- split_exposure(exposure_dat) ``` -------------------------------- ### Run Steiger Filtering Example Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Example demonstrating how to apply `steiger_filtering` to harmonized data after adding R-squared values. ```R harmonized_dat <- add_rsq(harmonized_dat) dat_filtered <- steiger_filtering(harmonized_dat) ``` -------------------------------- ### Example: Split Outcome Data Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md An example showing the usage of the `split_outcome` function for splitting outcome data. ```r outcome_list <- split_outcome(outcome_dat) ``` -------------------------------- ### Run Rucker's Test Example Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Example of how to run Rucker's test and print the results. The Q_prime_pval column indicates the significance of potential outliers. ```R rucker_results <- mr_rucker(dat = harmonized_dat) # Q_prime_pval suggests presence of outliers if significant print(rucker_results) ``` -------------------------------- ### Install TwoSampleMR from MRC IEU R-universe Source: https://github.com/mrcieu/twosamplemr/blob/master/README.md Use this command to install the latest version of the TwoSampleMR package from the MRC IEU R-universe repository. This is the recommended method for Windows and macOS users. ```r install.packages("TwoSampleMR", repos = c("https://mrcieu.r-universe.dev", "https://cloud.r-project.org")) ``` -------------------------------- ### Generate MR Scatter Plot Example Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/visualisation-functions.md Example of how to create and display an MR scatter plot. Requires MR results and harmonized data as input. ```R mr_res <- mr(dat = harmonized_dat) scatter <- mr_scatter_plot(mr_res, harmonized_dat) print(scatter$plot) ``` -------------------------------- ### Example Usage of mr_ivw_fe Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Shows a practical example of using the mr_ivw_fe function. Provide your exposure and outcome data, including their respective beta and standard error values. ```r result <- mr_ivw_fe( b_exp = dat$beta.exposure, b_out = dat$beta.outcome, se_exp = dat$se.exposure, se_out = dat$se.outcome ) ``` -------------------------------- ### Example Usage of mr_ivw Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Demonstrates how to call the mr_ivw function with sample data. The input data should contain beta and standard error values for exposure and outcome. ```r result <- mr_ivw( b_exp = dat$beta.exposure, b_out = dat$beta.outcome, se_exp = dat$se.exposure, se_out = dat$se.outcome ) ``` -------------------------------- ### Exposure Data Structure Example Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/types.md Displays the first few rows of an exposure data frame to illustrate the expected column structure and data types. This is useful for verifying data input. ```r # Structure head(exposure_dat) # SNP beta.exposure se.exposure effect_allele.exposure other_allele.exposure # 1 rs12345678 0.05 0.01 A G # 2 rs23456789 0.07 0.011 T C # ... # eaf.exposure pval.exposure exposure id.exposure # 1 0.3 1.2e-10 Exposure expos_1 # 2 0.4 5.6e-08 Exposure expos_1 # Attributes: none standard ``` -------------------------------- ### Generate MR Forest Plot Example Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/visualisation-functions.md Example of how to generate and display an MR forest plot using singlesnp and mr results. Requires prior MR analysis outputs. ```R singlesnp_res <- mr_singlesnp(dat = harmonized_dat) mr_res <- mr(dat = harmonized_dat) forest <- mr_forest_plot(singlesnp_res, mr_res) print(forest$plot) ``` -------------------------------- ### Example: One-to-Many Forest Plot Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/visualisation-functions.md Demonstrates how to generate and print a forest plot for one-to-many MR analysis. Requires harmonized data and MR results. ```r # MR of one exposure against many outcomes one_to_many_results <- mr(dat = harmonized_dat) forest <- forest_plot_1_to_many(one_to_many_results, exponentiate = FALSE) print(forest$plot) ``` -------------------------------- ### Example: Subset MR Results Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Demonstrates subsetting MR results to include only those from the 'Inverse variance weighted' method. ```r ivw_results <- subset_on_method(mr_results, "Inverse variance weighted") ``` -------------------------------- ### Run Steiger Directionality Test Example Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Example of how to use `mr_steiger` after harmonizing data and adding R-squared values. It shows how to filter the results to include only forward directionality. ```R # Must run add_rsq first harmonized_dat <- add_rsq(harmonized_dat) steiger_results <- mr_steiger(dat = harmonized_dat) # Filter to forward direction only forward_only <- subset(steiger_results, directionality == "Forward") ``` -------------------------------- ### Display and Filter MR Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md After obtaining the list of all MR methods with `mr_method_list()`, you can display specific columns like 'obj', 'name', and 'use_by_default'. You can also filter this list to get only the methods that are used by default in `mr()` analyses. ```r all_methods <- mr_method_list() print(all_methods[, c("obj", "name", "use_by_default")]) # Get only default methods default_methods <- subset(all_methods, use_by_default) ``` -------------------------------- ### Run MR Analyses with Specific Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Execute MR analyses using a custom list of methods by passing a character vector of method names to the `method_list` argument of the `mr` function. This example includes Wald Ratio, IVW, Egger Regression, and Weighted Median methods. ```r mr_results <- mr( dat = harmonized_dat, parameters = default_parameters(), method_list = c("mr_wald_ratio", "mr_ivw", "mr_egger_regression", "mr_weighted_median") ) # View main results head(mr_results$mr) ``` -------------------------------- ### Get Information on Available MR Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Retrieve a data frame containing details about all available MR methods using the `mr_method_list` function. This includes function names, display names, PubMed IDs, descriptions, and whether they are used by default or include heterogeneity testing. ```r mr_method_list() ``` -------------------------------- ### Format Data for MR Analysis (Outcome) Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/core-data-functions.md This example demonstrates how to use `format_data` for outcome data, specifying custom column names for SNPs, effect sizes, and standard errors. Ensure your input data frame (`mydata`) contains these columns. ```r dat <- format_data( mydata, type = "outcome", snp_col = "rsid", beta_col = "effect_size", se_col = "std_error" ) ``` -------------------------------- ### Get Default Parameters for MR Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Retrieves the default numeric parameters used by various MR methods. These parameters can be customized to control the behavior of MR analyses. ```r params <- default_parameters() # Parameters include: # - num_wald_ratio_snps: Number of SNPs for Wald ratio # - nboot: Number of bootstrap iterations for bootstrap methods # - seed: Random seed for reproducibility # And various other method-specific parameters ``` ```r params <- default_parameters() params$nboot <- 1000 # Increase bootstrap iterations mr_results <- mr( dat = harmonised_dat, parameters = params ) ``` -------------------------------- ### Specify Local LD Reference Panel Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md The `bfile` parameter takes the prefix of your PLINK binary files (.bed, .bim, .fam). Ensure these files are in the specified directory. ```r bfile = "reference_panel" ``` ```r clump_data( dat, bfile = "/path/to/reference_panel" ) ``` -------------------------------- ### Configure Bootstrap Iterations for MR Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Set the number of bootstrap iterations for MR methods by modifying the `nboot` parameter within the `parameters` list. The default is 1000. ```r params <- default_parameters() params$nboot <- 5000 mr_results <- mr_egger_regression_bootstrap( b_exp, b_out, se_exp, se_out, parameters = params ) ``` -------------------------------- ### mr_method_list Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Returns information about available MR methods including function names, descriptions, and properties. This is useful for understanding which methods are available and their default usage. ```APIDOC ## mr_method_list ### Description Returns information about available MR methods including function names, descriptions, and properties. ### Method Signature ```r mr_method_list() ``` ### Return data.frame with columns: - `obj`: Function name (e.g., "mr_wald_ratio", "mr_ivw") - `name`: Display name - `PubmedID`: PMID for method reference - `Description`: Method description - `use_by_default`: Whether included in default `mr()` analysis - `heterogeneity_test`: Whether method includes heterogeneity testing ### Example ```r all_methods <- mr_method_list() print(all_methods[, c("obj", "name", "use_by_default")]) # Get only default methods default_methods <- subset(all_methods, use_by_default) ``` ``` -------------------------------- ### Customize and Use MR Parameters Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Demonstrates how to obtain default parameters, modify specific values (e.g., `num_wald_ratio_snps`), and then use these customized parameters in the `mr()` function. ```r params <- default_parameters() # Customize parameters params$num_wald_ratio_snps <- 1 mr_results <- mr(harmonized_dat, parameters = params) ``` -------------------------------- ### Configure LD Clumping Window Size Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Set the `clump_kb` parameter to define the kilobase window size for LD clumping. Larger values capture more distant LD, with 10000 (10 Mb) being a common default for European ancestry. ```r clump_data(dat, clump_kb = 10000) ``` -------------------------------- ### Set OPENGWAS_JWT in .Renviron File Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Shows how to set the OPENGWAS_JWT environment variable by adding it to the user's .Renviron file. This ensures the token is available across R sessions. ```bash # In ~/.Renviron OPENGWAS_JWT=your_jwt_token_here ``` -------------------------------- ### Configure P-value Threshold for Seed SNPs Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md The `clump_p1` parameter filters seed SNPs based on their p-value. Setting it to 5e-8, for example, ensures only genome-wide significant SNPs are used for clumping. ```r clump_data(dat, clump_p1 = 5e-8) # Only clump SNPs genome-wide significant ``` -------------------------------- ### List Available Enrichment Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Returns a data frame describing the available enrichment analysis methods. No parameters are required. ```r enrichment_method_list() ``` -------------------------------- ### Robust Adjusted Profile Score (RAPS) MR Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Use `mr_raps` for the Robust Adjusted Profile Score (RAPS) MR method. This approach offers robust estimation, particularly when dealing with potential violations of standard MR assumptions. Ensure the `mr.raps` package is installed. ```r mr_raps( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### Specify PLINK Binary Path Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Use the `plink_bin` parameter to provide the explicit path to your PLINK executable. If set to NULL, the package attempts to auto-detect it. ```r # Auto-detect clump_data(dat, plink_bin = NULL) ``` ```r # Specify explicit path clump_data( dat, plink_bin = "/usr/local/bin/plink" ) ``` -------------------------------- ### Perform Bootstrap Rucker's Q-Q' Test Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Conducts a bootstrap-based version of Rucker's Q-Q' test for heterogeneity and outlier detection. Uses the same parameters as mr_rucker(). ```R mr_rucker_bootstrap( dat, parameters = default_parameters() ) ``` -------------------------------- ### Harmonize Data with Custom Strand Assignment per Outcome Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Provide a vector to `action` to specify different strand assignment strategies for multiple outcomes. For example, `action = c(2, 2, 3)` applies action 2 to the first two outcomes and action 3 to the third. ```r harmonise_data( exposure_dat, outcome_dat, action = c(2, 2, 3) # Three outcomes, third uses action=3 ) ``` -------------------------------- ### make_dat Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/core-data-functions.md Creates a standardized dataset combining exposure and outcome data with specified columns. ```APIDOC ## make_dat ### Description Creates a standardized dataset combining exposure and outcome data with specified columns. ### Method (Not specified, likely a data processing function) ### Endpoint (Not applicable, local function) ### Parameters #### Request Body - **exposure_dat** (data.frame) - Required - Formatted exposure data. - **outcome_dat** (data.frame) - Required - Formatted outcome data. - **outcome_snps** (logical) - Optional - Whether to use outcome SNPs for merging (TRUE) or exposure SNPs (FALSE). Defaults to FALSE. ### Return data.frame with combined exposure and outcome data, merged by SNP. ### Request Example ```r combined_dat <- make_dat(exposure_dat, outcome_dat) ``` ``` -------------------------------- ### GRIP MR Method Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Implements the Generalized Regression using Instrumental Polynomial (GRIP) method for MR. Similar parameter requirements to other MR methods. ```r mr_grip( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### Wrapper for Applying MR Methods Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md A flexible wrapper function to apply any specified MR method to harmonized data. Useful for easily switching between different MR techniques. ```r results <- mr_wrapper( dat = harmonized_dat, method_name = "mr_ivw" ) ``` -------------------------------- ### Add MR-GRIP Method to TwoSampleMR Source: https://github.com/mrcieu/twosamplemr/blob/master/NEWS.md Demonstrates how to add the new MR-GRIP method to the list of methods used by the `mr()` function. This method implements the MR-GRIP approach for Mendelian Randomization. ```R mr(dat, method_list = "mr_grip") ``` ```R mr(dat, method_list = c(subset(mr_method_list(), use_by_default)$obj, "mr_grip")) ``` -------------------------------- ### Format Data for MR Analysis (Exposure) Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/core-data-functions.md This snippet shows the default usage of `format_data` for preparing exposure data. It includes all possible column mapping parameters for comprehensive data standardization. ```r format_data( dat, type = "exposure", snps = NULL, header = TRUE, phenotype_col = "Phenotype", snp_col = "SNP", beta_col = "beta", se_col = "se", eaf_col = "eaf", effect_allele_col = "effect_allele", other_allele_col = "other_allele", pval_col = "pval", units_col = "units", ncase_col = "ncase", ncontrol_col = "ncontrol", samplesize_col = "samplesize", gene_col = "gene", id_col = "id", min_pval = 1e-200, log_pval = FALSE, chr_col = "chr", pos_col = "pos" ) ``` -------------------------------- ### Simple Median MR Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Implements the simple (unweighted) median MR method. This is a basic median-based approach. ```r mr_simple_median( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### Citation for Two Sample MR Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/overview.md This is the citation for the Two Sample MR project, as published in the European Journal of Epidemiology. ```text Hemani G, Bowden J, Davey Smith G (2018). Evaluating the potential role of pleiotropy in Mendelian randomization studies. European Journal of Epidemiology, 33(9), 807-818. DOI: 10.1007/s10654-018-0403-y ``` -------------------------------- ### Read Exposure Data with Log P-values Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Set `log_pval = TRUE` when your input p-values are already -log10 transformed. This avoids re-transformation. ```r read_exposure_data( filename = "data_with_log_p.txt", log_pval = TRUE ) ``` -------------------------------- ### enrichment_method_list Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Returns a list of available enrichment analysis methods. This function does not take any parameters and returns a data frame describing the available methods. ```APIDOC ## enrichment_method_list ### Description Returns list of available enrichment analysis methods. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Method Signature ```r enrichment_method_list() ``` ### Parameters - None ### Return data.frame describing available enrichment methods. ``` -------------------------------- ### Simple Fixed Effects Meta-Analysis MR Method Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Implements a simple fixed effects meta-analysis MR method. Requires exposure and outcome effect sizes and their standard errors. ```r mr_meta_fixed_simple( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### Format Data for One-to-Many Visualization Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/visualisation-functions.md Formats MR results data into a structure suitable for one-to-many analysis visualization. Takes MR results as input. ```r format_1_to_many( exposure_results ) ``` -------------------------------- ### get_r_from_bsen Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Calculates correlation coefficient from beta, standard error, and sample size. ```APIDOC ## get_r_from_bsen ### Description Calculates correlation coefficient from beta, standard error, and sample size. ### Parameters #### Path Parameters - **beta** (numeric vector) - Required - Effect size estimates. - **se** (numeric vector) - Required - Standard errors. - **n** (numeric vector) - Required - Sample sizes. ### Return numeric vector of correlation coefficients. ### Example ```r r <- get_r_from_bsen(beta = 0.05, se = 0.01, n = 100000) rsq <- r^2 ``` ``` -------------------------------- ### Local Data Analysis for MR in R Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/overview.md This pattern analyzes MR using local data files (exposure.txt, outcome.txt). It reads data, harmonizes it, and runs the MR analysis. Ensure the files are correctly formatted and located. ```r exposure <- read_exposure_data( filename = "exposure.txt", sep = "\t", clump = TRUE ) outcome <- read_outcome_data( filename = "outcome.txt", sep = "\t" ) harmonised <- harmonise_data(exposure, outcome) results <- mr(harmonised) ``` -------------------------------- ### Set Proxy SNP Lookup Batch Size with proxy_splitsize Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Configure the batch size for proxy SNP lookups using the `proxy_splitsize` parameter. The default value for this parameter is 500. ```r extract_outcome_data( snps = instruments$SNP, outcomes = "ieu-a-5", proxy_splitsize = 500 ) ``` -------------------------------- ### Align Proxy Alleles to Target Alleles Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Set `align_alleles = 1` to ensure that proxy SNP alleles are aligned to the target alleles. The default is 1. ```r extract_outcome_data( snps = instruments$SNP, outcomes = "ieu-a-5", align_alleles = 1 ) ``` -------------------------------- ### Format Proteomic QTL Data Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/harmonisation-functions.md Use this function to format proteomic (pQTL) data from a text file. Specify column names if they differ from defaults. The function returns a data.frame suitable for exposure or outcome data. ```r format_proteomic_qtls( filename, type = "exposure", snp_col = "SNP", phenotype_col = "protein", beta_col = "beta", se_col = "se", eaf_col = "eaf", effect_allele_col = "effect_allele", other_allele_col = "other_allele", pval_col = "p" ) ``` ```r pqtl_dat <- format_proteomic_qtls("protein_qtl.txt", type = "exposure") ``` -------------------------------- ### Set Random Seed for Reproducible MR-Presso Results Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Ensure reproducible results for stochastic methods like MR-Presso by setting the `seed` parameter. The default seed is 123. ```r set.seed(123) run_mr_presso(harmonised_dat, seed = 123) ``` -------------------------------- ### mr_grip Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Applies the Generalized Regression using Instrumental Polynomial (GRIP) method for MR analysis. ```APIDOC ## mr_grip ### Description Generalized Regression using Instrumental Polynomial (GRIP) method. ### Parameters Same as `mr_penalised_weighted_median()`. ### Return List with GRIP regression results. ``` -------------------------------- ### Create Contingency Table for Allele Combinations Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Generates a contingency table counting allele combinations from exposure and outcome data. Requires character vectors of alleles. ```r contingency(alleles_exp, alleles_out) ``` ```r cont <- contingency( alleles_exp = dat$effect_allele.exposure, alleles_out = dat$effect_allele.outcome ) ``` -------------------------------- ### Fetch and Display Available GWAS Studies Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/core-data-functions.md Fetches all available GWAS studies and displays the first few rows, showing key metadata like ID, trait, number of SNPs, and sample size. ```r all_studies <- available_outcomes() head(all_studies[, c("id", "trait", "nsnp", "sample_size")]) ``` -------------------------------- ### Unweighted Regression MR Method Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Implements the unweighted regression MR method. Requires exposure and outcome effect sizes and their standard errors. ```r mr_uwr( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### mr_meta_fixed_simple Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Simple fixed effects meta-analysis MR method. A simplified approach to fixed-effects meta-analysis for MR. ```APIDOC ## mr_meta_fixed_simple ### Description Simple fixed effects meta-analysis MR method. ### Parameters Same as `mr_penalised_weighted_median()`. ### Return List with elements: b, se, pval, nsnp. ``` -------------------------------- ### Create Density Plot of Causal Estimates Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/visualisation-functions.md Generates a density plot of causal estimates. Requires MR results and harmonized data. Customizable with line type and size. ```R mr_density_plot( mr_results, dat, linetype = 1, linesize = 1 ) ``` -------------------------------- ### Maximum Likelihood MR Method Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/mr-methods.md Utilize mr_two_sample_ml for maximum likelihood MR analysis. Parameters are the same as mr_ivw. ```r mr_two_sample_ml( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### Median of Estimates (MOE) MR Method Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Implements the Median of the Estimates (MOE) MR method. Requires exposure and outcome effect sizes and their standard errors. ```r mr_moe( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` ```r result <- mr_moe( b_exp = dat$beta.exposure, b_out = dat$beta.outcome, se_exp = dat$se.exposure, se_out = dat$se.outcome ) ``` -------------------------------- ### Perform Heterogeneity Test (Q statistic) Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Use `mr_heterogeneity` to assess the consistency of SNP estimates by performing a heterogeneity test. It returns the Q statistic, degrees of freedom, and p-value. This is useful for identifying potential violations of the MR assumptions. ```r mr_heterogeneity( dat, parameters = default_parameters(), method_list = mr_ivw() ) ``` ```r het_results <- mr_heterogeneity( dat = harmonized_dat, method_list = c("mr_ivw", "mr_egger_regression") ) # High p-value suggests homogeneity print(het_results) ``` -------------------------------- ### Configure LD Clumping r-squared Threshold Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Use the `clump_r2` parameter to set the LD r-squared threshold. A value of 0.001 is very strict, while 0.01 is moderate and commonly used. ```r clump_data(dat, clump_r2 = 0.001) ``` -------------------------------- ### mr_wrapper Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md A versatile wrapper function to apply any specified MR method to harmonized data. ```APIDOC ## mr_wrapper ### Description Wrapper function to apply any MR method to data. ### Parameters #### Parameters - **dat** (data.frame) - Required - Harmonized data. - **method_name** (character) - Required - Name of MR method to apply. - **parameters** (list) - Optional - Method parameters. Defaults to `default_parameters()`. ### Return data.frame with results from specified method. ### Example ```r results <- mr_wrapper( dat = harmonized_dat, method_name = "mr_ivw" ) ``` ``` -------------------------------- ### Perform Rucker's Q-Q' Test Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Applies Rucker's Q-Q' test to detect outlier SNPs and evaluate heterogeneity. Use the Q_prime_pval to identify potential outliers. ```R mr_rucker( dat, parameters = default_parameters() ) ``` -------------------------------- ### Format Metabolomic QTL Data Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/harmonisation-functions.md Use this function to format metabolomic QTL data from a text file. Specify column names if they differ from defaults. The function returns a data.frame suitable for exposure or outcome data. ```r format_metab_qtls( filename, type = "exposure", snp_col = "SNP", phenotype_col = "metabolite", beta_col = "beta", se_col = "se", eaf_col = "eaf", effect_allele_col = "effect_allele", other_allele_col = "other_allele", pval_col = "p" ) ``` ```r metab_dat <- format_metab_qtls("metabolite_qtl.txt", type = "exposure") ``` -------------------------------- ### Retrieve Population Allele Frequencies Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Fetches allele frequencies for specified SNPs from a reference panel for a given population. Requires a character vector of SNP rs IDs and a population code. ```r get_population_allele_frequency( snps = instruments$SNP, pop = "EUR" ) ``` -------------------------------- ### Set API Query Batch Size with splitsize Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Use the `splitsize` parameter to control the batch size for API queries when the number of SNPs or outcomes exceeds this value. The default is 10000. ```r extract_outcome_data( snps = instruments$SNP, outcomes = "ieu-a-5", splitsize = 10000 ) ``` -------------------------------- ### format_1_to_many Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/visualisation-functions.md Formats data specifically for one-to-many MR analysis visualization. ```APIDOC ## format_1_to_many ### Description Formats data for one-to-many analysis visualization. ### Parameters #### Path Parameters - **exposure_results** (data.frame) - Required - MR results. ### Return Formatted data.frame ready for visualization. ``` -------------------------------- ### weighted_median_bootstrap Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Bootstrap version of weighted median. It calculates MR estimates using bootstrapping for the weighted median method. ```APIDOC ## weighted_median_bootstrap ### Description Bootstrap version of weighted median. It calculates MR estimates using bootstrapping for the weighted median method. ### Parameters Same as `weighted_median()`, plus parameters list. ### Return List with bootstrap results. ``` -------------------------------- ### Perform Basic Multivariable MR Analysis Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/multivariable-mr.md Use `mv_basic` for a simplified multivariable MR analysis. This function requires pre-harmonized exposure data. ```r mv_basic( mv_harmonized_data ) ``` ```r basic_results <- mv_basic(mv_harmonized) ``` -------------------------------- ### get_population_allele_frequency Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Retrieves population allele frequencies from reference panels. ```APIDOC ## get_population_allele_frequency ### Description Retrieves population allele frequencies from reference panels. ### Parameters #### Path Parameters - **snps** (character vector) - Required - SNP rs IDs. - **pop** (character) - Optional - Population code ("EUR", "AFR", "SAS", "EAS", etc.). Defaults to "EUR". ### Return data.frame with SNP allele frequencies for specified population. ### Example ```r euron_freqs <- get_population_allele_frequency( snps = instruments$SNP, pop = "EUR" ) ``` ``` -------------------------------- ### Penalised Weighted Median MR Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Implements the penalised weighted median MR method. Use this for robust estimation when some instruments may be invalid. ```r mr_penalised_weighted_median( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` ```r result <- mr_penalised_weighted_median( b_exp = dat$beta.exposure, b_out = dat$beta.outcome, se_exp = dat$se.exposure, se_out = dat$se.outcome ) ``` -------------------------------- ### Subset MR Results by Method Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/statistical-utility-functions.md Subsets MR results based on a specified method name or pattern. Requires MR results and a method pattern. ```r subset_on_method(mr_results, method_pattern) ``` -------------------------------- ### Create Standardized Dataset Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/core-data-functions.md Combines exposure and outcome data into a standardized format, merging by SNP. The `outcome_snps` parameter determines whether to prioritize outcome or exposure SNPs for merging. ```r make_dat(exposure_dat, outcome_dat, outcome_snps = FALSE) ``` -------------------------------- ### Extract Outcome Data with Proxy SNP Search Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Set `proxies = TRUE` to enable searching for LD proxies for missing SNPs, increasing the number of available SNPs for analysis. The default is TRUE. ```r extract_outcome_data( snps = instruments$SNP, outcomes = "ieu-a-5", proxies = TRUE ) ``` -------------------------------- ### format_proteomic_qtls Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/harmonisation-functions.md Formats proteomic (pQTL) data from text files into a data.frame suitable for exposure or outcome data in TwoSampleMR. ```APIDOC ## format_proteomic_qtls ### Description Formats proteomic (pQTL) data from text files. ### Parameters #### Path Parameters - **filename** (character) - Required - Path to pQTL file. #### Query Parameters - **type** (character) - Optional - Default: "exposure" - "exposure" or "outcome". - **snp_col** (character) - Optional - Default: "SNP" - Column name for SNP ID. - **phenotype_col** (character) - Optional - Default: "protein" - Column name for protein/phenotype. - **beta_col** (character) - Optional - Default: "beta" - Column name for effect size. - **se_col** (character) - Optional - Default: "se" - Column name for standard error. - **eaf_col** (character) - Optional - Default: "eaf" - Column name for effect allele frequency. - **effect_allele_col** (character) - Optional - Default: "effect_allele" - Column name for effect allele. - **other_allele_col** (character) - Optional - Default: "other_allele" - Column name for other allele. - **pval_col** (character) - Optional - Default: "p" - Column name for p-value. ### Return data.frame formatted as exposure or outcome data. ### Example ```r pqtl_dat <- format_proteomic_qtls("protein_qtl.txt", type = "exposure") ``` ``` -------------------------------- ### Weighted Median Bootstrap Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/additional-methods.md Performs a bootstrap version of the weighted median calculation. Use for robust estimation when dealing with potential outliers or wanting to assess variability. ```r weighted_median_bootstrap( b_exp, b_out, se_exp, se_out, parameters = default_parameters() ) ``` -------------------------------- ### Run MR-PRESSO for Outlier Detection Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Executes the MR-PRESSO method to identify and correct potential outlier SNPs. Configurable with number of distributions, significance threshold, and random seed. ```r run_mr_presso( dat = harmonized_dat, NbDistribution = 10000, SignifThreshold = 0.05 ) ``` ```r print(presso_results$outliers) ``` -------------------------------- ### Set Minimum P-value Threshold Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Use `min_pval` to set the minimum allowed p-value threshold, preventing numerical underflow in calculations. The default is 1e-200. ```r read_exposure_data(filename, min_pval = 1e-200) ``` -------------------------------- ### Perform Jackknife Rucker's Q-Q' Test Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/api-reference/sensitivity-pleiotropy-tests.md Executes a jackknife leave-one-out version of Rucker's test to assess SNP contributions to heterogeneity. It shares the same parameters as mr_rucker(). ```R mr_rucker_jackknife( dat, parameters = default_parameters() ) ``` -------------------------------- ### Set OPENGWAS_JWT Environment Variable in R Source: https://github.com/mrcieu/twosamplemr/blob/master/_autodocs/configuration.md Demonstrates how to set the OPENGWAS_JWT environment variable within an R session for authenticating with the OpenGWAS API. This is useful for functions that retrieve data from protected endpoints. ```r Sys.setenv(OPENGWAS_JWT = "your_jwt_token_here") ```