-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathREADME.Rmd
234 lines (176 loc) · 7.51 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(message = FALSE, warning=FALSE)
```
[![version](http://www.r-pkg.org/badges/version/personalized)](https://cran.r-project.org/package=personalized)
[![Build Status](https://travis-ci.org/jaredhuling/personalized.svg?branch=master)](https://travis-ci.org/jaredhuling/personalized)
[![Appveyor Build
Status](https://ci.appveyor.com/api/projects/status/github/jaredhuling/personalized?branch=master&svg=true)](https://ci.appveyor.com/project/jaredhuling/personalized)
[![codecov](https://codecov.io/gh/jaredhuling/personalized/branch/master/graph/badge.svg)](https://app.codecov.io/gh/jaredhuling/personalized)
[![](http://cranlogs.r-pkg.org/badges/personalized)](https://cran.r-project.org/package=personalized)
```{r make_sticker, echo = FALSE, out.width= "100%"}
library(hexSticker)
library(jcolors)
sticker(expression({plot.new(); text(0.5, 0.5, "p", font=2, cex = 10, col = scales::alpha("#0091b6", 0.35))}),
package="personalized",
p_size = 7, s_x=0.8, s_y=.75, s_width=1.3, s_height=1,
p_y = 1.35,
h_size = 2,
h_color = scales::alpha(unname(jcolors()[2]), 0.75),
h_fill = scales::alpha(unname(jcolors()[2]), 1),
filename="man/figures/sticker.png")
```
<img src="man/figures/sticker.png" align="right" width="15%" height="15%" />
# Overview of 'personalized'
The 'personalized' package is designed for the analysis of data where the effect of a treatment or intervention may vary for different patients. It can be used for either data from randomized controlled trials or observational studies and is not limited specifically to the analysis of medical data.
The personalized package provides estimation methods for subgroup identification under the framework of [Chen et al (2017)](https://doi.org/10.1111/biom.12676). It also provides routines for valid estimation of the subgroup-specific treatment effects.
```{r usage_overview, echo = FALSE, out.width= "100%", fig.width = 14, fig.height=7, fig.path="vignettes/"}
library(diagram)
library(scales)
par(mai = c(0.01, 0, 0.01, 0))
openplotmat(xlim = c(0, 1), ylim = c(0, 0.5))
elpos <- coordinates (c(3,3))
elpos[c(2:3, 5:6),1] <- elpos[c(2:3, 5:6),1] - 0.075
elpos[1:3,2] <- elpos[1:3,2] - 0.01
elpos[4:6,2] <- elpos[4:6,2] + 0.01
arrpos <- matrix(ncol = 2, nrow = 3)
arrpos[1,] <- straightarrow (to = elpos[2,] - c(0.12, 0),
from = elpos[1,] + c(0.1, 0),
lwd = 2, arr.pos = 0.8, arr.length = 0.5, endhead = TRUE)
arrpos[2,] <- straightarrow (to = elpos[3,] - c(0.16, 0),
from = elpos[2,] + c(0.12, 0),
lwd = 2, arr.pos = 0.8, arr.length = 0.5, endhead = TRUE)
arrpos[3,] <- straightarrow (to = elpos[6,] - c(0.16, 0),
from = elpos[5,] + c(0.12, 0),
lwd = 2, arr.pos = 0.8, arr.length = 0.5, endhead = TRUE)
textrect(elpos[1,], lab = expression(X~","~Y~","~Treatment),
box.col = alpha("deepskyblue3", 0.85),
lcol = alpha("deepskyblue3", 1),
shadow.size = 0, lwd = 0.001,
radx = 0.1,
rady = 0.065,
adj = (0.495),
cex = 2)
textrect(elpos[2,], lab = "Estimate \n Subgroups",
box.col = alpha("chartreuse3", 0.85),
lcol = alpha("deepskyblue3", 1),
shadow.size = 0, lwd = 0.001,
radx = 0.12,
rady = 0.065,
adj = (0.5),
cex = 2)
textrect(elpos[3,], lab = "Estimate Treatment Effects \n within Subgroups",
box.col = alpha("red", 0.85),
lcol = alpha("deepskyblue3", 1),
shadow.size = 0, lwd = 0.001,
radx = 0.16,
rady = 0.065,
adj = (0.5),
cex = 2)
textrect(elpos[5,], lab = "model <- \n fit.subgroup(x, y, trt)",
box.col = alpha("chartreuse3", 0.85),
lcol = alpha("deepskyblue3", 1),
shadow.size = 0, lwd = 0.001,
radx = 0.12,
rady = 0.065,
adj = (0.5),
cex = 2)
textrect(elpos[6,], lab = "validate.subgroup(model)",
box.col = alpha("red", 0.85),
lcol = alpha("deepskyblue3", 1),
shadow.size = 0, lwd = 0.001,
radx = 0.16,
rady = 0.065,
adj = (0.5),
cex = 2)
```
## Documentation
[Documentation](https://jaredhuling.org/personalized/)
- [Tutorial / Vignette](https://jaredhuling.org/personalized/articles/usage_of_the_personalized_package.html) - tutorial of statistical methodology and usage of the package
- [Function help files](https://jaredhuling.org/personalized/reference/index.html)
# Installing the 'personalized' package
Install from CRAN using:
```{r, eval = FALSE}
install.packages("personalized")
```
or install the development version using the **devtools** package:
```{r, eval = FALSE}
devtools::install_github("jaredhuling/personalized")
```
or by cloning and building using `R CMD INSTALL`
# Quick Usage Overview
Load the package:
```{r, message = FALSE, warning = FALSE}
library(personalized)
```
```{r gen_data, echo = FALSE}
set.seed(123)
n.obs <- 500
n.vars <- 50
x <- matrix(rnorm(n.obs * n.vars, sd = 3), n.obs, n.vars)
# simulate non-randomized treatment
xbetat <- 0.5 + 0.5 * x[,21] - 0.5 * x[,41]
trt.prob <- exp(xbetat) / (1 + exp(xbetat))
trt01 <- rbinom(n.obs, 1, prob = trt.prob)
trt <- ifelse(trt01 == 1, "Trt", "Ctrl")
# simulate response
delta <- 2 * (0.5 + x[,2] - x[,3] - x[,11])
xbeta <- x[,1] + x[,11] - 2 * x[,12]^2 + x[,13] + 0.5 * x[,15] ^ 2
xbeta <- xbeta + delta * (2 * trt01 - 1)
# continuous outcomes
y <- drop(xbeta) + rnorm(n.obs, sd = 2)
```
### Create a propensity score model
(it should be a function which inputs covariates and treatments and returns propensity score):
```{r propens}
prop.func <- function(x, trt)
{
# fit propensity score model
propens.model <- cv.glmnet(y = trt,
x = x, family = "binomial")
pi.x <- predict(propens.model, s = "lambda.min",
newx = x, type = "response")[,1]
pi.x
}
```
### Fit a model to estimate subgroup:
```{r fit_contin}
subgrp.model <- fit.subgroup(x = x, y = y,
trt = trt,
propensity.func = prop.func,
loss = "sq_loss_lasso",
nfolds = 5) # option for cv.glmnet
```
### Display estimated subgroups and variables selected which determine the subgroups:
```{r summarize}
summary(subgrp.model)
```
### Use repeated train and test splitting to estimate subgroup treatment effects:
```{r validate}
val.model <- validate.subgroup(subgrp.model, B = 100,
method = "training_test",
train.fraction = 0.75)
```
### Display estimated subgroup treatment effects:
```{r vieweffects}
print(val.model, digits = 2, sample.pct = TRUE)
```
Visualize subgroup-specific treatment effect estimates across training/testing iterations:
```{r vis_val, out.width="75%", fig.path="vignettes/", fig.align="center"}
plot(val.model)
```
### Investigate the marginal characteristics of the two estimated subgroups
Here we only display covariates with a significantly different mean value (at level 0.05)
```{r summarize_characteristics}
summ <- summarize.subgroups(subgrp.model)
print(summ, p.value = 0.05)
```
## Accessing Help Files for Main Functions of `personalized`
Access help files for the main functions of the `personalized` package:
```{r, eval = FALSE}
?fit.subgroup
?validate.subgroup
```