## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

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

## ----simulate-----------------------------------------------------------------
library(causalASCEND)
set.seed(1)
sim <- simulateTwoTier(n = 2000, nBackground = 20, nForeground = 15,
    r2 = 0.7, sparsity = 0.9, pCross = 0.2, effect = 0.9)
dim(sim$background)
dim(sim$foreground)

## ----run----------------------------------------------------------------------
res <- ascend(sim$background, sim$foreground)
res

## ----edges--------------------------------------------------------------------
edges <- ancestralEdges(res)
head(edges)
table(edges$relation, edges$rule)

## ----all-relations------------------------------------------------------------
all <- ancestralEdges(res, include = c("ancestor", "non-descendant",
    "unrelated", "unresolved"))
table(all$relation)

## ----matrix-------------------------------------------------------------------
round(ancestralMatrix(res)[1:6, 1:6], 1)

## ----evaluate-----------------------------------------------------------------
evaluateAncestral(res, trueAncestral(sim))

## ----se-----------------------------------------------------------------------
library(SummarizedExperiment)
geno <- SummarizedExperiment(assays = list(dosage = sim$background))
expr <- SummarizedExperiment(assays = list(logcounts = sim$foreground))
res_se <- ascend(geno, expr, backgroundAssay = "dosage",
    foregroundAssay = "logcounts")
identical(ancestralMatrix(res_se), ancestralMatrix(res))

## ----mae, eval = requireNamespace("MultiAssayExperiment", quietly = TRUE)-----
library(MultiAssayExperiment)
mae <- MultiAssayExperiment(experiments = list(snp = sim$background,
    rna = sim$foreground))
res_mae <- ascendMultiAssay(mae, background = "snp", foreground = "rna")
identical(ancestralMatrix(res_mae), ancestralMatrix(res))

## ----settings-----------------------------------------------------------------
strict <- ascend(sim$background, sim$foreground, minVotes = 2)
table(ancestralEdges(strict)$relation)

## ----cost---------------------------------------------------------------------
st <- ascendStats(res)
st[c("nTests", "nPairTests", "nWitnessTests", "nBlanketTests",
    "meanPairCondSize", "maxPairCondSize", "nSweeps")]

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

