-
Notifications
You must be signed in to change notification settings - Fork 4
/
test2.rmd
132 lines (108 loc) · 4.49 KB
/
test2.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
---
title: "Toy Network Models"
output: html_document
date: '2022-07-18'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r libraries}
library("EpiModel")
library("igraph")
```
```{r params}
N<-20 #network population/ graph order
eprob<-0.1 #edge formation probability for ER model
phiv<-0.1 # underlying hiv prevalence
PrEP1<-0.1 # PReP assignment coverage in control treatment (a)
PrEP2<-0.2 # PReP assignment coverage in counterfactual treatment (a*)
```
```{r control}
# plot a random graph, 3 color options
#control scenario graph, 10% assignment prob
g <- sample_gnp(N,eprob)
vertex_attr(g) <- list(color =c(rep("red", gorder(g)*phiv), rep("blue",gorder(g)*PrEP1), rep("black", gorder(g)*(1-(phiv+PrEP1)))))
plot(g, vertex.size=10,vertex.label.cex=1, vertex.label.dist=2)
df_g<-as_long_data_frame(g)
colnames(df_g)<-c("from","to","color1","color2")
```
```{r}
#compute P(HIV|PrEP)
treat_g<-subset(df_g, (df_g$color1=="blue"|df_g$color2=="blue"))
treat_inf_contact_g<-subset(treat_g, treat_g$color1=="red"|treat_g$color2=="red")
hiv_given_prep_g<-ifelse(nrow(treat_g)!=0,nrow(treat_inf_contact_g)/nrow(treat_g),0)
```
```{r}
#Compute P(HIV|-PrEP)
no_treat_g<-subset(df_g, df_g$color1!="blue"&df_g$color2!="blue")
no_treat_inf_contact_g<-subset(no_treat_g, no_treat_g$color1=="red"|no_treat_g$color2=="red")
# hiv_given_no_prep_g<-nrow(no_treat_inf_contact_g)/nrow(no_treat_g)
hiv_given_prep_g<-ifelse(nrow(no_treat_g)!=0,nrow(no_treat_inf_contact_g)/nrow(no_treat_g),0)
```
```{r treat 1}
#Randomly assign 20% overall (shuffle attributes)
h<-set_vertex_attr(g,"color",value=gtools::permute(c(rep("red", gorder(g)*phiv), rep("blue",gorder(g)*PrEP2), rep("black", gorder(g)*(1-(phiv+PrEP2))))))
plot(h, vertex.size=10,vertex.label.cex=1, vertex.label.dist=2)
df_h<-as_long_data_frame(h)
colnames(df_h)<-c("from","to","color1","color2")
```
```{r}
#Compute P(HIV|PrEP)
treat_h<-subset(df_h, (df_h$color1=="blue"|df_h$color2=="blue"))
treat_inf_contact_h<-subset(treat_h, treat_h$color1=="red"|treat_h$color2=="red")
hiv_given_prep_h<-ifelse(nrow(treat_h)!=0,nrow(treat_inf_contact_h)/nrow(treat_h),0)
```
```{r}
#Compute P(HIV|-PrEP)
no_treat_h<-subset(df_h, df_h$color1!="blue"&df_h$color2!="blue")
no_treat_inf_contact_h<-subset(no_treat_h, no_treat_h$color1=="red"|no_treat_h$color2=="red")
hiv_given_no_prep_h<-ifelse(nrow(no_treat_h)!=0,nrow(no_treat_inf_contact_h)/nrow(no_treat_h),0)
```
```{r treat2}
#duplicate network structure, additional 10% treated
j<-set_vertex_attr(g,"color",value=c(rep("red", gorder(g)*phiv), rep("blue",gorder(g)*PrEP2), rep("black", gorder(g)*(1-(phiv+PrEP2)))))
plot(j, vertex.size=10,vertex.label.cex=1, vertex.label.dist=2)
df_j<-as_long_data_frame(j)
colnames(df_j)<-c("from","to","color1","color2")
```
```{r}
#compute (HIV|PrEP)
treat_j<-subset(df_j, (df_j$color1=="blue"|df_j$color2=="blue"))
treat_inf_contact_j<-subset(treat_j, treat_j$color1=="red"|treat_j$color2=="red")
hiv_given_prep_j<-ifelse(nrow(no_treat_j)!=0,nrow(treat_inf_contact_j)/nrow(treat_j),0)
```
```{r}
#Compute P(HIV|-PrEP)
no_treat_j<-subset(df_j, df_j$color1!="blue"&df_j$color2!="blue")
no_treat_inf_contact_j<-subset(no_treat_j, no_treat_j$color1=="red"|no_treat_j$color2=="red")
hiv_given_no_prep_j<-ifelse(nrow(no_treat_j)!=0,nrow(no_treat_inf_contact_j)/nrow(no_treat_j),0)
```
```{r regen}
# plot a random graph, 3 color options
k <- sample_gnp(N,eprob)
vertex_attr(k) <- list(color =c(rep("red", gorder(k)*phiv), rep("blue",gorder(k)*PrEP1), rep("black", gorder(k)*(1-(phiv+PrEP1)))))
plot(k, vertex.size=10,vertex.label.cex=1, vertex.label.dist=2)
df_k<-as_long_data_frame(k)
colnames(df_k)<-c("from","to","color1","color2")
```
```{r}
#Compute P(HIV|PrEP)
treat_k<-subset(df_k, (df_k$color1=="blue"|df_k$color2=="blue"))
treat_inf_contact_k<-subset(treat_k, treat_k$color1=="red"|treat_k$color2=="red")
hiv_given_prep_k<-ifelse(nrow(treak_k)!=0,nrow(treat_inf_contact_k)/nrow(treat_k),0)
```
```{r}
#Compute P(HIV|-PrEP)
no_treat_k<-subset(df_k, df_k$color1!="blue"&df_k$color2!="blue")
no_treat_inf_contact_k<-subset(no_treat_k, no_treat_k$color1=="red"|no_treat_k$color2=="red")
hiv_given_no_prep_k<-ifelse(nrow(no_treat_k)!=0,nrow(no_treat_inf_contact_k)/nrow(no_treat_k),0)
```
```{r}
# Combine effect estimates
prep<-c(hiv_given_prep_g,hiv_given_prep_h,hiv_given_prep_j,hiv_given_prep_k)
no_prep<-c(hiv_given_no_prep_g,hiv_given_no_prep_h,hiv_given_no_prep_j,hiv_given_no_prep_k)
ef<-prep+no_prep
names(ef)<-c("control,","ran20","ran10+10","regen")
cc<-ef[-1]-ef[1]
names(cc)<-c("ran20","ran10+10", "regen")
```