---
title: "Tissue Classification"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Tissue Classification}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
resource_files:
- figures/logo.svg
- figures/logo-dark.svg
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 7,
fig.height = 4.5,
message = FALSE,
warning = FALSE
)
```
```{r}
library(libscanR)
```
This vignette uses synthetic spectra. It illustrates exploratory calculations;
it does not validate tissue identity, diagnostic thresholds or clinical performance.
## Example: 5 simulated tissue types
```{r data}
ds <- ls_example_data("tissue")
table(ds$sample_info$tissue)
```
## Exploratory analysis with PCA
```{r pca, fig.alt="PC1 versus PC2 scores for 50 fictional spectra, coloured by five teaching tissue labels with group ellipses; this exploratory separation does not validate tissue identity."}
pca <- ls_pca(ds, n_components = 4)
ls_plot_pca(pca, color_by = "tissue")
```
```{r scree, fig.alt="Explained variance for four principal components of the fictional spectra: bars show each component percentage and the line shows cumulative percentage."}
ls_plot_scree(pca)
```
## Identifying discriminating elements
```{r discr}
d <- ls_tissue_discriminate(ds, "tissue",
group_a = "bone",
group_b = "muscle")
head(d[!is.na(d$element) & d$significant, c("wavelength_nm",
"element",
"fold_change",
"fdr")], 15)
```
`fold_change` is log2(mean intensity in group A / mean intensity in group B).
Channels with nonpositive means have no defined logarithmic fold change and retain
`NA`. Element annotations are wavelength candidates; inspect `candidate_count`
and instrument resolution before attributing a difference to an element.
## PLS-DA classification
The confusion matrix below uses held-out sample-level predictions. Its basis is
recorded in `evaluation_basis`; `validation = "none"` instead reports optimistic
training performance. Patient- or site-level separation must be designed outside
this illustrative sample-level evaluation.
```{r plsda, eval = requireNamespace("pls", quietly = TRUE), fig.alt="Cross-validated PLS-DA confusion matrix for five fictional teaching labels. Columns are predicted labels, rows are observed labels, and each tile shows its sample count; this is not clinical validation."}
set.seed(21)
plsda <- ls_plsda(ds, grouping = "tissue",
n_components = 5, validation = "CV")
plsda
ls_plot_plsda(plsda, type = "confusion")
```
## Rule-based classification with elemental ratios
These rules produce relative heuristic scores, not calibrated probabilities.
The output separates `score_type` and `status`; missing line coverage or undefined
ratios produce an unclassified row. The previous `confidence` column has been
replaced. A PLS-DA prediction score is likewise an unrestricted discriminant score.
```{r ratio}
ls_tissue_classify(ds[1:10], method = "ratio")
```