## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#",
  fig.align = "center",
  fig.width = 7,
  fig.height = 4,
  message   = FALSE,
  warning   = FALSE
)

## ----eval=FALSE---------------------------------------------------------------
# if (!require("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# 
# BiocManager::install("wavFeatExt")

## ----installation-------------------------------------------------------------
library(wavFeatExt)

## ----simulate-data------------------------------------------------------------
# One simulated data set with moderate dimension

sim_dat <- simulateCNA(
  n.obs   = 40,
  p       = 64,
  n.sim   = 1,
  n.block = 8,
  verbose = FALSE
)


## ----response-factor----------------------------------------------------------
X <- sim_dat[[1]]
n <- nrow(X)

y <- factor(rep(c("Subtype1", "Subtype2"), each = n / 2))
table(y)


## ----feature-extraction-------------------------------------------------------
# Extract wavelet detail coefficients (differences)

det_coef <- wavFeatExt(sim_dat, type = "detail")

# Extract wavelet scaling coefficients (averages)

sca_coef <- wavFeatExt(sim_dat, type = "scaling")

length(det_coef)          # number of simulated data sets
length(det_coef[[1]])     # number of scales for detail coefficients
dim(det_coef[[1]][[1]])   # samples x windows at the first scale


## ----wavelet-visualitation----------------------------------------------------

# Take one sample (row) from the simulated data

x1 <- X[1, ]

# Non-decimated Haar transform (detail coefficients)

nh_detail <- nhwt(x1, type = "detail")
nh_scaling <- nhwt(x1, type = "scaling")

# Plot coefficients by scale
plot(x1, type="l", xlab="", ylab="", main="Simulated CNA data")
plot(nh_detail, coef = "detail", type = "by.level", scale = "all")
plot(nh_scaling, coef = "scaling", type = "by.level", scale = "all")


## ----classification-----------------------------------------------------------

# Binary response (for example, first 20 vs last 20 samples)
y <- factor(c(rep("Group1", 20), rep("Group2", 20)))

# Perform classification using Lasso
res_KNN <- classifyWavFeatExt(sim_dat, y, det=det_coef, sca=sca_coef,
                          method = "KNN", k = 5, ite = 2)


## ----apply-PCAICA-------------------------------------------------------------
# Obtain PCA/ICA features

pca_feat <- getPca(sim_dat, k = 5)
ica_feat <- getIca(sim_dat, k = 5)

# Classification using PCA and ICA features

res_pcaica <- classifyPcaIca(
  sim_dat,
  y,
  pca_feat,
  ica_feat,
  method = "KNN",
  k = 5,
  ite = 1
)


## ----plot-results-------------------------------------------------------------

## Misclassification error per scale (and segmented baseline)

plot(res_KNN, type = "CE", ylab = "Misclassification error")

## AUC per scale (and segmented baseline)

plot(res_KNN, type = "AUC", ylab = "Area under ROC curve")


## ----plot-pcaica-classification-----------------------------------------------
plot(res_pcaica, type = "CE")   # or type = "AUC"

## ----session-info-------------------------------------------------------------
sessionInfo()

