Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -1,46 +1,80 @@
|
|
| 1 |
|
| 2 |
-
# app.R
|
| 3 |
-
|
| 4 |
-
|
| 5 |
library(shiny)
|
| 6 |
library(xgboost)
|
| 7 |
|
| 8 |
# ----------------------------
|
| 9 |
# Load XGBoost bundle
|
| 10 |
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# helper: %||% (use default if NULL)
|
| 13 |
`%||%` <- function(a, b) if (!is.null(a)) a else b
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
ui <- fluidPage(
|
| 23 |
-
titlePanel("Angioplasty-
|
| 24 |
sidebarLayout(
|
| 25 |
sidebarPanel(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
),
|
|
|
|
| 44 |
mainPanel(
|
| 45 |
h3("Result"),
|
| 46 |
verbatimTextOutput("result")
|
|
@@ -52,47 +86,48 @@ ui <- fluidPage(
|
|
| 52 |
# Server
|
| 53 |
# ----------------------------
|
| 54 |
server <- function(input, output) {
|
|
|
|
| 55 |
observeEvent(input$predict, {
|
| 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 |
output$result <- renderText({
|
| 89 |
-
paste0(
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
})
|
| 92 |
})
|
| 93 |
}
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
shinyApp(ui, server, options = list(host = "0.0.0.0", port = 7860))
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
| 2 |
library(shiny)
|
| 3 |
library(xgboost)
|
| 4 |
|
| 5 |
# ----------------------------
|
| 6 |
# Load XGBoost bundle
|
| 7 |
# ----------------------------
|
| 8 |
+
xgb_bundle <- readRDS("final_xgb_stepwise.rds")
|
| 9 |
+
model <- xgb_bundle$model
|
| 10 |
+
req_cols <- xgb_bundle$x_mat_colnames
|
| 11 |
+
threshold <- xgb_bundle$threshold %||% 0.5
|
| 12 |
|
| 13 |
# helper: %||% (use default if NULL)
|
| 14 |
`%||%` <- function(a, b) if (!is.null(a)) a else b
|
| 15 |
|
| 16 |
+
# ----------------------------
|
| 17 |
+
# Define features (your stepwise set)
|
| 18 |
+
# ----------------------------
|
| 19 |
+
feature_vars <- c(
|
| 20 |
+
"Height", "Age", "BMI",
|
| 21 |
+
"White.blood.cell..leukocyte..count", "Cystatin.C",
|
| 22 |
+
"statinsYes", "T2DYes", "HTNYes", "HyperlipidemiaYes", "Fx.CVDYes",
|
| 23 |
+
"rs9349379_G1", "rs9349379_G2", "rs10757274_G1", "rs10757274_G2",
|
| 24 |
+
"Lipoprotein.AhighYes", "ApoB_ApoA_ratio"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Split numeric vs binary inputs (for UI convenience)
|
| 28 |
+
num_vars <- c(
|
| 29 |
+
"Height", "Age", "BMI",
|
| 30 |
+
"White.blood.cell..leukocyte..count", "Cystatin.C",
|
| 31 |
+
"ApoB_ApoA_ratio"
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
bin_vars <- setdiff(feature_vars, num_vars)
|
| 35 |
|
| 36 |
+
defaults <- list(
|
| 37 |
+
Height = 165,
|
| 38 |
+
Age = 55,
|
| 39 |
+
BMI = 27,
|
| 40 |
+
White.blood.cell..leukocyte..count = 6.5,
|
| 41 |
+
Cystatin.C = 0.9,
|
| 42 |
+
ApoB_ApoA_ratio = 0.8
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
# ----------------------------
|
| 46 |
+
# UI
|
| 47 |
+
# ----------------------------
|
| 48 |
ui <- fluidPage(
|
| 49 |
+
titlePanel("Angioplasty-Treated CAD Risk (XGBoost)"),
|
| 50 |
sidebarLayout(
|
| 51 |
sidebarPanel(
|
| 52 |
+
h4("Enter patient features"),
|
| 53 |
+
|
| 54 |
+
# Numeric inputs
|
| 55 |
+
lapply(num_vars, function(v) {
|
| 56 |
+
numericInput(
|
| 57 |
+
inputId = v,
|
| 58 |
+
label = v,
|
| 59 |
+
value = defaults[[v]] %||% 0
|
| 60 |
+
)
|
| 61 |
+
}),
|
| 62 |
+
|
| 63 |
+
tags$hr(),
|
| 64 |
+
|
| 65 |
+
# Binary inputs
|
| 66 |
+
lapply(bin_vars, function(v) {
|
| 67 |
+
selectInput(
|
| 68 |
+
inputId = v,
|
| 69 |
+
label = v,
|
| 70 |
+
choices = c("0" = 0, "1" = 1),
|
| 71 |
+
selected = 0
|
| 72 |
+
)
|
| 73 |
+
}),
|
| 74 |
+
|
| 75 |
+
actionButton("predict", "Predict CAD risk")
|
| 76 |
),
|
| 77 |
+
|
| 78 |
mainPanel(
|
| 79 |
h3("Result"),
|
| 80 |
verbatimTextOutput("result")
|
|
|
|
| 86 |
# Server
|
| 87 |
# ----------------------------
|
| 88 |
server <- function(input, output) {
|
| 89 |
+
|
| 90 |
observeEvent(input$predict, {
|
| 91 |
+
|
| 92 |
+
# 1) Build a single-row data.frame from inputs
|
| 93 |
+
new_df <- as.data.frame(reactiveValuesToList(input), stringsAsFactors = FALSE)
|
| 94 |
+
|
| 95 |
+
# Keep only model features (drop button click, etc.)
|
| 96 |
+
new_df <- new_df[, intersect(names(new_df), feature_vars), drop = FALSE]
|
| 97 |
+
|
| 98 |
+
# 2) Convert types
|
| 99 |
+
for (v in num_vars) new_df[[v]] <- as.numeric(new_df[[v]])
|
| 100 |
+
for (v in bin_vars) new_df[[v]] <- as.numeric(new_df[[v]])
|
| 101 |
+
|
| 102 |
+
# 3) Convert to matrix
|
| 103 |
+
X_new <- as.matrix(new_df)
|
| 104 |
+
|
| 105 |
+
# 4) Align columns to training schema (important!)
|
| 106 |
+
# Add missing columns as 0
|
| 107 |
+
missing <- setdiff(req_cols, colnames(X_new))
|
| 108 |
+
if (length(missing) > 0) {
|
| 109 |
+
X_new <- cbind(X_new,
|
| 110 |
+
matrix(0, nrow = nrow(X_new), ncol = length(missing),
|
| 111 |
+
dimnames = list(NULL, missing)))
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
# Drop extra columns and order exactly like training
|
| 115 |
+
X_new <- X_new[, req_cols, drop = FALSE]
|
| 116 |
+
|
| 117 |
+
# 5) Predict
|
| 118 |
+
dnew <- xgb.DMatrix(X_new)
|
| 119 |
+
prob_yes <- as.numeric(predict(model, dnew))
|
| 120 |
+
pct_yes <- round(prob_yes * 100, 1)
|
| 121 |
+
pred <- ifelse(prob_yes >= threshold, "Yes", "No")
|
| 122 |
+
|
| 123 |
output$result <- renderText({
|
| 124 |
+
paste0(
|
| 125 |
+
"🎯 Estimated probability of angioplasty-treated CAD (Yes): ", pct_yes, "%\n",
|
| 126 |
+
"🔍 Classification (threshold=", threshold, "): ", pred
|
| 127 |
+
)
|
| 128 |
})
|
| 129 |
})
|
| 130 |
}
|
| 131 |
|
|
|
|
|
|
|
|
|
|
| 132 |
shinyApp(ui, server, options = list(host = "0.0.0.0", port = 7860))
|
| 133 |
+
|