Activity 2 - Individual Task

Author

Cynthia Chisom Nwajiobi

Load the packages

library(medicaldata)
library(epiR)
library(pROC)
library(dplyr)
library(ggplot2)
library(knitr)

Dataset 1: Blood storage

Load the dataset:

data("blood_storage")
dat <- blood_storage

Question 1 - Observe the dataset

dim(dat)
[1] 316  20

There are 316 observations and 20 variables present in the blood_storage dataset. The handout lists 7 variables, but the version of the dataset in the medicaldata package contains 20.

Question 2 - Inspect the dataset

str(dat)
'data.frame':   316 obs. of  20 variables:
 $ RBC.Age.Group   : num  3 3 3 2 2 3 3 1 1 2 ...
 $ Median.RBC.Age  : num  25 25 25 15 15 25 25 10 10 15 ...
 $ Age             : num  72.1 73.6 67.5 65.8 63.2 65.4 65.5 67.1 63.9 63 ...
 $ AA              : num  0 0 0 0 0 0 1 0 0 1 ...
 $ FamHx           : num  0 0 0 0 0 0 0 0 0 0 ...
 $ PVol            : num  54 43.2 102.7 46 60 ...
 $ TVol            : num  3 3 1 1 2 2 2 3 2 2 ...
 $ T.Stage         : num  1 2 1 1 1 1 1 1 1 1 ...
 $ bGS             : num  3 2 3 1 2 1 1 1 1 2 ...
 $ BN+             : num  0 0 0 0 0 0 0 0 0 0 ...
 $ OrganConfined   : num  0 1 1 1 1 0 1 0 1 1 ...
 $ PreopPSA        : num  14.08 10.5 6.98 4.4 21.4 ...
 $ PreopTherapy    : num  1 0 1 0 0 0 0 0 0 0 ...
 $ Units           : num  6 2 1 2 3 1 2 4 1 2 ...
 $ sGS             : num  1 3 1 3 3 3 3 3 3 3 ...
 $ AnyAdjTherapy   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ AdjRadTherapy   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Recurrence      : num  1 1 0 0 0 0 0 1 0 0 ...
 $ Censor          : num  0 0 1 1 1 1 1 0 1 1 ...
 $ TimeToRecurrence: num  2.67 47.63 14.1 59.47 1.23 ...

str() shows that all 20 variables are stored as numeric (num), including the categorical ones, which are coded as numbers. The table below classifies each variable by its statistical type and role.

Variable types and roles in the blood_storage dataset
Variable Description Statistical type Role
RBC.Age.Group RBC storage-duration group (1, 2, 3) Categorical – ordinal Exposure
Median.RBC.Age Median age of transfused RBCs (days) Numeric (3 distinct values: 10, 15, 25) Exposure (continuous form)
Age Patient age (years) Continuous Covariate
AA African-American race (0/1) Categorical – binary Covariate
FamHx Family history of prostate cancer (0/1) Categorical – binary Covariate
PVol Prostate volume (g) Continuous Covariate
TVol Tumour volume (1 = low, 2 = medium, 3 = extensive) Categorical – ordinal Covariate
T.Stage Clinical T stage (1 = T1–T2a, 2 = T2b–T4) Categorical – ordinal Covariate
bGS Biopsy Gleason score category (1–3) Categorical – ordinal Covariate
BN+ Lymph node involvement (0/1) Categorical – binary Covariate
OrganConfined Organ-confined disease (0/1) Categorical – binary Covariate
PreopPSA Preoperative PSA (ng/mL) Continuous Covariate / diagnostic marker
PreopTherapy Preoperative therapy (0/1) Categorical – binary Covariate
Units Number of blood units transfused Discrete count Covariate
sGS Surgical Gleason score category (1–4) Categorical – ordinal Covariate
AnyAdjTherapy Any adjuvant therapy (0/1) Categorical – binary Covariate
AdjRadTherapy Adjuvant radiation therapy (0/1) Categorical – binary Covariate
Recurrence Biochemical recurrence (0 = no, 1 = yes) Categorical – binary Outcome variable
Censor Censoring indicator (1 = censored, 0 = recurrence observed) Categorical – binary Time-to-event indicator
TimeToRecurrence Time to recurrence or censoring (months) Continuous Time variable
summary(dat)
 RBC.Age.Group   Median.RBC.Age       Age              AA        
 Min.   :1.000   Min.   :10.00   Min.   :38.40   Min.   :0.0000  
 1st Qu.:1.000   1st Qu.:10.00   1st Qu.:56.20   1st Qu.:0.0000  
 Median :2.000   Median :15.00   Median :61.85   Median :0.0000  
 Mean   :2.003   Mean   :16.71   Mean   :61.16   Mean   :0.1741  
 3rd Qu.:3.000   3rd Qu.:25.00   3rd Qu.:66.10   3rd Qu.:0.0000  
 Max.   :3.000   Max.   :25.00   Max.   :79.00   Max.   :1.0000  
                                                                 
     FamHx             PVol             TVol          T.Stage     
 Min.   :0.0000   Min.   : 19.40   Min.   :1.000   Min.   :1.000  
 1st Qu.:0.0000   1st Qu.: 40.85   1st Qu.:2.000   1st Qu.:1.000  
 Median :0.0000   Median : 49.00   Median :2.000   Median :1.000  
 Mean   :0.2152   Mean   : 56.45   Mean   :2.094   Mean   :1.112  
 3rd Qu.:0.0000   3rd Qu.: 64.05   3rd Qu.:3.000   3rd Qu.:1.000  
 Max.   :1.0000   Max.   :274.00   Max.   :3.000   Max.   :2.000  
                  NAs    :9        NAs    :6       NAs    :13     
      bGS           BN+          OrganConfined       PreopPSA     
 Min.   :1.0   Min.   :0.00000   Min.   :0.0000   Min.   : 1.300  
 1st Qu.:1.0   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.: 4.980  
 Median :1.0   Median :0.00000   Median :1.0000   Median : 6.200  
 Mean   :1.5   Mean   :0.05696   Mean   :0.6551   Mean   : 8.185  
 3rd Qu.:2.0   3rd Qu.:0.00000   3rd Qu.:1.0000   3rd Qu.: 9.000  
 Max.   :3.0   Max.   :1.00000   Max.   :1.0000   Max.   :40.100  
 NAs    :2                                        NAs    :3       
  PreopTherapy        Units             sGS        AnyAdjTherapy    
 Min.   :0.0000   Min.   : 1.000   Min.   :1.000   Min.   :0.00000  
 1st Qu.:0.0000   1st Qu.: 2.000   1st Qu.:2.000   1st Qu.:0.00000  
 Median :0.0000   Median : 2.000   Median :3.000   Median :0.00000  
 Mean   :0.1203   Mean   : 2.456   Mean   :2.557   Mean   :0.02215  
 3rd Qu.:0.0000   3rd Qu.: 2.000   3rd Qu.:3.000   3rd Qu.:0.00000  
 Max.   :1.0000   Max.   :19.000   Max.   :4.000   Max.   :1.00000  
                                                                    
 AdjRadTherapy        Recurrence         Censor       TimeToRecurrence 
 Min.   :0.000000   Min.   :0.0000   Min.   :0.0000   Min.   :  0.270  
 1st Qu.:0.000000   1st Qu.:0.0000   1st Qu.:1.0000   1st Qu.:  7.585  
 Median :0.000000   Median :0.0000   Median :1.0000   Median : 25.300  
 Mean   :0.003165   Mean   :0.1709   Mean   :0.8291   Mean   : 32.918  
 3rd Qu.:0.000000   3rd Qu.:0.0000   3rd Qu.:1.0000   3rd Qu.: 52.035  
 Max.   :1.000000   Max.   :1.0000   Max.   :1.0000   Max.   :103.600  
                                                      NAs    :1        

summary() gives means for every variable, but a mean is only meaningful for continuous variables. Below, continuous variables are summarised with median, IQR and range, and categorical variables with counts and percentages.

# Continuous and count variables: median, IQR and range
num_vars <- c("Age", "PVol", "PreopPSA", "Median.RBC.Age", "Units", "TimeToRecurrence")

desc_num <- data.frame(
  Variable = num_vars,
  Median   = sapply(dat[num_vars], median, na.rm = TRUE),
  Q1       = sapply(dat[num_vars], quantile, 0.25, na.rm = TRUE),
  Q3       = sapply(dat[num_vars], quantile, 0.75, na.rm = TRUE),
  Min      = sapply(dat[num_vars], min, na.rm = TRUE),
  Max      = sapply(dat[num_vars], max, na.rm = TRUE),
  Missing  = sapply(dat[num_vars], function(x) sum(is.na(x)))
)

kable(desc_num, digits = 1, row.names = FALSE,
      caption = "Descriptive statistics for continuous and count variables")
Descriptive statistics for continuous and count variables
Variable Median Q1 Q3 Min Max Missing
Age 61.8 56.2 66.1 38.4 79.0 0
PVol 49.0 40.9 64.0 19.4 274.0 9
PreopPSA 6.2 5.0 9.0 1.3 40.1 3
Median.RBC.Age 15.0 10.0 25.0 10.0 25.0 0
Units 2.0 2.0 2.0 1.0 19.0 0
TimeToRecurrence 25.3 7.6 52.0 0.3 103.6 1
# Categorical variables: counts and percentages
cat_vars <- c("RBC.Age.Group", "AA", "FamHx", "TVol", "T.Stage", "bGS", "BN+",
              "OrganConfined", "PreopTherapy", "sGS", "AnyAdjTherapy",
              "AdjRadTherapy", "Recurrence", "Censor")

for (v in cat_vars) {
  print(kable(dat |> count(.data[[v]]) |>
                mutate(percent = round(n / sum(n) * 100, 1)),
              caption = v))
}
RBC.Age.Group
RBC.Age.Group n percent
1 106 33.5
2 103 32.6
3 107 33.9
AA
AA n percent
0 261 82.6
1 55 17.4
FamHx
FamHx n percent
0 248 78.5
1 68 21.5
TVol
TVol n percent
1 64 20.3
2 153 48.4
3 93 29.4
NA 6 1.9
T.Stage
T.Stage n percent
1 269 85.1
2 34 10.8
NA 13 4.1
bGS
bGS n percent
1 189 59.8
2 93 29.4
3 32 10.1
NA 2 0.6
BN+
BN+ n percent
0 298 94.3
1 18 5.7
OrganConfined
OrganConfined n percent
0 109 34.5
1 207 65.5
PreopTherapy
PreopTherapy n percent
0 278 88
1 38 12
sGS
sGS n percent
1 38 12.0
2 85 26.9
3 172 54.4
4 21 6.6
AnyAdjTherapy
AnyAdjTherapy n percent
0 309 97.8
1 7 2.2
AdjRadTherapy
AdjRadTherapy n percent
0 315 99.7
1 1 0.3
Recurrence
Recurrence n percent
0 262 82.9
1 54 17.1
Censor
Censor n percent
0 54 17.1
1 262 82.9

Summary of descriptive statistics:

Variable Median (IQR) Range
Age (years) 61.9 (56.2–66.1) 38.4–79.0
Prostate volume (g) 49.0 (40.9–64.1) 19.4–274.0
Preoperative PSA (ng/mL) 6.2 (5.0–9.0) 1.3–40.1
Units transfused 2 (2–2) 1–19
Time to recurrence (months) 25.3 (7.6–52.0) 0.3–103.6

The 316 patients were evenly split across the three storage groups (106, 103 and 107). Most patients had organ-confined disease (65.5%) and no lymph node involvement (94.3%). 17.4% were African-American, and 21.5% had a family history of prostate cancer. PSA is right-skewed (mean 8.2 vs median 6.2), so medians and ranges describe it better than means. A few variables have missing values: PVol (9), T.Stage (13), TVol (6), PreopPSA (3), bGS (2) and TimeToRecurrence (1). Adjuvant radiation therapy was very rare (1 patient).

Question 3

# number and percentage of patients with biochemical recurrence
dat |>
  count(Recurrence) |>
  mutate(percent = round(n / sum(n) * 100, 1))
  Recurrence   n percent
1          0 262    82.9
2          1  54    17.1

Interpretation: 54 of the 316 patients (17.1%) had biochemical recurrence after radical prostatectomy, and 262 (82.9%) did not. The outcome is relatively uncommon, so later estimates rest on a fairly small number of events. Because patients were followed for different lengths of time, this is a crude proportion that does not account for differing follow-up.

Question 4 - Explore preoperative PSA

plot1 <- ggplot(dat, aes(x= factor(Recurrence), y = PreopPSA)) +
  geom_boxplot() +
  labs(
    x = "Biochemical recurrence",
    y = "Preoperative PSA (ng/mL)"
  )

plot1
Warning: Removed 3 rows containing non-finite outside the scale range
(`stat_boxplot()`).

# PSA summary by recurrence status
dat |>
  group_by(Recurrence) |>
  summarise(
    n      = sum(!is.na(PreopPSA)),
    median = median(PreopPSA, na.rm = TRUE),
    Q1     = quantile(PreopPSA, 0.25, na.rm = TRUE),
    Q3     = quantile(PreopPSA, 0.75, na.rm = TRUE),
    min    = min(PreopPSA, na.rm = TRUE),
    max    = max(PreopPSA, na.rm = TRUE),
    mean   = mean(PreopPSA, na.rm = TRUE)
  ) |>
  kable(digits = 1)
Recurrence n median Q1 Q3 min max mean
0 261 5.8 4.7 8.1 1.3 39.0 7.5
1 52 8.6 6.2 14.2 4.2 40.1 11.6

Interpretation:

The distribution of PSA in patients without recurrence: median 5.8 ng/mL (IQR 4.7–8.1), range 1.3–39.0. Most values are concentrated at low levels, with a long upper tail and several high outliers.

The distribution of PSA in patients with recurrence: median 8.6 ng/mL (IQR 6.2–14.2), range 4.2–40.1. The whole distribution sits higher and is more spread out than in patients without recurrence.

Overlap, skewness and extreme values: Patients with recurrence tend to have higher preoperative PSA, but the two distributions overlap considerably: the IQRs overlap between about 6.2 and 8.1 ng/mL, and many patients without recurrence have PSA values as high as those with recurrence. Both distributions are right-skewed (means of 7.5 and 11.6 ng/mL are above the medians), with extreme values near 40 ng/mL in both groups. Medians and IQRs are therefore more appropriate summaries than means. ggplot warns that 3 rows were removed because 3 patients have missing PSA.

5. Create a binary exposure and contingency table

dat <- dat |>
  mutate(
    long_storage = ifelse(RBC.Age.Group == 3, 1, 0)
  )

table(dat$long_storage)

  0   1 
209 107 

Why create a clearly defined binary exposure first?

A 2 × 2 table needs exactly two exposure categories. RBC.Age.Group has three levels, so it must be collapsed before the table can be built. Defining the binary variable explicitly, with a named variable and a stated rule (Group 3 = 1, Groups 1–2 = 0), makes the comparison transparent and reproducible: anyone reading the code knows exactly who counts as “exposed”. It also prevents R from silently ordering or grouping categories in an unintended way, and ensures that the same definition is used in every later analysis (tables, RR, OR). The trade-off is that collapsing categories discards information, for example any difference between Groups 1 and 2.

# 2x2 table (rows = exposure, columns = outcome)
tab <- table(dat$long_storage, dat$Recurrence)
tab
   
      0   1
  0 173  36
  1  89  18
2 × 2 table of RBC storage duration and biochemical recurrence
Recurrence = 1 Recurrence = 0 Total
Long storage 18 89 107
Short storage 36 173 209
# Recurrence proportion within each exposure group (row percentages)
round(prop.table(tab, margin = 1) * 100, 1)
   
       0    1
  0 82.8 17.2
  1 83.2 16.8

Recurrence proportion in each group:

The recurrence proportions are 16.8% (18/107 x 100) with long storage and 17.2% (36/209 x 100) with short storage. They’re almost identical, so this crude comparison shows essentially no association.

6. Crude Risk/Proportion Ratio and Odds Ratio

tab_rr <- table(
  factor(dat$long_storage, levels = c(1, 0),
         labels = c("Long storage", "Short storage")),
  factor(dat$Recurrence, levels = c(1, 0),
         labels = c("Recurrence", "No recurrence"))
)
tab_rr
               
                Recurrence No recurrence
  Long storage          18            89
  Short storage         36           173
res <- epi.2by2(tab_rr, method = "cohort.count", conf.level = 0.95)
res
             Outcome+    Outcome-      Total                 Inc risk *
Exposure+          18          89        107     16.82 (10.29 to 25.28)
Exposure-          36         173        209     17.22 (12.37 to 23.04)
Total              54         262        316     17.09 (13.11 to 21.70)

Point estimates and 95% CIs:
-------------------------------------------------------------------
Inc risk ratio                                 0.98 (0.58, 1.64)
Inc odds ratio                                 0.97 (0.52, 1.81)
Attrib risk in the exposed *                   -0.40 (-9.15, 8.34)
Attrib fraction in the exposed (%)            -2.39 (-71.92, 38.01)
Attrib risk in the population *                -0.14 (-6.73, 6.45)
Attrib fraction in the population (%)         -0.80 (-6.17, 5.65)
E-value                                        1.18 (NA, 1.00)
-------------------------------------------------------------------
Uncorrected chi2 test that OR = 1: chi2(1) = 0.008 Pr>chi2 = 0.928
Fisher exact test that OR = 1: Pr>chi2 = 1.000
Wald confidence limits
CI: confidence interval
* Outcomes per 100 population units 
res$massoc.summary[1:2, ] |>
  mutate(across(where(is.numeric), ~ round(.x, 2))) |>
  kable(col.names = c("Measure", "Estimate", "Lower 95% CI", "Upper 95% CI"))
Measure Estimate Lower 95% CI Upper 95% CI
Inc risk ratio 0.98 0.58 1.64
Inc odds ratio 0.97 0.52 1.81

Results:

Association between long RBC storage and biochemical recurrence
Measure Estimate 95% CI
Recurrence proportion, long storage 16.8% (18/107) 10.3–25.3%
Recurrence proportion, short/medium storage 17.2% (36/209) 12.4–23.0%
Crude risk/proportion ratio 0.98 0.58–1.64
Odds ratio 0.97 0.52–1.81

Is it appropriate to calculate the risk ratio? Why?

A ratio of proportions can be calculated here because this is a cohort: patients were grouped by exposure (storage duration), not selected by outcome as in a case-control study. But it shouldn’t be interpreted as a true risk ratio. A conventional risk ratio assumes everyone was followed for the same fixed period, so that the proportion with the outcome is the risk over that period. In this dataset, patients were followed for different lengths of time and some were censored before recurrence could be observed (TimeToRecurrence, Censor). The 16.8% and 17.2% are therefore crude cumulative proportions, not risks over a defined time, so the measure is better called a crude proportion ratio. Because the study is retrospective, there is also potential for selection and information bias, and the comparison is unadjusted for confounders such as PSA, tumour stage or age. A time-to-event method, such as a hazard ratio from a Cox model, would use the follow-up information properly.

Interpretation: Both the proportion ratio (0.98) and the odds ratio (0.97) are close to 1, and both confidence intervals comfortably include 1. There is no evidence that long RBC storage is associated with biochemical recurrence in this crude analysis. Recurrence is fairly common (about 17%), so the OR would not approximate the RR closely in general, but here both are near 1 and lead to the same conclusion.

7. Interpret the odds ratio in words

The exposure is receiving red blood cells from the longest storage-duration group (Group 3), compared with Groups 1 and 2, and the outcome is biochemical prostate cancer recurrence. The odds of recurrence among patients who received long-stored RBCs were about 3% lower than among patients who received shorter-stored RBCs (OR = 0.97, 95% CI 0.52–1.81). The point estimate suggests a very slight negative (protective) direction, but the magnitude is negligible: an OR of 0.97 is practically equal to 1. The confidence interval is wide and includes 1, ranging from about 48% lower odds to about 81% higher odds of recurrence. The data are therefore compatible with a moderate decrease, no difference or a moderate increase in the odds of recurrence, and provide no evidence of an association between RBC storage duration and recurrence. This is a crude, unadjusted estimate from a retrospective cohort with variable follow-up, so it should be interpreted cautiously.

Diagnostic testing using PSA

Question 8

dat <- dat |> mutate(PSA10 = ifelse(PreopPSA >= 10, 1, 0))

# create the diagnostic 2x2 table:
tab_psa10 <- table(
  factor(dat$PSA10,      levels = c(1, 0), labels = c("PSA ≥ 10", "PSA < 10")),
  factor(dat$Recurrence, levels = c(1, 0), labels = c("Recurrence", "No recurrence"))
)
tab_psa10
          
           Recurrence No recurrence
  PSA ≥ 10         22            44
  PSA < 10         30           217
res_psa10 <- epi.tests(tab_psa10, conf.level = 0.95)
res_psa10
          Outcome +    Outcome -      Total
Test +           22           44         66
Test -           30          217        247
Total            52          261        313

Point estimates and 95% CIs:
--------------------------------------------------------------
Apparent prevalence *                  0.21 (0.17, 0.26)
True prevalence *                      0.17 (0.13, 0.21)
Sensitivity *                          0.42 (0.29, 0.57)
Specificity *                          0.83 (0.78, 0.87)
Positive predictive value *            0.33 (0.22, 0.46)
Negative predictive value *            0.88 (0.83, 0.92)
Positive likelihood ratio              2.51 (1.66, 3.81)
Negative likelihood ratio              0.69 (0.55, 0.88)
False T+ proportion for true D- *      0.17 (0.13, 0.22)
False T- proportion for true D+ *      0.58 (0.43, 0.71)
False T+ proportion for T+ *           0.67 (0.54, 0.78)
False T- proportion for T- *           0.12 (0.08, 0.17)
Correctly classified proportion *      0.76 (0.71, 0.81)
--------------------------------------------------------------
* Exact CIs

Three patients have missing PreopPSA, so they have a missing PSA10 value and are dropped from the table. The diagnostic analysis uses 313 patients, including 52 with recurrence.

Diagnostic 2 × 2 table for PSA ≥ 10 ng/mL
Recurrence No recurrence Total
PSA ≥ 10 TP = 22 FP = 44 66
PSA < 10 FN = 30 TN = 217 247
Total 52 261 313
Diagnostic accuracy of PSA ≥ 10 ng/mL for biochemical recurrence
Measure Formula Estimate (95% CI)
Sensitivity 22 / 52 42.3% (28.7–56.8)
Specificity 217 / 261 83.1% (78.0–87.5)
PPV 22 / 66 33.3% (22.2–46.0)
NPV 217 / 247 87.9% (83.1–91.7)

Question 9

  • Among patients who had biochemical recurrence, about 42% had a preoperative PSA of at least 10 ng/mL. (sensitivity)

  • Among patients who did not have recurrence, about 83% had a PSA below 10 ng/mL. (specificity)

  • Among patients with a PSA of at least 10 ng/mL, about 33% had recurrence. (PPV)

  • Among patients with a PSA below 10 ng/mL, about 88% did not have recurrence. (NPV)

Interpretation: At 10 ng/mL, PSA misses most recurrences: it detects fewer than half of them. Its specificity is reasonably good. The high NPV mostly reflects the low prevalence of recurrence (about 17%), not a strong test. When a disease is uncommon, most negative results will be true negatives anyway. PPV and NPV depend on prevalence, so they wouldn’t carry over to a population with a different recurrence rate. Sensitivity and specificity don’t depend on prevalence in the same way.

Question 10. Changing the threshold, and the Youden Index

thresholds <- c(4, 6, 8, 10, 12, 15, 20)
d <- dat |> filter(!is.na(PreopPSA))

thr_table <- lapply(thresholds, function(t) {
  pos <- d$PreopPSA >= t
  TP <- sum(pos  & d$Recurrence == 1);  FP <- sum(pos  & d$Recurrence == 0)
  FN <- sum(!pos & d$Recurrence == 1);  TN <- sum(!pos & d$Recurrence == 0)
  data.frame(threshold = t, TP, FP, FN, TN,
             sensitivity = TP / (TP + FN),
             specificity = TN / (TN + FP),
             PPV = TP / (TP + FP),
             NPV = TN / (TN + FN))
}) |> bind_rows() |>
  mutate(youden = sensitivity + specificity - 1)

kable(thr_table, digits = 3)
threshold TP FP FN TN sensitivity specificity PPV NPV youden
4 52 238 0 23 1.000 0.088 0.179 1.000 0.088
6 42 125 10 136 0.808 0.521 0.251 0.932 0.329
8 31 69 21 192 0.596 0.736 0.310 0.901 0.332
10 22 44 30 217 0.423 0.831 0.333 0.879 0.254
12 17 30 35 231 0.327 0.885 0.362 0.868 0.212
15 9 18 43 243 0.173 0.931 0.333 0.850 0.104
20 6 12 46 249 0.115 0.954 0.333 0.844 0.069
# Determine the best threshold across all observed PSA values
roc_psa <- roc(dat$Recurrence, dat$PreopPSA,
               levels = c(0, 1), direction = "<")

coords(roc_psa, "best", best.method = "youden",
       ret = c("threshold", "sensitivity", "specificity", "youden"))
  threshold sensitivity specificity   youden
1      7.05   0.6730769   0.6934866 1.366564

Interpretation: As the threshold rises, sensitivity falls and specificity rises. A higher cut-off means fewer patients test positive. You miss more true recurrences (more false negatives), but you also wrongly flag fewer patients without recurrence (fewer false positives). This happens because PSA distributions in the two groups overlap heavily: there’s no cut-off that separates them cleanly, so any gain on one measure costs you on the other. Of the fixed thresholds, 8 ng/mL has the highest Youden index (0.33; sensitivity 59.6%, specificity 73.6%), with 6 ng/mL close behind (0.33). The Youden-optimal cut-off from coords() is about 7.05 ng/mL, with sensitivity 67.3% and specificity 69.3% (J ≈ 0.37), which balances sensitivity and specificity better than any of the fixed values. (pROC’s youden column reports sensitivity + specificity, i.e. J + 1.) The commonly used 10 ng/mL gives up a lot of sensitivity for its specificity. The Youden index weights false negatives and false positives equally, but in practice the right threshold depends on which error is worse. An “optimal” threshold chosen from the same data it is evaluated on is also likely to look better here than in new patients.

Question 11

roc_psa <- roc(dat$Recurrence, dat$PreopPSA,
               levels = c(0, 1), direction = "<", ci = TRUE)

auc(roc_psa)
Area under the curve: 0.726
ci.auc(roc_psa)          # DeLong 95% CI
95% CI: 0.6557-0.7963 (DeLong)
plot(roc_psa, print.auc = TRUE, legacy.axes = TRUE,
     main = "ROC curve: preoperative PSA for biochemical recurrence")

AUC = 0.73 (95% CI 0.66–0.80, DeLong)

What AUC measures: the AUC is the probability that a randomly chosen patient who had recurrence has a higher PSA than a randomly chosen patient who did not. It summarises how well PSA discriminates across all possible thresholds. 0.5 means no better than chance and 1.0 means perfect separation.

Interpretation: an AUC of 0.73 means that in about 73% of random pairs (one patient with recurrence, one without), the patient with recurrence has the higher PSA. That’s usually described as acceptable or moderate discrimination. It is clearly better than chance, since the lower CI limit of 0.66 is well above 0.5, but far from good enough to use PSA alone to predict recurrence. The fairly wide CI reflects the small number of recurrences (52).

Question 12 - Synthesis

This analysis showed that association and diagnostic accuracy answer different questions. The RR (0.98) and OR (0.97) asked whether long RBC storage is associated with recurrence. Both were close to 1 with CIs including 1, so there was no evidence of association. Association measures compare groups. They don’t tell us whether a variable can classify individual patients. Even a strong association, such as a large OR, can come with poor discrimination if the distributions in the two groups overlap.

The PSA analysis asked a different question: can a single measurement separate patients who will and won’t have recurrence? Sensitivity and specificity describe how the test performs within patients with and without the outcome. PPV and NPV describe what a result means for an individual and depend on prevalence: the high NPV here mostly reflects the low (about 17%) recurrence rate. Changing the threshold showed a direct trade-off. Lower cut-offs gained sensitivity at the cost of specificity, and the reverse for higher ones. Any single threshold, including the conventional 10 ng/mL or the Youden-optimal value of about 7 ng/mL, is a choice that implicitly weighs false negatives against false positives. The ROC curve and AUC (0.73) summarise discrimination across all thresholds without committing to one, and show that PSA has moderate, not strong, discriminative ability.

Limitations: the data are retrospective and from a single source. The analysis was crude, with no adjustment for confounders such as tumour stage, Gleason score or age. Differing follow-up and censoring mean recurrence proportions aren’t true risks, and some patients classed as “no recurrence” may have recurred after follow-up ended. Three patients with missing PSA were excluded. With only around 52 to 54 events, the estimates are imprecise. The Youden-optimal threshold was derived and evaluated on the same data, so it is likely optimistic and would need validation in an independent cohort.

Dataset 2: Low birth weight and maternal risk factors

library(MASS)

Adjuntando el paquete: 'MASS'
The following object is masked from 'package:dplyr':

    select
data("birthwt")
dat <- birthwt

Question 1 - Observations and variables present

dim(dat)
[1] 189  10

There are 189 observations and 10 variables present in the birthweight data.

Question 2

str(dat)
'data.frame':   189 obs. of  10 variables:
 $ low  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ age  : int  19 33 20 21 18 21 22 17 29 26 ...
 $ lwt  : int  182 155 105 108 107 124 118 103 123 113 ...
 $ race : int  2 3 1 1 1 3 1 3 1 1 ...
 $ smoke: int  0 0 1 1 1 0 0 0 1 1 ...
 $ ptl  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ ht   : int  0 0 0 0 0 0 0 0 0 0 ...
 $ ui   : int  1 0 0 1 1 0 0 0 0 0 ...
 $ ftv  : int  0 3 1 2 0 0 1 1 1 0 ...
 $ bwt  : int  2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 ...

str() shows that all 10 variables are stored as integers, including the categorical ones, which are coded as numbers (0/1 or 1/2/3).

Variable types and roles in the birthwt dataset
Variable Description Statistical type Role
low Low birth weight (1 = <2.5 kg, 0 = ≥2.5 kg) Categorical – binary Outcome variable
age Mother’s age (years) Continuous Covariate
lwt Mother’s weight at last menstrual period (lb) Continuous Covariate / candidate predictor
race Maternal race (1 = white, 2 = black, 3 = other) Categorical – nominal (3 levels) Covariate
smoke Smoking during pregnancy (0/1) Categorical – binary Exposure (Question 5)
ptl Number of previous premature labours Discrete count Covariate
ht History of hypertension (0/1) Categorical – binary Covariate
ui Uterine irritability (0/1) Categorical – binary Covariate
ftv Number of first-trimester physician visits Discrete count Covariate
bwt Infant birth weight (g) Continuous Continuous measure from which low is derived
Time variable None: each pregnancy is recorded once, with no follow-up time — —
# Convert categorical variables to labelled factors for description
# (stored in dat_desc so that dat keeps its original 0/1 coding for later questions)
dat_desc <- dat |>
  mutate(
    low   = factor(low,   levels = c(0, 1), labels = c("≥2.5 kg", "<2.5 kg")),
    race  = factor(race,  levels = 1:3,     labels = c("White", "Black", "Other")),
    smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")),
    ht    = factor(ht,    levels = c(0, 1), labels = c("No", "Yes")),
    ui    = factor(ui,    levels = c(0, 1), labels = c("No", "Yes"))
  )

summary(dat_desc)
      low           age             lwt           race    smoke    
 ≥2.5 kg:130   Min.   :14.00   Min.   : 80.0   White:96   No :115  
 <2.5 kg: 59   1st Qu.:19.00   1st Qu.:110.0   Black:26   Yes: 74  
               Median :23.00   Median :121.0   Other:67            
               Mean   :23.24   Mean   :129.8                       
               3rd Qu.:26.00   3rd Qu.:140.0                       
               Max.   :45.00   Max.   :250.0                       
      ptl           ht        ui           ftv              bwt      
 Min.   :0.0000   No :177   No :161   Min.   :0.0000   Min.   : 709  
 1st Qu.:0.0000   Yes: 12   Yes: 28   1st Qu.:0.0000   1st Qu.:2414  
 Median :0.0000                       Median :0.0000   Median :2977  
 Mean   :0.1958                       Mean   :0.7937   Mean   :2945  
 3rd Qu.:0.0000                       3rd Qu.:1.0000   3rd Qu.:3487  
 Max.   :3.0000                       Max.   :6.0000   Max.   :4990  
# Continuous and count variables: median, IQR and range
num_vars2 <- c("age", "lwt", "bwt", "ptl", "ftv")

desc_num2 <- data.frame(
  Variable = num_vars2,
  Median = sapply(dat[num_vars2], median),
  Q1     = sapply(dat[num_vars2], quantile, 0.25),
  Q3     = sapply(dat[num_vars2], quantile, 0.75),
  Min    = sapply(dat[num_vars2], min),
  Max    = sapply(dat[num_vars2], max),
  Mean   = sapply(dat[num_vars2], mean),
  SD     = sapply(dat[num_vars2], sd)
)

kable(desc_num2, digits = 1, row.names = FALSE,
      caption = "Descriptive statistics for continuous and count variables")
Descriptive statistics for continuous and count variables
Variable Median Q1 Q3 Min Max Mean SD
age 23 19 26 14 45 23.2 5.3
lwt 121 110 140 80 250 129.8 30.6
bwt 2977 2414 3487 709 4990 2944.6 729.2
ptl 0 0 0 0 3 0.2 0.5
ftv 0 0 1 0 6 0.8 1.1
# Categorical variables: counts and percentages
for (v in c("low", "race", "smoke", "ht", "ui")) {
  print(kable(dat_desc |> count(.data[[v]]) |>
                mutate(percent = round(n / sum(n) * 100, 1)),
              caption = v))
}
low
low n percent
≥2.5 kg 130 68.8
<2.5 kg 59 31.2
race
race n percent
White 96 50.8
Black 26 13.8
Other 67 35.4
smoke
smoke n percent
No 115 60.8
Yes 74 39.2
ht
ht n percent
No 177 93.7
Yes 12 6.3
ui
ui n percent
No 161 85.2
Yes 28 14.8
# The count variables are very skewed, so frequency tables are also informative
table(dat$ptl)

  0   1   2   3 
159  24   5   1 
table(dat$ftv)

  0   1   2   3   4   6 
100  47  30   7   4   1 
Summary of continuous and count variables
Variable Median (IQR) Range Mean (SD)
age (years) 23 (19–26) 14–45 23.2 (5.3)
lwt (lb) 121 (110–140) 80–250 129.8 (30.6)
bwt (g) 2977 (2414–3487) 709–4990 2944.6 (729.2)
ptl 0 (0–0) 0–3 0.2 (0.5)
ftv 0 (0–1) 0–6 0.8 (1.1)
Summary of categorical variables
Variable Category n (%)
low <2.5 kg 59 (31.2%)
race White / Black / Other 96 (50.8%) / 26 (13.8%) / 67 (35.4%)
smoke Yes 74 (39.2%)
ht Yes 12 (6.3%)
ui Yes 28 (14.8%)
# Proportion of infants with low birth weight
dat |>
  count(low) |>
  mutate(percent = round(n / sum(n) * 100, 1))
  low   n percent
1   0 130    68.8
2   1  59    31.2

Interpretation: The dataset contains 189 pregnancies and 10 variables, all stored as integers. Five are categorical (low, race, smoke, ht, ui), three are continuous (age, lwt, bwt) and two are discrete counts (ptl, ftv). The outcome is low. There is no time variable, because this is a cross-sectional dataset without follow-up. Mothers were young (median age 23, range 14–45). Median pre-pregnancy weight was 121 lb (range 80–250), and median birth weight was 2977 g (range 709–4990). Most mothers had no previous premature labour, and over half had no first-trimester physician visits. 39.2% smoked during pregnancy.

Proportion with low birth weight: 59 of the 189 infants (31.2%) had low birth weight (<2.5 kg), and 130 (68.8%) did not.

Question 3 - Birth weight distribution

# Histogram of birth weight
ggplot(dat, aes(x = bwt)) +
  geom_histogram(bins = 20) +
  labs(
    x = "Birth weight (g)",
    y = "Number of infants"
  )

# Boxplot of birth weight by low-birth-weight status
ggplot(dat, aes(
  x = factor(low),
  y = bwt
)) +
  geom_boxplot() +
  labs(
    x = "Low birth weight",
    y = "Birth weight (g)"
  )

dat |>
  group_by(low) |>
  summarise(n = n(),
            median = median(bwt),
            min = min(bwt),
            max = max(bwt)) |>
  kable()
low n median min max
0 130 3267 2523 4990
1 59 2211 709 2495

Description: Birth weight is roughly symmetric, with a mean of 2945 g and a median of 2977 g, and a slightly longer lower tail of very small infants (minimum 709 g). The boxplot shows that the two groups do not overlap at all: infants with low = 1 weighed 709–2495 g (median 2211 g) and infants with low = 0 weighed 2523–4990 g (median 3267 g). This is because low is not a separate measurement. It is created directly from bwt by dichotomising at 2500 g, so low = 1 whenever bwt < 2500.

Would it be inappropriate to use bwt itself as a diagnostic predictor of low? Why?

Yes, it would be inappropriate. Because low is defined from bwt, using bwt to “predict” low is circular: at a threshold of 2500 g it would classify every infant correctly, with sensitivity, specificity, PPV, NPV and AUC all equal to 1. This perfect performance tells us nothing, because the test and the reference standard are the same measurement (a form of incorporation bias). A diagnostic test must be independent of the reference standard and should be available before or separately from the outcome. Birth weight is only known at delivery, at which point low birth weight is already known. This is why the later questions use predictors measured independently of the outcome, such as smoking and maternal weight.

Question 5

# Investigate whether maternal smoking is associated with low birth weight

# Create the 2 x 2 table:
tab_smoke <- table(
  dat$smoke,
  dat$low
)

tab_smoke
   
     0  1
  0 86 29
  1 44 30
2 × 2 table of birth weight by smoking status
Low weight = 1 Low weight = 0 Total
Smoker 30 44 74
Non-Smoker 29 86 115
tab_smoke2 <- table(
  factor(dat$smoke, levels = c(1, 0), labels = c("Smoker", "Non-smoker")),
  factor(dat$low,   levels = c(1, 0), labels = c("Low weight = 1", "Low weight = 0"))
)
tab_smoke2
            
             Low weight = 1 Low weight = 0
  Smoker                 30             44
  Non-smoker             29             86
# Proportion of low birth weight in each smoking group
round(prop.table(tab_smoke2, margin = 1) * 100, 1)
            
             Low weight = 1 Low weight = 0
  Smoker               40.5           59.5
  Non-smoker           25.2           74.8
# Ratio measures with 95% CIs
res_smoke <- epi.2by2(tab_smoke2, method = "cross.sectional", conf.level = 0.95)
res_smoke
          Outcome+ Outcome- Total               Prev risk *
Exposure+       30       44    74    40.54 (29.27 to 52.59)
Exposure-       29       86   115    25.22 (17.58 to 34.17)
Total           59      130   189    31.22 (24.69 to 38.35)

Point estimates and 95% CIs:
-------------------------------------------------------------------
Prev risk ratio                                1.61 (1.06, 2.44)
Prev odds ratio                                2.02 (1.08, 3.78)
Attrib prev in the exposed *                   15.32 (1.61, 29.04)
Attrib fraction in the exposed (%)            37.80 (5.38, 58.95)
Attrib prev in the population *                6.00 (-4.33, 16.33)
Attrib fraction in the population (%)         19.22 (10.89, 28.78)
E-value                                        2.60 (1.31, NA)
-------------------------------------------------------------------
Uncorrected chi2 test that OR = 1: chi2(1) = 4.924 Pr>chi2 = 0.026
Fisher exact test that OR = 1: Pr>chi2 = 0.036
Wald confidence limits
CI: confidence interval
* Outcomes per 100 population units 
res_smoke$massoc.summary[1:2, ] |>
  mutate(across(where(is.numeric), ~ round(.x, 2))) |>
  kable(col.names = c("Measure", "Estimate", "Lower 95% CI", "Upper 95% CI"))
Measure Estimate Lower 95% CI Upper 95% CI
Prev risk ratio 1.61 1.06 2.44
Prev odds ratio 2.02 1.08 3.78

Results:

Association between maternal smoking and low birth weight
Measure Estimate 95% CI
Low-birth-weight proportion, smokers 40.5% (30/74) 29.3–52.6%
Low-birth-weight proportion, non-smokers 25.2% (29/115) 17.6–34.2%
Crude proportion (risk) ratio 1.61 1.06–2.44
Odds ratio 2.02 1.08–3.78

Low birth weight was more common among infants of smokers (40.5%) than among infants of non-smokers (25.2%). The crude proportion ratio was 1.61 (95% CI 1.06–2.44), meaning that low birth weight was about 61% more common among smokers’ infants in this sample.

Is it appropriate to calculate the risk ratio?

A ratio of proportions can be calculated from this table, but it should not be interpreted as a true risk ratio. A risk ratio compares the incidence of an outcome in exposed and unexposed people followed over time. The birthwt data are cross-sectional: each pregnancy was recorded once, with smoking status and birth weight observed together, and there is no follow-up period or time variable. The proportions are therefore better described as prevalences of low birth weight in each group, and the measure as a crude prevalence (proportion) ratio.

There is also a caution about how the sample was selected. Low birth weight occurs in 31.2% of this sample, much higher than in the general population. If pregnancies were selected or enriched based on the outcome, the observed proportions would not represent the real risk in smokers and non-smokers, and the ratio would be biased. The odds ratio does not have this problem. Smoking was also self-reported, so some misclassification is possible, and the estimate is unadjusted for confounders. Because the outcome is common, the OR (2.02) is noticeably further from 1 than the proportion ratio (1.61); the OR only approximates the RR when the outcome is rare.

Interpretation of the odds ratio:

The exposure is maternal smoking during pregnancy and the outcome is low infant birth weight (<2.5 kg). The odds of low birth weight were about 2 times higher among infants of mothers who smoked than among infants of non-smokers (OR = 2.02, 95% CI 1.08–3.78). The association is positive: smoking goes with higher odds of low birth weight. The magnitude is moderate, about a doubling of the odds. Because the whole confidence interval lies above 1, the data are consistent with a real positive association at the 5% level. The interval is wide, though: the true OR could be anywhere from a small increase (about 8% higher odds) to nearly four times the odds, which reflects the small sample (59 low-birth-weight infants). This is a crude, unadjusted association from observational data, so it does not on its own establish that smoking causes low birth weight. Confounding by factors such as race, maternal weight or previous premature labour has not been accounted for.

Question 6

# Treat smoking as a "test": smoker = test positive, low birth weight = condition
tab_smoke2
            
             Low weight = 1 Low weight = 0
  Smoker                 30             44
  Non-smoker             29             86
res_smoke_test <- epi.tests(tab_smoke2, conf.level = 0.95)
res_smoke_test
          Outcome +    Outcome -      Total
Test +           30           44         74
Test -           29           86        115
Total            59          130        189

Point estimates and 95% CIs:
--------------------------------------------------------------
Apparent prevalence *                  0.39 (0.32, 0.47)
True prevalence *                      0.31 (0.25, 0.38)
Sensitivity *                          0.51 (0.37, 0.64)
Specificity *                          0.66 (0.57, 0.74)
Positive predictive value *            0.41 (0.29, 0.53)
Negative predictive value *            0.75 (0.66, 0.82)
Positive likelihood ratio              1.50 (1.06, 2.13)
Negative likelihood ratio              0.74 (0.56, 0.99)
False T+ proportion for true D- *      0.34 (0.26, 0.43)
False T- proportion for true D+ *      0.49 (0.36, 0.63)
False T+ proportion for T+ *           0.59 (0.47, 0.71)
False T- proportion for T- *           0.25 (0.18, 0.34)
Correctly classified proportion *      0.61 (0.54, 0.68)
--------------------------------------------------------------
* Exact CIs
Smoking treated as a diagnostic test for low birth weight
Low weight = 1 Low weight = 0
Smoker (“test positive”) TP = 30 FP = 44
Non-smoker (“test negative”) FN = 29 TN = 86
  • TP = 30: smokers whose infants had low birth weight
  • FP = 44: smokers whose infants had normal birth weight
  • FN = 29: non-smokers whose infants had low birth weight
  • TN = 86: non-smokers whose infants had normal birth weight
Measure Estimate (95% CI)
Sensitivity 50.8% (37.5–64.1)
Specificity 66.2% (57.3–74.2)
PPV 40.5% (29.3–52.6)
NPV 74.8% (65.8–82.4)
Youden index 0.17

Why smoking is not normally considered a diagnostic test for low birth weight:

Smoking would perform poorly as a test. It identifies only about half of low-birth-weight infants (sensitivity 50.8%), and it wrongly flags about a third of normal-weight infants (specificity 66.2%). Only 40.5% of smokers’ infants had low birth weight, so a “positive” result is wrong more often than it is right. The Youden index of 0.17 is close to 0, the value for a test with no discriminatory ability.

More fundamentally, smoking isn’t a test of anything. A diagnostic test measures something that reflects whether a condition is present. Smoking is a behaviour and a risk factor that comes before the outcome and may contribute to it. It doesn’t detect low birth weight. Many infants of smokers are born at normal weight, and many low-birth-weight infants have mothers who did not smoke, because low birth weight has many other causes (hypertension, previous premature labour, maternal weight, uterine irritability and others). Birth weight is also measured directly and easily at delivery, so there is no need for a stand-in “test”. Smoking information is useful for predicting risk and targeting prevention during pregnancy, not for diagnosis.

Conceptual difference between “associated with” and “a useful diagnostic test”:

“Smoking is associated with low birth weight” is a statement about groups: low birth weight is more common among infants of smokers than among infants of non-smokers. This is measured with the RR or OR (here OR = 2.02, proportion ratio = 1.61), which compare how often the outcome occurs between exposure groups.

“Smoking is a useful diagnostic test for low birth weight” is a statement about individuals: knowing whether a mother smoked would correctly classify whether her infant has low birth weight. This is measured with sensitivity, specificity, predictive values and AUC, which describe how much the exposure groups and outcome groups overlap.

The two don’t imply each other. An exposure can double the odds of an outcome, a real and important association at population level, and still classify individuals badly, because most exposed people don’t develop the outcome and many unexposed people do. That is the case here: smoking roughly doubles the odds of low birth weight, but misclassifies about half of affected infants and a third of unaffected ones. A statistically significant association is therefore not evidence of diagnostic usefulness.

Question 7

# Create a scatterplot

ggplot(dat, aes(
  x = lwt,
  y = bwt,
  shape = factor(low)
)) +
  geom_point() +
  labs(
    x = "Maternal weight at last menstrual period (lb)",
    y = "Infant birth weight (g)",
    shape = "Low birth weight"
  )

# Create a boxplot

ggplot(dat, aes(
  x = factor(low),
  y = lwt
)) +
  geom_boxplot() +
  labs(
    x = "Low birth weight",
    y = "Maternal weight (lb)"
  )

# Maternal weight by low-birth-weight status, and correlation with birth weight
dat |>
  group_by(low) |>
  summarise(n = n(),
            median = median(lwt),
            Q1 = quantile(lwt, 0.25),
            Q3 = quantile(lwt, 0.75),
            min = min(lwt),
            max = max(lwt)) |>
  kable(digits = 1)
low n median Q1 Q3 min max
0 130 123.5 113 147 85 250
1 59 120.0 104 130 80 200
cor(dat$lwt, dat$bwt)                        # Pearson
[1] 0.1857333
cor(dat$lwt, dat$bwt, method = "spearman")   # Spearman (less sensitive to outliers)
[1] 0.2488882

The relationship between maternal weight and low birth weight

The scatterplot shows a weak positive relationship between maternal weight and infant birth weight: heavier mothers tend to have slightly heavier babies, but the points are widely scattered (Pearson r = 0.19, Spearman ρ = 0.25). Low-birth-weight infants appear across almost the whole range of maternal weights, although they are somewhat more concentrated among lighter mothers.

The boxplot shows that mothers of low-birth-weight infants weighed slightly less (median 120 lb, IQR 104–130, range 80–200) than mothers of normal-weight infants (median 123.5 lb, IQR 113–147, range 85–250). The difference in medians is small and the two distributions overlap substantially. Maternal weight is right-skewed in both groups, with a few heavy mothers (up to 250 lb) appearing as outliers. Lower maternal weight is therefore associated with low birth weight, but the overlap suggests that maternal weight on its own will not separate the two groups well.

Question 8

dat <- dat |>
  mutate(
    low_lwt = ifelse(lwt < 120, 1, 0)
  )

#Create the diagnostic table:
# Test-positive / outcome-positive in the top-left
tab_lwt <- table(
  factor(dat$low_lwt, levels = c(1, 0), labels = c("lwt < 120 lb", "lwt ≥ 120 lb")),
  factor(dat$low,     levels = c(1, 0), labels = c("Low weight = 1", "Low weight = 0"))
)

tab_lwt
              
               Low weight = 1 Low weight = 0
  lwt < 120 lb             29             46
  lwt ≥ 120 lb             30             84
res_lwt <- epi.tests(tab_lwt, conf.level = 0.95)
res_lwt
          Outcome +    Outcome -      Total
Test +           29           46         75
Test -           30           84        114
Total            59          130        189

Point estimates and 95% CIs:
--------------------------------------------------------------
Apparent prevalence *                  0.40 (0.33, 0.47)
True prevalence *                      0.31 (0.25, 0.38)
Sensitivity *                          0.49 (0.36, 0.63)
Specificity *                          0.65 (0.56, 0.73)
Positive predictive value *            0.39 (0.28, 0.51)
Negative predictive value *            0.74 (0.65, 0.81)
Positive likelihood ratio              1.39 (0.98, 1.97)
Negative likelihood ratio              0.79 (0.59, 1.04)
False T+ proportion for true D- *      0.35 (0.27, 0.44)
False T- proportion for true D+ *      0.51 (0.37, 0.64)
False T+ proportion for T+ *           0.61 (0.49, 0.72)
False T- proportion for T- *           0.26 (0.19, 0.35)
Correctly classified proportion *      0.60 (0.52, 0.67)
--------------------------------------------------------------
* Exact CIs
Diagnostic 2 × 2 table for maternal weight < 120 lb
Low weight = 1 Low weight = 0 Total
lwt < 120 lb (test +) TP = 29 FP = 46 75
lwt ≥ 120 lb (test −) FN = 30 TN = 84 114
Total 59 130 189
Diagnostic accuracy of maternal weight < 120 lb for low birth weight
Measure Formula Estimate (95% CI)
Sensitivity 29 / 59 49.2% (35.9–62.5)
Specificity 84 / 130 64.6% (55.8–72.8)
PPV 29 / 75 38.7% (27.6–50.6)
NPV 84 / 114 73.7% (64.6–81.5)

Interpretation:

Sensitivity (49.2%, 95% CI 35.9–62.5%): Among infants born with low birth weight, about half had mothers who weighed less than 120 lb before pregnancy. The threshold therefore misses about half of low-birth-weight infants: 30 of the 59 had mothers weighing 120 lb or more and would be classed as “test negative”.

Specificity (64.6%, 95% CI 55.8–72.8%): Among infants with normal birth weight, about 65% had mothers weighing 120 lb or more. The remaining 35% (46 infants) had mothers under 120 lb despite normal birth weight, so they would be wrongly flagged as at risk.

PPV (38.7%, 95% CI 27.6–50.6%): Among mothers weighing less than 120 lb, about 39% had a low-birth-weight infant. A “positive” result is wrong more often than right: most low-weight mothers still had normal-weight babies.

NPV (73.7%, 95% CI 64.6–81.5%): Among mothers weighing 120 lb or more, about 74% had a normal-weight infant. Put the other way, about 1 in 4 of these “test negative” mothers still had a low-birth-weight baby. The NPV looks better than the other measures partly because most infants in the sample (69%) had normal birth weight anyway. Predictive values depend on prevalence, so these figures would change in a population where low birth weight is less common, as it usually is outside this sample (31%).

Overall: At 120 lb, maternal weight is a weak classifier of low birth weight. Sensitivity and specificity are both modest, and their sum (Youden J = 0.49 + 0.65 − 1 ≈ 0.14) is close to 0, the value for a test with no discriminating ability. Lower maternal weight is related to low birth weight, but the two groups overlap too much for a single cut-off to separate them well. The confidence intervals are also wide, reflecting the small number of low-birth-weight infants (59). These results are very similar to smoking in Question 6 (sensitivity about 51%, specificity about 66%).

Question 9: Changing the threshold

thresholds <- c(90, 100, 110, 120, 130, 140, 150)

thr_lwt <- lapply(thresholds, function(t) {
  pos <- dat$lwt < t                      # test positive = weight BELOW threshold
  TP <- sum(pos  & dat$low == 1);  FP <- sum(pos  & dat$low == 0)
  FN <- sum(!pos & dat$low == 1);  TN <- sum(!pos & dat$low == 0)
  data.frame(threshold = t, TP, FP, FN, TN,
             sensitivity = TP / (TP + FN),
             specificity = TN / (TN + FP),
             PPV = TP / (TP + FP),
             NPV = TN / (TN + FN))
}) |> bind_rows() |>
  mutate(youden = sensitivity + specificity - 1)

kable(thr_lwt, digits = 3)
threshold TP FP FN TN sensitivity specificity PPV NPV youden
90 3 1 56 129 0.051 0.992 0.750 0.697 0.043
100 9 10 50 120 0.153 0.923 0.474 0.706 0.076
110 21 21 38 109 0.356 0.838 0.500 0.741 0.194
120 29 46 30 84 0.492 0.646 0.387 0.737 0.138
130 38 72 21 58 0.644 0.446 0.345 0.734 0.090
140 47 92 12 38 0.797 0.292 0.338 0.760 0.089
150 50 98 9 32 0.847 0.246 0.338 0.780 0.094

Sensitivity/specificity plot

ggplot(thr_lwt, aes(x = threshold)) +
  geom_line(aes(y = sensitivity, colour = "Sensitivity"), linewidth = 1) +
  geom_point(aes(y = sensitivity, colour = "Sensitivity"), size = 2) +
  geom_line(aes(y = specificity, colour = "Specificity"), linewidth = 1) +
  geom_point(aes(y = specificity, colour = "Specificity"), size = 2) +
  geom_line(aes(y = youden, colour = "Youden J"), linewidth = 1, linetype = "dashed") +
  scale_y_continuous(labels = scales::percent, limits = c(0, 1)) +
  labs(
    title = "Sensitivity and specificity by maternal weight threshold",
    x = "Threshold: maternal weight < x lb counts as test positive",
    y = "Value",
    colour = ""
  ) +
  theme_minimal()

Optimal threshold from pROC

roc_lwt <- roc(dat$low, dat$lwt, levels = c(0, 1), direction = ">")

coords(roc_lwt, "best", best.method = "youden",
       ret = c("threshold", "sensitivity", "specificity", "youden"))
  threshold sensitivity specificity   youden
1       111   0.4237288   0.7846154 1.208344

Interpretation:

Because a lower weight counts as a positive result, raising the threshold classifies more mothers as positive. As the threshold increases from 90 to 150 lb, sensitivity rises (from 5% to 85%) and specificity falls (from 99% to 25%). At a high threshold, almost every mother counts as positive, so most low-birth-weight infants are caught, but most normal-weight infants are wrongly flagged too. At a low threshold the reverse happens: very few mothers count as positive, so false positives are rare, but most low-birth-weight infants are missed.

This trade-off happens because maternal weight distributions in the two outcome groups overlap heavily. Moving the threshold doesn’t separate the groups; it only changes which type of error you accept. Each mother reclassified from negative to positive increases either TP (raising sensitivity) or FP (lowering specificity).

Among the fixed thresholds, 110 lb has the highest Youden index (J = 0.19), with sensitivity 35.6% and specificity 83.8%. The pROC search across all observed values gives a similar optimum, with two thresholds tied: about 106 lb (sensitivity 33.9%, specificity 86.9%) and about 111 lb (sensitivity 42.4%, specificity 78.5%), both with J ≈ 0.21. The 120 lb threshold from Question 8 performs worse (J = 0.14). Even the best threshold is a weak classifier, though: J values close to 0 at every threshold show that maternal weight has little discriminating ability on its own. The “optimal” threshold was also chosen and evaluated on the same 189 pregnancies, so it is likely to look better here than it would in new data. The PPV is highest at 90 lb (75%), but that figure rests on only 4 test-positive mothers and is very unreliable.

Question 10

roc_lwt <- roc(dat$low, dat$lwt, levels = c(0, 1),
               direction = ">", ci = TRUE)

auc(roc_lwt)
Area under the curve: 0.6131
ci.auc(roc_lwt)   # DeLong 95% CI
95% CI: 0.5245-0.7017 (DeLong)
plot(roc_lwt, print.auc = TRUE, legacy.axes = TRUE,
     main = "ROC curve: maternal weight for low birth weight")

AUC = 0.61 (95% CI 0.52–0.70, DeLong)

What the AUC measures

The AUC is the probability that, for a randomly chosen pair of one low-birth-weight infant and one normal-weight infant, the low-birth-weight infant’s mother has the lower pre-pregnancy weight. It summarises how well maternal weight discriminates between the two groups across all possible thresholds, without choosing one. An AUC of 0.5 means no better than chance, and 1.0 means perfect separation.

Interpretation:

The AUC of 0.61 means that in about 61% of random pairs, the mother of the low-birth-weight infant weighed less, only somewhat better than a coin toss (50%). Maternal weight therefore has poor discriminative ability for low birth weight. The 95% CI (0.52–0.70) is wide, and its lower limit sits just above 0.5, so the data are consistent with anything from almost no discrimination to modest discrimination. The ROC curve stays close to the diagonal reference line, which matches the low Youden indices in Question 9.

This does not mean maternal weight is irrelevant. Lower weight is associated with low birth weight, and it may still help as one component of a multivariable risk model alongside smoking, hypertension and previous premature labour. On its own, however, it is not useful for classifying individual pregnancies.

Final synthesis

Across both datasets, association and diagnostic accuracy behaved as separate properties. In Dataset 1, RBC storage duration showed no association with recurrence (RR 0.98, OR 0.97), while preoperative PSA showed moderate discrimination (AUC 0.73). In Dataset 2, smoking showed a clear positive association with low birth weight (OR 2.02, 95% CI 1.08–3.78) but would be a poor classifier (sensitivity 51%, specificity 66%), and maternal weight showed only poor discrimination (AUC 0.61). A variable can therefore be associated with an outcome without being useful for classifying individuals, because RR and OR compare groups, whereas sensitivity, specificity and AUC depend on how much the distributions in the two outcome groups overlap.

Both analyses also showed that diagnostic performance depends on the threshold chosen. Changing the cut-off always traded sensitivity against specificity, and the Youden index identifies only the threshold that weights both errors equally. PPV and NPV additionally depend on prevalence, so they would not transfer directly to other populations. The ROC curve and AUC summarise discrimination without fixing a threshold.

The main limitations are shared: both datasets are observational and small, all estimates are crude and unadjusted for confounding, confidence intervals are wide, and optimal thresholds were chosen and evaluated on the same data. Dataset 1 also has variable follow-up and censoring, so its proportions are not true risks. Dataset 2 is cross-sectional with an unusually high prevalence of low birth weight, so its proportion ratio should not be read as a true risk ratio. Finally, using bwt to predict low illustrated that a “test” derived from the reference standard itself gives meaningless, perfect accuracy.

Session information

sessionInfo()
R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26300)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=Spanish_Spain.utf8  LC_CTYPE=Spanish_Spain.utf8   
[3] LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C                  
[5] LC_TIME=Spanish_Spain.utf8    

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] MASS_7.3-65       knitr_1.52        ggplot2_4.0.4     dplyr_1.2.1      
[5] pROC_1.19.1       epiR_3.0.1        survival_3.8-6    medicaldata_0.2.0

loaded via a namespace (and not attached):
 [1] gtable_0.3.6            xfun_0.61               htmlwidgets_1.6.4      
 [4] lattice_0.22-9          vctrs_0.7.3             tools_4.6.1            
 [7] generics_0.1.4          tibble_3.3.1            proxy_0.4-29           
[10] pkgconfig_2.0.3         Matrix_1.7-5            KernSmooth_2.23-26     
[13] data.table_1.18.6.1     RColorBrewer_1.1-3      S7_0.2.2               
[16] uuid_1.2-2              lifecycle_1.0.5         flextable_0.10.4       
[19] compiler_4.6.1          farver_2.1.2            textshaping_1.0.5      
[22] fontquiver_0.2.1        fontLiberation_0.1.0    htmltools_0.5.9        
[25] class_7.3-23            yaml_2.3.12             pillar_1.11.1          
[28] tidyr_1.3.2             openssl_2.4.2           classInt_0.4-11        
[31] BiasedUrn_2.0.12        fontBitstreamVera_0.1.1 tidyselect_1.2.1       
[34] zip_3.0.2               digest_0.6.39           sf_1.1-3               
[37] pander_0.6.6            purrr_1.2.2             labeling_0.4.3         
[40] splines_4.6.1           fastmap_1.2.0           grid_4.6.1             
[43] cli_3.6.6               magrittr_2.0.5          e1071_1.7-17           
[46] withr_3.0.3             gdtools_0.5.3           scales_1.4.0           
[49] lubridate_1.9.5         timechange_0.4.0        rmarkdown_2.32         
[52] officer_0.7.8           otel_0.2.0              askpass_1.2.1          
[55] ragg_1.5.2              zoo_1.9-1               evaluate_1.0.5         
[58] rlang_1.3.0             Rcpp_1.1.2              glue_1.8.1             
[61] DBI_1.3.0               xml2_1.6.0              rstudioapi_0.19.0      
[64] jsonlite_2.0.0          R6_2.6.1                systemfonts_1.3.2      
[67] units_1.0-1