joinet.Rmd 4.37 KB
Newer Older
Armin Rauschenberger's avatar
Armin Rauschenberger committed
1
---
Armin Rauschenberger's avatar
Armin Rauschenberger committed
2
title: Multivariate Elastic Net Regression
Armin Rauschenberger's avatar
Armin Rauschenberger committed
3
4
5
6
7
8
9
10
11
12
13
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{vignette}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
14
set.seed(1)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
15
16
17
18
```

## Installation

Armin Rauschenberger's avatar
Armin Rauschenberger committed
19
Install the current release from [CRAN](https://CRAN.R-project.org/package=joinet):
Armin Rauschenberger's avatar
Armin Rauschenberger committed
20
21

```{r,eval=FALSE}
Armin Rauschenberger's avatar
Armin Rauschenberger committed
22
install.packages("joinet")
Armin Rauschenberger's avatar
Armin Rauschenberger committed
23
24
```

Armin Rauschenberger's avatar
Armin Rauschenberger committed
25
Or install the latest development version from [GitHub](https://github.com/rauschenberger/joinet):
Armin Rauschenberger's avatar
Armin Rauschenberger committed
26
27
28

```{r,eval=FALSE}
#install.packages("devtools")
Armin Rauschenberger's avatar
Armin Rauschenberger committed
29
devtools::install_github("rauschenberger/joinet")
Armin Rauschenberger's avatar
Armin Rauschenberger committed
30
```
Armin Rauschenberger's avatar
Armin Rauschenberger committed
31

Armin Rauschenberger's avatar
Armin Rauschenberger committed
32
33
34
35
36
37
38
39
40
## Initialisation

Load and attach the package:

```{r}
library(joinet)
```

And access the [documentation](https://rauschenberger.github.io/joinet/):
Armin Rauschenberger's avatar
Armin Rauschenberger committed
41
42
43
44

```{r,eval=FALSE}
?joinet
help(joinet)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
browseVignettes("joinet")
```

## Simulation

For `n` samples, we simulate `p` inputs (features, covariates) and `q` outputs (outcomes, responses). We assume high-dimensional inputs (`p` $\gg$ `n`) and low-dimensional outputs (`q` $\ll$ `n`).

```{r}
n <- 100
q <- 2
p <- 500
```

We simulate the `p` inputs from a multivariate normal distribution. For the mean, we use the `p`-dimensional vector `mu`, where all elements equal zero. For the covariance, we use the `p` $\times$ `p` matrix `Sigma`, where the entry in row $i$ and column $j$ equals `rho`$^{|i-j|}$. The parameter `rho`  determines the strength of the correlation among the inputs, with small `rho` leading weak correlations, and large `rho` leading to strong correlations (0 < `rho` < 1). The input matrix `X` has `n` rows and `p` columns.

```{r}
mu <- rep(0,times=p)
rho <- 0.90
Sigma <- rho^abs(col(diag(p))-row(diag(p)))
X <- MASS::mvrnorm(n=n,mu=mu,Sigma=Sigma)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
65
66
```

Armin Rauschenberger's avatar
Armin Rauschenberger committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
We simulate the input-output effects from independent Bernoulli distributions. The parameter `pi` determines the number of effects, with small `pi` leading to few effects, and large `pi` leading to many effects (0 < `pi` < 1). The scalar `alpha` represents the intercept, and the `p`-dimensional vector `beta` represents the slopes.

```{r}
pi <- 0.01
alpha <- 0
beta <- rbinom(n=p,size=1,prob=pi)
```

From the intercept `alpha`, the slopes `beta` and the inputs `X`, we calculate the linear predictor, the `n`-dimensional vector `eta`. Rescale the linear predictor to make the effects weaker or stronger. Set the argument `family` to `"gaussian"`, `"binomial"`, or `"poisson"` to define the distribution. The `n` times `p` matrix `Y` represents the outputs. We assume the outcomes are *positively* correlated.

```{r,results="hide"}
eta <- alpha + X %*% beta
eta <- 1.5*scale(eta)
family <- "gaussian"

if(family=="gaussian"){
  mean <- eta
  Y <- replicate(n=q,expr=rnorm(n=n,mean=mean))
}

if(family=="binomial"){
  prob <- 1/(1+exp(-eta))
  Y <- replicate(n=q,expr=rbinom(n=n,size=1,prob=prob))
}

if(family=="poisson"){
  lambda <- exp(eta)
  Y <- replicate(n=q,expr=rpois(n=n,lambda=lambda))
}

cor(Y)
```

## Application

The function `joinet` fits univariate and multivariate regression. Set the argument `alpha.base` to 0 (ridge) or 1 (lasso).

```{r}
object <- joinet(Y=Y,X=X,family=family)
```

Standard methods are available. The function `predict` returns the predicted values from the univariate (`base`) and multivariate (`meta`) models. The function `coef` returns the estimated intercepts (`alpha`) and slopes (`beta`) from the multivariate model (input-output effects). And the function `weights` returns the weights from stacking (output-output effects).

```{r,eval=FALSE}
predict(object,newx=X)

coef(object)

weights(object)
```

The function `cv.joinet` compares the predictive performance of univariate (`base`) and multivariate (`meta`) regression by nested cross-validation. The argument `type.measure` determines the loss function.

```{r}
cv.joinet(Y=Y,X=X,family=family)
```

## Reference

Armin Rauschenberger's avatar
Armin Rauschenberger committed
126
Armin Rauschenberger and Enrico Glaab (2019). "joinet: predicting correlated outcomes jointly to improve clinical prognosis". *Manuscript in preparation.* 
Armin Rauschenberger's avatar
Armin Rauschenberger committed
127
<!--
Armin Rauschenberger's avatar
Armin Rauschenberger committed
128
129
130
131
132
```{r,eval=FALSE}
#install.packages("plsgenomics")
data(Ecoli,package="plsgenomics")
X <- Ecoli$CONNECdata
Y <- Ecoli$GEdata
Armin Rauschenberger's avatar
Armin Rauschenberger committed
133
loss <- cv.joinet(Y=Y,X=X)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
134
135
136
137
138
139
140

#install.packages("BiocManager")
#BiocManager::install("mixOmics")
data(liver.toxicity,package="mixOmics")
X <- liver.toxicity$gene
Y <- liver.toxicity$clinic
Y$Cholesterol.mg.dL. <- -Y$Cholesterol.mg.dL.
Armin Rauschenberger's avatar
Armin Rauschenberger committed
141
loss <- cv.joinet(Y=Y,X=X)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
142
```
Armin Rauschenberger's avatar
Armin Rauschenberger committed
143
-->