6.1 Reconstruction error using fixed healthy-image centers
We evaluate how closely the assigned centroids approximate each
original pixel intensity. We compute mean absolute error (MAE), mean
squared error (MSE), and root mean squared error (RMSE):
\[
\mathrm{MAE}=\frac{1}{n}\sum_{i=1}^n|x_i-\hat{x}_i|,\quad
\mathrm{MSE}=\frac{1}{n}\sum_{i=1}^n(x_i-\hat{x}_i)^2,\quad
\mathrm{RMSE}=\sqrt{\mathrm{MSE}}.
\]
We compute the same metrics for both images using the same five
healthy-image centers. We normalize by the number of pixels so the
results are comparable despite unequal image dimensions.
intensity_metrics <- function(actual, labels, centers) {
predicted <- centers[labels]
errors <- actual - predicted
c(MAE = mean(abs(errors)),
MSE = mean(errors^2),
RMSE = sqrt(mean(errors^2)),
WCSS = sum(errors^2))
}
healthy_error <- intensity_metrics(healthy_vector, healthy_labels,
ordered_centers)
tumor_error <- intensity_metrics(tumor_vector, tumor_labels,
ordered_centers)
# The healthy fit should reproduce its own reported within-cluster SSE.
stopifnot(isTRUE(all.equal(unname(healthy_error["WCSS"]),
unname(healthy_kmeans$tot.withinss),
tolerance = 1e-7)))
error_table <- data.frame(
Image = c("Healthy (training)", "Tumor (transfer)"),
MAE = c(healthy_error["MAE"], tumor_error["MAE"]),
MSE = c(healthy_error["MSE"], tumor_error["MSE"]),
RMSE = c(healthy_error["RMSE"], tumor_error["RMSE"]),
WCSS = c(healthy_error["WCSS"], tumor_error["WCSS"])
)
knitr::kable(error_table, digits = 6,
caption = "Fixed-centroid intensity reconstruction errors")
Fixed-centroid intensity reconstruction errors
| Healthy (training) |
0.022047 |
0.000826 |
0.028746 |
302.1291 |
| Tumor (transfer) |
0.029319 |
0.001573 |
0.039661 |
459.8580 |
Code explanation. We replace each pixel with its
assigned centroid and calculate the residual intensity. We verify that
the healthy-image total squared error matches the K-Means model’s
reported within-cluster sum of squares. We report MAE, MSE, and RMSE per
pixel, while noting that total WCSS depends on image size.
Output interpretation. We obtain a healthy-image
RMSE of 0.02875 and a tumor-image RMSE of 0.03966. We interpret any
difference as a change in the numerical fit of the same
intensity codebook to two different images. We do not interpret
a lower reconstruction error as evidence of better tumor identification
or a higher error as proof of pathology.
6.2 Examine how far observed intensities lie from their assigned
centroids
We compare the distribution of absolute quantization residuals. We
also report the fraction of pixels outside the span of the reference
centroids. We treat the latter as a descriptive indicator of how much of
each image lies beyond the fitted center range—not as an anomaly
detector.
healthy_abs_error <- abs(healthy_vector - ordered_centers[healthy_labels])
tumor_abs_error <- abs(tumor_vector - ordered_centers[tumor_labels])
residual_summary <- data.frame(
Image = c("Healthy", "Tumor"),
Median_Abs_Error = c(median(healthy_abs_error), median(tumor_abs_error)),
P90_Abs_Error = c(unname(quantile(healthy_abs_error, 0.90)),
unname(quantile(tumor_abs_error, 0.90))),
Beyond_Centroid_Range_Percent = 100 * c(
mean(healthy_vector < min(ordered_centers) |
healthy_vector > max(ordered_centers)),
mean(tumor_vector < min(ordered_centers) |
tumor_vector > max(ordered_centers)))
)
knitr::kable(residual_summary, digits = 5,
caption = "Absolute reconstruction residual diagnostics")
Absolute reconstruction residual diagnostics
| Healthy |
0.02085 |
0.04223 |
25.6356 |
| Tumor |
0.02189 |
0.05540 |
46.4382 |
breaks_common <- seq(0, 1, by = 0.01)
hist(healthy_abs_error, breaks = breaks_common, freq = FALSE,
col = grDevices::adjustcolor("gray35", alpha.f = 0.5),
border = NA, xlim = c(0, max(c(healthy_abs_error, tumor_abs_error))),
main = "Absolute Intensity Residuals", xlab = "Absolute residual")
hist(tumor_abs_error, breaks = breaks_common, freq = FALSE,
col = grDevices::adjustcolor("gray75", alpha.f = 0.6),
border = NA, add = TRUE)
legend("topright", legend = c("Healthy", "Tumor"),
fill = c("gray35", "gray75"), bty = "n")
Output interpretation. We use the residual
distributions to assess how efficiently the healthy-image intensity
codebook summarizes each image. We recognize that the two samples have
different backgrounds and grayscale frequencies; a distribution shift is
not automatically a tumor-specific signal.