-
Notifications
You must be signed in to change notification settings - Fork 0
/
sesplots.R
55 lines (45 loc) · 1.28 KB
/
sesplots.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
library(ggplot2)
SES_bymedicaid <- function(stanmod, indata, stat = NULL, maxn=2000)
{
if(is.character(indata)) {
indata <- readRDS(indata)
}
sesvals <- extract(stanmod, pars='SES')$SES
if(!is.null(stat)) {
sesvals <- matrix(apply(sesvals, 2, stat), nrow=1)
}
pltdata <-
dplyr::bind_rows(
lapply(c(0,1),
function(dstat) {
vals <- sesvals[ , indata$D == dstat]
d <- tibble::tibble(SES=as.vector(vals), medicaid=as.integer(dstat))
if(nrow(d) > maxn) {
d <- dplyr::slice_sample(d, n=maxn)
}
d
}))
ggplot(pltdata, aes(x=medicaid, y=SES, group=medicaid)) +
geom_boxplot() +
theme_bw()
}
SES_bygroundtruth <- function(stanmod, indata, stat=mean, maxn=2000)
{
if(is.character(indata)) {
indata <- readRDS(indata)
}
sesvals <- extract(stanmod, pars='SES')$SES
if(!is.null(stat)) {
sesvals <- apply(sesvals, 2, stat)
}
else {
sesvals <- apply(sesvals, 2, sample, size=1)
}
pltdata <- tibble::tibble(SES_model=sesvals, SES_data = indata$us)
if(nrow(pltdata) > maxn) {
pltdata <- dplyr::slice_sample(pltdata, n=maxn)
}
ggplot(pltdata, aes(x=SES_data, y=SES_model)) +
geom_point() +
theme_bw()
}