1- # ' PPC calibration
1+ # x ' PPC calibration
22# '
33# ' Assess the calibration of the predictive distributions `yrep` in relation to
44# ' the data `y'.
1414# '
1515# ' @section Plot Descriptions:
1616# ' \describe{
17+ # ' \item{`ppc_calibration()`,`ppc_calibration_grouped()`}{
18+ # '
19+ # ' },
1720# ' \item{`ppc_calibration_overlay()`,`ppc_calibration_overlay_grouped()`}{
1821# '
22+ # ' },
23+ # ' \item{`ppc_loo_calibration()`,`ppc_loo_calibration_grouped()`}{
24+ # '
1925# ' }
2026# ' }
2127# '
2228NULL
2329
30+
2431# ' @rdname PPC-calibration
2532# ' @export
26- .ppc_calibration_data <- function (y , prep , group = NULL ) {
27- y <- validate_y(y )
28- n_obs <- length(y )
29- prep <- validate_predictions(prep , n_obs )
30- if (any(prep > 1 | prep < 0 )) {
31- stop(" Values of ´prep´ should be predictive probabilities between 0 and 1." )
32- }
33- if (! is.null(group )) {
34- group <- validate_group(group , n_obs )
35- } else {
36- group <- rep(1 , n_obs * nrow(prep ))
37- }
33+ ppc_calibration_overlay <- function (
34+ y , prep , ... , linewidth = 0.25 , alpha = 0.5 ) {
35+ check_ignored_arguments(... )
36+ data <- .ppc_calibration_data(y , prep )
37+ ggplot(data ) +
38+ geom_abline(color = " black" , linetype = 2 ) +
39+ geom_line(
40+ aes(value , cep , group = rep_id , color = " yrep" ),
41+ linewidth = linewidth , alpha = alpha
42+ ) +
43+ scale_color_ppc() +
44+ bayesplot_theme_get() +
45+ legend_none() +
46+ coord_equal(xlim = c(0 , 1 ), ylim = c(0 , 1 ), expand = FALSE ) +
47+ xlab(" Predicted probability" ) +
48+ ylab(" Conditional event probability" ) +
49+ NULL
50+ }
3851
39- if (requireNamespace(" monotone" , quietly = TRUE )) {
40- monotone <- monotone :: monotone
41- } else {
42- monotone <- function (y ) {
43- stats :: isoreg(y )$ yf
44- }
45- }
46- .ppd_data(prep , group = group ) %> %
47- group_by(group , rep_id ) %> %
48- mutate(
49- ord = order(value ),
50- value = value [ord ],
51- cep = monotone(y [ord ])
52- ) | >
53- ungroup()
52+ # ' @rdname PPC-calibration
53+ # ' @export
54+ ppc_calibration_overlay_grouped <- function (
55+ y , prep , group , ... , linewidth = 0.25 , alpha = 0.7 ) {
56+ check_ignored_arguments(... )
57+ data <- .ppc_calibration_data(y , prep , group )
58+ ggplot(data ) +
59+ geom_abline(color = " black" , linetype = 2 ) +
60+ geom_line(aes(value , cep , group = rep_id , color = " yrep" ),
61+ linewidth = linewidth , alpha = alpha
62+ ) +
63+ facet_wrap(vars(group )) +
64+ scale_color_ppc() +
65+ bayesplot_theme_get() +
66+ legend_none() +
67+ coord_equal(xlim = c(0 , 1 ), ylim = c(0 , 1 ), expand = FALSE ) +
68+ xlab(" Predicted probability" ) +
69+ ylab(" Conditional event probability" ) +
70+ NULL
5471}
5572
5673# ' @rdname PPC-calibration
5774# ' @export
58- ppc_calibration_overlay <- function (
59- y , prep , ... , linewidth = 0.25 , alpha = 0.7 ) {
75+ ppc_calibration <- function (
76+ y , prep , prob = .95 , show_mean = TRUE , ... , linewidth = 0.25 , alpha = 0.7 ) {
77+ check_ignored_arguments(... )
78+ data <- .ppc_calibration_data(y , prep ) %> %
79+ group_by(y_id ) %> %
80+ summarise(
81+ value = median(value ),
82+ lb = quantile(cep , .5 - .5 * prob ),
83+ ub = quantile(cep , .5 + .5 * prob ),
84+ cep = median(cep )
85+ )
86+
87+ ggplot(data ) +
88+ aes(value , cep ) +
89+ geom_abline(color = " black" , linetype = 2 ) +
90+ geom_ribbon(aes(ymin = lb , ymax = ub , fill = " yrep" ), alpha = alpha ) +
91+ geom_line(
92+ aes(color = " y" ),
93+ linewidth = linewidth
94+ ) +
95+ scale_color_ppc() +
96+ scale_fill_ppc() +
97+ bayesplot_theme_get() +
98+ # legend_none() +
99+ coord_equal(xlim = c(0 , 1 ), ylim = c(0 , 1 ), expand = FALSE ) +
100+ xlab(" Predicted probability" ) +
101+ ylab(" Conditional event probability" ) +
102+ NULL
103+ }
104+
105+ # ' @rdname PPC-calibration
106+ # ' @export
107+ ppc_calibration_grouped <- function (
108+ y , prep , show_mean , ... , linewidth = 0.25 , alpha = 0.7 ) {
109+ check_ignored_arguments(... )
110+ data <- .ppc_calibration_data(y , prep , group )
111+ ggplot(data ) +
112+ geom_abline(color = " black" , linetype = 2 ) +
113+ geom_line(aes(value , cep , group = rep_id , color = " yrep" ),
114+ linewidth = linewidth , alpha = alpha
115+ ) +
116+ facet_wrap(vars(group )) +
117+ scale_color_ppc() +
118+ bayesplot_theme_get() +
119+ legend_none() +
120+ coord_equal(xlim = c(0 , 1 ), ylim = c(0 , 1 ), expand = FALSE ) +
121+ xlab(" Predicted probability" ) +
122+ ylab(" Conditional event probability" ) +
123+ NULL
124+ }
125+
126+ # ' @rdname PPC-calibration
127+ # ' @export
128+ ppc_loo_calibration <- function (
129+ y , prep , lw , ... , linewidth = 0.25 , alpha = 0.7 ) {
60130 check_ignored_arguments(... )
61131 data <- .ppc_calibration_data(y , prep )
62132 ggplot(data ) +
@@ -76,8 +146,8 @@ ppc_calibration_overlay <- function(
76146
77147# ' @rdname PPC-calibration
78148# ' @export
79- ppc_calibration_overlay_grouped <- function (
80- y , prep , group , ... , linewidth = 0.25 , alpha = 0.7 ) {
149+ ppc_loo_calibration_grouped <- function (
150+ y , prep , lw , ... , linewidth = 0.25 , alpha = 0.7 ) {
81151 check_ignored_arguments(... )
82152 data <- .ppc_calibration_data(y , prep , group )
83153 ggplot(data ) +
@@ -94,3 +164,39 @@ ppc_calibration_overlay_grouped <- function(
94164 ylab(" Conditional event probability" ) +
95165 NULL
96166}
167+
168+ .ppc_calibration_data <- function (y , prep , group = NULL ) {
169+ y <- validate_y(y )
170+ n_obs <- length(y )
171+ prep <- validate_predictions(prep , n_obs )
172+ if (any(prep > 1 | prep < 0 )) {
173+ stop(" Values of ´prep´ should be predictive probabilities between 0 and 1." )
174+ }
175+ if (! is.null(group )) {
176+ group <- validate_group(group , n_obs )
177+ } else {
178+ group <- rep(1 , n_obs * nrow(prep ))
179+ }
180+
181+ if (requireNamespace(" monotone" , quietly = TRUE )) {
182+ monotone <- monotone :: monotone
183+ } else {
184+ monotone <- function (y ) {
185+ stats :: isoreg(y )$ yf
186+ }
187+ }
188+ .ppd_data(prep , group = group ) %> %
189+ group_by(group , rep_id ) %> %
190+ mutate(
191+ ord = order(value ),
192+ value = value [ord ],
193+ cep = monotone(y [ord ])
194+ ) | >
195+ ungroup()
196+ }
197+
198+ .loo_resample_data <- function (prep , lw , psis_object ) {
199+ lw <- .get_lw(lw , psis_object )
200+ stopifnot(identical(dim(prep ), dim(lw )))
201+ # Work in progress here...
202+ }
0 commit comments