-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.rmd
158 lines (110 loc) · 3.65 KB
/
README.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
output:
html_document:
toc: true
keep_md: true
fig_width: 7
fig_height: 5
self_contained: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.path = "vignettes/readme_figs/")
```
[![version](http://www.r-pkg.org/badges/version/vennLasso)](https://cran.r-project.org/package=vennLasso)
[![Build Status](https://travis-ci.org/jaredhuling/vennLasso.svg?branch=master)](https://travis-ci.org/jaredhuling/vennLasso)
# vennLasso
The `vennLasso` package provides methods for hierarchical variable selection for models with covariate effects stratified by multiple binary factors.
## Installation and Help Files
The `vennLasso` package can be installed from CRAN using:
```{r, eval = FALSE}
install.packages("vennLasso")
```
The development version can be installed using the **devtools** package:
```{r, eval = FALSE}
devtools::install_github("jaredhuling/vennLasso")
```
or by cloning and building.
Load the **vennLasso** package:
```{r, warning=FALSE, message=FALSE}
library(vennLasso)
```
Access help file for the main fitting function ``vennLasso()`` by running:
```{r, eval = FALSE}
?vennLasso
```
Help file for cross validation function ``cv.vennLasso()`` can be accessed by running:
```{r, eval = FALSE}
?cv.vennLasso
```
## A Quick Example
Simulate heterogeneous data:
```{r}
set.seed(100)
dat.sim <- genHierSparseData(ncats = 3, # number of stratifying factors
nvars = 25, # number of variables
nobs = 150, # number of observations per strata
nobs.test = 10000,
hier.sparsity.param = 0.5,
prop.zero.vars = 0.75, # proportion of variables
# zero for all strata
snr = 0.5, # signal-to-noise ratio
family = "gaussian")
# design matrices
x <- dat.sim$x
x.test <- dat.sim$x.test
# response vectors
y <- dat.sim$y
y.test <- dat.sim$y.test
# binary stratifying factors
grp <- dat.sim$group.ind
grp.test <- dat.sim$group.ind.test
```
Inspect the populations for each strata:
```{r}
plotVenn(grp)
```
Fit vennLasso model with tuning parameter selected with 5-fold cross validation:
```{r}
fit.adapt <- cv.vennLasso(x, y,
grp,
adaptive.lasso = TRUE,
nlambda = 50,
family = "gaussian",
standardize = FALSE,
intercept = TRUE,
nfolds = 5)
```
Plot selected variables for each strata (not run):
```{r, eval = TRUE}
library(igraph)
plotSelections(fit.adapt)
```
Predict response for test data:
```{r}
preds.vl <- predict(fit.adapt, x.test, grp.test, s = "lambda.min",
type = 'response')
```
Evaluate mean squared error:
```{r}
mean((y.test - preds.vl) ^ 2)
```
```{r}
mean((y.test - mean(y.test)) ^ 2)
```
Compare with naive model with all interactions between covariates and stratifying binary factors:
```{r}
df.x <- data.frame(y = y, x = x, grp = grp)
df.x.test <- data.frame(x = x.test, grp = grp.test)
# create formula for interactions between factors and covariates
form <- paste("y ~ (", paste(paste0("x.", 1:ncol(x)), collapse = "+"), ")*(grp.1*grp.2*grp.3)" )
```
Fit linear model and generate predictions for test set:
```{r}
lmf <- lm(as.formula(form), data = df.x)
preds.lm <- predict(lmf, df.x.test)
```
Evaluate mean squared error:
```{r}
mean((y.test - preds.lm) ^ 2)
mean((y.test - preds.vl) ^ 2)
```