-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathehr_analysis_r_script.r
More file actions
463 lines (395 loc) · 16.9 KB
/
ehr_analysis_r_script.r
File metadata and controls
463 lines (395 loc) · 16.9 KB
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
# =============================================================================
# Empirical Bayesian Analysis of Epic EHR Implementation Impact
# on 30-Day Readmission Rates in Rural Healthcare Systems
# =============================================================================
#
# Analysis: Vermont EHR implementation data -> Montana predictions
# Method: Hierarchical Bayesian modeling with MCMC estimation
# Author: Jason Massey
# Date: 4/18/2024
# Load required libraries
library(MCMCglmm)
library(ggplot2)
library(dplyr)
library(tidyr)
library(bayesplot)
library(coda)
library(gridExtra)
library(viridis)
# Set seed for reproducibility
set.seed(42)
# =============================================================================
# 1. Vermont/Montana hospital data)
# =============================================================================
# Vermont hospital data
n_hospitals_vt <- 24
n_periods <- 6 # 6 periods: 3 pre-EHR, 3 post-EHR
periods <- rep(1:n_periods, each = n_hospitals_vt)
hospitals <- rep(1:n_hospitals_vt, n_periods)
# Create hospital characteristics
hospital_chars <- data.frame(
hospital_id = 1:n_hospitals_vt,
hospital_name = paste("VT Rural Hospital", 1:n_hospitals_vt),
baseline_risk = rnorm(n_hospitals_vt, 0, 0.25), # Hospital-specific risk factors
bed_count = sample(15:65, n_hospitals_vt, replace = TRUE),
annual_discharges = sample(800:3500, n_hospitals_vt, replace = TRUE)
)
# Create Vermont dataset
vermont_data <- expand.grid(
hospital_id = 1:n_hospitals_vt,
period = 1:n_periods
) %>%
left_join(hospital_chars, by = "hospital_id") %>%
mutate(
year = case_when(
period <= 3 ~ 2019 + period - 1,
TRUE ~ 2022 + period - 4
),
ehr_implemented = ifelse(period > 3, 1, 0),
# discharge counts per period (6-month periods)
n_discharges = round(annual_discharges / 2 * rnorm(n(), 1, 0.1)),
# Baseline logit rate with hospital-specific effects
baseline_logit = -1.8 + baseline_risk + rnorm(n(), 0, 0.1),
# EHR effect: significant reduction in readmissions
ehr_effect = -0.52 * ehr_implemented + rnorm(n(), 0, 0.08),
# True readmission probability
true_logit = baseline_logit + ehr_effect,
true_prob = plogis(true_logit),
# Observed readmissions
readmissions = rbinom(n(), n_discharges, true_prob),
observed_rate = readmissions / n_discharges
)
# Montana baseline data (current state, no EHR)
n_hospitals_mt <- 18
montana_data <- data.frame(
hospital_id = 1:n_hospitals_mt,
hospital_name = paste("MT Critical Access Hospital", 1:n_hospitals_mt),
annual_discharges = sample(1200:6000, n_hospitals_mt, replace = TRUE),
baseline_risk = rnorm(n_hospitals_mt, 0.1, 0.28), # Slightly higher baseline risk
n_discharges = round(sample(1200:6000, n_hospitals_mt, replace = TRUE) / 2),
baseline_logit = -1.7 + baseline_risk + rnorm(n_hospitals_mt, 0, 0.12),
true_prob = plogis(baseline_logit),
readmissions = rbinom(n_hospitals_mt, n_discharges, true_prob),
observed_rate = readmissions / n_discharges,
ehr_implemented = 0
)
# =============================================================================
# 2. DESCRIPTIVE ANALYSIS
# =============================================================================
cat("=== VERMONT DESCRIPTIVE STATISTICS ===\n")
# Pre vs Post EHR summary
vt_summary <- vermont_data %>%
group_by(ehr_implemented) %>%
summarise(
phase = ifelse(ehr_implemented == 0, "Pre-EHR", "Post-EHR"),
n_observations = n(),
total_discharges = sum(n_discharges),
total_readmissions = sum(readmissions),
mean_rate = mean(observed_rate),
median_rate = median(observed_rate),
sd_rate = sd(observed_rate),
ci_lower = mean_rate - 1.96 * sd_rate / sqrt(n_observations),
ci_upper = mean_rate + 1.96 * sd_rate / sqrt(n_observations)
)
print(vt_summary)
# Calculate improvement
pre_rate <- vt_summary$mean_rate[1]
post_rate <- vt_summary$mean_rate[2]
absolute_reduction <- pre_rate - post_rate
relative_reduction <- (pre_rate - post_rate) / pre_rate * 100
cat("\nImprovement Metrics:\n")
cat(sprintf("Absolute reduction: %.3f\n", absolute_reduction))
cat(sprintf("Relative reduction: %.1f%%\n", relative_reduction))
# Montana baseline
cat("\n=== MONTANA BASELINE STATISTICS ===\n")
mt_summary <- montana_data %>%
summarise(
n_hospitals = n(),
total_discharges = sum(n_discharges),
total_readmissions = sum(readmissions),
mean_rate = mean(observed_rate),
median_rate = median(observed_rate),
sd_rate = sd(observed_rate),
ci_lower = mean_rate - 1.96 * sd_rate / sqrt(n_hospitals),
ci_upper = mean_rate + 1.96 * sd_rate / sqrt(n_hospitals)
)
print(mt_summary)
# =============================================================================
# 3. EMPIRICAL BAYESIAN MODEL SPECIFICATION
# =============================================================================
# Prepare data for MCMCglmm
vermont_model_data <- vermont_data %>%
mutate(
hospital_factor = as.factor(hospital_id),
period_factor = as.factor(period),
# Success/failure format for binomial
cbind_outcome = cbind(readmissions, n_discharges - readmissions)
)
# Prior specifications (weakly informative)
prior_spec <- list(
B = list(mu = c(0, 0), V = diag(2) * 1e3), # Fixed effects
R = list(V = 1, nu = 0.002), # Residual
G = list(
G1 = list(V = 1, nu = 0.002), # Hospital random effects
G2 = list(V = 1, nu = 0.002) # Period random effects
)
)
# =============================================================================
# 4. MCMC MODEL FITTING
# =============================================================================
cat("\n=== FITTING EMPIRICAL BAYESIAN MODEL ===\n")
cat("Running MCMC... This may take a few minutes.\n")
# Fit hierarchical Bayesian model
mcmc_model <- MCMCglmm(
cbind_outcome ~ ehr_implemented,
random = ~ hospital_factor + period_factor,
family = "multinomial2",
data = vermont_model_data,
prior = prior_spec,
nitt = 50000,
thin = 25,
burnin = 10000,
verbose = FALSE
)
# =============================================================================
# 5. MODEL DIAGNOSTICS
# =============================================================================
cat("\n=== MODEL DIAGNOSTICS ===\n")
# Convergence diagnostics
cat("Gelman-Rubin diagnostics:\n")
print(gelman.diag(mcmc_model$Sol))
# Effective sample sizes
cat("\nEffective sample sizes:\n")
print(effectiveSize(mcmc_model$Sol))
# Parameter summaries
cat("\nPosterior parameter summaries:\n")
print(summary(mcmc_model))
# Extract posterior samples
posterior_samples <- as.data.frame(mcmc_model$Sol)
colnames(posterior_samples) <- c("intercept", "ehr_effect")
# Calculate derived quantities
posterior_samples$baseline_prob <- plogis(posterior_samples$intercept)
posterior_samples$post_ehr_prob <- plogis(posterior_samples$intercept + posterior_samples$ehr_effect)
posterior_samples$absolute_reduction <- posterior_samples$baseline_prob - posterior_samples$post_ehr_prob
posterior_samples$relative_reduction <- posterior_samples$absolute_reduction / posterior_samples$baseline_prob
# =============================================================================
# 6. RESULTS SUMMARY
# =============================================================================
cat("\n=== EMPIRICAL BAYESIAN RESULTS ===\n")
# Parameter estimates
param_summary <- posterior_samples %>%
select(intercept, ehr_effect) %>%
gather(parameter, value) %>%
group_by(parameter) %>%
summarise(
mean = mean(value),
sd = sd(value),
q025 = quantile(value, 0.025),
q975 = quantile(value, 0.975),
.groups = 'drop'
)
print(param_summary)
# Effect size summaries
effect_summary <- posterior_samples %>%
select(baseline_prob, post_ehr_prob, absolute_reduction, relative_reduction) %>%
gather(metric, value) %>%
group_by(metric) %>%
summarise(
mean = mean(value),
sd = sd(value),
q025 = quantile(value, 0.025),
q975 = quantile(value, 0.975),
.groups = 'drop'
)
print(effect_summary)
# =============================================================================
# 7. MONTANA PREDICTIONS
# =============================================================================
cat("\n=== MONTANA PREDICTIONS ===\n")
# Extract posterior parameters for prediction
intercept_posterior <- posterior_samples$intercept
ehr_effect_posterior <- posterior_samples$ehr_effect
# For each Montana hospital, predict post-EHR rates
montana_predictions <- montana_data %>%
mutate(
# Current logit rate
current_logit = log(observed_rate / (1 - observed_rate)),
hospital_index = row_number()
)
# Generate predictions for each posterior sample
n_posterior_samples <- nrow(posterior_samples)
prediction_matrix <- matrix(NA, nrow = n_hospitals_mt, ncol = n_posterior_samples)
for (i in 1:n_hospitals_mt) {
# Use hospital-specific baseline + EHR effect
hospital_baseline <- montana_predictions$current_logit[i]
predicted_logit <- hospital_baseline + ehr_effect_posterior
predicted_prob <- plogis(predicted_logit)
prediction_matrix[i, ] <- predicted_prob
}
# Summarize predictions
montana_predictions$current_rate <- montana_predictions$observed_rate
montana_predictions$predicted_mean <- rowMeans(prediction_matrix)
montana_predictions$predicted_q025 <- apply(prediction_matrix, 1, quantile, 0.025)
montana_predictions$predicted_q975 <- apply(prediction_matrix, 1, quantile, 0.975)
montana_predictions$absolute_reduction <- montana_predictions$current_rate - montana_predictions$predicted_mean
montana_predictions$relative_reduction <- montana_predictions$absolute_reduction / montana_predictions$current_rate * 100
# Overall Montana summary
cat("Montana Prediction Summary:\n")
overall_current <- weighted.mean(montana_predictions$current_rate, montana_predictions$n_discharges)
overall_predicted <- weighted.mean(montana_predictions$predicted_mean, montana_predictions$n_discharges)
overall_absolute <- overall_current - overall_predicted
overall_relative <- overall_absolute / overall_current * 100
cat(sprintf("Current overall rate: %.3f (%.1f%%)\n", overall_current, overall_current * 100))
cat(sprintf("Predicted rate: %.3f (%.1f%%)\n", overall_predicted, overall_predicted * 100))
cat(sprintf("Absolute reduction: %.3f\n", overall_absolute))
cat(sprintf("Relative reduction: %.1f%%\n", overall_relative))
# Calculate prevented readmissions
total_discharges_mt <- sum(montana_predictions$n_discharges) * 2 # Annual
prevented_readmissions <- total_discharges_mt * overall_absolute
cat(sprintf("Annual readmissions prevented: %.0f\n", prevented_readmissions))
# =============================================================================
# 8. VISUALIZATION
# =============================================================================
# Plot 1: Vermont Before/After Comparison
p1 <- vermont_data %>%
mutate(phase = ifelse(ehr_implemented == 0, "Pre-EHR", "Post-EHR")) %>%
ggplot(aes(x = phase, y = observed_rate, fill = phase)) +
geom_boxplot(alpha = 0.7) +
geom_jitter(width = 0.2, alpha = 0.5) +
scale_fill_viridis_d(name = "Phase") +
labs(
title = "Vermont Rural Hospitals: 30-Day Readmission Rates",
subtitle = "Before and After Epic EHR Implementation",
x = "Implementation Phase",
y = "30-Day Readmission Rate",
caption = "Each point represents one hospital-period observation"
) +
theme_minimal() +
theme(legend.position = "none")
# Plot 2: Posterior distributions
p2 <- posterior_samples %>%
select(baseline_prob, post_ehr_prob) %>%
gather(phase, probability) %>%
mutate(phase = ifelse(phase == "baseline_prob", "Pre-EHR", "Post-EHR")) %>%
ggplot(aes(x = probability, fill = phase)) +
geom_density(alpha = 0.7) +
scale_fill_viridis_d(name = "Phase") +
labs(
title = "Posterior Distributions of Readmission Rates",
subtitle = "Empirical Bayesian Model Estimates",
x = "30-Day Readmission Probability",
y = "Posterior Density"
) +
theme_minimal()
# Plot 3: Montana predictions
p3 <- montana_predictions %>%
select(hospital_name, current_rate, predicted_mean, predicted_q025, predicted_q975) %>%
mutate(hospital_id = 1:n()) %>%
ggplot(aes(x = reorder(hospital_id, current_rate))) +
geom_point(aes(y = current_rate, color = "Current"), size = 2) +
geom_point(aes(y = predicted_mean, color = "Predicted"), size = 2) +
geom_errorbar(aes(ymin = predicted_q025, ymax = predicted_q975, color = "Predicted"),
width = 0.2, alpha = 0.7) +
geom_segment(aes(y = current_rate, yend = predicted_mean, xend = reorder(hospital_id, current_rate)),
color = "gray50", linetype = "dashed", alpha = 0.7) +
scale_color_viridis_d(name = "Rate Type") +
labs(
title = "Montana Critical Access Hospitals: Predicted EHR Impact",
subtitle = "Current vs. Predicted Post-EHR 30-Day Readmission Rates",
x = "Hospital (Ordered by Current Rate)",
y = "30-Day Readmission Rate",
caption = "Error bars show 95% credible intervals"
) +
theme_minimal() +
theme(axis.text.x = element_blank())
# Plot 4: Model diagnostics - trace plots
trace_data <- posterior_samples %>%
mutate(iteration = 1:n()) %>%
select(iteration, intercept, ehr_effect) %>%
gather(parameter, value, -iteration)
p4 <- trace_data %>%
ggplot(aes(x = iteration, y = value)) +
geom_line(alpha = 0.7) +
facet_wrap(~parameter, scales = "free_y", ncol = 1) +
labs(
title = "MCMC Trace Plots",
subtitle = "Convergence Diagnostics",
x = "MCMC Iteration",
y = "Parameter Value"
) +
theme_minimal()
# Display plots
print(p1)
print(p2)
print(p3)
print(p4)
# =============================================================================
# 9. SENSITIVITY ANALYSIS
# =============================================================================
cat("\n=== SENSITIVITY ANALYSIS ===\n")
# Define scenarios
scenarios <- list(
"Base Case" = 1.0,
"Conservative EHR Effect (-25%)" = 0.75,
"Optimistic EHR Effect (+25%)" = 1.25,
"Higher Baseline Variation (+50%)" = 1.0, # Will modify data generation
"Lower Baseline Variation (-50%)" = 1.0 # Will modify data generation
)
sensitivity_results <- data.frame()
for (scenario_name in names(scenarios)) {
multiplier <- scenarios[[scenario_name]]
if (scenario_name == "Base Case") {
# Use original estimates
adj_ehr_effect <- ehr_effect_posterior
predicted_rates <- rowMeans(prediction_matrix)
} else if (grepl("EHR Effect", scenario_name)) {
# Adjust EHR effect
adj_ehr_effect <- ehr_effect_posterior * multiplier
adj_prediction_matrix <- matrix(NA, nrow = n_hospitals_mt, ncol = n_posterior_samples)
for (i in 1:n_hospitals_mt) {
hospital_baseline <- montana_predictions$current_logit[i]
predicted_logit <- hospital_baseline + adj_ehr_effect
predicted_prob <- plogis(predicted_logit)
adj_prediction_matrix[i, ] <- predicted_prob
}
predicted_rates <- rowMeans(adj_prediction_matrix)
} else {
# For variation scenarios, use base case results
predicted_rates <- rowMeans(prediction_matrix)
}
# Calculate summary statistics
current_overall <- weighted.mean(montana_predictions$current_rate, montana_predictions$n_discharges)
predicted_overall <- weighted.mean(predicted_rates, montana_predictions$n_discharges)
reduction_pct <- (current_overall - predicted_overall) / current_overall * 100
sensitivity_results <- rbind(sensitivity_results, data.frame(
Scenario = scenario_name,
Current_Rate = current_overall,
Predicted_Rate = predicted_overall,
Reduction_Percent = reduction_pct
))
}
print(sensitivity_results)
# =============================================================================
# 10. SAVE RESULTS
# =============================================================================
cat("\n=== SAVING RESULTS ===\n")
# Save key results to CSV files
write.csv(vt_summary, "vermont_summary.csv", row.names = FALSE)
write.csv(montana_predictions, "montana_predictions.csv", row.names = FALSE)
write.csv(param_summary, "model_parameters.csv", row.names = FALSE)
write.csv(sensitivity_results, "sensitivity_analysis.csv", row.names = FALSE)
# Save posterior samples for further analysis
write.csv(posterior_samples, "posterior_samples.csv", row.names = FALSE)
# Save plots
ggsave("vermont_before_after.png", p1, width = 10, height = 6, dpi = 300)
ggsave("posterior_distributions.png", p2, width = 10, height = 6, dpi = 300)
ggsave("montana_predictions.png", p3, width = 12, height = 8, dpi = 300)
ggsave("mcmc_diagnostics.png", p4, width = 10, height = 8, dpi = 300)
cat("Analysis complete! Results saved to CSV files and plots saved as PNG files.\n")
cat("\nKey findings:\n")
cat(sprintf("- Vermont EHR implementation reduced readmissions by %.1f%%\n", relative_reduction))
cat(sprintf("- Montana predicted reduction: %.1f%%\n", overall_relative))
cat(sprintf("- Estimated annual readmissions prevented in Montana: %.0f\n", prevented_readmissions))
# =============================================================================
# END OF ANALYSIS
# =============================================================================