-
Notifications
You must be signed in to change notification settings - Fork 1
/
survey-correlated-ratios.R
71 lines (58 loc) · 1.99 KB
/
survey-correlated-ratios.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
library(survey)
library(srvyr)
library(magrittr)
# also used: stringr
# experimenting with nonlinear contrasts to develop method for computing
# rate ratios and confidence intervals comparing a subregion (e.g. county)
# to a parent region (e.g. state)
# using data from survey package
data(api)
dclus1 <-
as_survey_design(apiclus1, id = dnum, weights = pw, fpc = fpc)
# compute survey totals for
# A = indicator Yes in subregion
# B = indicator NO in subregion
# C = indicator Yes in parent region
# D = indicator No in parent region
# prevalence in subregion = A / (A + B)
# prevalence in parent region = (A + C) / (A + B + C + D)
# Rate ratio of subregion to parent region is
# (A / (A + B)) / ((A + C) / (A + B + C + D))
# example is ratio of Met Comparable Improvement Target (comp.imp) of
# year-round schools (yr.rnd) to that of all schools
# get estimated totals for categories described above
ab <- svytotal(~ interaction(yr.rnd, comp.imp), dclus1) %>%
# simplify level names
setNames(stringr::str_remove(names(.), "(.*?)\\)"))
# rate ratio
# A B C D
# "Yes.Yes" "No.Yes" "Yes.No" "No.No"
ratio_ab <- ab %>%
svycontrast(
quote((Yes.Yes / (No.Yes + Yes.Yes))
/ ((Yes.No + Yes.Yes) / (No.No + Yes.No + No.Yes + Yes.Yes))
))
ratio_ab
# confidence interval
ab %>%
svycontrast(
quote(log((Yes.Yes / (No.Yes + Yes.Yes))
/ ((Yes.No + Yes.Yes) / (No.No + Yes.No + No.Yes + Yes.Yes))
))) %>%
exp() %>%
confint()
# num <- "Yes.Yes"
# den <- "No.Yes"
# svycontrast(ab, quote(Yes.Yes / No.Yes))
# indicator.subregion
# 1 2 3 4
# "No.No" "Yes.No" "No.Yes" "Yes.Yes"
# indicator in subregion, about 0.0602
# (coef(ab)[4] / sum(coef(ab)[c(3, 4)]) ) /
# # indicator in parent region, about 0.049
# (sum(coef(ab)[c(2, 4)]) / sum(coef(ab)))
# # ratio about 1.22
#
#
# ab %>%
# svycontrast(c(-1, -1, -1, 1, 1, 1))