%
% NOTE -- ONLY EDIT THE .Rnw FILE!!!  The .tex file is
% likely to be overwritten.
%
%\VignetteIndexEntry{GLIDE}
%\VignetteDepends{}
%\VignetteKeywords{Documentation}
%\VignettePackage{GLIDE}
\documentclass[12pt]{article}
\usepackage{times}
\usepackage{hyperref}
%\usepackage[authoryear,round]{natbib}

\textwidth=6.2in
\textheight=8.5in
%\parskip=.3cm
\oddsidemargin=.1in
\evensidemargin=.1in
\headheight=-.3in

\newcommand{\scscst}{\scriptscriptstyle}
\newcommand{\scst}{\scriptstyle}

\newcommand{\R}{{\textsf{R}}}
\newcommand{\code}[1]{{\texttt{#1}}}
\newcommand{\term}[1]{{\emph{#1}}}
\newcommand{\Rpackage}[1]{\textsf{#1}}
\newcommand{\Rfunction}[1]{\texttt{#1}}
\newcommand{\Robject}[1]{\texttt{#1}}
\newcommand{\Rclass}[1]{{\textit{#1}}}
\newcommand{\Rmethod}[1]{{\textit{#1}}}
\newcommand{\Rfunarg}[1]{{\textit{#1}}}

%\bibliographystyle{plainnat}

\begin{document}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{concordance=TRUE}

\title{The \Rpackage{GLIDE} package: Global and Individual Tests for Direct Effects in Mendelian randomization studies}
\author{Xiaoyu Wang, James Y. Dai}
\maketitle

\section{Introduction}

``Mendelian randomization'', an inferential method that uses germline genotypes as instrumental variables (IVs) to study the environmentally modifiable cause of human disease, has been increasingly popular in genetic epidemiology. Exploiting the random assortment of genes from parents to offsprings at the time of gamete formation, likened to ``Nature$^{\prime}$s randomized experiment.'' Mendelian randomization holds promise for removing confounding and reverse causation that loom over observational epidemiology. While conceptually appealing, skepticism has persisted on feasibility of strong assumptions required by Mendelian randomization, stated informally, that genetic variants are independent of confounders of the exposure disease relation, that genetic variants are associated with the exposure, preferably strongly, and that there is no direct effect from genetic variants to the disease other than the pathway through the exposure, also known as no ``pleiotropy''. Perhaps the Achilles$^{\prime}$ heel for Mendelian randomization is potential pleiotropic effects among candidate genetic IVs, which can be sometimes plausible given complex biological pathways and networks. This threat to the validity of Mendelian randomization is heightened by the very practice of including a large number of risk variants to boost the strength of IVs. The \Rpackage{GLIDE} package performs the diagnostic tests for assessing global and individual variant pleiotropy in Mendelian randomization studies.

<<loadLibrary, echo=FALSE, results=hide>>=

library(GLIDE)
@

\section{Main functions}

We show an example analysis using simulation data that resemble the BMI analysis for GECCO presented in the paper \cite{Dai2017}.
Briefly the dataset contains genotype data for 75 SNPs and 20,000 subjects, as well as demographic variables including age, gender
and 3 principal components for the parent genome-wide association study.
First we load the example dataset:
<<data>>=
data(simdata)
#The example dataset is a list composed of two dataframes. 
#simat stores 20,000 observations of 81 variables, 
#including outcome, 5 ajusting covariates, and 75 SNPs.  
simdat=simdata$simdat
dim(simdat)
head(colnames(simdat),n=10)
#coeff stores the 75 external regression cofficients.
coeff=simdata$coeff
head(coeff)

@

We define the columns in simdat that contain genotype data:
<<genotype_columns>>=
genotype_columns=which(grepl("^SNP",colnames(simdat)))
@

We next define the regression formula for outcome and adjusting covariates (variables to be adjusted for in addition to genotype
variables, typically include age, gender, and principal components), as in other generic R regression functions:
<<formula>>=
formula=as.formula("outcome~age+sex+pc1+pc2+pc3")
formula
@

Much of computation time is spent in calculating the correlation matrix of the surrogate direct effects as defined in the paper
\cite{Dai2017}. When there are many SNPs to be evaluated as candidate instrumental variables, parallel computing can be deployed
to speed up the glide function by setting the corenumber argument. If the requested number of cores is greater than number of
cores available, GLIDE will user the latter number. GLIDE allows users to define a q-value cut-off that declares the significance
of evaluating individual pleiotropy accounting for multiple testing. The default is qcutoff=0.2, to be conservative about validity
of individual SNPs. Users can set the number of permutations for q-value computation. The default number is np=100,000. We then
run the glide function with inputs as:
<<out>>=
out=glide(formula=formula,exposure_coeff=coeff,genotype_columns,data=simdat,
          np=100000,qcutoff=0.2,parallel=TRUE,corenumber=1,verbose=TRUE)
head(out)
@

\section{plot result}
GLIDE provides two functions for plotting the results: the first shows the q-q plot of individual variant$^{\prime}$s p-value for
evaluating direct effects, the other shows the Egger regression plot with point estimates of genetic association with the exposure
and the outcome. SNPs that were detected to have evidence of pleiotropy (for example, FDR<0.2) will be shown in both plots.

We draw the q-q plot for the example dataset: plot.glide(out).
We draw the Egger plot  for the example dataset: plot.egger(out,exposure\_coeff=coeff).
The figures are shown as follows:
@<<fig=TRUE,echo=FALSE, width=10, height=5>>=
@@
<<echo = FALSE, results=tex>>=
cat('\\begin{figure}[h]\n')
file = "./plot.png"
cat('\\includegraphics{', file, '}\n', sep = '')
cat('\\end{figure}\n')
@

There is one SNP showing evidence of pleiotropy that may be deleted in the subsequent Mendelian randomization analysis.

\section{session information}

The version number of \R{} and packages loaded for generating the vignette were:

\begin{verbatim}
<<sessionInfo, echo=FALSE, results=tex>>=
sessionInfo()
@
\end{verbatim}

\bibliographystyle{plain}
\bibliography{GLIDE}
 

\end{document}
