-
Notifications
You must be signed in to change notification settings - Fork 2
/
cholsim - simulating expected effect of intervention for a defined period, calculating and plotting.R
170 lines (140 loc) · 5.66 KB
/
cholsim - simulating expected effect of intervention for a defined period, calculating and plotting.R
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
rm(list=ls())#clear environment
set.seed(1234)
N <- 750000
id <- c(1:N)
#log(x) curve
par(mfrow=c(1,1), oma=c(0,0,0,0))
curve(log10(x), from = 1, to = 10,
main = "Guidelines Adherence",
xlab = "effort",
ylab = "proportion adhering to guidelines",
xaxt = "n")
points(x = 10^0.5, y = 0.5, pch=0)
points(x = 10^0.6, y = 0.6, pch=6)
points(x = 10^0.7, y = 0.7, pch=8)
#creates a sex variable for men and appends women
treated <- rep.int(0,125000)
treated <- append(treated, rep.int(1,125000))
treated <- append(treated, rep.int(0,100000))
treated <- append(treated, rep.int(1,150000))
treated <- append(treated, rep.int(0,75000))
treated <- append(treated, rep.int(1,175000))
groupname <- rep.int(1,250000)
groupname <- c(groupname, rep.int(2,250000))
groupname <- c(groupname, rep.int(3,250000))
#creates dataframe from sex and id vectors
data = data.frame(treated, id, groupname)
class(data$treated)
data$treated <- factor(data$treated, levels = c(0,1), labels = c("untreated","treated"))
data$groupname <- factor(data$groupname, levels = c(1,2,3), labels = c("group 1", "group 2", "group 3"))
#generates death to overall
data$year_0 <- 1
for (i in 1:40){
if (i == 1){
n <- ifelse(data$treated=="treated", rbinom(N, 1, 1-0.035/48), rbinom(N, 1, 1-0.05/48))
}
else {
n <- ifelse(data$treated=="treated",
ifelse(data[[paste0("year_", i-1)]] == 0, 0, rbinom(N, 1, 1-0.035/48)),
ifelse(data[[paste0("year_", i-1)]] == 0, 0, rbinom(N, 1, 1-0.05/48))
)
}
data[[paste0("year_", i)]] <- n
}
gc() #clears memory
data_long <- reshape(data, direction="long", varying= c(list(4:44)), sep = "_",
idvar="id", timevar=c("year"))
class(data_long$year)
data_long$year <- as.numeric(data_long$year)
data_long$year <- data_long$year -1
### plots improvement in effort
par(mfrow=c(1,2), oma=c(0,0,2,0))
logdataframe <- 1:100
logdataframe <- as.numeric(logdataframe)
logdataframe <- as.data.frame(logdataframe)
logdataframe$log <- log10(logdataframe$logdataframe)
class(logdataframe$log)
#plots
mysum <- function(x){
250000-sum(x)
}
##plot all cause mortality
plot(aggregate(year_0~year,data = subset(data_long, groupname=="group 1"), FUN= function(z) mysum(z)),
main = "All Cause Mortality",
pch=20,
col= "red",
xlab= "Quarters",
ylab= "accummulated all-cause mortality")
text(x=16, y=1, labels= "Current 50% treated")
points(x=32, y=1, pch=20, col="red")
text(x=16, y=1000, labels= "60% treated")
points(x=32, y=1000, pch=20, col ="blue")
text(x=16, y=2800, labels= "70% treated")
points(x=32, y=2800, pch=20, col="green")
points(aggregate(year_0~year,data = subset(data_long, groupname=="group 2"), FUN= function(z) mysum(z)),
col="blue",
pch=20)
points(aggregate(year_0~year,data = subset(data_long, groupname=="group 3"), FUN= function(z) mysum(z)),
col="green",
pch=20)
title("Expected outcome of improvement in LLD adherence in Denmark", outer=TRUE, sub = "Simulation on Danish population with indication for LLD treatment")
survivortable <- 1
as.data.frame(survivortable)
group1 <- aggregate(year_0~year,data = subset(data_long, groupname=="group 1"), FUN= function(z) mysum(z))
group2 <- aggregate(year_0~year,data = subset(data_long, groupname=="group 2"), FUN= function(z) mysum(z))
group3 <- aggregate(year_0~year,data = subset(data_long, groupname=="group 3"), FUN= function(z) mysum(z))
groups <- data.frame(group1, group2, group3)
groups$year.1 <- NULL
groups$year.2 <- NULL
colnames(groups) <- c("year","group 1", "group 2", "group 3")
####---- CVD EVENT
#generates death to overall
data$year_0 <- 1
for (i in 1:40){
if (i == 1){
n <- ifelse(data$treated=="treated", rbinom(N, 1, 1-0.016/48), rbinom(N, 1, 1-0.03/48))
}
else {
n <- ifelse(data$treated=="treated",
ifelse(data[[paste0("year_", i-1)]] == 0, 0, rbinom(N, 1, 1-0.016/48)),
ifelse(data[[paste0("year_", i-1)]] == 0, 0, rbinom(N, 1, 1-0.03/48))
)
}
data[[paste0("year_", i)]] <- n
}
data_long <- reshape(data, direction="long", varying= c(list(4:44)), sep = "_",
idvar="id", timevar=c("year"))
class(data_long$year)
data_long$year <- as.numeric(data_long$year)
data_long$year <- data_long$year -1
#plots
mysum <- function(x){
250000-sum(x)
}
plot(aggregate(year_0~year,data = subset(data_long, groupname=="group 1"), FUN= function(z) mysum(z)),
main = "Myocardial infarction, Stroke, CV Death ",
pch=20,
col="red",
xlab= "Quarters",
ylab= "accumulated MI, Stroke, CV Death")
points(aggregate(year_0~year,data = subset(data_long, groupname=="group 2"), FUN= function(z) mysum(z)),
col="blue",
pch=20)
points(aggregate(year_0~year,data = subset(data_long, groupname=="group 3"), FUN= function(z) mysum(z)),
col="green",
pch=20)
text(x=4, y=1, labels= "Current 50% treated")
points(x=8, y=1, pch=20, col="red")
text(x=4, y=500, labels= "60% treated")
points(x=8, y=500, pch=20, col="blue")
text(x=4, y=1000, labels= "70% treated")
points(x=8, y=1000, pch=20, col="green")
survivortable <- 1
as.data.frame(survivortable)
group1 <- aggregate(year_0~year,data = subset(data_long, groupname=="group 1"), FUN= function(z) mysum(z))
group2 <- aggregate(year_0~year,data = subset(data_long, groupname=="group 2"), FUN= function(z) mysum(z))
group3 <- aggregate(year_0~year,data = subset(data_long, groupname=="group 3"), FUN= function(z) mysum(z))
groupsCVD <- data.frame(group1, group2, group3)
groupsCVD$year.1 <- NULL
groupsCVD$year.2 <- NULL
colnames(groupsCVD) <- c("year","group 1", "group 2", "group 3")