-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path-Effi.Rhistory
executable file
·512 lines (512 loc) · 24.3 KB
/
-Effi.Rhistory
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
category = c("nha", "nhanesyrsfu", "nhanesother"), # replaced nhanesna with nha in the category
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank"
)
v=draw.triple.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
category = c("Missing BP", "No follow-up", "Other Ethnicity"),
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank"
)
v=draw.triple.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
category = c("Missing BP", "No follow-up", "Other Ethnicity"),
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank"
)
v=draw.triple.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
category = c("Missing BP", "No follow-up", "Other Ethnicity"),
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank",
cat.pos = c(-40, 0,180)
)
v=draw.triple.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
category = c("Missing BP", "No follow-up", "Other Ethnicity"),
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank",
cat.pos = c(-40, 0,180)
)
grid.draw(v)
draw.triple.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
category = c("Missing BP", "No follow-up", "Other Ethnicity"),
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank",
cat.pos = c(-40, 0,180)
)
v <- draw.triple.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
category = c("Missing BP", "No follow-up", "Other Ethnicity"),
fill = c("skyblue", "pink1", "mediumorchid"),
lty = "blank",
cat.pos = c(-40, 0,180),
cex = 2
)
bprange <- nhanessysrange | nhanesdiasrange
v <- draw.quad.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
area4 = sum(bprange), # BP out of range
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n14 = sum(!nhanesna & bprange),
n24 = sum(nhanes$yrsfu==0 & bprange),
n34 = sum(nhanes$other==1 & bprange),
n124 = sum(!nhanesna & nhanes$yrsfu==0 & bprange),
n134 = sum(!nhanesna & nhanes$other==1 & bprange),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
n234=sum(nhanes$yrsfu==0 & nhanes$other==1 & bprange),
n1234=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1 & bprange),
category = c("Missing BP", "No follow-up", "Other Ethnicity","BP out of range"),
fill = c("skyblue", "pink1", "mediumorchid", "orange"),
lty = "blank",
cat.pos = c(180, 0,180,0),
cex = 1
)
sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1 & bprange)
sum(nhanes$yrsfu==0 & nhanes$other==1 & bprange)
sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1)
sum(!nhanesna & nhanes$other==1 & bprange)
sum(bprange)
bprange <- nhanessysrange | nhanesdiasrange
bprange[is.na(bprange)] <- FALSE
v <- draw.quad.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
area4 = sum(bprange), # BP out of range
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n14 = sum(!nhanesna & bprange),
n24 = sum(nhanes$yrsfu==0 & bprange),
n34 = sum(nhanes$other==1 & bprange),
n124 = sum(!nhanesna & nhanes$yrsfu==0 & bprange),
n134 = sum(!nhanesna & nhanes$other==1 & bprange),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
n234=sum(nhanes$yrsfu==0 & nhanes$other==1 & bprange),
n1234=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1 & bprange),
category = c("Missing BP", "No follow-up", "Other Ethnicity","BP out of range"),
fill = c("skyblue", "pink1", "mediumorchid", "orange"),
lty = "blank",
cat.pos = c(180, 0,180,0),
cex = 1
)
v <- draw.quad.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
area4 = sum(bprange), # BP out of range
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n14 = sum(!nhanesna & bprange),
n24 = sum(nhanes$yrsfu==0 & bprange),
n34 = sum(nhanes$other==1 & bprange),
n124 = sum(!nhanesna & nhanes$yrsfu==0 & bprange),
n134 = sum(!nhanesna & nhanes$other==1 & bprange),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
n234=sum(nhanes$yrsfu==0 & nhanes$other==1 & bprange),
n1234=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1 & bprange),
category = c("Missing BP", "No follow-up", "Other Ethnicity","BP out of range"),
fill = c("skyblue", "pink1", "mediumorchid", "orange"),
lty = "blank",
cat.pos = c(180, 0,180,0),
cex = 1
)
v <- draw.quad.venn(
area1=sum(!nhanesna), # Missing BP
area2=sum(nhanes$yrsfu==0), # No follow-up
area3=sum(nhanes$other==1), # Other ethnicity
area4 = sum(bprange), # BP out of range
n12=sum(!nhanesna & nhanes$yrsfu==0),
n23=sum(nhanes$yrsfu==0 & nhanes$other==1),
n13=sum(!nhanesna & nhanes$other==1),
n14 = sum(!nhanesna & bprange),
n24 = sum(nhanes$yrsfu==0 & bprange),
n34 = sum(nhanes$other==1 & bprange),
n124 = sum(!nhanesna & nhanes$yrsfu==0 & bprange),
n134 = sum(!nhanesna & nhanes$other==1 & bprange),
n123=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1),
n234=sum(nhanes$yrsfu==0 & nhanes$other==1 & bprange),
n1234=sum(!nhanesna & nhanes$yrsfu==0 & nhanes$other==1 & bprange),
category = c("Missing BP", "No follow-up", "Other Ethnicity","BP out of range"),
fill = c("skyblue", "pink1", "mediumorchid", "orange"),
lty = "blank",
cat.pos = c(220, 0,0,0),
cex = 1
)
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE)
library(tidyverse)
library(formatR)
library(magrittr)
library(knitr)
library(tinytex)
library(kableExtra)
library(VennDiagram)
# Load data
nhanes_old=read.csv('Data_raw/nh3bpdat290716.csv')
# Update mortality with 2017 public release
nhanes_update=read.csv('Data_raw/NHANES_ML_update.csv',colClasses = 'integer') %>%
mutate(yrsfuExam = round(permth_exm/12,2), yrsfuHome = round(permth_int/12, 2)) %>%
filter(eligstat == 1)
nhanes_old %<>% mutate(yrsfuExam = round(yrsfuExam, 2), yrsfuHome = round(yrsfuHome,2), permth_exm = round(yrsfuExam *12), permth_int = round(yrsfuHome*12))
# Compare old and new mortality
old_dead <- sort(nhanes_old$SEQN[nhanes_old$dead==1])
new_dead <- sort(nhanes_update$seqn[nhanes_update$mortstat==1])
conflict_dead <- old_dead[ !(old_dead %in% new_dead)] # Tested: No resurrections
missing_update <- nhanes_old$SEQN[!(nhanes_old$SEQN %in% nhanes_update$seqn)] #Tested: No one from old data set missing from followup
missing_old <- !(nhanes_update$seqn %in% nhanes_old$SEQN) # 6 subjects in the updated data set with mortality data (all alive) who were not in the old data set.
nhanes_update %<>% filter(!missing_old) # remove them
identical(nhanes_old$SEQN,nhanes_update$seqn) # Sequence of ids now identical
fu_diff <- nhanes_update$permth_int - nhanes_update$permth_exm
fu_diff_update <- nhanes_update$permth_int - nhanes_old$permth_int
identical(nhanes_old$UCOD_LEADING[!is.na(nhanes_old$UCOD_LEADING)], nhanes_update$ucod_leading[!is.na(nhanes_old$UCOD_LEADING)])
#TRUE, so cause-of-death codes for all individuals who were dead in the first data set are identical
conflict_old <- subset(nhanes_old, nhanes_old$SEQN %in% conflict_dead)
conflict_new <- subset(nhanes_update, nhanes_update$seqn %in% conflict_dead)
nhanes <- nhanes_old
nhanes$UCOD_LEADING <- nhanes_update$ucod_leading
nhanes$yrsfuExam <- nhanes_update$yrsfuExam
nhanes$yrsfuHome <- nhanes_update$yrsfuHome
nhanes$dead <- nhanes_update$mortstat
nhanes$mrtHrt <- as.integer(nhanes$UCOD_LEADING==1) # 1 codes for Heart
nhanes$mrtNeo <- as.integer(nhanes$UCOD_LEADING==2) # 2 codes for Neoplasm
nhanes$mrtInj <- as.integer(nhanes$UCOD_LEADING==4) # 4 codes for Injury
nhanes$mrtOtherCVD <- as.integer(nhanes$UCOD_LEADING==5) # 5 codes for other CVD
# Change NAs to 0
nhanes$mrtHrt[is.na(nhanes$mrtHrt)] <- 0
nhanes$mrtNeo[is.na(nhanes$mrtNeo)] <- 0
nhanes$mrtInj[is.na(nhanes$mrtInj)] <- 0
nhanes$mrtOtherCVD[is.na(nhanes$mrtOtherCVD)] <- 0
nhanes$eventhrt <- with(nhanes, mrtOtherCVD+mrtHrt)
nhanes$eventother <- with(nhanes, dead-eventhrt)
nhanes$yrsfu=pmin(nhanes_update$yrsfuHome,nhanes$yrsfuExam, na.rm = TRUE)
# Note: The most recent examination (home or clinic) is a left truncation time,
# so follow-up is the minimum. For some the clinic is missing.
# We call the follow-up the home follow-up time, but it doesn't matter,
# since those individuals will be excluded from analysis.
whichsys=match(c('systolicA','systolicB','systolicC'),names(nhanes))
whichdias=match(c('diastolicA','diastolicB','diastolicC'),names(nhanes))
whichsyshome=match(c('systolicAhome','systolicBhome','systolicChome'),names(nhanes))
whichdiashome=match(c('diastolicAhome','diastolicBhome','diastolicChome'),names(nhanes))
whichBP=c(whichsys,whichdias)
whichBPhome=c(whichsyshome,whichdiashome)
allBP=c(whichBP,whichBPhome)
allsys=c(whichsys,whichsyshome)
alldias=c(whichdias,whichdiashome)
### Means and variances
sys=nhanes[,whichsys]
dias=nhanes[,whichdias]
sysH=nhanes[,whichsyshome]
diasH=nhanes[,whichdiashome]
nhanes$meandias=apply(dias,1,mean)
nhanes$meansys=apply(sys,1,mean)
nhanes$sysDel=apply(sysH,1,mean)-apply(sys,1,mean)
nhanes$diasDel=apply(diasH,1,mean)-apply(dias,1,mean)
nhanes$vardias=apply(dias,1,var)
nhanes$varsys=apply(sys,1,var)
nhanes$sddias=sqrt(nhanes$vardias)
nhanes$sdsys=sqrt(nhanes$varsys)
nhanes$precdias=1/(nhanes$vardias+1/3)
nhanes$precsys=1/(nhanes$varsys+1/3)
race <- with(nhanes, ifelse(white==1, 2, ifelse(black==1, 1,ifelse(mexican==1,3, 0))))
nhanes$race <- as.factor(race)
type=factor(1+race+3*(1-nhanes$female)*(race>0)+6*(race==0)) # other race all type 7
levels(type)=c('female, black','female, white','female, Mex','male, black','male, white','male, Mex','other')
nhanes$type=type
nhanesna=!(is.na(apply(nhanes[,allBP],1,prod)))
diaslow <- apply(nhanes[,alldias],1,min)<40
diashigh <- apply(nhanes[,alldias],1,max)>140
syslow <- apply(nhanes[,allsys],1,min)<60
syshigh <- apply(nhanes[,allsys],1,max)>250
nhanessysrange=(syslow | syshigh)
nhanesdiasrange=(diaslow | diashigh)
dias0 <- apply(nhanes[,alldias],1,min) == 0
sys0 <- apply(nhanes[,allsys],1,min) == 0
nhanesgood=(nhanesna&(nhanes$yrsfu>0)&nhanes$other==0)
n_original <- dim(nhanes)[1]
n_no_other <- sum(nhanes$other==0)
n_followup <- sum(nhanes$yrsfu>0 & nhanesna & nhanes$other==0)
n_no_followup <- sum(nhanes$yrsfu==0 & nhanesna & nhanes$other==0)
frs=read.csv('Data_raw/FRS.csv')
whichfrs <- '1998' # 'ATP' or '1998'
#Choose versions of FRS
if(whichfrs=='ATP'){nhanes$FRS=frs$ATP.FRS[match(nhanes$SEQN,frs$SEQN)]} else{nhanes$FRS=frs$X1998.FRS[match(nhanes$SEQN,frs$SEQN)]}
#################################################
#
# Add in observer data
#
#################################################
exm=read.csv('Data_raw/Examiners.csv')
nhanes$Exam=exm$PEPTECH[match(nhanes$SEQN,exm$SEQN)]
nhanes$Exam[nhanes$Exam>4000]=0 ## Combine the 88888 examiner with 0
nhanes$Exam[is.na(nhanes$Exam)]=0
#nhanesA=subset(nhanesA,Exam>0)
examname=sort(unique(nhanes$Exam))
examlist=lapply(examname,function(en) subset(nhanes,Exam==en)$nunq.dias)
examlists=lapply(examname,function(en) subset(nhanes,Exam==en)$nunq.sys)
nhanesA=nhanes[nhanesgood & !nhanessysrange&!nhanesdiasrange, ]
nhanesA$type=factor(nhanesA$type,exclude=7)
N=dim(nhanesA)[1]
k=3
BP_type_names <- c('Systolic','Diastolic')
BP_place_names <- c('Home','Clinic')
whichsys=match(c('systolicA','systolicB','systolicC'),names(nhanesA))
whichdias=match(c('diastolicA','diastolicB','diastolicC'),names(nhanesA))
whichsyshome=match(c('systolicAhome','systolicBhome','systolicChome'),names(nhanesA))
whichdiashome=match(c('diastolicAhome','diastolicBhome','diastolicChome'),names(nhanesA))
whichBP=c(whichsys,whichdias)
whichBPhome=c(whichsyshome,whichdiashome)
# Make BP measures into array
sys=data.matrix(nhanesA[,whichsys])
dias=data.matrix(nhanesA[,whichdias])
sysH=data.matrix(nhanesA[,whichsyshome])
diasH=data.matrix(nhanesA[,whichdiashome])
allBP <- list(Systolic = list(Home = sysH, Clinic = sys), Diastolic = list(Home = diasH, Clinic = dias))
L=length(sys)
gamma_dimnames <- list( c('alpha','theta','beta') , c('Clinic' , 'Home'))
norm_dimnames <- c('m_M','m_Delta', 'sigma2_M', 'sigma2_Delta')
demog.data <- data.frame(Ethnicity = droplevels(nhanesA$race) %>% fct_recode(Black = '1',White = '2',Mexican = '3'),
Sex = factor(nhanesA$female) %>% fct_recode(Male = '0', Female = '1') )
# Make a table of means for systolic home BP by ethnicity and sex
bp_table <- list()
bp.data <- data.frame()
which_diff <- function(x) { # for a vector with 3 entries, with two the same, return the one that is different
if (x[1] == x[2]) {return(3)} else {
if (x[1] == x[3]) {return(2)} else {return(1)}
}
}
for (BPtype in BP_type_names){
for (BPplace in BP_place_names){
bp.data %<>% rbind(cbind(demog.data, BPtype = factor(BPtype, levels = BP_type_names) , BPplace = factor(BPplace), Mean = apply(allBP[[BPtype]][[BPplace]],1,mean), SD = apply(allBP[[BPtype]][[BPplace]],1,sd),
Number = apply(allBP[[BPtype]][[BPplace]],1,function(x) length(unique(x))),
Which_Diff = apply(allBP[[BPtype]][[BPplace]],1,which_diff)))
}
}
mean_sd_summary <- mean_sd_summary <- bp.data %>%
group_by(BPplace,BPtype, Sex, Ethnicity) %>%
summarise(
Mean_of_Mean = mean(Mean, na.rm = TRUE),
Mean_of_SD = mean(SD, na.rm = TRUE)
)
bp.data.cor <- bp.data %>%
group_by(BPtype, BPplace) %>%
summarize(correlation = cor(Mean, SD, use = "complete.obs"))
# Make a table of last digit fractions for bp by type and place
bp_last_digit <- matrix(0,nrow=0,ncol=5)
for (BPtype in BP_type_names){
for (BPplace in BP_place_names){
digit_table <- unname(table(allBP[[BPtype]][[BPplace]]%%10))
digits=digit_table/sum(digit_table)
bp_last_digit %<>% rbind(digits)
}
}
bp_last_digit %<>% as_tibble
names(bp_last_digit) <- 2*(0:4)
bp_last_digit$Place <- rep(BP_place_names,each=2)
bp_last_digit$Type <- rep(BP_type_names,2)
bp_last_digit %<>% select(Place,Type,everything())
# Calculate expected number of all with same last digit given 3 observations of sd = S
prob_repeated3 <- function(S, intervalwidth=2){
# Assume overall mean is uniform
# so average over the interval [0,10]
# then probability of same is sum_j (Phi(10j+intervalwidth-x)-Phi(10j-x))^3
sum(sapply(seq(-1,1),function(j)
.1*integrate(function(x) #(1-x/intervalwidth)*dnorm(x,sd=S)*(pnorm(x,sd=S)-.5),0,intervalwidth)$value
(pnorm(10*j+intervalwidth-x,sd=S)-pnorm(10*j-x,sd=S))^3,-5,5)$value) )
}
# Calculate expected number of all with same last digit given 3 observations of sd = S
prob_repeated3 <- function(S, intervalwidth=2){
# Assume overall mean is uniform
# so average over the interval [0,10]
# then probability of same is sum_j (Phi(10j+intervalwidth-x)-Phi(10j-x))^3
sum(sapply(seq(-1,1),function(j)
.1*integrate(function(x) #(1-x/intervalwidth)*dnorm(x,sd=S)*(pnorm(x,sd=S)-.5),0,intervalwidth)$value
(pnorm(10*j+intervalwidth-x,sd=S)-pnorm(10*j-x,sd=S))^3,-5,5)$value) )
}
prob_repeated2 <- function(S, intervalwidth=2){
# Assume overall mean is uniform
# so average over the interval [0,10]
# then probability of same is sum_j 3*(Phi(10j+intervalwidth-x)-Phi(10j-x))^2 - 2*prob_repeated3
sum(sapply(seq(-1,1),function(j)
.1*integrate(function(x) #(1-x/intervalwidth)*dnorm(x,sd=S)*(pnorm(x,sd=S)-.5),0,intervalwidth)$value
3*(pnorm(10*j+intervalwidth-x,sd=S)-pnorm(10*j-x,sd=S))^2,-5,5)$value) ) + 2*prob_repeated3(S,intervalwidth)
}
sd_summary <- bp.data %>%
group_by(BPplace,BPtype) %>%
summarise(
Mean_of_SD = mean(SD, na.rm = TRUE),
# fraction with all three measurements equal
Fraction3 = mean(Number==1, na.rm = TRUE),
Fraction2 = mean(Number==2, na.rm = TRUE)
)
# Calculate probability of three measurements being equal given unequal interval lengths
prob_repeated3_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated(S,intervalwidth)))
}
prob_repeated2_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated2(S,intervalwidth)))
}
sd_summary$Prob_repeated3 <- sapply(sd_summary$Mean_of_SD,prob_repeated)
# Calculate expected number of all with same last digit given 3 observations of sd = S
prob_repeated3 <- function(S, intervalwidth=2){
# Assume overall mean is uniform
# so average over the interval [0,10]
# then probability of same is sum_j (Phi(10j+intervalwidth-x)-Phi(10j-x))^3
sum(sapply(seq(-1,1),function(j)
.1*integrate(function(x) #(1-x/intervalwidth)*dnorm(x,sd=S)*(pnorm(x,sd=S)-.5),0,intervalwidth)$value
(pnorm(10*j+intervalwidth-x,sd=S)-pnorm(10*j-x,sd=S))^3,-5,5)$value) )
}
prob_repeated2 <- function(S, intervalwidth=2){
# Assume overall mean is uniform
# so average over the interval [0,10]
# then probability of same is sum_j 3*(Phi(10j+intervalwidth-x)-Phi(10j-x))^2 - 2*prob_repeated3
sum(sapply(seq(-1,1),function(j)
.1*integrate(function(x) #(1-x/intervalwidth)*dnorm(x,sd=S)*(pnorm(x,sd=S)-.5),0,intervalwidth)$value
3*(pnorm(10*j+intervalwidth-x,sd=S)-pnorm(10*j-x,sd=S))^2,-5,5)$value) ) + 2*prob_repeated3(S,intervalwidth)
}
sd_summary <- bp.data %>%
group_by(BPplace,BPtype) %>%
summarise(
Mean_of_SD = mean(SD, na.rm = TRUE),
# fraction with all three measurements equal
Fraction3 = mean(Number==1, na.rm = TRUE),
Fraction2 = mean(Number==2, na.rm = TRUE)
)
# Calculate probability of three measurements being equal given unequal interval lengths
prob_repeated3_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated(S,intervalwidth)))
}
prob_repeated2_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated2(S,intervalwidth)))
}
sd_summary$Prob_repeated3 <- sapply(sd_summary$Mean_of_SD,prob_repeated3)
sd_summary$Prob_repeated2 <- sapply(sd_summary$Mean_of_SD,prob_repeated2)
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Fraction repeated", "Prob repeated"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Fraction all repeated", "Prob all repeated", "Fraction 2 repeated", "Prob 2 repeated"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
head(bp.data)
head(bp.data,15)
mean(bp.data$Number==2)
sd_summary
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Frac all rep", "Frac 2 rep", "Prob all rep", "Prob 2 rep"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
prob_repeated3(2.8,2)*5
sd_summary$Prob_repeated3 <- sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j],unlist(bp_last_digit[j,3:7])]))
sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j],unlist(bp_last_digit[j,3:7])))
# Calculate probability of three measurements being equal given unequal interval lengths
prob_repeated3_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated3(S,intervalwidth)))
}
prob_repeated2_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated2(S,intervalwidth)))
}
sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j],unlist(bp_last_digit[j,3:7])))
j=1
sd_summary$Mean_of_SD[j]
unlist(bp_last_digit[j,3:7])
sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j],10*unlist(bp_last_digit[j,3:7])))
sapply(1:4, function(j) prob_repeated2_intervals(sd_summary$Mean_of_SD[j],10*unlist(bp_last_digit[j,3:7])))
sd_summary$Prob_repeated3 <- sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j],10*unlist(bp_last_digit[j,3:7])))
sd_summary$Prob_repeated2 <- sapply(1:4, function(j) prob_repeated2_intervals(sd_summary$Mean_of_SD[j],10*unlist(bp_last_digit[j,3:7])))
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Frac all rep", "Frac 2 rep", "Prob all rep", "Prob 2 rep"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
prob_repeated2 <- function(S, intervalwidth=2){
# Assume overall mean is uniform
# so average over the interval [0,10]
# then probability of same is sum_j 3*(Phi(10j+intervalwidth-x)-Phi(10j-x))^2 - 2*prob_repeated3
sum(sapply(seq(-1,1),function(j)
.1*integrate(function(x)
3*(pnorm(10*j+intervalwidth-x,sd=S)-pnorm(10*j-x,sd=S))^2,-5,5)$value) ) - 2*prob_repeated3(S,intervalwidth)
}
sd_summary <- bp.data %>%
group_by(BPplace,BPtype) %>%
summarise(
Mean_of_SD = mean(SD, na.rm = TRUE),
# fraction with all three measurements equal
Fraction3 = mean(Number==1, na.rm = TRUE),
Fraction2 = mean(Number==2, na.rm = TRUE)
)
# Calculate probability of three measurements being equal given unequal interval lengths
prob_repeated3_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated3(S,intervalwidth)))
}
prob_repeated2_intervals <- function(S,intervals){
sum(sapply(intervals, function(intervalwidth) prob_repeated2(S,intervalwidth)))
}
sd_summary$Prob_repeated3 <- sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j],10*unlist(bp_last_digit[j,3:7])))
sd_summary$Prob_repeated2 <- sapply(1:4, function(j) prob_repeated2_intervals(sd_summary$Mean_of_SD[j],10*unlist(bp_last_digit[j,3:7])))
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Frac all rep", "Frac 2 rep", "Prob all rep", "Prob 2 rep"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
sd_summary$Prob_repeated3 <- sapply(1:4, function(j) prob_repeated3_intervals(sd_summary$Mean_of_SD[j]+1/3,10*unlist(bp_last_digit[j,3:7])))
sd_summary$Prob_repeated2 <- sapply(1:4, function(j) prob_repeated2_intervals(sd_summary$Mean_of_SD[j]+1/3,10*unlist(bp_last_digit[j,3:7])))
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Frac all rep", "Frac 2 rep", "Prob all rep", "Prob 2 rep"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
sd_summary$Prob_repeated3 <- sapply(1:4, function(j) prob_repeated3_intervals(sqrt(sd_summary$Mean_of_SD[j]^ 2+1/3),10*unlist(bp_last_digit[j,3:7])))
sd_summary$Prob_repeated2 <- sapply(1:4, function(j) prob_repeated2_intervals(sqrt(sd_summary$Mean_of_SD[j]^2+1/3),10*unlist(bp_last_digit[j,3:7])))
#Print table of sd_summary
cat(kable(sd_summary,format="latex", escape = F,booktabs = T, digits=3,
linesep = rep(c(rep("",5),"\\addlinespace"),4),
col.names=c("Place","Sys/Dias","Mean of SD","Frac all rep", "Frac 2 rep", "Prob all rep", "Prob 2 rep"),caption = 'Summary data for repeated measures') %>% kable_styling(latex_options = "hold_position") )
sqrt(2.739^2+1/3)
5*prob_repeated2(2.8)
5*prob_repeated3(2.8)
sqrt(3.775^2+1/3)
5*prob_repeated3(3.82)
sqrt(3.082^2+1/3)
5*prob_repeated3(3.136)