## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "##",
  fig.width = 6, 
  fig.height = 4
)
library(DistributionalDDA)

## ----Rintrospection-----------------------------------------------------------
library(DistributionalDDA)
data(DTD)
dim(DTD)
names(DTD)
head(DTD)

## ----Rintrospection2----------------------------------------------------------
head(DTD$data[[1]])

## ----Rintrospection3----------------------------------------------------------
head(DTD$shapePDF)
length(DTD$shapePDF)
plot(DTD$shapePDF[[1]])

## ----DFsubsetting-------------------------------------------------------------
DTD["shapePDF"]

## ----getSubsetter-------------------------------------------------------------
theName = format(DTD$shapePDF)[[1]]
theName
get(theName,ListDataEnv)

## ----B2Unif01-----------------------------------------------------------------
B2Unif01

## ----defaultUnifBases---------------------------------------------------------
B2Unif01Basis5
plot(B2Unif01Basis5,as="PDF",main="PDF 5-basis-elements", col=2:6)
plot(B2Unif01Basis5,as="logPDF",main="logDensity 5-basis-elements", col=2:6)
B2Unif01Basis7
plot(B2Unif01Basis7,as="PDF",main="PDF 7-basis-elements", col=2:8)
plot(B2Unif01Basis7,as="logPDF",main="logDensities 7-basis-elements", col=2:8)

## ----parallelSummaries1-------------------------------------------------------
sapply(DTD$data, \(x) summary(x$shape))

## ----parallelSummaries2-------------------------------------------------------
head(DTD$data) %.% summary(.$shape)

## ----parallelSummaries3-------------------------------------------------------
# DTD$data %.% summary(.$shape) |> simplify2array() |> t() # works as well
DTD$data %..% summary(.$shape) |> t()

## ----parallelSummaries4-------------------------------------------------------
DTD$data %..% mean(.$shape) 
DTD$data %..% range(.$shape)

## ----StdRefMeasUnif01---------------------------------------------------------
StdReferenceMeasureUnif01

## ----StdRefMeasN01------------------------------------------------------------
StdReferenceMeasureN01

## ----defaultB2spaces----------------------------------------------------------
B2Unif01
B2N01

## ----stats2constructB2--------------------------------------------------------
# hist(unlist(lapply(DTD$data, function(x) x$shape))) # also works
DTD$data %.% shape |> unlist() |> hist()

## ----model2constructB2--------------------------------------------------------
rbeta(10000,shape1=8,shape2=0.5) |> hist()

## ----myRef2constructB2--------------------------------------------------------
myRM = ReferenceMeasureTransformed("pbeta",shape1=8,shape2=0.5)

## ----constructB2--------------------------------------------------------------
myB2 = BayesSpace(myRM)
myB2

## ----constructB2basis---------------------------------------------------------
myBasis = BayesSpaceBasis(myB2, nbasis=7)

## ----constructB2basisPlot-----------------------------------------------------
plot(myBasis, as="is", col=2:8)

## -----------------------------------------------------------------------------
plot(myBasis, as="logDensity", close=FALSE)

## -----------------------------------------------------------------------------

## ----BDataSamples0------------------------------------------------------------
shapeSampleList = DTD$data %.% shape
shapeSampleList %.% head(.)

## ----BDataSamples-------------------------------------------------------------
shapeSamples = BDataSamples(DTD$data %.% shape, basis=myBasis)
shapeSamples %..% head(.) 

## ----dotPlot------------------------------------------------------------------
plot(shapeSamples)

## ----BDataPDFs----------------------------------------------------------------
shapePDFs = BDataPDFs(DTD$shapePDF, basis=myBasis)
head(shapePDFs)

## ----BDataPDFsPlot------------------------------------------------------------
DTD["shapePDFs"] = as.data.frame(shapePDFs)
DTD
DTD$shapePDFs |> head() # to save space
plot(DTD$shapePDFs)

## ----BDataPDFCenters----------------------------------------------------------
shapeDistMean = mean(DTD$shapePDFs)
shapeDistMedian = median(DTD$shapePDFs)
plot(DTD$shapePDFs)
plot(shapeDistMean, add=TRUE, lwd=4, col="green")
plot(shapeDistMedian, add=TRUE, lty=2, lwd=3, col="red")

## ----BDataPDFsMVar, eval=FALSE------------------------------------------------
# mvar(DTD$shapePDFs)

## ----classes------------------------------------------------------------------
class(DTD$shapePDFs)
class(shapeSamples)

## ----as.Histograms------------------------------------------------------------
shapeHists = as.BDataFrequencyHistograms(shapeSamples)
class(shapeHists)
plot(shapeHists)

## ----convertersGraph, fig.height=6, fig.width=8, echo=FALSE-------------------
asfuns = ls(envir = asNamespace("DistributionalDDA"), pattern = "^as\\.")
aslist = strsplit(asfuns, split = ".", fixed = TRUE)
aslist = aslist[sapply(aslist, \(x) length(x)==3)]
asfuns = sapply(aslist, \(x)x[-1]) |> t()
asfuns = asfuns[grep("BData", asfuns[,1]),]

library(ggraph)
library(igraph)

g <- graph_from_data_frame(data.frame(from=asfuns[,2], to=asfuns[,1]), directed = TRUE)

ggraph(g, layout = "auto") +
  geom_edge_link(arrow = arrow(length = unit(3, "mm")),
                 end_cap = circle(5, "mm"),
                 colour  = "gray40") +
  geom_node_point(size = 8, colour = "lightblue") +
  geom_node_text(aes(label = name), size = 4) +
  theme_void()

## ----as.Splines---------------------------------------------------------------
shapeSplineCoefs = as.BDataSplines(shapePDFs)
getBayesSpaceBasis(shapePDFs)

## ----splinePlots--------------------------------------------------------------
plot(shapeSplineCoefs, col=2:6, lty=1:4, xlab="basis coefficients")

## ----lmSplines----------------------------------------------------------------
l1 = lm(shapeSplineCoefs[,-1]~shapeSplineCoefs[,1])
l1

## ----lmSplinesR2--------------------------------------------------------------
summary(l1) |> sapply(\(x)x$r.squared)

## ----dendroSplines------------------------------------------------------------
hc = shapeSplineCoefs |> dist() |> hclust()
plot(hc)

## ----expectation, eval=FALSE--------------------------------------------------
# expectation(measure, f, ..., method)

## ----otherOperators, eval=FALSE-----------------------------------------------
# CovarianceOperator(measure, X, Y)
# scalarProduct(x, y, space)

