cynthia-hg commited on
Commit
de68d5a
·
verified ·
1 Parent(s): ac6b0cb

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +97 -62
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
- bundle <- readRDS("final_xgb_stepwise.rds")
16
- model <- bundle$model
17
- cols <- bundle$x_mat_colnames
18
- thr <- bundle$threshold %||% 0.5
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
 
20
 
 
 
 
 
 
 
 
 
21
 
 
 
 
22
  ui <- fluidPage(
23
- titlePanel("Angioplasty-treated CAD Risk"),
24
  sidebarLayout(
25
  sidebarPanel(
26
- numericInput("Age", "Age", 55),
27
- numericInput("Height", "Height (cm)", 165),
28
- numericInput("BMI", "BMI", 27),
29
- numericInput("White.blood.cell..leukocyte..count", "WBC", 7),
30
- numericInput("Cystatin.C", "Cystatin C", 0.9),
31
- numericInput("ApoB_ApoA_ratio", "ApoB/ApoA1 ratio", 0.8),
32
- selectInput("statinsYes", "Statins", c("No","Yes")),
33
- selectInput("T2DYes", "T2D", c("No","Yes")),
34
- selectInput("HTNYes", "Hypertension", c("No","Yes")),
35
- selectInput("HyperlipidemiaYes", "Hyperlipidaemia", c("No","Yes")),
36
- selectInput("Fx.CVDYes", "Family history of CVD", c("No","Yes")),
37
- selectInput("Lipoprotein.AhighYes", "Lp(a) high", c("No","Yes")),
38
- selectInput("rs9349379_G1", "rs9349379_G1", c(0,1)),
39
- selectInput("rs9349379_G2", "rs9349379_G2", c(0,1)),
40
- selectInput("rs10757274_G1", "rs10757274_G1", c(0,1)),
41
- selectInput("rs10757274_G2", "rs10757274_G2", c(0,1)),
42
- actionButton("predict", "Predict")
 
 
 
 
 
 
 
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
- df <- data.frame(
58
- Height = as.numeric(input$Height),
59
- Age = as.numeric(input$Age),
60
- BMI = as.numeric(input$BMI),
61
- White.blood.cell..leukocyte..count = as.numeric(input$White.blood.cell..leukocyte..count),
62
- Cystatin.C = as.numeric(input$Cystatin.C),
63
- statinsYes = ifelse(input$statinsYes=="Yes", 1, 0),
64
- T2DYes = ifelse(input$T2DYes=="Yes", 1, 0),
65
- HTNYes = ifelse(input$HTNYes=="Yes", 1, 0),
66
- HyperlipidemiaYes = ifelse(input$HyperlipidemiaYes=="Yes", 1, 0),
67
- Fx.CVDYes = ifelse(input$Fx.CVDYes=="Yes", 1, 0),
68
- rs9349379_G1 = as.numeric(input$rs9349379_G1),
69
- rs9349379_G2 = as.numeric(input$rs9349379_G2),
70
- rs10757274_G1 = as.numeric(input$rs10757274_G1),
71
- rs10757274_G2 = as.numeric(input$rs10757274_G2),
72
- Lipoprotein.AhighYes = ifelse(input$Lipoprotein.AhighYes=="Yes", 1, 0),
73
- ApoB_ApoA_ratio = as.numeric(input$ApoB_ApoA_ratio),
74
- check.names = FALSE
75
- )
76
-
77
- X <- as.matrix(df)
78
-
79
- # enforce exact columns/order expected by training
80
- X2 <- matrix(0, nrow = 1, ncol = length(cols))
81
- colnames(X2) <- cols
82
- common <- intersect(colnames(X), cols)
83
- X2[, common] <- X[, common]
84
-
85
- d <- xgb.DMatrix(X2)
86
- p <- as.numeric(predict(model, d))
87
-
88
  output$result <- renderText({
89
- paste0("Predicted probability of CAD (Yes): ", round(100*p, 1), "%\n",
90
- "Classification (threshold ", thr, "): ", ifelse(p >= thr, "Yes", "No"))
 
 
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
+