treepcor: treepcor: correlation from tree

View source: R/treepcor.R

treepcorR Documentation

treepcor: correlation from tree

Description

A tree with two kind of nodes, parents and children. The parents are nodes with children. The children are nodes with no children. This is used to model correlation matrices, where parents represent latent variables, and children represent the variables of interest. A tree is defined from a formula for each parent.

Usage

treepcor(...)

## S3 method for class 'treepcor'
print(x, ...)

## S3 method for class 'treepcor'
summary(object, ...)

## S3 method for class 'treepcor'
dim(x, ...)

drop1(object)

## S3 method for class 'treepcor'
plot(x, y, ...)

## S3 method for class 'treepcor'
prec(model, ...)

etreepcor2precision(d.el)

## S3 method for class 'treepcor'
vcov(object, ...)

etreepcor2variance(d.el)

## S3 method for class 'treepcor'
cgeneric(model, ...)

Arguments

...

used to pass theta as a numeric vector with the model parameters

x

treepcor object

object

treepcor

y

not used

model

treepcor

d.el

list of the first n edges of a treepcor.

Details

In the formula, the left side are parent variables, and the right side include all the children and parents that are also children. The children variables are those with an ancestor (parent), and are identified as c1, ..., cn, where n is the total number of children variables. The parent variables are identified as p1, ..., pm, where the m is the number of parent variables. The main parent (first) should be identified as p1. Except p1 all the other parent variables have an ancestor, which is a parent variable.

Value

a treepcor object

Methods (by generic)

  • print(treepcor): The print method for a treepcor

  • summary(treepcor): The summary method for a treepcor

  • dim(treepcor): The dim for a treepcor

  • plot(treepcor): The plot method for a treepcor

  • prec(treepcor): The prec for a treepcor

  • vcov(treepcor): The vcov method for a treepcor

  • cgeneric(treepcor): The cgeneric method for treepcor, uses cgeneric_treepcor()

Functions

  • drop1(): The drop1 method for a treepcor

  • etreepcor2precision(): Internal function to extract elements to build the precision from the treepcor edges.

  • etreepcor2variance(): Internal function to extract elements to build the covariance matrix from a treepcor.

Examples

## for details see
## https://link.springer.com/article/10.1007/s10260-025-00788-y

if(FALSE) {
    
### examples of what is not allowed
    treepcor(p1 ~ p2)
    treepcor(p1 ~ c2)
    
    treepcor(
        p1 ~ c1 + c2,
        p2 ~ c3)
    
    treepcor(
        p1 ~ c1 + c2,
        p2 ~ p1 + c2 + c3)
    
    treepcor(
        p1 ~ c1 + c2,
        p2 ~ p3 + c2 + c3)
    
    treepcor(
        p1 ~ p2 + c1 + c2,
        p2 ~ c2 + c3)

}

### allowed cases

## 3 children and 1 parent
tree1 <- treepcor(p1 ~ c1 + c2 - c3)

tree1

dim(tree1)

summary(tree1)

plot(tree1)

prec(tree1)

(q1 <- prec(tree1, theta = c(0)))

v1 <- chol2inv(chol(q1))

v1

cov2cor(v1)

vcov(tree1, raw = TRUE)
cov2cor(vcov(tree1, raw = TRUE))
vcov(tree1)

vcov(tree1, theta = 0)
vcov(tree1, theta = -1)
vcov(tree1, theta = 1)

cov2cor(vcov(tree1))
cov2cor(vcov(tree1, theta = -1))
cov2cor(vcov(tree1, theta = 1))

## 4 children and 2 parent
tree2 <- treepcor(
    p1 ~ p2 + c1 + c2,
    p2 ~ -c3 + c4)
tree2
dim(tree2)
summary(tree2)

prec(tree2)
prec(tree2, theta = c(0, 0))
prec(tree2, theta = c(-1, 1))

vcov(tree2, raw = TRUE)
cov2cor(vcov(tree2, raw = TRUE))

cov2cor(solve(prec(tree2, theta = c(0,0))))
vcov(tree2, theta = c(0,0))
vcov(tree2, theta = c(log(4:1), 0,0))

vcov(tree2)

tree2

## 4 children and 2 parent (notice the signs)
tree2b <- treepcor(
    p1 ~ -p2 + c1 + c2,
    p2 ~ -c3 + c4)
tree2b
dim(tree2b)
summary(tree2b)

summary(tree2)
summary(tree2b)

par(mfrow = c(1, 2), mar = c(0,0,0,0))
plot(tree2)
plot(tree2b)

prec(tree2)
prec(tree2b)

## prec is not equal
all.equal(prec(tree2, theta = c(0, 0)),
          prec(tree2b, theta = c(0, 0)))

## vcov is equal
all.equal(vcov(tree2, theta = c(0, 0)),
          vcov(tree2b, theta = c(0, 0)))



graphpcor documentation built on March 23, 2026, 9:07 a.m.