tpc: Perform causal discovery using the temporal PC algorithm...

View source: R/tpc.R

tpcR Documentation

Perform causal discovery using the temporal PC algorithm (TPC)

Description

Perform causal discovery using the temporal PC algorithm (TPC)

Usage

tpc(
  data = NULL,
  order,
  sparsity = 10^(-1),
  test = regTest,
  suffStat = NULL,
  method = "stable.fast",
  methodNA = "none",
  methodOri = "conservative",
  output = "tpdag",
  varnames = NULL,
  ...
)

Arguments

data

A data.frame with data. All variables should be assigned to exactly one period by prefixing them with the period name (see example below).

order

A character vector with period-prefixes in their temporal order (see example below).

sparsity

The sparsity level to be used for independence testing (i.e. significance level threshold to use for each test).

test

A procedure for testing conditional independence. The default, regTest uses a regression-based information loss test. Another available option is corTest which tests for vanishing partial correlations. User supplied functions may also be used, see details below about the required syntax.

suffStat

Sufficient statistic. If this argument is supplied, the sufficient statistic is not computed from the inputted data. The format and contents of the sufficient statistic depends on which test is being used.

method

Which method to use for skeleton construction, must be "stable", "original", or "stable.fast" (the default). See skeleton for details.

methodNA

Method for handling missing information (NA values). Must be one of "none" (default, an error is thrown if NAs are present), "cc" (complete case analysis, deletes all observations that have any NA values), or "twd" (test wise deletion, omits observations with missing information test-by-test) (further details below).

methodOri

Method for handling conflicting separating sets when orienting edges. Currently only the conservative method is available.

output

One of "tpdag", "tskeleton" or "pcAlgo". If "tskeleton", a temporal skeleton is constructed and outputted, but the edges are not directed. If "tpdag" (the default), a the edges are directed, resulting in a temporal partially directed acyclic graph (TPDAG). If "pcAlgo" the TPDAG is outputted as the object class pcAlgo-class from the pcalg package. This is intended for compatability with tools from that package.

varnames

A character vector of variable names. It only needs to be supplied if the data argument is not used, and data are hence passed exclusively through the suffStat argument.

...

Further optional arguments which are passed to skeleton for the skeleton constructing phase.

Details

Note that all independence test procedures implemented in the pcalg package may be used, see pc.

The methods for handling missing information require that the data, rather than the suffStat argument is used for inputting data; the latter assumes no missing information and hence always sets methodNA = "none". If the test is corTest, test-wise deletion is performed when computing the sufficient statistic (correlation matrix) (so for each pair of variables, only complete cases are used). If the test is regTest, test-wise deletion is performed for each conditional independence test instead.

Value

A tpdag or tskeleton object. Both return types are S3 objects, i.e., lists with entries: $tamat (the estimated adjacency matrix), $order (character vector with the order, as inputted to this function), $psi (the significance level used for testing), and $ntests (the number of tests conducted).

Examples

#TPC on included example data, use sparsity psi = 0.01, default test (regression-based
#information loss):
data(tpcExample)
tpc(tpcExample, order = c("child", "youth", "oldage"), sparsity = 0.01)


#TPC on included example data, use sparsity psi = 0.01, use test for vanishing partial
# correlations:
data(tpcExample)
tpc(tpcExample, order = c("child", "youth", "oldage"), sparsity = 0.01,
test = corTest)


#TPC on another simulated data set

#Simulate data
set.seed(123)
n <- 500
child_x <- rnorm(n)^2
child_y <- 0.5*child_x + rnorm(n)
child_z <- sample(c(0,1), n, replace = TRUE,
                  prob = c(0.3, 0.7))

adult_x <- child_x + rnorm(n)
adult_z <- as.numeric(child_z + rnorm(n) > 0)
adult_w <- 2*adult_z + rnorm(n)
adult_y <- 2*sqrt(child_x) + adult_w^2 + rnorm(n)

simdata <- data.frame(child_x, child_y, child_z,
                      adult_x, adult_z, adult_w,
                      adult_y)

#Define order
simorder <- c("child", "adult")

#Perform TPC with sparsity psi = 0.001
results <- tpc(simdata, order = simorder, sparsity = 10^(-3))


causalDisco documentation built on Jan. 20, 2026, 5:09 p.m.

Related to tpc in causalDisco...