-
Notifications
You must be signed in to change notification settings - Fork 0
/
netdiffuseR_sunbelt2016_workshop_survival.Rmd
242 lines (188 loc) · 5.51 KB
/
netdiffuseR_sunbelt2016_workshop_survival.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
233
234
235
236
237
238
239
240
241
---
title: |
| Understanding Diffusion with __netdiffuseR__
| _Survival Analysis_
subtitle: Sunbelt 2016 INSNA
author: George Vega Yon \and Thomas Valente
date: |
| Newport Beach, CA
| April 5, 2016
institute: |
| Department of Preventive Medicine
| University of Southern California
header-includes: \usepackage{grffile}
output: beamer_presentation
fontsize: 10pt
---
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(SparseM)
```
# Setup
We will use the medical innovations data
\scriptsize
```{r setup, include=TRUE}
# Loading the required packages
library(survival)
library(netdiffuseR)
# Loading the data
data("medInnovationsDiffNet")
medInnovationsDiffNet
```
\normalsize
# Preparing the data
From __netdiffuseR__ we will get the following covariates:
- Cohesive exposure: Proportion of ego's adopters at each time period
- Structural equivalence exposure: Same as before but using the structural equivalence graph instead of the baseline network.
\scriptsize
```{r}
# Structural equivalence exposure
medInnovationsDiffNet[["seexp"]] <- exposure(
medInnovationsDiffNet, groupvar="city", alt.graph = "se")
# Cohesive exposure
medInnovationsDiffNet[["cohexp"]] <- exposure(medInnovationsDiffNet)
```
\normalsize
# Coercing data into a dataframe
\tiny
```{r}
# Getting the data for running the cox regression
dat <- diffnet.attrs(medInnovationsDiffNet, as.df = TRUE)
dat <- subset(
dat,
subset =
per <= toa &
per < 18 &
!is.na(date),
select = c(id, per, toa, date, city, proage, proage2, seexp, cohexp))
# Creating the event variable
dat$event <- with(dat, toa==per)
# Here, since its survival, we only care from when the doctor is aware.
dat <- subset(dat, per - date >=0)
# Checking out the data
dat <- dat[with(dat, order(id, per)),]
head(dat,10)
```
\normalsize
Notice that `diffnet.attrs` generates two extra variables: `per` (time period) and `id`.
# The `survival` package
- In order to work, the `survival` package works with `Surv` objects.
- These store the response/events and the time frame during which these occurred.
- Usually take the following form: `Surv(start, end, event)`.
- For this tutorial we will use the Cox model, from Andersen and Gill (1982)
$$\mathcal{L}(\beta) = \prod_{i=1}^n\left\{\frac{\exp\beta'x_i(T_i)}{\sum_{j}\exp{\beta'x_j(T_j)}}\right\}^{\delta_i}$$
Which can extended to time-variant covariates.
# The `survival` package
## The `Suvreg` object
- First, we need to create the `Suvreg` object using the function with the same name
\scriptsize
```{r}
# Needs a start, stop, event
surv_mi <- with(dat, Surv(per-1, per, event))
```
\normalsize
Notice the warning as the time frames should be grater than 1.
- Now, let's take a look at the object itself
\scriptsize
```{r}
head(surv_mi, 10)
```
\normalsize
# Fitting the model
## All cities
\scriptsize
```{r}
# Fitting a model
set.seed(1988)
mymodel <- formula(surv_mi ~ factor(city) + proage + I(proage^2) +
seexp + cohexp + cluster(id))
out <- coxph(mymodel, data=dat)
```
\normalsize
# Fitting the model
## All cities (cont. 1)
\scriptsize
```{r, echo=FALSE}
pander::pander(out)
```
\normalsize
# Fitting the model
## All cities (cont. 2)
More diganostics can be done as follows:
\tiny
```{r out.width=".6\\linewidth"}
# Diagnostics
fit <- survfit(out)
plot(fit, mark.time=TRUE, lty=1:2,
xlab="Time period", ylab="Survival")
legend("bottomleft", c("Adopters", "Non-adopters"),
lty=2:1, bty="n")
```
\normalsize
# Example with brazilian farmers
\scriptsize
```{r, out.width='.8\\linewidth'}
data("brfarmersDiffNet")
# Creating a classification for village
village <- as.integer(factor(brfarmersDiffNet[["village"]])) + 2
nvillage <- length(unique(village))
plot_threshold(brfarmersDiffNet,
vertex.sides = village, # Creates polygons
vertex.col = terrain.colors(nvillage)[village]) # Colors!
```
\normalsize
# Example with brazilian farmers
## Preparing the data
\tiny
```{r}
# Exposure variables
brfarmersDiffNet[["seexp"]] <- exposure(brfarmersDiffNet, alt.graph = "se",
groupvar="village", valued = TRUE)
brfarmersDiffNet[["cohexp"]] <- exposure(brfarmersDiffNet)
# Creating a dynamic version of age
age <- brfarmersDiffNet[["age"]]
pers <- brfarmersDiffNet$meta$pers
brfarmersDiffNet[["age_dyn"]] <- lapply(
seq_len(nslices(brfarmersDiffNet)), function(x) {
age + (pers[x] - 1966) # Surveyed in 1966
})
# Subset
dat <- diffnet.attrs(brfarmersDiffNet, as.df = TRUE)
dat <- subset(dat, per <= toa, select=c(per, toa, age_dyn, village, seexp, cohexp, id))
# Creating the event variable
dat$event <- with(dat, toa==per)
# Checking out the data
dat <- dat[with(dat, order(id, per)),]
head(dat,10)
```
\normalsize
# Example with brazilian farmers
## Fitting the data
\tiny
```{r}
out <- coxph(Surv(per-1, per, event) ~ factor(village) + seexp + cohexp + age_dyn + cluster(id),
data=dat)
```
```{r, echo=FALSE, warning=FALSE, message=FALSE}
pander::pander(out)
```
\normalsize
# Example with brazilian farmers
## Diagnotstics
More diganostics can be done as follows:
\tiny
```{r out.width=".6\\linewidth"}
# Diagnostics
fit <- survfit(out)
plot(fit, mark.time=TRUE, lty=1:2,
xlab="Time period", ylab="Survival", firstx = min(dat$per))
legend("bottomleft", c("Adopters", "Non-adopters"),
lty=2:1, bty="n")
```
\normalsize
# Example with brazilian farmers
## Diagnotstics (cont.)
\tiny
```{r out.width=".6\\linewidth"}
cox.zph(out)
```
\normalsize