]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/summarize_estimator.R
cleaning up + implementing robustness checks
[ml_measurement_error_public.git] / simulations / summarize_estimator.R
1
2 summarize.estimator <- function(df, suffix='naive', coefname='x'){
3
4     part <- df[,c('N',
5                   'm',
6                   'Bxy',
7                   paste0('B',coefname,'y.est.',suffix),
8                   paste0('B',coefname,'y.ci.lower.',suffix),
9                   paste0('B',coefname,'y.ci.upper.',suffix),
10                   'y_explained_variance',
11                   'Bzx',
12                   'Bzy',
13                   'accuracy_imbalance_difference'
14                   ),
15                with=FALSE]
16
17
18     true.in.ci <- as.integer((part$Bxy >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (part$Bxy <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]]))
19     zero.in.ci <- as.integer(0 >= part[[paste0('B',coefname,'y.ci.lower.',suffix)]]) & (0 <= part[[paste0('B',coefname,'y.ci.upper.',suffix)]])
20     bias <- part[[paste0('B',coefname,'y')]] - part[[paste0('B',coefname,'y.est.',suffix)]]
21     sign.correct <- as.integer(sign(part$Bxy) == sign(part[[paste0('B',coefname,'y.est.',suffix)]]))
22
23     part <- part[,':='(true.in.ci = true.in.ci,
24                        zero.in.ci = zero.in.ci,
25                        bias=bias,
26                        sign.correct =sign.correct)]
27
28     part.plot <- part[, .(p.true.in.ci = mean(true.in.ci),
29                           mean.bias = mean(bias),
30                           mean.est = mean(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
31                           var.est = var(.SD[[paste0('B',coefname,'y.est.',suffix)]]),
32                           est.upper.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.975,na.rm=T),
33                           est.lower.95 = quantile(.SD[[paste0('B',coefname,'y.est.',suffix)]],0.025,na.rm=T),
34                           mean.ci.upper = mean(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],na.rm=T),
35                           mean.ci.lower = mean(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]],na.rm=T),
36                           ci.upper.975 = quantile(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],0.975,na.rm=T),
37                           ci.upper.025 = quantile(.SD[[paste0('B',coefname,'y.ci.upper.',suffix)]],0.025,na.rm=T),
38                           ci.lower.975 = quantile(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]],0.975,na.rm=T),
39                           ci.lower.025 = quantile(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]],0.025,na.rm=T),
40                           N.ci.is.NA = sum(is.na(.SD[[paste0('B',coefname,'y.ci.lower.',suffix)]])),
41                           N.sims = .N,
42                           p.sign.correct = mean(as.integer(sign.correct & (! zero.in.ci))),
43                           variable=coefname,
44                           method=suffix
45                           ),
46                       by=c("N","m",'y_explained_variance','Bzx', 'Bzy', 'accuracy_imbalance_difference')
47                       ]
48     
49     return(part.plot)
50 }

Community Data Science Collective || Want to submit a patch?