---
title: "xlink"
author: "Wei Xu, Meiling Hao, Yi Zhu"
date: "`r Sys.Date()`"
show_toc: true
slug: xlink
githubEditURL: https://github.com/qiuanzhu/xlink/blob/master/vignettes/xlink.Rmd
output: 
     rmarkdown::html_vignette:
     toc: yes
vignette: >
  %\VignetteIndexEntry{An Introduction to xlink}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# 1. Introduction
**xlink** from [github](https://github.com/qiuanzhu/xlink) is a package for the unified partial likelihood approach for X-chromosome association on time-to-event/ continuous/ binary outcomes. The expression of X-chromosome undergoes three possible biological processes: X-chromosome inactivation (XCI), escape of the X-chromosome inactivation (XCI-E), and skewed X-chromosome inactivation (XCI-S). Although these expressions are included in various predesigned genetic variation chip platforms, the X-chromosome has generally been excluded from the majority of genome-wide association studies analyses; this is most likely due to the lack of a standardized method in handling X-chromosomal genotype data. To analyze the X-linked genetic association for time-to-event outcomes with the actual process unknown, we propose a unified approach of maximizing the partial likelihood over all of the potential biological processes. The proposed method can be used to infer the true biological process and derive unbiased estimates of the genetic association parameters.

## Reference:
> "Xu, Wei, and Meiling Hao. "A unified partial likelihood approach for X‐chromosome association on time‐to‐event outcomes." Genetic epidemiology 42.1 (2018): 80-94." ([via](https://onlinelibrary.wiley.com/doi/abs/10.1002/gepi.22097))

> "Han, D., Hao, M., Qu, L., & Xu, W. (2019). ”A novel model for the X-chromosome inactivation association on survival data." Statistical Methods in Medical Research." 
([via](https://www.ncbi.nlm.nih.gov/pubmed/31258049))

# 2. Installation
You can install **xlink** from [github]((https://github.com/qiuanzhu/xlink):

```{r eval=FALSE}
library("devtools")
install_github("qiuanzhu/xlink")

```


#3. Examples


In the following examples, we choose the **model** is "survival" model, which could also applied to "linear" model for continuous response and "binary" for fitting logistic regression model.


## 3.1 Select significant SNPs from XCI or XCI-E model type:
In the sample data with 10 SNPs and 4 clinic covariates, 
```{r eval=FALSE}
library("xlink")
head(Rdata)

```


```{r, echo=FALSE, results='asis'}
library(xlink)
knitr::kable(head(Rdata))
```

If the Model type is chosen to be **XCI** and threshold for **MAF_v** is set to be 0.05, the output for snp_1 with coefficient, P value and loglikelihood information

```{r eval=FALSE}
Covars<-c("Age","Smoking","Treatment")
SNPs<-c("snp_1","snp_2")
output<-xlink_fit(os="OS",ostime="OS_time",snps=SNPs,gender="gender",covars=Covars, option =list(type="XCI",MAF_v=0.05),model="survival",data = Rdata)

```

```{r echo=FALSE, results='asis'}
Covars<-c("Age","Smoking","Treatment")
SNPs<-c("snp_1","snp_2")
output<-xlink_fit(os="OS",ostime="OS_time",snps=SNPs,gender="gender",covars=Covars, option =list(type="XCI",MAF_v=0.05),model="survival",data = Rdata)

```

```{r echo=FALSE, results='asis'}
knitr::kable(output[1]$snp_1$coefficients)


knitr::kable(output[1]$snp_1$loglik)

```


## 3.2 Select significant SNPs from all model type:

If the Model type is chosen to be **all**  and threshold for **MAF_v** is set to be 0.1, the output for snp_1 with coefficient , P value and log-likelihood function information for **XCI-E**, **XCI** and **XCI-S** respectively,

```{r eval=FALSE}
Covars<-c("Age","Smoking","Treatment")
SNPs<-c("snp_1","snp_2")
output<-xlink_fit(os="OS",ostime="OS_time",snps=SNPs,gender="gender",covars=Covars, option =list(type="all",MAF_v=0.05),model="survival",data = Rdata)

```

For XCI-E model, snp_1 with coefficient, P value and log-likelihood function information

```{r echo=FALSE, results='asis'}
output<-xlink_fit(os="OS",ostime="OS_time",snps=SNPs,gender="gender",covars=Covars, option =list(MAF_v=0.1),model="survival",data = Rdata)


knitr::kable(output$snp_1$`XCI-E`$coefficients)

knitr::kable(output$snp_1$`XCI-E`$loglik)

```


For XCI model, snp_1 with coefficient, P value and log-likelihood function information

```{r echo=FALSE, results='asis'}

knitr::kable(output$snp_1$`XCI`$coefficients)

knitr::kable(output$snp_1$`XCI`$loglik)

```


For XCI-S model, snp_1 with coefficient , log-likelihood function information and **gamma** estimation

```{r echo=FALSE, results='asis'}

knitr::kable(output$snp_1$`XCI-S`$coefficients)

knitr::kable(output$snp_1$`XCI-S`$loglik)

knitr::kable(output$snp_1$`XCI-S`$Gamma , col.names ="Gamma")

```

The best model for snp_1 among model type XCI-E, XCI and XCI-S by using the AIC is 

```{r echo=FALSE, results='asis'}

knitr::kable(output$snp_1$`Best model by AIC`, col.names = "Best model by AIC" )


```


 
## 3.3 Output for the significant SNPs by P value:
By setting the threshold for **pv_thold**, the select output become 

```{r eval=FALSE}
Covars<-c("Age","Smoking","Treatment")
SNPs<-c("snp_1","snp_2","snp_3")
result<-xlink_fit(os="OS",ostime ="OS_time",snps=SNPs,gender ="gender",covars=Covars, 
                   option =list(type="all",MAF_v=0.05), model="survival", data = Rdata)
select_output(input=result,pv_thold=10^-5)

```

```{r echo=FALSE, results='asis'}
Covars<-c("Age","Smoking","Treatment")
SNPs<-c("snp_1","snp_2","snp_3")
result<-xlink_fit(os="OS",ostime ="OS_time",snps=SNPs,gender ="gender",covars=Covars, 
                   option =list(type="all",MAF_v=0.05), model="survival", data = Rdata)


knitr::kable( select_output(input=result,pv_thold=10^-5) )

```
