| Title: | Graphical Markov Models with Mixed Graphs |
|---|---|
| Description: | Provides functions for defining mixed graphs containing three types of edges, directed, undirected and bi-directed, with possibly multiple edges. These graphs are useful because they capture fundamental independence structures in multivariate distributions and in the induced distributions after marginalization and conditioning. The package is especially concerned with Gaussian graphical models for (i) ML estimation for directed acyclic graphs, undirected and bi-directed graphs and ancestral graph models (ii) testing several conditional independencies (iii) checking global identification of DAG Gaussian models with one latent variable (iv) testing Markov equivalences and generating Markov equivalent graphs of specific types. Note: The Bioconductor package 'graph' is now a suggested dependency and is only required when working with 'graphNEL' objects. |
| Authors: | Giovanni M. Marchetti [aut, cre] (ORCID: <https://orcid.org/0000-0003-4413-9349>), Mathias Drton [aut] (ORCID: <https://orcid.org/0000-0001-5614-3025>), Kayvan Sadeghi [aut] (ORCID: <https://orcid.org/0000-0001-7314-744X>) |
| Maintainer: | Giovanni M. Marchetti <[email protected]> |
| License: | GPL-2 |
| Version: | 2.5.5 |
| Built: | 2026-09-19 09:13:22 UTC |
| Source: | https://github.com/stathin/ggm |
This package provides functions for defining, manipulating and fitting graphical Markov models with mixed graphs. It is intended as a contribution to the gR-project described by Lauritzen (2002).
For a tutorial illustrating the new functions in the package ggm that deal
with ancestral, summary and ribbonless graphs see Sadeghi and Marchetti
(2012) in the references.
The main functions can be classified as follows:
Functions for defining graphs (undirected, directed acyclic, ancestral and summary graphs): UG, DAG, makeMG, grMAT;
Functions for doing graph operations (parents, boundary, cliques, connected components, fundamental cycles, d-separation, m-separation): pa, bd, conComp, fundCycles;
Functions for testing independence statements and generating maximal graphs from non-maximal graphs: dSep, msep, Max;
Function for finding covariance and concentration graphs induced by marginalization and conditioning: inducedCovGraph, inducedConGraph;
Functions for finding multivariate regression graphs and chain graphs induced by marginalization and conditioning: inducedRegGraph, inducedChainGraph, inducedDAG;
Functions for finding stable mixed graphs (ancestral, summary and ribbonless) after marginalization and conditioning: AG, SG, RG;
Functions for fitting by ML Gaussian DAGs, concentration graphs, covariance graphs and ancestral graphs: fitDag, fitConGraph, fitCovGraph, fitAncestralGraph;
Functions for testing several conditional independences: shipley.test;
Functions for checking global identification of DAG Gaussian models with one latent variable (Stanghellini-Vicard's condition for concentration graphs, new sufficient conditions for DAGs): isGident, checkIdent;
Functions for fitting Gaussian DAG models with one latent variable: fitDagLatent;
Functions for testing Markov equivalences and generating Markov equivalent graphs of specific types: MarkEqRcg, MarkEqMag, RepMarDAG, RepMarUG, RepMarBG.
Giovanni M. Marchetti, Dipartimento di Statistica, Informatica, Applicazioni 'G. Parenti'. University of Florence, Italy
Mathias Drton, Department of Statistics, University of Washington, USA
Kayvan Sadeghi, Department of Statistics, Carnegie Mellon University, USA
Many thanks to Fulvia Pennoni for testing some of the functions, to Elena Stanghellini for discussion and examples and to Claus Dethlefsen and Jens Henrik Badsberg for suggestions and corrections. The function fitConGraph was corrected by Ilaria Carobbi. Helpful discussions with Steffen Lauritzen and Nanny Wermuth, are gratefully acknowledged. Thanks also to Michael Perlman, Thomas Richardson and David Edwards.
Giovanni Marchetti has been supported by MIUR, Italy, under grant scheme PRIN 2002, and Mathias Drton has been supported by NSF grant DMS-9972008 and University of Washington RRF grant 65-3010.
Maintainer: Giovanni M. Marchetti [email protected] (ORCID)
Authors:
Giovanni M. Marchetti [email protected] (ORCID)
Mathias Drton [email protected] (ORCID)
Kayvan Sadeghi [email protected] (ORCID)
Lauritzen, S. L. (2002). gRaphical Models in R. R News, 3(2)39.
Sadeghi, K. and Marchetti, G.M. (2012). Graphical Markov models with mixed graphs in R. The R Journal, 4(2):65-73. https://journal.r-project.org/archive/2012/RJ-2012-015/RJ-2012-015.pdf
Transforms the edgematrix() of a graph into the adjacency matrix.
adjMatrix(E)adjMatrix(E)
E |
A square matrix representing the edge matrix of a graph. |
Given the edge matrix E of a graph, this can be transformed
into an adjacency matrix A with t(A) - diag(ncol(A).
The adjacency matrix of the graph.
Giovanni M. Marchetti
A <- DAG(y ~ x + z, z ~ u + v) E <- edgematrix(A) adjMatrix(E)A <- DAG(y ~ x + z, z ~ u + v) E <- edgematrix(A) adjMatrix(E)
AG generates and plots ancestral graphs after marginalization
and conditioning.
AG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )AG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )
amat |
An adjacency matrix, or a graph object. This can be:
|
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
|
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG() and Max().
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
Kayvan Sadeghi
Richardson, T.S. and Spirtes, P. (2002). Ancestral graph Markov models. Annals of Statistics, 30(4), 962-1030.
Sadeghi, K. (2013). Stable mixed graphs. Bernoulli, 19(5B), 2330–2358.
## The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) AG(ex, M, C, plot = TRUE)## The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) AG(ex, M, C, plot = TRUE)
Finds the set of edges of a graph. That is the set of undirected edges if the graph is undirected and the set of arrows if the graph is directed.
allEdges(amat)allEdges(amat)
amat |
A square Boolean matrix, with dimnames, the adjacency matrix of an undirected or directed acyclic graph. |
A matrix with two columns. Each row of the matrix is a pair of
indices indexing an edge of the graph. If the graph is undirected,
only one of the pairs is reported.
Giovanni M. Marchetti
## A UG graph allEdges(UG(~ y * v * k + v * k * d + y * d)) ## A DAG allEdges(DAG(u ~ h + o + p, h ~ o, o ~ p))## A UG graph allEdges(UG(~ y * v * k + v * k * d + y * d)) ## A DAG allEdges(DAG(u ~ h + o + p, h ~ o, o ~ p))
Anger data
angeranger
A covariance matrix for 4 variables measured on 684 female students.
anxiety state
anger state
anxiety trait
anger trait
Trait variables are viewed as stable personality characteristics, and state variables denote behaviour in specific situations. See Cox and Wermuth (1996).
Cox, D. R. and Wermuth, N. (1996). Multivariate dependencies. London: Chapman and Hall.
Cox, D.R. and Wermuth, N. (1990). An approximation to maximum likelihood estimates in reduced models. 77(4), 747-761.
# Fit a chordless 4-cycle model data(anger) G = UG(~ Y*X + X*Z + Z*U + U*Y) fitConGraph(G,anger, 684)# Fit a chordless 4-cycle model data(anger) G = UG(~ Y*X + X*Z + Z*U + U*Y) fitConGraph(G,anger, 684)
Finds a basis set for the conditional independencies implied by a directed acyclic graph, that is a minimal set of independencies that imply all the other ones.
basiSet(amat)basiSet(amat)
amat |
An adjacency matrix with dimnames representing the adjacency matrix of a DAG. |
Given a DAG and a pair of non adjacent nodes such
that has higher causal order than , the set of
independency statements independent of given
the union of the parents of both and is a basis set (see Shipley, 2000).
This basis set has the property to lead to independent test
statistics.
A list of vectors representing several conditional independence statements. Each vector contains the names of two non adjacent nodes followed by the names of nodes in the conditioning (possibly empty) set.
Giovanni M. Marchetti
Shipley, B. (2000). A new inferential test for path models based on directed acyclic graphs. Structural Equation Modeling, 7(2), 206–218.
## See Shipley (2000), Figure 2, p. 213 A <- DAG(x5 ~ x3 + x4, x3 ~ x2, x4 ~ x2, x2 ~ x1) basiSet(A)## See Shipley (2000), Figure 2, p. 213 A <- DAG(x5 ~ x3 + x4, x3 ~ x2, x4 ~ x2, x2 ~ x1) basiSet(A)
Breadth-first search of a connected undirected graph.
bfsearch(amat, v = 1)bfsearch(amat, v = 1)
amat |
A symmetric matrix with dimnames specifying the adjacency matrix of the undirected graph. |
v |
An integer indicating the starting node of the search. Defaults to the first node. |
Breadth-first search is a systematic method for exploring a graph. The algorithm is taken from Aho, Hopcroft & Ullman (1983).
A list with the following components:
tree: the edge matrix of the resulting spanning tree.
branches: a matrix with two columns, giving the indices of the
branches of the spanning tree.
chords: a matrix with two columns, giving the indices
of the chords of the spanning tree.
Giovanni M. Marchetti
Aho, A.V., Hopcrtoft, J.E. & Ullman, J.D. (1983). Data structures and algorithms. Reading: Addison-Wesley.
Thulasiraman, K. & Swamy, M.N.S. (1992). Graphs: theory and algorithms. New York: Wiley.
UG(), findPath(), cycleMatrix()
# Finding a spanning tree of the butterfly graph bfsearch(UG(~ a * b * o + o * u * j)) # Starting from another node bfsearch(UG(~ a * b * o + o * u * j), v = 3)# Finding a spanning tree of the butterfly graph bfsearch(UG(~ a * b * o + o * u * j)) # Starting from another node bfsearch(UG(~ a * b * o + o * u * j), v = 3)
A marginal log-linear link is defined by . See Bartolucci et al. (2007) for details.
binve(eta, C, M, G, maxit = 500, print = FALSE, tol = 1e-10)binve(eta, C, M, G, maxit = 500, print = FALSE, tol = 1e-10)
eta |
a vector of dimension |
C |
A contrast matrix. |
M |
A marginalization matrix. |
G |
G is the model matrix of the loglinear parameterization with no constant term. |
maxit |
an integer, specifying the maximum number of iterations. Default 500. |
print |
a logical value: if |
tol |
A small value specifying the tolerance
for the convergence criterion. Default: |
A vector of probabilities p.
From a Matlab function by Antonio Forcina, University of Perugia, Italy.
Antonio Forcina, Giovanni M. Marchetti
mat.mlogit()
Bartolucci, F., Colombi, R. and Forcina, A. (2007). An extended class of marginal link functions for modelling contingency tables by equality and inequality constraints. Statist. Sinica, 17, 691-711.
Block diagonal concatenation of input arguments.
blkdiag(...)blkdiag(...)
... |
Variable number of matrices |
A block diagonal matrix diag(M1, M2, ...).
Giovanni M. Marchetti
X <- c(1, 1, 2, 2) Z <- c(10, 20, 30, 40) A <- factor(c(1, 2, 2, 2)) blkdiag(model.matrix(~ X + Z), model.matrix(~A))X <- c(1, 1, 2, 2) Z <- c(10, 20, 30, 40) A <- factor(c(1, 2, 2, 2)) blkdiag(model.matrix(~ X + Z), model.matrix(~A))
Split a vector x into a block diagonal matrix.
blodiag(x, blo)blodiag(x, blo)
x |
A vector of length |
blo |
A vector of positive integers such that
|
A block-diagonal matrix with as many row
as elements of blo and n columns.
The vector x is split into length(blo)sub-vectors and
these are the blocks of the resulting matrix.
Giovanni M. Marchetti
blkdiag(), diag(), fitmlogit().
blodiag(1:10, blo = c(2, 3, 5)) blodiag(1:8, blo = c(3, 4, 0, 1)) blodiag(1:4, blo = c(2, 0, 2)) blodiag(1:4, blo = c(2, 2, 0))blodiag(1:10, blo = c(2, 3, 5)) blodiag(1:8, blo = c(3, 4, 0, 1)) blodiag(1:4, blo = c(2, 0, 2)) blodiag(1:4, blo = c(2, 2, 0))
Checks four sufficient conditions for identifiability of a Gaussian DAG model with one latent variable.
checkIdent(amat, latent)checkIdent(amat, latent)
amat |
A square matrix with dimnames, representing the adjacency matrix of a DAG. |
latent |
An integer representing the latent variables among the nodes or the name of the node. |
Stanghellini and Wermuth (2005) give some sufficient conditions for checking if a Gaussian model that factorizes according to a DAG is identified when there is one hidden node over which we marginalize. Specifically, the function checks the conditions of Theorem 1, (i) and (ii) and of Theorem 2 (i) and (ii).
A vector of length four, indicating if the model
is identified according to the conditions of theorems 1 and 2 in
Stanghellini & Wermuth (2005). The answer is TRUE if the
condition holds and thus the model is globally identified
or FALSE if the condition fails, and thus we do not know
if the model is identifiable.
Giovanni M. Marchetti
Stanghellini, E. & Wermuth, N. (2005). On the identification of path-analysis models with one hidden variable. Biometrika, 92(2), 337-350.
# See DAG in Figure 4 (a) in Stanghellini & Wermuth (2005) d <- DAG(y1 ~ y3, y2 ~ y3 + y5, y3 ~ y4 + y5, y4 ~ y6) checkIdent(d, "y3") # Identifiable checkIdent(d, "y4") # Not identifiable? # See DAG in Figure 5 (a) in Stanghellini & Wermuth (2005) d <- DAG(y1 ~ y5 + y4, y2 ~ y5 + y4, y3 ~ y5 + y4) checkIdent(d, "y4") # Identifiable checkIdent(d, "y5") # Identifiable # A simple function to check identifiability for each node is.ident <- function(amat) { # Check suff. conditions on each node of a DAG. p <- nrow(amat) # Degrees of freedom df <- p * (p + 1) / 2 - p - sum(amat == 1) - p + 1 if (df <= 0) { warning(paste("The degrees of freedom are ", df)) } a <- rownames(amat) for (i in a) { b <- checkIdent(amat, latent = i) if (TRUE %in% b) { cat("Node", i, names(b)[!is.na(b)], "\n") } else { cat("Unknown.\n") } } }# See DAG in Figure 4 (a) in Stanghellini & Wermuth (2005) d <- DAG(y1 ~ y3, y2 ~ y3 + y5, y3 ~ y4 + y5, y4 ~ y6) checkIdent(d, "y3") # Identifiable checkIdent(d, "y4") # Not identifiable? # See DAG in Figure 5 (a) in Stanghellini & Wermuth (2005) d <- DAG(y1 ~ y5 + y4, y2 ~ y5 + y4, y3 ~ y5 + y4) checkIdent(d, "y4") # Identifiable checkIdent(d, "y5") # Identifiable # A simple function to check identifiability for each node is.ident <- function(amat) { # Check suff. conditions on each node of a DAG. p <- nrow(amat) # Degrees of freedom df <- p * (p + 1) / 2 - p - sum(amat == 1) - p + 1 if (df <= 0) { warning(paste("The degrees of freedom are ", df)) } a <- rownames(amat) for (i in a) { b <- checkIdent(amat, latent = i) if (TRUE %in% b) { cat("Node", i, names(b)[!is.na(b)], "\n") } else { cat("Unknown.\n") } } }
Finds the complementary graph of an undirected graph.
cmpGraph(amat, value = 1)cmpGraph(amat, value = 1)
amat |
The adjacency matrix of an undirected graph. |
value |
the value 1 or 10 indicating the edges present. The default is
|
The complementary graph of an UG is the graph that has the
same set of nodes and an undirected edge connecting
and whenever there is not an edge in the
original UG.
The edge matrix of the complementary graph.
Giovanni M. Marchetti
Lauritzen, S. (2026). Graphical models 2nd ed. Oxford: Oxford University Press.
## A chordless four-cycle four <- UG(~ a * b + b * d + d * e + e * a) four cmpGraph(four)## A chordless four-cycle four <- UG(~ a * b + b * d + d * e + e * a) four cmpGraph(four)
Finds the connectivity components of an undirected graph.
conComp(amat, method = 1)conComp(amat, method = 1)
amat |
The adjacency matrix of an UG. |
method |
An integer 1 or 2 to choose the method used to find the components. Method 2 is more efficient for large graphs. |
An integer vector representing a partition of the set of nodes.
Giovanni M. Marchetti
#' Lauritzen, S. (2026). Graphical models 2nd ed. Oxford: Oxford University Press.
# three connected components conComp(UG(~ a * c + c * d + e + g * o * u)) # one connected graph conComp(UG(~ a * b + b * c + c * d + d * a))# three connected components conComp(UG(~ a * c + c * d + e + g * o * u)) # one connected graph conComp(UG(~ a * b + b * c + c * d + d * a))
Computes a correlation matrix with ones along the diagonal, marginal correlations in the lower triangle and partial correlations given all remaining variables in the upper triangle.
correlations(x, placement = upper.tri)correlations(x, placement = upper.tri)
x |
A square symmetric matrix, a covariance matrix,
or a data.frame for |
placement |
is the function |
A square correlation matrix with marginal correlations
and partial correlations with the chosen placement.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
## See Table 6.1 in Cox & Wermuth (1996) data(glucose) correlations(glucose) correlations(glucose, placement = lower.tri)## See Table 6.1 in Cox & Wermuth (1996) data(glucose) correlations(glucose) correlations(glucose, placement = lower.tri)
Finds the matrix of fundamental cycles of a connected undirected graph.
cycleMatrix(amat)cycleMatrix(amat)
amat |
A symmetric matrix with dimnames denoting the adjacency matrix an the undirected graph. The graph must be connected, otherwise the function returns an error message. |
All the cycles in an UG can be obtained from combination (ring sum) of the set of fundamental cycles. The matrix of fundamental cycles is a Boolean matrix having as rows the fundamental cycles and as columns the edges of the graph. If an entry is one then the edge associated to that column belongs to the cycle associated to the row.
A Boolean matrix of the fundamental cycles of the
undirected graph. If there is no cycle the function returns NULL.
This function is used by isGident(). The row sum of
the matrix gives the length of the cycles.
Giovanni M. Marchetti
Thulasiraman, K. & Swamy, M.N.S. (1992). Graphs: theory and algorithms. New York: Wiley.
UG(), findPath(), fundCycles(), isGident(), bfsearch()
## Three cycles cycleMatrix(UG(~ a * b * d + d * e + e * a * f)) ## No cycle cycleMatrix(UG(~ a * b)) ## two cycles: the first is even and the second is odd cm <- cycleMatrix(UG(~ a * b + b * c + c * d + d * a + a * u * v)) apply(cm, 1, sum)## Three cycles cycleMatrix(UG(~ a * b * d + d * e + e * a * f)) ## No cycle cycleMatrix(UG(~ a * b)) ## two cycles: the first is even and the second is odd cm <- cycleMatrix(UG(~ a * b + b * c + c * d + d * a + a * u * v)) apply(cm, 1, sum)
A simple way to define a DAG by means of regression model formulae.
DAG(..., order = FALSE)DAG(..., order = FALSE)
... |
A sequence of model formulae. |
order |
Logical, defaulting to |
The DAG is defined by a sequence of recursive regression models. Each regression is defined by a model formula. For each formula the response defines a node of the graph and the explanatory variables the parents of that node. If the regressions are not recursive the function returns an error message.
Some authors prefer the terminology acyclic directed graphs (ADG).
The adjacency matrix of the DAG, i.e., a square Boolean
matrix of order equal to the number of nodes of the graph and
a one in position if there is an arrow from
to and zero otherwise. The rownames of the
adjacency matrix are the nodes of the DAG.
If order = TRUE the adjacency matrix is permuted to have parents
before children. This can always be done (in more than one way)
for DAGs. The resulting adjacency matrix is upper triangular.
The model formulae may contain interactions, but they are ignored in the graph.
G. M. Marchetti
Lauritzen, S. (1996). Graphical models 2nd ed. Oxford: Oxford University Press.
UG(), topSort(), edgematrix(), fitDag()
# A Markov chain DAG(y ~ x, x ~ z, z ~ u) # Another DAG DAG(y ~ x + z + u, x ~ u, z ~ u) # A DAG with an isolated node DAG(v ~ v, y ~ x + z, z ~ w + u) # There can be repetitions DAG(y ~ x + u + v, y ~ z, u ~ v + z) # Interactions are ignored DAG(y ~ x * z + z * v, x ~ z) # A cyclic graph returns an error! ## Not run: DAG(y ~ x, x ~ z, z ~ y) ## End(Not run) ## The order can be changed DAG(y ~ z, y ~ x + u + v, u ~ v + z) ## If you want to order the nodes (topological sort of the DAG) DAG(y ~ z, y ~ x + u + v, u ~ v + z, order = TRUE)# A Markov chain DAG(y ~ x, x ~ z, z ~ u) # Another DAG DAG(y ~ x + z + u, x ~ u, z ~ u) # A DAG with an isolated node DAG(v ~ v, y ~ x + z, z ~ w + u) # There can be repetitions DAG(y ~ x + u + v, y ~ z, u ~ v + z) # Interactions are ignored DAG(y ~ x * z + z * v, x ~ z) # A cyclic graph returns an error! ## Not run: DAG(y ~ x, x ~ z, z ~ y) ## End(Not run) ## The order can be changed DAG(y ~ z, y ~ x + u + v, u ~ v + z) ## If you want to order the nodes (topological sort of the DAG) DAG(y ~ z, y ~ x + u + v, u ~ v + z, order = TRUE)
Raw data on blood pressure, body mass and age on 44 female patients, and covariance matrix for derived variables.
derivedderived
A list containing a dataframe raw with 44 lines and 5 columns
and a symmetric 4x4 covariance matrix S.
The following is the description of the variables in the dataframe
raw
Systolic blood pressure, in mm Hg
Diastolic blood pressure, in mm Hg
Age of the patient, in years
Height, in cm
Weight, in kg
The following is the description
of the variables for the covariance matrix S.
Derived variable Y=log(Sys/Dia)
Derived variables X=log(Dia)
Body
mass index Z=Wei/(Hei/100)^2
Age
Wermuth N. and Cox D.R. (1995). Derived variables calculated from similar joint responses: some characteristics and examples. Computational Statistics and Data Analysis, 19, 223-234.
# A DAG model with a latent variable U G = DAG(Y ~ Z + U, X ~ U + W, Z ~ W) data(derived) # The model fitted using the derived variables out = fitDagLatent(G, derived$S, n = 44, latent = "U") # An ancestral graph model marginalizing over U H = AG(G, M = "U") # The ancestral graph model fitted obtaining the # same result out2 = fitAncestralGraph(H, derived$S, n = 44)# A DAG model with a latent variable U G = DAG(Y ~ Z + U, X ~ U + W, Z ~ W) data(derived) # The model fitted using the derived variables out = fitDagLatent(G, derived$S, n = 44, latent = "U") # An ancestral graph model marginalizing over U H = AG(G, M = "U") # The ancestral graph model fitted obtaining the # same result out2 = fitAncestralGraph(H, derived$S, n = 44)
Defines the adjacency matrix of a directed graph.
DG(...)DG(...)
... |
a sequence of model formulae |
The directed graph is defined by a sequence of models formulae. For each formula the response defines a node of the graph and its parents. The graph contains no loops, but can have directed cycles.
the adjacency matrix of the directed graph,
i.e., a square Boolean matrix of order equal to the
number of nodes of the graph and a one in
position if there is an arrow from
to and zero otherwise. The dimnames of the
adjacency matrix are the labels for the nodes of the
graph.
G. M. Marchetti
Lauritzen, S., L. (1996). Graphical models 2nd ed. Oxford: Oxford University Press.
# A DAG DG(y ~ x, x ~ z, z ~ u) # A cyclic directed graph DG(y ~ x, x ~ z, z ~ y) # A graph with two arrows between two nodes double <- DG(y ~ x, x ~ y) double plotGraph(double, directed = TRUE) # There can be isolated nodes DG(y ~ x, x ~ x)# A DAG DG(y ~ x, x ~ z, z ~ u) # A cyclic directed graph DG(y ~ x, x ~ z, z ~ y) # A graph with two arrows between two nodes double <- DG(y ~ x, x ~ y) double plotGraph(double, directed = TRUE) # There can be isolated nodes DG(y ~ x, x ~ x)
Computes faster the product of a diagonal matrix times a full matrix.
diagv(v, M)diagv(v, M)
v |
A numeric vector specifying the elements on the diagonal of a matrix. |
M |
A numeric matrix compatible with the
product |
Computes where is diagonal
avoiding the base::diag() operator.
A matrix N.
v <- 1:1000 M <- matrix(runif(3000), 1000, 3) dim(diagv(v, M))v <- 1:1000 M <- matrix(runif(3000), 1000, 3) dim(diagv(v, M))
Draw a graph from its adjacency matrix representation.
drawGraph( amat, coor = NULL, adjust = FALSE, alpha = 1.5, beta = 3, lwd = 1, ecol = "blue", bda = 0.1, layout = igraph::layout_nicely )drawGraph( amat, coor = NULL, adjust = FALSE, alpha = 1.5, beta = 3, lwd = 1, ecol = "blue", bda = 0.1, layout = igraph::layout_nicely )
amat |
the adjacency matrix representation of the graph.
This can be an undirected graph, a directed acyclic graph or
a mixed graph with at most a summary graph structure.
See also |
coor |
an optional matrix of dimensions |
adjust |
a logical value, defaults to |
alpha |
a positive value between controlling the distance from the end of the edges to the nodes of the graph. |
beta |
a positive value controlling the distance of the labels of the variables from the nodes. |
lwd |
line width of the edges (default: 1). |
ecol |
color of the edges ( |
bda |
bidirected edge arrow length (default: 0.1). |
layout |
The name of a function used to compute the
(initial) layout of the graph. The default is
|
The function is a very simple tool useful for displaying small
mixed graphs, with a rudimentary interface for moving nodes
and edges of a given graph and adjusting the final plot.
The function plotGraph() is more elaborate. Packages like
visNetwork or DiagrammeR are better for advanced network
graph rendering. See the references. Also one of the first
interactive plots for graphs is the package dynamicGraph
that is now archived by CRAN since 2021.
A plot of the graph with a initial positioning of the
nodes, as specified by coor and remains in a waiting state.
The position of each node can be shifted by pointing and
clicking (with the first mouse button) close to the node.
When the mouse button is pressed the node which is closer
to the selected point is moved to that position.
Thus, one must be careful to click closer to the selected
node than to any other node. The nodes can be moved to any
position by repeating the previous operation. The adjustment
process is terminated by pressing any mouse button other
than the first.
At the end of the process, the function returns invisibly the
coordinates of the nodes. The coordinates may be used later
to redisplay the graph.
Giovanni M. Marchetti
Richard Iannone (2026). DiagrammeR: Graph/Network Visualization. https://CRAN.R-project.org/package=DiagrammeR
Benoit Thieurmel (2025). visNetwork: Network Visualization using 'vis.js' Library. https://CRAN.R-project.org/package=visNetwork
#' Badsberg, J.H (2021). dynamicGraph: An Interactive Graphical Tool for Manipulating Graphs. https://CRAN.R-project.org/package=dynamicGraph
UG(), DAG(), makeMG(), plotGraph()
## A directed acyclic graph d <- DAG(y1 ~ y2 + y6, y2 ~ y3, y3 ~ y5 + y6, y4 ~ y5 + y6) ## Not run: drawGraph(d) ## End(Not run) ## An undirected graph g <- UG(~ giova * anto * armo + anto * arj * sara) ## Not run: drawGraph(d) ## End(Not run) ## An ancestral graph ag <- makeMG(ug = UG(~ y0 * y1), dg = DAG(y4 ~ y2, y2 ~ y1), bg = UG(~ y2 * y3 + y3 * y4)) drawGraph(ag, adjust = FALSE) drawGraph(ag, adjust = FALSE) ## A more complex example with coordinates: the UNIX evolution xy <- structure(c( 5, 15, 23, 25, 26, 17, 8, 6, 6, 7, 39, 33, 23, 49, 19, 34, 13, 29, 50, 68, 70, 86, 89, 64, 81, 45, 64, 49, 64, 87, 65, 65, 44, 37, 64, 68, 73, 85, 83, 95, 84, 0, 7, 15, 27, 44, 37, 36, 20, 51, 65, 44, 64, 59, 73, 69, 78, 81, 90, 97, 89, 72, 85, 74, 62, 68, 59, 52, 48, 43, 50, 34, 21, 18, 5, 1, 10, 2, 11, 2, 1, 44 ), .Dim = c(41, 2), .Dimnames = list(NULL, c( "x", "y" ))) Unix <- DAG( SystemV.3 ~ SystemV.2, SystemV.2 ~ SystemV.0, SystemV.0 ~ TS4.0, TS4.0 ~ Unix.TS3.0 + Unix.TS.PP + CB.Unix.3, PDP11.SysV ~ CB.Unix.3, CB.Unix.3 ~ CB.Unix.2, CB.Unix.2 ~ CB.Unix.1, Unix.TS.PP ~ CB.Unix.3, Unix.TS3.0 ~ Unix.TS1.0 + PWB2.0 + USG3.0 + Interdata, USG3.0 ~ USG2.0, PWB2.0 ~ Interdata + PWB1.2, USG2.0 ~ USG1.0, CB.Unix.1 ~ USG1.0, PWB1.2 ~ PWB1.0, USG1.0 ~ PWB1.0, PWB1.0 ~ FifthEd, SixthEd ~ FifthEd, LSX ~ SixthEd, MiniUnix ~ SixthEd, Interdata ~ SixthEd, Wollongong ~ SixthEd, SeventhEd ~ Interdata, BSD1 ~ SixthEd, Xenix ~ SeventhEd, V32 ~ SeventhEd, Uniplus ~ SeventhEd, BSD3 ~ V32, BSD2 ~ BSD1, BSD4 ~ BSD3, BSD4.1 ~ BSD4, EigthEd ~ SeventhEd + BSD4.1, NinethEd ~ EigthEd, Ultrix32 ~ BSD4.2, BSD4.2 ~ BSD4.1, BSD4.3 ~ BSD4.2, BSD2.8 ~ BSD4.1 + BSD2, BSD2.9 ~ BSD2.8, Ultrix11 ~ BSD2.8 + V7M + SeventhEd, V7M ~ SeventhEd ) drawGraph(Unix, coor = xy, adjust = FALSE) # dev.print(file="unix.fig", device=xfig) # Edit the graph with Xfig## A directed acyclic graph d <- DAG(y1 ~ y2 + y6, y2 ~ y3, y3 ~ y5 + y6, y4 ~ y5 + y6) ## Not run: drawGraph(d) ## End(Not run) ## An undirected graph g <- UG(~ giova * anto * armo + anto * arj * sara) ## Not run: drawGraph(d) ## End(Not run) ## An ancestral graph ag <- makeMG(ug = UG(~ y0 * y1), dg = DAG(y4 ~ y2, y2 ~ y1), bg = UG(~ y2 * y3 + y3 * y4)) drawGraph(ag, adjust = FALSE) drawGraph(ag, adjust = FALSE) ## A more complex example with coordinates: the UNIX evolution xy <- structure(c( 5, 15, 23, 25, 26, 17, 8, 6, 6, 7, 39, 33, 23, 49, 19, 34, 13, 29, 50, 68, 70, 86, 89, 64, 81, 45, 64, 49, 64, 87, 65, 65, 44, 37, 64, 68, 73, 85, 83, 95, 84, 0, 7, 15, 27, 44, 37, 36, 20, 51, 65, 44, 64, 59, 73, 69, 78, 81, 90, 97, 89, 72, 85, 74, 62, 68, 59, 52, 48, 43, 50, 34, 21, 18, 5, 1, 10, 2, 11, 2, 1, 44 ), .Dim = c(41, 2), .Dimnames = list(NULL, c( "x", "y" ))) Unix <- DAG( SystemV.3 ~ SystemV.2, SystemV.2 ~ SystemV.0, SystemV.0 ~ TS4.0, TS4.0 ~ Unix.TS3.0 + Unix.TS.PP + CB.Unix.3, PDP11.SysV ~ CB.Unix.3, CB.Unix.3 ~ CB.Unix.2, CB.Unix.2 ~ CB.Unix.1, Unix.TS.PP ~ CB.Unix.3, Unix.TS3.0 ~ Unix.TS1.0 + PWB2.0 + USG3.0 + Interdata, USG3.0 ~ USG2.0, PWB2.0 ~ Interdata + PWB1.2, USG2.0 ~ USG1.0, CB.Unix.1 ~ USG1.0, PWB1.2 ~ PWB1.0, USG1.0 ~ PWB1.0, PWB1.0 ~ FifthEd, SixthEd ~ FifthEd, LSX ~ SixthEd, MiniUnix ~ SixthEd, Interdata ~ SixthEd, Wollongong ~ SixthEd, SeventhEd ~ Interdata, BSD1 ~ SixthEd, Xenix ~ SeventhEd, V32 ~ SeventhEd, Uniplus ~ SeventhEd, BSD3 ~ V32, BSD2 ~ BSD1, BSD4 ~ BSD3, BSD4.1 ~ BSD4, EigthEd ~ SeventhEd + BSD4.1, NinethEd ~ EigthEd, Ultrix32 ~ BSD4.2, BSD4.2 ~ BSD4.1, BSD4.3 ~ BSD4.2, BSD2.8 ~ BSD4.1 + BSD2, BSD2.9 ~ BSD2.8, Ultrix11 ~ BSD2.8 + V7M + SeventhEd, V7M ~ SeventhEd ) drawGraph(Unix, coor = xy, adjust = FALSE) # dev.print(file="unix.fig", device=xfig) # Edit the graph with Xfig
Determines if in a directed acyclic graph two set of nodes are d-separated by a third set of nodes.
dSep(amat, first, second, cond)dSep(amat, first, second, cond)
amat |
A Boolean matrix with |
first |
A vector representing a subset of nodes
of the DAG. The vector should be a character vector of
the names of the variables matching the names of the nodes
in |
second |
A vector representing another subset
of nodes of the DAG. The set |
cond |
A vector representing a conditioning subset of
nodes. The set |
d-separation is a fundamental concept introduced by Pearl (1988).
A logical value. Is TRUE if first and second
are d-separated by cond.
Giovanni M. Marchetti
Pearl, J. (1988). Probabilistic reasoning in intelligent systems. San Mateo: Morgan Kaufmann.
Lauritzen, S. (2026). Graphical models, 2nd ed. Oxford: Oxford University Press.
DAG(), shipley.test(), msep(), induced-graphs
# Conditioning on a transition node dSep(DAG(y ~ x, x ~ z), first = "y", second = "z", cond = "x") # Conditioning on a collision node (collider) dSep(DAG(y ~ x, y ~ z), first = "x", second = "z", cond = "y") # Conditioning on a source node dSep(DAG(y ~ x, z ~ x), first = "y", second = "z", cond = "x") # Marginal independence dSep(DAG(y ~ x, y ~ z), first = "x", second = "z", cond = NULL) # The DAG defined on p.~47 of Lauritzen (1996) dag <- DAG(g ~ x, h ~ x + f, f ~ b, x ~ l + d, d ~ c, c ~ a, l ~ y, y ~ b) dSep(dag, first = "a", second = "b", cond = c("x", "y")) dSep(dag, first = "a", second = c("b", "d"), cond = c("x", "y"))# Conditioning on a transition node dSep(DAG(y ~ x, x ~ z), first = "y", second = "z", cond = "x") # Conditioning on a collision node (collider) dSep(DAG(y ~ x, y ~ z), first = "x", second = "z", cond = "y") # Conditioning on a source node dSep(DAG(y ~ x, z ~ x), first = "y", second = "z", cond = "x") # Marginal independence dSep(DAG(y ~ x, y ~ z), first = "x", second = "z", cond = NULL) # The DAG defined on p.~47 of Lauritzen (1996) dag <- DAG(g ~ x, h ~ x + f, f ~ b, x ~ l + d, d ~ c, c ~ a, l ~ y, y ~ b) dSep(dag, first = "a", second = "b", cond = c("x", "y")) dSep(dag, first = "a", second = c("b", "d"), cond = c("x", "y"))
In some matrix computations the adjacency matrix of a graph
is transformed into an "edge matrix".
Briefly, if is the adjacency matrix of a DAG,
the corresponding edge matrix is a binary matrix
such that if and only if
or and otherwise.
If the adjacency matrix is ordered according to a
topological ordering the edge matrix is upper triangular
with ones on the main diagonal.
This order is the inverse of the topological order.
edgematrix(B, ord = FALSE)edgematrix(B, ord = FALSE)
B |
A square matrix, representing the adjacency matrix of a graph. |
ord |
A logical value. If |
A: the edge matrix of the graph.
Giovanni M. Marchetti
Giovanni M. Marchetti and Nanny Wermuth (2009). Matrix representations and independencies in direct acyclic graphs, The Annals of Statistics, Vol. 37, No. 2, 961-978.
B <- DAG(z ~ u + v, y ~ x + z) edgematrix(B) edgematrix(B, ord = TRUE)B <- DAG(z ~ u + v, y ~ x + z) edgematrix(B) edgematrix(B, ord = TRUE)
Find the essential graph from a given directed acyclic graph.
essentialGraph(dagx)essentialGraph(dagx)
dagx |
the adjacency matrix of a directed acyclic graph. The names of rows and of the columns are the nodes of the DAG. |
Converts a DAG into the Essential Graph. Is implemented by the algorithm by D.M. Chickering (1995).
the adjacency matrix of the essential graph. Note that
the essential graph is a mixed graph where the edges can be
arrows and/or lines. Then, the elements of the adjacency
matrix are coded as (0,1) if and
(1,0) if and (10, 10) if .
Giovanni M. Marchetti, from a MATLAB function by Tomas Kocka, AAU.
Chickering, D.M. (1995). A transformational characterization of equivalent Bayesian Network structures. Proceedings of Eleventh Conference on Uncertainty in Artificial Intelligence, Montreal, QU, 87-98. Morgan Kaufmann.
makeMG() DAG(), induced-graphs.
dag <- DAG(U ~ Y + Z, Y ~ X, Z ~ X) plotGraph(essentialGraph(dag))dag <- DAG(U ~ Y + Z, Y ~ X, Z ~ X) plotGraph(essentialGraph(dag))
Finds one path between two nodes of a graph.
findPath(amat, st, en, path = c())findPath(amat, st, en, path = c())
amat |
A square Boolean matrix with dimnames, the adjacency matrix of a graph. |
st |
An integer, the starting node index. |
en |
An integer, the ending node index. |
path |
A vector of integers, used in recursive calls.
Defaults to an empty vector |
A vector of integers containing the sequence
of nodes forming a path from st to en. If no path
is found, it returns NULL.
In some graphs (like spanning trees) there is only one path
between two nodes.
Giovanni M. Marchetti, translating the original Python code (see references).
Python Software Foundation (2003). Python Patterns Implementing Graphs. https://www.python.org/doc/essays/graphs/.
## A (single) path on a spanning tree findPath(bfsearch(UG(~ a * b * c + b * d + d * e + e * c))$tree, st = 1, en = 5)## A (single) path on a spanning tree findPath(bfsearch(UG(~ a * b * c + b * d + d * e + e * c))$tree, st = 1, en = 5)
Iterative conditional fitting of Gaussian Ancestral Graph Models.
fitAncestralGraph(amat, S, n, tol = 1e-06)fitAncestralGraph(amat, S, n, tol = 1e-06)
amat |
A square matrix, representing the adjacency matrix of an ancestral graph. |
S |
A symmetric positive definite matrix with row and column names, the sample covariance matrix. |
n |
The sample size, a positive integer. |
tol |
A small positive number indicating the tolerance
used in convergence checks. Defaults to |
In the Gaussian case, the models can be parameterized
using precision parameters, regression coefficients, and
error covariances (see Richardson and Spirtes, 2002, Section 8).
This function finds the MLE of
the precision parameters by fitting a concentration graph model.
The MLE of the regression coefficients
and the MLE of the error covariances
are obtained by iterative conditional fitting
(Drton and Richardson, 2003, 2004). The three sets of
parameters are combined to the MLE of the
covariance matrix by matrix multiplication:
Note that in Richardson and Spirtes (2002), the matrices
and are defined
as submatrices.
A list with the following components:
Shat The fitted covariance matrix.
Lhat Matrix of the fitted precisions associated with
undirected edges and vertices that do not have an arrowhead
pointing at them.
Bhat Matrix of the fitted regression coefficients
associated to the directed edges. Precisely, Bhat
contains ones along the diagonal and the off-diagonal
entry equals the negated MLE of the
regression coefficient for variable
in the regression of variable on its parents.
Note that this entry in Bhat
corresponds to a directed edge ,
and thus to a one as entry in the
adjacency matrix.
Ohat Matrix of the error covariances and variances
of the residuals between regression equations associated
with bi-directed edges and vertices with an arrowhead
pointing at them.
dev The deviance of the model.
df The degrees of freedom.
it The number of iterations.
Mathias Drton
Drton, M. and Richardson, T. S. (2003). A new algorithm for maximum likelihood estimation in Gaussian graphical models for marginal independence. Proceedings of the Nineteenth Conference on Uncertainty in Artificial Intelligence, 184-191.
Drton, M. and Richardson, T. S. (2004). Iterative Conditional Fitting for Gaussian Ancestral Graph Models. Proceedings of the 20th Conference on Uncertainty in Artificial Intelligence, Department of Statistics, 130-137.
Richardson, T. S. and Spirtes, P. (2002). Ancestral Graph Markov Models. Annals of Statistics, 30(4), 962-1030.
fitCovGraph(), icf(), makeMG(), fitDag()
# A covariance matrix S <- structure( c( 2.93, -1.7, 0.76, -0.06, -1.7, 1.64, -0.78, 0.1, 0.76, -0.78, 1.66, -0.78, -0.06, 0.1, -0.78, 0.81 ), .Dim = c(4, 4), .Dimnames = list(c("y", "x", "z", "u"), c("y", "x", "z", "u")) ) ## Fit an ancestral graph y -> x <-> z <- u fitAncestralGraph(makeMG(dg = DAG(x ~ y, z ~ u), bg = UG(~ x * z)), S, n = 100) # Fit an ancestral graph y <-> x <-> z <-> u fitAncestralGraph(makeMG(bg = UG(~ y * x + x * z + z * u)), S, n = 100) ## Fit the same graph with fitCovGraph fitCovGraph(makeMG(bg = UG(~ y * x + x * z + z * u)), S, n = 100) # Another example for the mathematics marks data data(marks) S_marks <- var(marks) mag1 <- makeMG(bg = UG(~ mechanics * vectors * algebra + algebra * analysis * statistics)) fitAncestralGraph(mag1, S_marks, n = 88)# A covariance matrix S <- structure( c( 2.93, -1.7, 0.76, -0.06, -1.7, 1.64, -0.78, 0.1, 0.76, -0.78, 1.66, -0.78, -0.06, 0.1, -0.78, 0.81 ), .Dim = c(4, 4), .Dimnames = list(c("y", "x", "z", "u"), c("y", "x", "z", "u")) ) ## Fit an ancestral graph y -> x <-> z <- u fitAncestralGraph(makeMG(dg = DAG(x ~ y, z ~ u), bg = UG(~ x * z)), S, n = 100) # Fit an ancestral graph y <-> x <-> z <-> u fitAncestralGraph(makeMG(bg = UG(~ y * x + x * z + z * u)), S, n = 100) ## Fit the same graph with fitCovGraph fitCovGraph(makeMG(bg = UG(~ y * x + x * z + z * u)), S, n = 100) # Another example for the mathematics marks data data(marks) S_marks <- var(marks) mag1 <- makeMG(bg = UG(~ mechanics * vectors * algebra + algebra * analysis * statistics)) fitAncestralGraph(mag1, S_marks, n = 88)
Fits a concentration graph (a covariance selection model).
fitConGraph(amat, S, n, cli = NULL, alg = 3, pri = FALSE, tol = 1e-06)fitConGraph(amat, S, n, cli = NULL, alg = 3, pri = FALSE, tol = 1e-06)
amat |
a square Boolean matrix representing the adjacency matrix of an UG |
S |
the sample covariance matrix |
n |
an integer denoting the sample size |
cli |
a list containing the cliques of the graph. The components of the list are character vectors containing the names of the nodes in the cliques. The names must match the names of the vertices. The knowledge of the cliques is not needed. If the cliques are not specified the function uses the algorithm by Hastie et al. (2009, p. 446). |
alg |
The algorithm used. |
pri |
If TRUE is verbose |
tol |
a small positive number indicating the tolerance used in convergence tests. |
The algorithms for fitting concentration graph models by maximum likelihood are discussed in Speed and Kiiveri (1986). If the cliques are known the function uses the iterative proportional fitting algorithm described by Whittaker (1990, p. 184). If the cliques are not specified the function uses the algorithm by Hastie et al. (2009, p. 631ff).
Shat the fitted covariance matrix
dev the deviance of the model
df the degrees of freedom
it the number of iterations
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
Hastie, T., Tibshirani, R. and Friedman, J. (2009). The elements of statistical learning. Springer Verlag: New York.
Speed, T.P. & Kiiveri, H (1986). Gaussian Markov distributions over finite graphs. Annals of Statistics, 14, 138-150.
Whittaker, J. (1990). Graphical models in applied multivariate statistics. Chichester: Wiley.
# A model for the mathematics marks (Whittaker, 1990) data(marks) # A butterfly concentration graph G <- UG(~ mechanics * vectors * algebra + algebra * analysis * statistics) fitConGraph(G, cov(marks), nrow(marks)) # Using the cliques cl <- list(c("mechanics", "vectors", "algebra"), c("algebra", "analysis", "statistics")) fitConGraph(G, S = cov(marks), n = nrow(marks), cli = cl)# A model for the mathematics marks (Whittaker, 1990) data(marks) # A butterfly concentration graph G <- UG(~ mechanics * vectors * algebra + algebra * analysis * statistics) fitConGraph(G, cov(marks), nrow(marks)) # Using the cliques cl <- list(c("mechanics", "vectors", "algebra"), c("algebra", "analysis", "statistics")) fitConGraph(G, S = cov(marks), n = nrow(marks), cli = cl)
Fits a Gaussian covariance graph model by maximum likelihood.
fitCovGraph( amat, S, n, alg = "icf", dual.alg = 2, start.icf = NULL, tol = 1e-06 )fitCovGraph( amat, S, n, alg = "icf", dual.alg = 2, start.icf = NULL, tol = 1e-06 )
amat |
A symmetric Boolean matrix with dimnames representing the adjacency matrix of an UG. |
S |
A symmetric positive definite matrix with dimnames, the sample covariance matrix. |
n |
A positive integer, the sample size. |
alg |
A character string, the algorithm used.
|
dual.alg |
An integer equal to 1 or 2 used when
|
start.icf |
A symmetric matrix used as starting
value of the algorithm. If |
tol |
A small positive number indicating the tolerance used in convergence tests (default |
A covariance graph is an undirected graph in which the variables associated to two non-adjacent nodes are marginally independent. The edges of these models are represented by bi-directed edges (Drton and Richardson, 2003) or by dashed lines (Cox and Wermuth, 1996).
By default, this function gives the ML estimates in
the covariance graph model, by iterative conditional
fitting (Drton and Richardson, 2003). Otherwise, the
estimates from a dual likelihood estimator can be
obtained (Kauermann, 1996; Edwards, 2000, section 7.4).
Shat the fitted covariance matrix
dev the deviance of the fitted model
df the degrees of freedom
it' the number of iterations.
Mathias Drton
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
Drton, M. and Richardson, T. S. (2003). A new algorithm for maximum likelihood estimation in Gaussian graphical models for marginal independence. Proceedings of the Nineteenth Conference on Uncertainty in Artificial Intelligence, 184-191.
Kauermann, G. (1996). On a dualization of graphical Gaussian models. Scandinavian Journal of Statistics. 23, 105-116.
# Correlations among four strategies to cope with stress # for 72 students. Cox & Wermuth (1996), p. 73. data(stress) # A chordless 4-cycle covariance graph G <- UG(~ Y * X + X * U + U * V + V * Y) fitCovGraph(G, S = stress, n = 72) fitCovGraph(G, S = stress, n = 72, alg = "dual")# Correlations among four strategies to cope with stress # for 72 students. Cox & Wermuth (1996), p. 73. data(stress) # A chordless 4-cycle covariance graph G <- UG(~ Y * X + X * U + U * V + V * Y) fitCovGraph(G, S = stress, n = 72) fitCovGraph(G, S = stress, n = 72, alg = "dual")
Fits linear recursive regressions with independent residuals specified by a DAG.
fitDag(amat, S, n)fitDag(amat, S, n)
amat |
a square matrix with dimnames representing the adjacency matrix of a DAG |
S |
a symmetric positive definite matrix, the sample covariance matrix |
n |
an integer |
fitDag checks if the order of the nodes in adjacency
matrix is the same of S and if not it reorders the
adjacency matrix to match the order of the variables
in S. The nodes of the adjacency matrix may
form a subset of the variables in S.
Shat the fitted covariance matrix
Ahat a square matrix of the fitted regression
coefficients. The entry Ahat[i,j] is minus the
regression coefficient of variable i in the regression
equation j. Thus there is a non zero partial
regression coefficient Ahat[i,j] corresponding to each
non zero value amat[j,i] in the adjacency matrix.
Dhat a vector containing the partial variances
of each variable given the parents.
dev the deviance of the fitted model
df the degrees of freedom.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
dag <- DAG(y ~ x + u, x ~ z, z ~ u) S <- structure( c( 2.93, -1.7, 0.76, -0.06, -1.7, 1.64, -0.78, 0.1, 0.76, -0.78, 1.66, -0.78, -0.06, 0.1, -0.78, 0.81 ), .Dim = c(4, 4), .Dimnames = list(c("y", "x", "z", "u"), c("y", "x", "z", "u")) ) fitDag(dag, S, 200)dag <- DAG(y ~ x + u, x ~ z, z ~ u) S <- structure( c( 2.93, -1.7, 0.76, -0.06, -1.7, 1.64, -0.78, 0.1, 0.76, -0.78, 1.66, -0.78, -0.06, 0.1, -0.78, 0.81 ), .Dim = c(4, 4), .Dimnames = list(c("y", "x", "z", "u"), c("y", "x", "z", "u")) ) fitDag(dag, S, 200)
Fits by maximum likelihood a Gaussian DAG model where one of the nodes of the graph is latent and it is marginalised over.
fitDagLatent( amat, Syy, n, latent, norm = 1, seed, maxit = 9000, tol = 1e-06, pri = FALSE )fitDagLatent( amat, Syy, n, latent, norm = 1, seed, maxit = 9000, tol = 1e-06, pri = FALSE )
amat |
a square matrix with dimnames representing the adjacency matrix of the DAG. |
Syy |
a symmetric positive definite matrix, with
dimnames, the sample covariance matrix of the observed
variables. The set of the observed nodes of the graph
must be a subset of the set of the names of the
variables in |
n |
a positive integer, the sample size |
latent |
the name of the latent variable |
norm |
an integer, the kind of normalization of the latent variable.
|
seed |
an integer, used by |
maxit |
an integer denoting the maximum number of
iterations allowed for the EM algorithm. If the
convergence criterion is not satisfied within
|
tol |
a small real value, denoting the tolerance
used in testing convergence (default |
pri |
logical, if |
For the EM algorithm used see Kiiveri (1987).
Shat the fitted covariance matrix of all the
variables including the latent one. The latent variable
is the last. If norm=1 then the variance of the
latent variable is constrained to 1.
Ahat a square matrix of the fitted regression
coefficients. The entry Ahat[i,j] is minus the
regression coefficient of variable i in the
regression equation j. Thus there is a non zero
partial regression coefficient Ahat[i,j]
corresponding to each non zero value amat[j,i]
in the adjacency matrix
Dhat a vector containing the partial variances of
each variable given the parents. If norm=2
then the partial variance of the latent variable is
constrained to 1.
dev the deviance of the fitted model
df the degrees of freedom of the model
it the number of EM algorithm iterations at
convergence.
Giovanni M. Marchetti
Kiiveri,H. T. (1987). An incomplete data approach to the analysis of covariance structures. Psychometrika, 52, 4, 539-554.
Joreskog, K.G. and Goldberger, A.S. (1975). Estimation of a model with multiple indicators and multiple causes of a single latent variable. Journal of the American Statistical Association, 10, 631-639.
# data from Joreskog and Goldberger (1975) V <- matrix(c( 1, 0.36, 0.21, 0.10, 0.156, 0.158, 0.36, 1, 0.265, 0.284, 0.192, 0.324, 0.210, 0.265, 1, 0.176, 0.136, 0.226, 0.1, 0.284, 0.176, 1, 0.304, 0.305, 0.156, 0.192, 0.136, 0.304, 1, 0.344, 0.158, 0.324, 0.226, 0.305, 0.344, 1 ), 6, 6) nod <- c("y1", "y2", "y3", "x1", "x2", "x3") dimnames(V) <- list(nod, nod) dag <- DAG(y1 ~ z, y2 ~ z, y3 ~ z, z ~ x1 + x2 + x3, x1 ~ x2 + x3, x2 ~ x3) fitDagLatent(dag, V, n = 530, latent = "z", seed = 4564) fitDagLatent(dag, V, n = 530, latent = "z", norm = 2, seed = 145)# data from Joreskog and Goldberger (1975) V <- matrix(c( 1, 0.36, 0.21, 0.10, 0.156, 0.158, 0.36, 1, 0.265, 0.284, 0.192, 0.324, 0.210, 0.265, 1, 0.176, 0.136, 0.226, 0.1, 0.284, 0.176, 1, 0.304, 0.305, 0.156, 0.192, 0.136, 0.304, 1, 0.344, 0.158, 0.324, 0.226, 0.305, 0.344, 1 ), 6, 6) nod <- c("y1", "y2", "y3", "x1", "x2", "x3") dimnames(V) <- list(nod, nod) dag <- DAG(y1 ~ z, y2 ~ z, y3 ~ z, z ~ x1 + x2 + x3, x1 ~ x2 + x3, x2 ~ x3) fitDagLatent(dag, V, n = 530, latent = "z", seed = 4564) fitDagLatent(dag, V, n = 530, latent = "z", norm = 2, seed = 145)
Fits a logistic regression model to multivariate binary responses. The explanatory variables can be binary and/or individual-level continuous covariates.
fitmlogit(..., C = c(), D = c(), mit = 100, ep = 1e-80, acc = 1e-04, daf)fitmlogit(..., C = c(), D = c(), mit = 100, ep = 1e-80, acc = 1e-04, daf)
... |
Model formulae of marginal logistic models for each response and for each association parameters (log-odds ratios). |
C |
Matrix of equality constraints. |
D |
Matrix of inequality constraints. |
mit |
A positive integer: maximum number of iterations.
(default: |
ep |
A tolerance used in the algorithm
(default |
acc |
A tolerance used in the algorithm
(default |
daf |
A data frame for the individual units containing the responses and the explanatory variables. |
See Evans and Forcina (2013).
LL The maximized log-likelihood.
be The vector of the ML estimates of the parameters.
S The estimated asymptotic covariance matrix.
P The estimated cell probabilities for each individual.
Antonio Forcina, Giovanni M. Marchetti
Evans, R.J. and Forcina, A. (2013). Two algorithms for fitting constrained marginal models. Computational Statistics and Data Analysis, 66, 1-7.
data(surdata) out1 <- fitmlogit(A ~ X, B ~ Z, cbind(A, B) ~ X * Z, daf = surdata) out1$beta out2 <- fitmlogit(A ~ X, B ~ Z, cbind(A, B) ~ 1, daf = surdata) out2$betadata(surdata) out1 <- fitmlogit(A ~ X, B ~ Z, cbind(A, B) ~ X * Z, daf = surdata) out1$beta out2 <- fitmlogit(A ~ X, B ~ Z, cbind(A, B) ~ 1, daf = surdata) out2$beta
Finds the list of fundamental cycles of a connected undirected graph.
fundCycles(amat)fundCycles(amat)
amat |
A symmetric matrix with dimnames denoting
the adjacency matrix of the undirected graph. The graph must
be connected, otherwise the function returns |
All the cycles in an UG can be obtained from combination (ring sum) of the set of fundamental cycles.
This function is used by cycleMatrix() and
isGident().
A list of matrices with two columns.
Every component of the list is associated to a cycle.
The cycle is described by a
matrix whose rows are the edges of the cycle.
If there is no cycle, the function returns NULL.
Giovanni M. Marchetti
Thulasiraman, K. & Swamy, M.N.S. (1992). Graphs: theory and algorithms. New York: Wiley.
UG(), findPath(), cycleMatrix()
isGident(), bfsearch()
## Three fundamental cycles fundCycles(UG(~ a * b * d + d * e + e * a * f))## Three fundamental cycles fundCycles(UG(~ a * b * d + d * e + e * a * f))
Data on glucose control of diabetes patients.
glucoseglucose
A data frame with 68 observations on the following 8 variables.
a numeric vector, Glucose control (glycosylated haemoglobin), values up to about 7 or 8 indicate good glucose control.
a numeric vector, a score for knowledge about the illness.
a numeric vector, a score for fatalistic externality (mere chance determines what occurs).
a numeric vector, a score for social externality (powerful others are responsible).
a numeric vector, a score for internality (the patient is him or herself responsible).
a numeric vector, duration of the illness in years.
a
numeric vector, level of education, with levels -1: at least 13 years
of formal schooling, 1: less then 13 years.
a numeric
vector, gender with levels -1: females, 1: males.
Data on 68 patients with fewer than 25 years of diabetes. They were collected at the University of Mainz to identify psychological and socio-economic variables possibly important for glucose control, when patients choose the appropriate dose of treatment depending on the level of blood glucose measured several times per day.
The variable of primary interest is Y, glucose control, measured by
glycosylated haemoglobin. X, knowledge about the illness, is a
response of secondary interest. Variables Z, U and V
measure patients' type of attribution, called fatalistic externality, social
externality and internality. These are intermediate variables. Background
variables are W, the duration of the illness, A the duration
of formal schooling and B, gender. The background variables A
and B are binary variables with coding -1, 1.
Cox & Wermuth (1996), p. 229.
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
data(glucose) ## See Cox & Wermuth (1996), Figure 6.3 p. 140 coplot(Y ~ W | A, data=glucose)data(glucose) ## See Cox & Wermuth (1996), Figure 6.3 p. 140 coplot(Y ~ W | A, data=glucose)
grMAT converts graph objects to a mixed adjacency matrix.
grMAT(agr)grMAT(agr)
agr |
A graph object. This can be:
|
Support for graphNEL objects requires the graph package
from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
Kayvan Sadeghi, Giovanni Marchetti
## Generating the adjacency matrix from a vector exvec <- c( "b", 1, 2, "b", 1, 14, "a", 9, 8, "l", 9, 11, "a", 10, 8, "a", 11, 2, "a", 11, 10, "a", 12, 1, "b", 12, 14, "a", 13, 10, "a", 13, 12) grMAT(exvec) ## Not run: ## Example with graphNEL (requires the 'graph' package from Bioconductor) if (requireNamespace("graph", quietly = TRUE)) { V <- c("a", "b", "c") g <- graph::graphNEL(nodes = V, edgemode = "undirected") grMAT(g) } ## End(Not run) g <- c("a", 3, 1, "l", 1, 2, "*", 4, 4) grMAT(g)## Generating the adjacency matrix from a vector exvec <- c( "b", 1, 2, "b", 1, 14, "a", 9, 8, "l", 9, 11, "a", 10, 8, "a", 11, 2, "a", 11, 10, "a", 12, 1, "b", 12, 14, "a", 13, 10, "a", 13, 12) grMAT(exvec) ## Not run: ## Example with graphNEL (requires the 'graph' package from Bioconductor) if (requireNamespace("graph", quietly = TRUE)) { V <- c("a", "b", "c") g <- graph::graphNEL(nodes = V, edgemode = "undirected") grMAT(g) } ## End(Not run) g <- c("a", 3, 1, "l", 1, 2, "*", 4, 4) grMAT(g)
Create a Boolean matrix from all combinations of binary variables
hypercube(n)hypercube(n)
n |
a positive integer, |
a matrix with rows and columns.
Each row of the matrix is a TRUE/FALSE vector
representing the coordinates of the vertices of
a hypercube, ordered in inverse lexicographic order.
hypercube(4)hypercube(4)
Finds the indicator matrix of the zeros of a matrix.
In(A)In(A)
A |
a matrix. |
The indicator matrix is a matrix of zeros and ones which has a zero element
iff the corresponding element of A is (exactly) zero.
a matrix of the same dimensions as A.
Giovanni M. Marchetti
Wermuth, N. & Cox, D.R. (2004). Joint response graphs and separation induced by triangular systems. J.R. Statist. Soc. B, 66, Part 3, 687-717.
DAG(), inducedCovGraph(), inducedConGraph(),
In().
# A simple way to find the overall induced concentration graph # The DAG on p. 198 of Cox & Wermuth (1996) amat <- DAG(y1 ~ y2 + y3, y3 ~ y5, y4 ~ y5) A <- edgematrix(amat) In(crossprod(A))# A simple way to find the overall induced concentration graph # The DAG on p. 198 of Cox & Wermuth (1996) amat <- DAG(y1 ~ y2 + y3, y3 ~ y5, y4 ~ y5) A <- edgematrix(amat) In(crossprod(A))
Functions to find induced graphs after conditioning on a set of variables and marginalizing over another set.
inducedChainGraph(amat, cc = rownames(amat), cond = NULL, type = "LWF") inducedCovGraph(amat, sel = rownames(amat), cond = NULL) inducedConGraph(amat, sel = rownames(amat), cond = NULL) inducedRegGraph(amat, sel = rownames(amat), cond = NULL) inducedDAG(amat, order, cond = NULL)inducedChainGraph(amat, cc = rownames(amat), cond = NULL, type = "LWF") inducedCovGraph(amat, sel = rownames(amat), cond = NULL) inducedConGraph(amat, sel = rownames(amat), cond = NULL) inducedRegGraph(amat, sel = rownames(amat), cond = NULL) inducedDAG(amat, order, cond = NULL)
amat |
a square Boolean matrix, the adjacency matrix of a directed acyclic graph. The names of rows and of the columns are the nodes of the DAG. |
cc |
a list of character vectors specifying the chain components for the chain graph. |
cond |
a character vector representing the variables on which
you want to condition. |
type |
a string indicating the interpretation of the chain graph. It can be either
|
sel |
a character vector representing a subset of selected
variables. The elements of the vector must be a subset of the names
of the nodes i.e., of |
order |
a character vector indicating the ordering of the vertices of a DAG (left to right, past to future). |
Given a directed acyclic graph representing a set of conditional independencies it is possible to obtain other graphs of conditional independence implied after marginalizing over and conditionig on sets of nodes. Such graphs are the covariance graph, the concentration graph, the multivariate regression graph and the chain graph with different interpretations (see Cox & Wermuth, 1996, 2004).
inducedCovGraph returns the adjacency matrix of the covariance
graph of the variables in set sel given the variables in
set cond, implied by the original directed acyclic graph with
adjacency matrix amat.
inducedConGraph returns the adjacency matrix of the concentration
graph of the variables in set sel given the variables in set
cond, implied by the original directed acyclic graph with
adjacency matrix amat.
inducedRegGraph returns the adjacency matrix of the multivariate
regression graph of the variables in set sel given the variables in
set cond, implied by the original directed acyclic graph with
adjacency matrix amat.
inducedChainGraph returns the adjacency matrix of the chain graph
for the variables in the chain components cc, given the variables
in set cond, with interpretation specified by string
type, implied by the original directed acyclic graph with
adjacency matrix amat.
inducedDAG returns the adjacency matrix of the DAG with the
ordering order, implied by the original directed acyclic graph
with adjacency matrix amat.
If sel = NULL the functions return the null matrix.
If cond = NULL, the conditioning set is empty and the functions
inducedConGraph and inducedCovGraph return the overall induced
covariance or concentration matrices of the selected variables. I
f you do not specify sel you cannot specify a non-NULL
value of cond.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
Wermuth, N. & Cox, D.R. (2004). Joint response graphs and separation induced by triangular systems. J. R. Statist. Soc. B, 66, 687-717.
if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) # Define a DAG dag <- DAG(a ~ x, c ~ b + d, d ~ x) dag # Induced covariance graph of a, b, d given the empty set. inducedCovGraph(dag, sel = c("a", "b", "d"), cond = NULL) # Induced concentration graph of a, b, c given x inducedConGraph(dag, sel = c("a", "b", "c"), cond = "x") # Overall covariance graph inducedCovGraph(dag) # Overall concentration graph inducedConGraph(dag) # Induced covariance graph of x, b, d given c, x. inducedCovGraph(dag, sel = c("a", "b", "d"), cond = c("c", "x")) # Induced concentration graph of a, x, c given d, b. inducedConGraph(dag, sel = c("a", "x", "c"), cond = c("d", "b")) # The DAG on p. 198 of Cox & Wermuth (1996) dag <- DAG(y1 ~ y2 + y3, y3 ~ y5, y4 ~ y5) # Cf. figure 8.7 p. 203 in Cox & Wermuth (1996) inducedCovGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = "y1") inducedCovGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = "y3") inducedCovGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = "y5") # Cf. figure 8.8 p. 203 in Cox & Wermuth (1996) inducedConGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = "y1") inducedConGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = "y3") inducedConGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = "y5") # Cf. figure 8.9 p. 204 in Cox & Wermuth (1996) inducedCovGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = NULL) inducedCovGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = NULL) inducedCovGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = NULL) # Cf. figure 8.10 p. 204 in Cox & Wermuth (1996) inducedConGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = NULL) inducedConGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = NULL) inducedConGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = NULL) # An induced regression graph dag2 <- DAG(Y ~ X + U, W ~ Z + U) inducedRegGraph(dag2, sel = "W", cond = c("Y", "X", "Z")) # An induced DAG inducedDAG(dag2, order = c("X", "Y", "Z", "W")) # An induced multivariate regression graph inducedRegGraph(dag2, sel = c("Y", "W"), cond = c("X", "Z")) # An induced chain graph with LWF interpretation dag3 <- DAG(X ~ W, W ~ Y, U ~ Y + Z) cc <- list(c("W", "U"), c("X", "Y", "Z")) inducedChainGraph(dag3, cc = cc, type = "LWF") # ... with AMP interpretation inducedChainGraph(dag3, cc = cc, type = "AMP") # ... with multivariate regression interpretation cc <- list(c("U"), c("Z", "Y"), c("X", "W")) inducedChainGraph(dag3, cc = cc, type = "MRG") }if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) # Define a DAG dag <- DAG(a ~ x, c ~ b + d, d ~ x) dag # Induced covariance graph of a, b, d given the empty set. inducedCovGraph(dag, sel = c("a", "b", "d"), cond = NULL) # Induced concentration graph of a, b, c given x inducedConGraph(dag, sel = c("a", "b", "c"), cond = "x") # Overall covariance graph inducedCovGraph(dag) # Overall concentration graph inducedConGraph(dag) # Induced covariance graph of x, b, d given c, x. inducedCovGraph(dag, sel = c("a", "b", "d"), cond = c("c", "x")) # Induced concentration graph of a, x, c given d, b. inducedConGraph(dag, sel = c("a", "x", "c"), cond = c("d", "b")) # The DAG on p. 198 of Cox & Wermuth (1996) dag <- DAG(y1 ~ y2 + y3, y3 ~ y5, y4 ~ y5) # Cf. figure 8.7 p. 203 in Cox & Wermuth (1996) inducedCovGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = "y1") inducedCovGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = "y3") inducedCovGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = "y5") # Cf. figure 8.8 p. 203 in Cox & Wermuth (1996) inducedConGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = "y1") inducedConGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = "y3") inducedConGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = "y5") # Cf. figure 8.9 p. 204 in Cox & Wermuth (1996) inducedCovGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = NULL) inducedCovGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = NULL) inducedCovGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = NULL) # Cf. figure 8.10 p. 204 in Cox & Wermuth (1996) inducedConGraph(dag, sel = c("y2", "y3", "y4", "y5"), cond = NULL) inducedConGraph(dag, sel = c("y1", "y2", "y4", "y5"), cond = NULL) inducedConGraph(dag, sel = c("y1", "y2", "y3", "y4"), cond = NULL) # An induced regression graph dag2 <- DAG(Y ~ X + U, W ~ Z + U) inducedRegGraph(dag2, sel = "W", cond = c("Y", "X", "Z")) # An induced DAG inducedDAG(dag2, order = c("X", "Y", "Z", "W")) # An induced multivariate regression graph inducedRegGraph(dag2, sel = c("Y", "W"), cond = c("X", "Z")) # An induced chain graph with LWF interpretation dag3 <- DAG(X ~ W, W ~ Y, U ~ Y + Z) cc <- list(c("W", "U"), c("X", "Y", "Z")) inducedChainGraph(dag3, cc = cc, type = "LWF") # ... with AMP interpretation inducedChainGraph(dag3, cc = cc, type = "AMP") # ... with multivariate regression interpretation cc <- list(c("U"), c("Z", "Y"), c("X", "W")) inducedChainGraph(dag3, cc = cc, type = "MRG") }
Checks if a given graph is acyclic.
isAcyclic(amat, method = 2)isAcyclic(amat, method = 2)
amat |
a square Boolean matrix with dimnames, the adjacency matrix of a graph. |
method |
an integer 1 or 2 specifying the method used. If
|
a logical value, TRUE if the graph is acyclic and
FALSE otherwise.
David Edwards, Giovanni M. Marchetti
Aho, A.V., Hopcroft, J.E. & Ullman, J.D. (1983). Data structures and algorithms. Reading: Addison-Wesley.
# A cyclic graph d <- matrix(0, 3, 3) rownames(d) <- colnames(d) <- c("x", "y", "z") d["x", "y"] <- d["y", "z"] <- d["z", "x"] <- 1 ## Test if the graph is acyclic isAcyclic(d) isAcyclic(d, method = 1)# A cyclic graph d <- matrix(0, 3, 3) rownames(d) <- colnames(d) <- c("x", "y", "z") d["x", "y"] <- d["y", "z"] <- d["z", "x"] <- 1 ## Test if the graph is acyclic isAcyclic(d) isAcyclic(d, method = 1)
Check if it is an adjacency matrix of an ADMG.
isADMG(amat)isADMG(amat)
amat |
An adjacency matrix. |
Checks if the following conditions must hold:
no undirected edge meets an arrowhead
no directed cycles
A logical value, TRUE if it is an ADMG and
FALSE otherwise.
Giovanni M. Marchetti, Mathias Drton
Richardson, T. S. and Spirtes, P. (2002). Ancestral Graph Markov Models. Annals of Statistics, 30(4), 962-1030.
# Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg = DAG(a ~ b, b ~ d, d ~ c), bg = UG(~ a * c)) isADMG(a1) # Not an AG. (a2) p.969 a2 <- makeMG(dg = DAG(b ~ a, d ~ c), bg = UG(~ a * c + c * b + b * d)) # Fig. 3 (b1) p.969 isADMG(a2)# Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg = DAG(a ~ b, b ~ d, d ~ c), bg = UG(~ a * c)) isADMG(a1) # Not an AG. (a2) p.969 a2 <- makeMG(dg = DAG(b ~ a, d ~ c), bg = UG(~ a * c + c * b + b * d)) # Fig. 3 (b1) p.969 isADMG(a2)
Check if it is an adjacency matrix of an ancestral graph.
isAG(amat)isAG(amat)
amat |
an adjacency matrix. |
Checks if the following conditions hold:
no undirected edge meets an arrowhead
no directed cycles
spouses cannot be ancestors. For details see Richardson and Spirtes (2002).
a logical value. TRUE if it is an ancestral
graph and FALSE otherwise.
Giovanni M. Marchetti, Mathias Drton
Richardson, T. S. and Spirtes, P. (2002). Ancestral Graph Markov Models. Annals of Statistics, 30(4), 962-1030.
# Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg = DAG(a ~ b, b ~ d, d ~ c), bg = UG(~ a * c)) suppressWarnings(isAG(a1)) # Not an AG. (a2) p. 969 a2 <- makeMG(dg = DAG(b ~ a, d ~ c), bg = UG(~ a * c + c * b + b * d)) # Fig. 3 (b1) p.969 isAG(a2)# Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg = DAG(a ~ b, b ~ d, d ~ c), bg = UG(~ a * c)) suppressWarnings(isAG(a1)) # Not an AG. (a2) p. 969 a2 <- makeMG(dg = DAG(b ~ a, d ~ c), bg = UG(~ a * c + c * b + b * d)) # Fig. 3 (b1) p.969 isAG(a2)
Tests if an undirected graph is G-identifiable.
isGident(amat)isGident(amat)
amat |
a symmetric matrix with dimnames representing the adjacency matrix of an undirected graph |
An undirected graph is said G-identifiable if every connected component of the complementary graph contains an odd cycle (Stanghellini and Wermuth, 2005). See also Tarantola and Vicard (2002).
a logical value, TRUE if the graph is G-identifiable and
FALSE if it is not.
Giovanni M. Marchetti
UG(), cmpGraph(), cycleMatrix()
Stanghellini, E. & Wermuth, N. (2005). On the identification of path-analysis models with one hidden variable. Biometrika, 92(2), 337-350.
Stanghellini, E. (1997). Identification of a single-factor model using graphical Gaussian rules. Biometrika, 84, 241-244.
Tarantola, C. & Vicard, P. (2002). Spanning trees and identifiability of a single-factor model. Statistical Methods & Applications, 11, 139–152.
Vicard, P. (2000). On the identification of a single-factor model with correlated residuals. Biometrika, 87, 199-205.
# A not G-identifiable UG G1 <- UG(~ a * b + u * v) isGident(G1) # G-identifiable UG G2 <- UG(~ a + b + u * v) isGident(G2) # G-identifiable UG G3 <- cmpGraph(UG(~ a * b * c + x * y * z)) isGident(G3)# A not G-identifiable UG G1 <- UG(~ a * b + u * v) isGident(G1) # G-identifiable UG G2 <- UG(~ a + b + u * v) isGident(G2) # G-identifiable UG G3 <- cmpGraph(UG(~ a * b * c + x * y * z)) isGident(G3)
Computes the deviance of a Gaussian model given the concentration matrix, the sample covariance matrix, the sample size, and the number of variables.
likGau(K, S, n, k)likGau(K, S, n, k)
K |
A square concentration matrix (inverse covariance matrix). |
S |
A square sample covariance matrix. |
n |
An integer indicating the sample size. |
k |
An integer indicating the number of variables
(number of rows of |
A numeric value representing the deviance of the Gaussian model.
Kayvan Sadeghi, Giovanni M. Marchetti
MAG generates and plots maximal ancestral graphs after
marginalisation and conditioning.
MAG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )MAG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )
amat |
An adjacency matrix, or a graph object. This can be:
|
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
|
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG() and Max().
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
Kayvan Sadeghi
Richardson, T. S. and Spirtes, P. (2002). Ancestral graph Markov models. Annals of Statistics, 30(4), 962-1030.
Sadeghi, K. (2013). Stable mixed graphs. Bernoulli, 19(5B), 2330–2358.
Sadeghi, K. and Lauritzen, S.L. (2014). Markov properties for loopless mixed graphs. Bernoulli, 20(2), 676-696.
# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) MAG(ex, M, C, plot = TRUE) H <- matrix(c(0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0), 4, 4) Max(H)# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) MAG(ex, M, C, plot = TRUE) H <- matrix(c(0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0), 4, 4) Max(H)
Defines a loopless mixed graph from directed, undirected and bidirected components.
makeMG(dg = NULL, ug = NULL, bg = NULL)makeMG(dg = NULL, ug = NULL, bg = NULL)
dg |
The adjacency matrix of a directed graph specifying the arrows of the mixed graph. |
ug |
The adjacency matrix of an undirected graph specifying the lines of the mixed graph. |
bg |
The adjacency matrix of an undirected graph specifying the bidirected edges of the mixed graph. |
Note that the three adjacency matrices must have labels
and may be defined using the functions DG (or 'DAG'),
UG. The adjacency matrices of the undirected graphs
may be just symmetric Boolean matrices.
A square matrix obtained by combining the three
graph components into an adjacency matrix of a mixed
graph. The matrix consists of 4 different integers as
an -element:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and
.
These numbers are added to be associated with
multiple edges of different types. The matrix is
symmetric w.r.t full lines and bi-directed arrows.
Giovanni M. Marchetti, Mathias Drton, Kayvan Sadeghi
Richardson, T. S. and Spirtes, P. (2002). Ancestral Graph Markov Models. Annals of Statistics, 30(4), 962–1030.
# Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg = DAG(a ~ b, b ~ d, d ~ c), bg = UG(~ a * c)) suppressWarnings(isAG(a1)) # Not an AG. (a2) p.969 a2 <- makeMG(dg = DAG(b ~ a, d ~ c), bg = UG(~ a * c + c * b + b * d)) # Fig. 3 (b1) p.969 isAG(a2) a3 <- makeMG(ug = UG(~ a * c), dg = DAG(b ~ a, d ~ c), bg = UG(~ b * d)) # Fig. 3 (b2) p.969 a3 a5 <- makeMG(bg = UG(~ alpha * beta + gamma * delta), dg = DAG(alpha ~ gamma, delta ~ beta)) # Fig. 6 p. 973 a5 # Another Example a4 <- makeMG(ug = UG(~ y0 * y1), dg = DAG(y4 ~ y2, y2 ~ y1), bg = UG(~ y2 * y3 + y3 * y4)) a4 # A mixed graphs with double edges mg <- makeMG( dg = DG(Y ~ X, Z ~ W, W ~ Z, Q ~ X), ug = UG(~ X * Q), bg = UG(~ Y * X + X * Q + Q * W + Y * Z) ) mg # Chronic pain data: a regression graph chronic_pain <- makeMG( dg = DAG(Y ~ Za, Za ~ Zb + A, Xa ~ Xb, Xb ~ U + V, U ~ A + V, Zb ~ B, A ~ B), bg = UG(~ Za * Xa + Zb * Xb) ) chronic_pain# Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg = DAG(a ~ b, b ~ d, d ~ c), bg = UG(~ a * c)) suppressWarnings(isAG(a1)) # Not an AG. (a2) p.969 a2 <- makeMG(dg = DAG(b ~ a, d ~ c), bg = UG(~ a * c + c * b + b * d)) # Fig. 3 (b1) p.969 isAG(a2) a3 <- makeMG(ug = UG(~ a * c), dg = DAG(b ~ a, d ~ c), bg = UG(~ b * d)) # Fig. 3 (b2) p.969 a3 a5 <- makeMG(bg = UG(~ alpha * beta + gamma * delta), dg = DAG(alpha ~ gamma, delta ~ beta)) # Fig. 6 p. 973 a5 # Another Example a4 <- makeMG(ug = UG(~ y0 * y1), dg = DAG(y4 ~ y2, y2 ~ y1), bg = UG(~ y2 * y3 + y3 * y4)) a4 # A mixed graphs with double edges mg <- makeMG( dg = DG(Y ~ X, Z ~ W, W ~ Z, Q ~ X), ug = UG(~ X * Q), bg = UG(~ Y * X + X * Q + Q * W + Y * Z) ) mg # Chronic pain data: a regression graph chronic_pain <- makeMG( dg = DAG(Y ~ Za, Za ~ Zb + A, Xa ~ Xb, Xb ~ U + V, U ~ A + V, Zb ~ B, A ~ B), bg = UG(~ Za * Xa + Zb * Xb) ) chronic_pain
Provides the contrast and marginalization matrices for the marginal parametrization of a probability vector.
marg.param(lev, type)marg.param(lev, type)
lev |
Integer vector containing the number of levels of each variable. |
type |
A character vector with elements
|
See Bartolucci, Colombi and Forcina (2007).
The function assumes that the vector of
probabilities is in inverse lexicographic order.
The interactions are returned in order of dimension,
like e.g., 1, 2, 3, 12, 13, 23, 123.
a list with slots
C Matrix of constrasts (the first sum(lev)-length(r)
elements are referred to univariate logits)
M Marginalization matrix with elements 0 and 1
G The design matrix for the corresponding log-linear model.
Francesco Bartolucci, Antonio Forcina. Giovanni M. Marchetti translated the Matlab function by Bartolucci and Forcina.
Bartolucci, F., Colombi, R. and Forcina, A. (2007). An extended class of marginal link functions for modelling contingency tables by equality and inequality constraints. Statistica Sinica, 17, 691-711.
marg.param(c(3, 3), c("l", "g"))marg.param(c(3, 3), c("l", "g"))
MarkEqMag determines whether two maximal ancestral graphs (MAGs)
are Markov equivalent. The function checks whether the two graphs
have the same skeleton and colliders with order.
MarkEqMag(amat, bmat)MarkEqMag(amat, bmat)
amat |
An adjacency matrix, or a graph object. This can be:
|
bmat |
The same as |
@details Support for graphNEL objects requires the graph package
from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
A character string: either "Markov Equivalent" or "NOT Markov Equivalent".
Kayvan Sadeghi
Ali, R.A., Richardson, T.S. and Spirtes, P. (2009) Markov equivalence for ancestral graphs. Annals of Statistics, 37(5B), 2808-2837.
H1 <- matrix(c( 0, 100, 0, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) H2 <- matrix(c( 0, 0, 0, 0, 1, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) H3 <- matrix(c( 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 100, 0, 1, 100, 0 ), 4, 4) MarkEqMag(H1, H2) MarkEqMag(H1, H3) MarkEqMag(H2, H3)H1 <- matrix(c( 0, 100, 0, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) H2 <- matrix(c( 0, 0, 0, 0, 1, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) H3 <- matrix(c( 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 100, 0, 1, 100, 0 ), 4, 4) MarkEqMag(H1, H2) MarkEqMag(H1, H3) MarkEqMag(H2, H3)
MarkEqRcg determines whether two regression chain graphs
(RCGs) or subclasses of RCGs are Markov equivalent.
The function checks whether the two graphs have the same
skeleton and "unshielded colliders".
MarkEqRcg(amat, bmat)MarkEqRcg(amat, bmat)
amat |
An adjacency matrix, or a graph object. This can be:
|
bmat |
The same as |
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
A character string: either "Markov Equivalent" or "NOT Markov Equivalent".
Kayvan Sadeghi
Wermuth, N. and Sadeghi, K. (2012). Sequences of regressions and their independences. Test, 21:215-252.
H1 <- matrix(c( 0, 100, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 1, 100, 0 ), 5, 5) H2 <- matrix(c( 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 1, 100, 0 ), 5, 5) H3 <- matrix(c( 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 ), 5, 5) MarkEqRcg(H1, H2) MarkEqRcg(H1, H3) MarkEqRcg(H2, H3)H1 <- matrix(c( 0, 100, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 1, 100, 0 ), 5, 5) H2 <- matrix(c( 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 1, 100, 0 ), 5, 5) H3 <- matrix(c( 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 ), 5, 5) MarkEqRcg(H1, H2) MarkEqRcg(H1, H3) MarkEqRcg(H2, H3)
Examination marks of 88 students in five subjects.
marksmarks
A data frame with 88 observations on the following 5 variables.
a numeric vector, mark in Mechanics
a numeric vector, mark in Vectors
a numeric vector, mark in Algebra
a numeric vector, mark in Analysis
a numeric vector, mark in Statistics
Mechanics and Vectors were closed book examinations. Algebra, Analysis and Statistics were open book examinations.
Mardia, K.V., Kent, J.T. and Bibby, J.M. (1979). Multivariate analysis. London: Academic Press.
Whittaker, J. (1990). Graphical models in applied multivariate statistics. Chichester: Wiley.
data(marks) pairs(marks)data(marks) pairs(marks)
Find matrices C (contrast) and M (marginalization)
of a binary multivariate logistic parameterization
mat.mlogit(d, P = powerset(1:d))mat.mlogit(d, P = powerset(1:d))
d |
A positive integer, the number of binary responses. |
P |
A list of vectors of integers specifying the
margins. For instance: |
The power set used in the marginal model is listed in the order of dimensions of the sets.
C A contrast matrix
L A marginalization matrix
Giovanni M. Marchetti
Glonek, G. J. N. and McCullagh, P. (1995). Multivariate logistic models. Journal of the Royal Statistical Society, Ser. B, 57, 533-546.
stats::binomial(), marg.param().
mat.mlogit(2)mat.mlogit(2)
Max generates a maximal graph that induces the same
independence model from a non-maximal graph.
Max(amat)Max(amat)
amat |
An adjacency matrix, or a graph object. This can be:
|
Max looks for non-adjacent pais of nodes that are connected by
primitive inducing paths, and connect such pairs by an
appropriate edge.
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG().
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
Kayvan Sadeghi
Richardson, T.S. and Spirtes, P. (2002). Ancestral graph Markov models. Annals of Statistics, 30(4), 962-1030.
Sadeghi, K. and Lauritzen, S.L. (2014). Markov properties for loopless mixed graphs. Bernoulli, 20(2), 676-696.
H <- matrix(c( 0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) Max(H)H <- matrix(c( 0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) Max(H)
MRG generates and plots maximal ribbonless graphs
(a modification of MC graph to use m-separation) after
marginalisation and conditioning.
MRG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )MRG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )
amat |
An adjacency matrix, or a graph object. This can be:
|
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
|
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
Support for graphNEL objects requires the graph package
from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG() and Max().
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
This function uses the functions RG() and Max().
Kayvan Sadeghi
Koster, J.T.A. (2002). Marginalizing and conditioning in graphical models. Bernoulli, 8(6), 817-840.
Richardson, T.S. and Spirtes, P. (2002). Ancestral graph Markov models. Annals of Statistics, 30(4), 962-1030.
Sadeghi, K. (2013). Stable mixed graphs. Bernoulli, 19(5B), 2330–2358.
Sadeghi, K. and Lauritzen, S.L. (2014). Markov properties for loopless mixed graphs. Bernoulli, 20(2), 676-696.
# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) MRG(ex, M, C, plot = TRUE) H <- matrix(c( 0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) Max(H)# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) MRG(ex, M, C, plot = TRUE) H <- matrix(c( 0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0 ), 4, 4) Max(H)
msep determines whether two set of nodes are m-separated
by a third set of nodes.
msep(amat, alpha, beta, C = c())msep(amat, alpha, beta, C = c())
amat |
An adjacency matrix, or a graph object. This can be:
|
alpha |
A subset of the node set of |
beta |
Another disjoint subset of the node set of |
C |
A third disjoint subset of the node set of |
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG() and Max().
A logical value: TRUE if alpha and beta are
m-separated given C or FALSE otherwise.
Kayvan Sadeghi
Richardson, T.S. and Spirtes, P. (2002) Ancestral graph Markov models. Annals of Statistics, 30(4), 962-1030.
Sadeghi, K. and Lauritzen, S.L. (2014). Markov properties for loopless mixed graphs. Bernoulli, 20(2), 676-696.
H <- matrix(c( 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 ), 4, 4) msep(H, 1, 4, 2) msep(H, 1, 4, c()) dimnames(H) <- list(c("A", "B", "C", "D"), c("A", "B", "C", "D")) msep(H, "A", "D", "B") msep(H, "A", "D", c()) a <- makeMG(dg= DG(W ~ Z, Z ~ Y + X), bg= UG(~ Y*Z)) a msep(a, "X", "Y", "Z") msep(a, "X", "Y", c()) msep(a, "X", "Y") library(igraph) g <- make_graph(~ X -+ Z, Y -+ Z) msep(g, "X", "Y", "Z") msep(g, "X", "Y", c())H <- matrix(c( 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 ), 4, 4) msep(H, 1, 4, 2) msep(H, 1, 4, c()) dimnames(H) <- list(c("A", "B", "C", "D"), c("A", "B", "C", "D")) msep(H, "A", "D", "B") msep(H, "A", "D", c()) a <- makeMG(dg= DG(W ~ Z, Z ~ Y + X), bg= UG(~ Y*Z)) a msep(a, "X", "Y", "Z") msep(a, "X", "Y", c()) msep(a, "X", "Y") library(igraph) g <- make_graph(~ X -+ Z, Y -+ Z) msep(g, "X", "Y", "Z") msep(g, "X", "Y", c())
MSG generates and plots maximal summary graphs after
marginalization and conditioning.
MSG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )MSG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )
amat |
An adjacency matrix, or a graph object. This can be:
|
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
|
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
This function uses the functions SG() and Max().
Kayvan Sadeghi
Richardson, T.S. and Spirtes, P. (2002). Ancestral graph Markov models. Annals of Statistics, 30(4), 962-1030.
Sadeghi, K. (2013). Stable mixed graphs. Bernoulli, 19(5B), 2330–2358.
Sadeghi, K. and Lauritzen, S.L. (2014). Markov properties for loopless mixed graphs. Bernoulli, 20(2), 676-696.
Wermuth, N. (2011). Probability distributions with summary graph structure. Bernoulli, 17(3), 845-879.
# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) MSG(ex, M, C, plot = TRUE) H <- matrix(c(0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0), 4, 4) Max(H)# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) MSG(ex, M, C, plot = TRUE) H <- matrix(c(0, 100, 1, 0, 100, 0, 100, 0, 0, 100, 0, 100, 0, 1, 100, 0), 4, 4) Max(H)
Given a matrix M find a matrix N such that
.
null(M)null(M)
M |
A matrix. |
The matrix N with the basis for the null space, or an empty
vector if the matrix M is square and of maximal rank.
null(c(1, 1, 1))null(c(1, 1, 1))
Finds the matrix of the partial correlations between pairs of variables given the rest.
parcor(S)parcor(S)
S |
a symmetric positive definite matrix, representing a covariance matrix. |
The algorithm computes
where are concentrations,
i.e. elements of the inverse covariance matrix.
A symmetric matrix with ones along the diagonal and in position
the partial correlation between variables and
given all the remaining variables.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). *Multivariate dependencies]. London: Chapman & Hall.
stats::var(), stats::cor(), correlations()
# Partial correlations for the mathematics marks data data(marks) S <- var(marks) parcor(S)# Partial correlations for the mathematics marks data data(marks) S <- var(marks) parcor(S)
Computes the partial correlation between two variables given a set of other variables.
pcor(u, S)pcor(u, S)
u |
a vector of integers of length |
S |
a symmetric positive definite matrix, a sample covariance matrix. |
a scalar, the partial correlation matrix between
variables u[1] and u[2] given u[-c(1,2)].
Giovanni M. Marchetti
cor(), parcor(), correlations()
data(marks) # The correlation between vectors and algebra # given analysis and statistics pcor(c("vectors", "algebra", "analysis", "statistics"), var(marks)) # The same pcor(c(2, 3, 4, 5), var(marks)) # The correlation between vectors and algebra given statistics pcor(c("vectors", "algebra", "statistics"), var(marks)) # The marginal correlation between analysis and statistics pcor(c("analysis", "statistics"), var(marks))data(marks) # The correlation between vectors and algebra # given analysis and statistics pcor(c("vectors", "algebra", "analysis", "statistics"), var(marks)) # The same pcor(c(2, 3, 4, 5), var(marks)) # The correlation between vectors and algebra given statistics pcor(c("vectors", "algebra", "statistics"), var(marks)) # The marginal correlation between analysis and statistics pcor(c("analysis", "statistics"), var(marks))
Test for conditional independence between two variables, given the other ones, assuming a multivariate normal distribution.
pcor_test(r, q, n) pcor.test(r, q, n)pcor_test(r, q, n) pcor.test(r, q, n)
r |
a partial correlation coefficient, computed by |
q |
the number of variables in the conditioning set. |
n |
integer |
pcor.test() is deprecated and will be removed in future
versions to avoid conflicts with R's S3 class system.
Use pcor_test().
tval The Student's t-test statistic.
df The degrees of freedom
pvalue The p-value, assuming a two-sided alternative.
Giovanni M. Marchetti
pcor
## Are 2,3 independent given 1? data(marks) pcor_test(pcor(c(2, 3, 1), var(marks)), 1, n = 88)## Are 2,3 independent given 1? data(marks) pcor_test(pcor(c(2, 3, 1), var(marks)), 1, n = 88)
This function takes an adjacency matrix or a graph object and generates a highly customizable, clean static visual representation of the network.
plotGraph( a, dashed = FALSE, layout = igraph::layout_nicely, directed = FALSE, noframe = FALSE, nodesize = 20, vld = 0, vc = "gray80", vfc = NA, colbid = "FireBrick3", coloth = "gray40", cex = 1, ew = 1.8, eas = 0.7, eaw = 1, ... )plotGraph( a, dashed = FALSE, layout = igraph::layout_nicely, directed = FALSE, noframe = FALSE, nodesize = 20, vld = 0, vc = "gray80", vfc = NA, colbid = "FireBrick3", coloth = "gray40", cex = 1, ew = 1.8, eas = 0.7, eaw = 1, ... )
a |
A square adjacency matrix, an igraph::igraph object,
a |
dashed |
Logical. If |
layout |
An |
directed |
Logical. Indicates whether the graph should be treated as
directed. Defaults to |
noframe |
Logical. If |
nodesize |
Numeric. The size of the vertices (nodes). Default is 20. |
vld |
Numeric. Distance of the vertex labels from the nodes. Defaults to 0. |
vc |
Character. The fill color of the nodes. Defaults to |
vfc |
Character. The frame (border) color of the nodes. Defaults to |
colbid |
Character. Color for bidirectional or special edges. Defaults to |
coloth |
Character. Color for standard edges. Defaults to |
cex |
Numeric. Text expansion factor for vertex labels. Defaults to 1. |
ew |
Numeric. The width (thickness) of the edges. Defaults to 1.8. |
eas |
Numeric. The size of the edge arrowheads. Defaults to 0.7. |
eaw |
Numeric. The size of the edge arrow width. Defaults to 1. |
... |
Additional arguments passed directly to the underlying |
An invisible list containing two elements:
tkp.id |
|
igraph |
The processed internal graph object of class |
Sadeghi, K. and Marchetti, G.M. (2012). Graphical Markov models with mixed graphs in R. The R Journal, 4(2):65-73. https://journal.r-project.org/articles/RJ-2012-015/RJ-2012-015.pdf
# Generating the adjacency matrix from a vector and plotting exvec <- c( "b", 1, 2, "b", 1, 14, "a", 9, 8, "l", 9, 11, "a", 10, 8, "a", 11, 2, "a", 11, 9, "a", 11, 10, "a", 12, 1, "b", 12, 14, "a", 13, 10, "a", 13, 12 ) plotGraph(exvec) ## Plotting from a direct adjacency matrix amat <- matrix(c(0, 11, 0, 0, 10, 0, 100, 0, 0, 100, 0, 1, 0, 0, 1, 0), 4, 4) plotGraph(amat) ## Examples using mixed graphs generation (makeMG) plotGraph(makeMG(bg = UG(~ a * b * c + c * d), dg = DAG(a ~ x + z, b ~ z))) plotGraph(makeMG(bg = UG(~ a * b * c + c * d), dg = DAG(a ~ x + z, b ~ z)), dashed = FALSE) # A graph with double and triple edges G <- structure(c( 0, 101, 0, 0, 100, 0, 100, 100, 0, 100, 0, 100, 0, 111, 100, 0 ), .Dim = c(4L, 4L), .Dimnames = list(c("X", "Z", "Y", "W"), c("X", "Z", "Y", "W"))) plotGraph(G) # A regression chain graph G_chain <- makeMG( bg = UG(~ Love * C + C * R + A * D), dg = DAG(L ~ A + D, C ~ D, R ~ D, A ~ F, D ~ F), ug = UG(~ F * S + S * Age) ) plotGraph(G_chain, noframe = TRUE) # A graph with 4 edges between two nodes G4 <- matrix(0, 2, 2) G4[1, 2] <- 111 G4[2, 1] <- 111 plotGraph(G4)# Generating the adjacency matrix from a vector and plotting exvec <- c( "b", 1, 2, "b", 1, 14, "a", 9, 8, "l", 9, 11, "a", 10, 8, "a", 11, 2, "a", 11, 9, "a", 11, 10, "a", 12, 1, "b", 12, 14, "a", 13, 10, "a", 13, 12 ) plotGraph(exvec) ## Plotting from a direct adjacency matrix amat <- matrix(c(0, 11, 0, 0, 10, 0, 100, 0, 0, 100, 0, 1, 0, 0, 1, 0), 4, 4) plotGraph(amat) ## Examples using mixed graphs generation (makeMG) plotGraph(makeMG(bg = UG(~ a * b * c + c * d), dg = DAG(a ~ x + z, b ~ z))) plotGraph(makeMG(bg = UG(~ a * b * c + c * d), dg = DAG(a ~ x + z, b ~ z)), dashed = FALSE) # A graph with double and triple edges G <- structure(c( 0, 101, 0, 0, 100, 0, 100, 100, 0, 100, 0, 100, 0, 111, 100, 0 ), .Dim = c(4L, 4L), .Dimnames = list(c("X", "Z", "Y", "W"), c("X", "Z", "Y", "W"))) plotGraph(G) # A regression chain graph G_chain <- makeMG( bg = UG(~ Love * C + C * R + A * D), dg = DAG(L ~ A + D, C ~ D, R ~ D, A ~ F, D ~ F), ug = UG(~ F * S + S * Age) ) plotGraph(G_chain, noframe = TRUE) # A graph with 4 edges between two nodes G4 <- matrix(0, 2, 2) G4[1, 2] <- 111 G4[2, 1] <- 111 plotGraph(G4)
Finds the power set, that is the list of all subsets of a set.
powerset(set, sort = TRUE, nonempty = TRUE)powerset(set, sort = TRUE, nonempty = TRUE)
set |
A numeric or character vector. |
sort |
Logical value. If |
nonempty |
Logical value. If |
A list of all subsets of set.
Giovanni M. Marchetti
powerset(c("A", "B", "C"), nonempty = FALSE) powerset(1:3, sort = FALSE, nonempty = TRUE)powerset(c("A", "B", "C"), nonempty = FALSE) powerset(1:3, sort = FALSE, nonempty = TRUE)
Generates a random correlation matrix with the method of Marsaglia and Olkin (1984).
rcorr(d)rcorr(d)
d |
an integer > 0, the order of the correlation matrix. |
The algorithm uses rsphere() to generate vectors on a
sphere in -space. If is a matrix with such vectors as rows,
then the random correlation matrix is .
a correlation matrix of order d.
Giovanni M. Marchetti
Marshall, G.& Olkin, I. (1984). Generating correlation matrices. SIAM J. Sci. Stat. Comput., 5, 2, 470-475.
# A random correlation matrix of order 3 rcorr(3) ## A random correlation matrix of order 5 rcorr(5)# A random correlation matrix of order 3 rcorr(3) ## A random correlation matrix of order 5 rcorr(5)
RepMarBG determines whether a given maximal ancestral graph can be
Markov equivalent to a bidirected graph, and if that is the case,
it finds a bidirected graph that is Markov equivalent to the
given graph.
RepMarBG(amat)RepMarBG(amat)
amat |
An adjacency matrix, or a graph that can be a
|
RepMarBG looks for presence of an unshielded non-collider
V-configuration in graph.
A list with two components: verify and amat.
verify is a logical value, TRUE if there is a
representational Markov equivalence and FALSE otherwise.
amat is either NA if verify ia FALSE or is the
adjacency matrix of the generated graph, if verify
is TRUE. In this case amat represent a mixed graph
consisting of 4 different integers as an -element:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added to be associated with multiple
edges of different types. The matrix is symmetric
w.r.t full lines and bi-directed arrows.
Kayvan Sadeghi
Sadeghi, K. (2011). Markov equivalences for subclasses of loopless mixed graphs. Arxiv, https://arxiv.org/abs/1110.4539.
MarkEqMag(), MarkEqRcg(), RepMarDAG(),RepMarUG()
H <- matrix(c(0, 10, 0, 0, 10, 0, 1, 0, 0, 0, 0, 100, 0, 0, 100, 0), 4, 4, byrow = TRUE) RepMarBG(H) G <- c("a", "A", "B", "b", "B", "C", "a", "C", "D", "a", "E", "B", "a", "E", "C") RepMarBG(G)H <- matrix(c(0, 10, 0, 0, 10, 0, 1, 0, 0, 0, 0, 100, 0, 0, 100, 0), 4, 4, byrow = TRUE) RepMarBG(H) G <- c("a", "A", "B", "b", "B", "C", "a", "C", "D", "a", "E", "B", "a", "E", "C") RepMarBG(G)
RepMarDAG determines whether a given maximal ancestral graph
can be Markov equivalent to a directed acyclic graph,
and if that is the case, it finds a directed acyclic graph
that is Markov equivalent to the given graph.
RepMarDAG(amat)RepMarDAG(amat)
amat |
An adjacency matrix, or a graph that can be a
|
RepMarDAG first looks whether the subgraph induced by
full lines is chordal and whether there is a minimal collider path
or cycle of length 4 in graph.
A list with two components: verify and amat.
verify is a logical value, TRUE if there is a representational
Markov equivalence and FALSE otherwise.
amat is either NA if verify ia FALSE or is the
adjacency matrix of the generated graph, if verify is TRUE.
In this case amat represent a mixed graph consisting of 4
different integers as an -element:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added to be associated with multiple edges of
different types. The matrix is symmetric w.r.t full lines
and bi-directed arrows.
Kayvan Sadeghi
Sadeghi, K. (2011). Markov equivalences for subclasses of loopless mixed graphs. Arxiv, https://arxiv.org/abs/1110.4539.
MarkEqMag() MarkEqRcg() RepMarBG() RepMarUG()
# 1. h <- makeMG(bg = UG(~ A*B + B*C + C*D + D*A + E + F), dg = DAG(B ~ E, C~ E+F, D~F)) RepMarDAG(h) # 2. H <- matrix(c(0, 10, 0, 0, 10, 0, 1, 0, 0, 0, 0, 100, 0, 0, 100, 0), 4, 4, byrow = TRUE) nod <- c("A", "B", "C", "D") dimnames(H) <- list(nod, nod) RepMarDAG(H) # 3. wrong G <- makeMG(bg = UG(~ A*B*C + C*D + C*E)) RepMarDAG(G) # 4. wrong g <- makeMG(dg = DAG(A ~ C, B ~ D), bg = UG( ~ A*B)) RepMarDAG(g)# 1. h <- makeMG(bg = UG(~ A*B + B*C + C*D + D*A + E + F), dg = DAG(B ~ E, C~ E+F, D~F)) RepMarDAG(h) # 2. H <- matrix(c(0, 10, 0, 0, 10, 0, 1, 0, 0, 0, 0, 100, 0, 0, 100, 0), 4, 4, byrow = TRUE) nod <- c("A", "B", "C", "D") dimnames(H) <- list(nod, nod) RepMarDAG(H) # 3. wrong G <- makeMG(bg = UG(~ A*B*C + C*D + C*E)) RepMarDAG(G) # 4. wrong g <- makeMG(dg = DAG(A ~ C, B ~ D), bg = UG( ~ A*B)) RepMarDAG(g)
RepMarUG determines whether a given maximal ancestral graph can be
Markov equivalent to an undirected graph, and if that is the case,
it finds an undirected graph that is Markov equivalent to
the given graph.
RepMarUG(amat)RepMarUG(amat)
amat |
An adjacency matrix, or a graph that can be a
|
RepMarUG looks for presence of an unshielded collider
V-configuration in graph.
A list with two components: verify and amat.
verify is a logical value, TRUE if there is a representational
Markov equivalence and FALSE otherwise.
amat is either NA if verify ia FALSE or is the
adjacency matrix of the generated graph, if verify is TRUE.
In this case amat represent a mixed graph consisting of 4
different integers as an -element:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and#' .
These numbers are added to be associated with multiple edges of
different types. The matrix is symmetric w.r.t full lines
and bi-directed arrows.
Kayvan Sadeghi
Sadeghi, K. (2011). Markov equivalences for subclasses of loopless mixed graphs. Arxiv, https://arxiv.org/abs/1110.4539.
MarkEqMag(), MarkEqRcg(), RepMarBG(),
RepMarDAG()
H <- matrix(c(0, 10, 0, 0, 10, 0, 0, 0, 0, 1, 0, 100, 0, 0, 100, 0), 4, 4) RepMarUG(H)H <- matrix(c(0, 10, 0, 0, 10, 0, 0, 0, 0, 1, 0, 100, 0, 0, 100, 0), 4, 4) RepMarUG(H)
RG generates and plots ribbonless graphs
(a modification of MC graph to use m-separation)
after marginalization and conditioning.
RG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )RG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )
amat |
An adjacency matrix, or a graph object. This can be:
|
M |
A subset of the node set of |
C |
Another disjoint subset of the node set
of |
showmat |
A logical value. |
plot |
|
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of
different types are present. The matrix is symmetric
with respect to full lines and bi-directed arrows.
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG() and Max().
Kayvan Sadeghi
Koster, J.T.A. (2002). Marginalizing and conditioning in graphical models. Bernoulli, 8(6), 817-840.
Sadeghi, K. (2013). Stable mixed graphs. Bernoulli 19(5B), 2330–2358.
# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) RG(ex, M, C, plot = TRUE)# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) RG(ex, M, C, plot = TRUE)
Generates a sample from a mean centered multivariate normal distribution whose covariance matrix has a given triangular decomposition.
rnormDag(n, A, Delta)rnormDag(n, A, Delta)
n |
an integer > 0, the sample size. |
A |
a square, upper triangular matrix with ones along the diagonal.
It defines, together with |
Delta |
a numeric vector of length equal to the number of columns
of |
The value in position of A (with ) is
a regression coefficient with the sign changed in the regression of
variable on variables .
The value in position of Delta is the residual variance
in the above regression.
a matrix with n rows and nrow(A) columns, a sample from a
multivariate normal distribution with mean zero and covariance matrix
.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) # Generate a sample of 100 observation from a multivariate normal # The matrix of the path coefficients A <- matrix( c( 1, -2, -3, 0, 0, 0, 0, 0, 1, 0, -4, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 1, -5, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 1, -4, 0, 0, 0, 0, 0, 0, 1 ), 7, 7, byrow = TRUE ) D <- rep(1, 7) X <- rnormDag(100, A, D) # The true covariance matrix solve(A) %*% diag(D) %*% t(solve(A)) ## Triangular decomposition of the sample covariance matrix triDec(cov(X))$A }if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) # Generate a sample of 100 observation from a multivariate normal # The matrix of the path coefficients A <- matrix( c( 1, -2, -3, 0, 0, 0, 0, 0, 1, 0, -4, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 1, 1, -5, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 0, 1, -4, 0, 0, 0, 0, 0, 0, 1 ), 7, 7, byrow = TRUE ) D <- rep(1, 7) X <- rnormDag(100, A, D) # The true covariance matrix solve(A) %*% diag(D) %*% t(solve(A)) ## Triangular decomposition of the sample covariance matrix triDec(cov(X))$A }
Generates a sample of points uniformly distributed on the surface of a
sphere in -space.
rsphere(n, d)rsphere(n, d)
n |
an integer, the sample size. |
d |
an integer, the dimension of the space. For example, a circle is defined in 2D-space, a sphere in 3D-space. |
The algorithm is based on normalizing to length 1 each -vector
of a sample from a multivariate standard normal .
a matrix of n rows and d columns.
Giovanni M. Marchetti
# 100 points on circle z <- rsphere(100, 2) plot(z) # 100 points on a sphere z <- rsphere(100, 3) pairs(z)# 100 points on circle z <- rsphere(100, 2) plot(z) # 100 points on a sphere z <- rsphere(100, 3) pairs(z)
SG generates and plots summary graphs after marginalization
and conditioning.
SG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )SG( amat, M = c(), C = c(), showmat = TRUE, plot = FALSE, plotfun = plotGraph, ... )
amat |
An adjacency matrix, or a graph object. This can be:
|
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
|
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
Support for graphNEL objects requires the graph
package from Bioconductor, which is a suggested dependency.
If the package is missing, passing a graphNEL object will
trigger an informative error.
This function uses the internal functions AG() and Max().
A matrix consisting of 4 different integers
representing the -elements:
0 for a missing edge between and ,
1 for an arrow from to ,
10 for a full line between and , and
100 for a bi-directed arrow between and .
These numbers are added when multiple edges of different types
are present. The matrix is symmetric with respect to full lines
and bi-directed arrows.
Kayvan Sadeghi
Sadeghi, K. (2013). Stable mixed graphs. Bernoulli, 19(5B), 2330–2358.
Wermuth, N. (2011). Probability distributions with summary graph structure. Bernoulli, 17(3),845-879.
# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) SG(ex, M, C, plot = TRUE) SG(ex, M, C, plot = TRUE, plotfun = drawGraph, adjust = FALSE)# The adjacency matrix of a DAG ex <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 ), 16, 16, byrow = TRUE) M <- c(3, 5, 6, 15, 16) C <- c(4, 7) SG(ex, M, C, plot = TRUE) SG(ex, M, C, plot = TRUE, plotfun = drawGraph, adjust = FALSE)
Computes a simultaneous test of all independence relationships implied by a given Gaussian DAG model based on the sample covariance matrix.
shipley_test(amat, S, n) shipley.test(amat, S, n)shipley_test(amat, S, n) shipley.test(amat, S, n)
amat |
a square Boolean matrix, of the same dimension as |
S |
a symmetric positive definite matrix, the sample covariance matrix. |
n |
a positive integer, the sample size. |
shipley.test() is deprecated and will be removed in
the next versions of ggm to avoid conflicts with the S3 class system of R. Use instead shipley_test().
The test statistic is
where are the p-values of tests of
conditional independence in the basis set
computed with basiSet(A). If the p-values are
independent uniform variables on Fisher
proved that $C$ has an asymptotic chi-squared
distribution with degrees of freedom
where $$ is the number of elements of
the basis set. Shipley (2000) proved that,
although there are logical and statistical
dependencies in finite samples, the tests
in the base set are asymptotically independent
and the C test is still asymptotically chi-squared.
ctest Test statistic .
df The degrees of freedom.
pvalue The p-value of the test, assuming
a two-sided alternative.
Giovanni M. Marchetti
Shipley, B. (2000). A new inferential test for path models based on directed acyclic graphs. Structural Equation Modeling, 7(2), 206-218.
# A decomposable model for the mathematics marks data data(marks) dag <- DAG( mechanics ~ vectors + algebra, vectors ~ algebra, statistics ~ algebra + analysis, analysis ~ algebra ) shipley_test(dag, cov(marks), n = 88)# A decomposable model for the mathematics marks data data(marks) dag <- DAG( mechanics ~ vectors + algebra, vectors ~ algebra, statistics ~ algebra + analysis, analysis ~ algebra ) shipley_test(dag, cov(marks), n = 88)
Finds the boundary, children, parents of a subset of nodes of a graph.
bd(nn, amat) ch(nn, amat) pa(nn, amat)bd(nn, amat) ch(nn, amat) pa(nn, amat)
nn |
a vector of nodes. It may either a numeric vector, or a
character vector. If it is character vector must be a subset of
the |
amat |
a square matrix with dimnames specifying the adjacency matrix of the graph. |
For definitions of the operators see Lauritzen (1996).
A character vector specifying the boundary or the
children or the parents of nodes nn in the graph.
This is a numeric or a character vector depending on
the mode of nn.
Giovanni M. Marchetti
Lauritzen, S. (1996). Graphical models. Oxford: Clarendon Press.
if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) ## find boundary of a subset of nodes of a DAG G <- DAG(y ~ x + b + a, b ~ a, x ~ a) bd("b", G) bd(c("b", "x"), G) bd("x", G) bd(c("x", "b"), G) ## find boundary of a subset of nodes of an UG G <- UG(~ y * x * z + z * h * v) bd("z", G) bd(c("y", "x"), G) bd("v", G) bd(c("x", "v"), G) ## children of a subset of nodes of a DAG G <- DAG(y ~ x + b + a, b ~ a, x ~ a) ch("b", G) ch(c("b", "x"), G) ch("x", G) ch(c("a", "x"), G) ## parents of a subset of nodes of a DAG pa("b", G) pa(c("b", "x"), G) pa("x", G) pa(c("x", "b"), G) }if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) ## find boundary of a subset of nodes of a DAG G <- DAG(y ~ x + b + a, b ~ a, x ~ a) bd("b", G) bd(c("b", "x"), G) bd("x", G) bd(c("x", "b"), G) ## find boundary of a subset of nodes of an UG G <- UG(~ y * x * z + z * h * v) bd("z", G) bd(c("y", "x"), G) bd("v", G) bd(c("x", "v"), G) ## children of a subset of nodes of a DAG G <- DAG(y ~ x + b + a, b ~ a, x ~ a) ch("b", G) ch(c("b", "x"), G) ch("x", G) ch(c("a", "x"), G) ## parents of a subset of nodes of a DAG pa("b", G) pa(c("b", "x"), G) pa("x", G) pa(c("x", "b"), G) }
Covariance matrix from a study on coping with surgical stress.
stressstress
A covariance matrix for the following variables:
Stress score Y
Stress score V
Stress score X
Stress score U
See Cox and Wermuth (1996) for details on multivariate dependencies.
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
Slangen K., Kleemann P.P and Krohne H.W. (1993). Coping with surgical stress. In: Krohne H. W. (ed.). Attention and avoidance: Strategies in coping with aversiveness. New York, Heidelberg: Springer, 321-346.
data(stress) G <- UG(~ Y*X + X*V + V*U + U*Y) fitConGraph(G, stress, 100)data(stress) G <- UG(~ Y*X + X*V + V*U + U*Y) fitConGraph(G, stress, 100)
Simulated data following a seemingly unrelated regression model.
surdatasurdata
A data frame with 600 observations on the following 4 variables.
a numeric response vector
a numeric response vector
a numeric vector
a numeric vector with codes 1 and -1 for a binary variable
data(surdata) pairs(surdata)data(surdata) pairs(surdata)
Sweeps a covariance matrix with respect to a subset of indices.
swp(V, b)swp(V, b)
V |
a symmetric positive definite matrix, the covariance matrix. |
b |
a subset of indices of the columns of |
The sweep operator has been introduced by Beaton (1964) as a tool for inverting symmetric matrices (see Dempster, 1969).
a square matrix U of the same order as
V. If a is the complement of b, then
U[a,b] is the matrix of regression coefficients
of a given b and U[a,a] is the
corresponding residual covariance matrix.
If b is empty the function returns V.
If b is the vector 1:nrow(V) (or its
permutation) then the function returns the
opposite of the inverse of V.
Giovanni M. Marchetti
Beaton, A.E. (1964). The use of special matrix operators in statistical calculus. Ed.D. thesis, Harvard University. Reprinted as Educational Testing Service Research, Bulletin 64-51. Princeton.
Dempster, A.P. (1969). Elements of continuous multivariate analysis. Reading: Addison-Wesley.
# A simple example V <- matrix(c(10, 1, 1, 2), 2, 2) swp(V, 2)# A simple example V <- matrix(c(10, 1, 1, 2), 2, 2) swp(V, 2)
topOrder returns the topological order of a directed
acyclic graph (i.e., parents, before children).
topSort permutates the adjacency matrix according
to the topological order.
topOrder(amat) topSort(amat)topOrder(amat) topSort(amat)
amat |
a square Boolean matrix with dimnames, representing the adjacency matrix of a directed acyclic graph. |
The topological order needs not to be unique.
After the permutation the adjacency matrix of the graph
is upper triangular. This function is a
translation of the Matlab function topological_sort
in Matlab Toolbox BNT written by Kevin P. Murphy.
topOrder(amat) returns a vector of integers
representing the permutation of the nodes.
topSort(amat) returns the adjacency matrix
with rows and columns permutated.
The order of the nodes defined by DAG()
is that of their first appearance in the model formulae
(from left to right).
Kevin P. Murphy, Giovanni M. Marchetti
Aho, A.V., Hopcrtoft, J.E. & Ullman, J.D. (1983). Data structures and algorithms. Reading: Addison-Wesley.
Lauritzen, S. (2026). Graphical models, 2nd ed. Oxford: Clarendon Press.
if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) ## A simple example dag <- DAG(a ~ b, c ~ a + b, d ~ c + b) dag topOrder(dag) topSort(dag) }if (requireNamespace("ggm", quietly = TRUE)) { library(ggm) ## A simple example dag <- DAG(a ~ b, c ~ a + b, d ~ c + b) dag topOrder(dag) topSort(dag) }
Computes the transitive closure of a graph (undirected or directed acyclic).
transClos(amat)transClos(amat)
amat |
a Boolean matrix with dimnames representing the adjacency matrix of a graph. |
The transitive closure of a directed graph with adjacency
matrix is a graph with adjacency matrix
such that if there is a directed path
from to . The transitive closure of
an undirected graph is defined similarly (by substituting
path to directed path).
A the adjacency matrix of the transitive closure.
Giovanni M. Marchetti
# Closure of a DAG d <- DAG(y ~ x, x ~ z) transClos(d) # Closure of an UG g <- UG(~ x * y * z + z * u + u * v) transClos(g)# Closure of a DAG d <- DAG(y ~ x, x ~ z) transClos(d) # Closure of an UG g <- UG(~ x * y * z + z * u + u * v) transClos(g)
Decomposes a symmetric positive definite matrix with a variant of the Cholesky decomposition.
triDec(Sigma)triDec(Sigma)
Sigma |
a symmetric positive definite matrix. |
Any symmetric positive definite matrix
can be decomposed as
where is upper triangular with ones along the main
diagonal and is diagonal.
If is a covariance matrix, the
concentration matrix is
where is the matrix of the
regression coefficients with the sign changed of a
system of linear recursive regression equations with
independent residuals. In the equations each variable
is regressed on the variables .
The elements on the diagonal of are the
partial variances.
A a square upper triangular matrix of the same order as
Sigma with ones along the diagonal.
B the inverse of A, another triangular matrix
with unit diagonal.
Delta a vector containing the diagonal values of
.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
# Triangular decomposition of a covariance matrix B <- matrix(c( 1, -2, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 ), 4, 4, byrow = TRUE) B D <- diag(c(3, 1, 2, 1)) S <- B %*% D %*% t(B) triDec(S) solve(B)# Triangular decomposition of a covariance matrix B <- matrix(c( 1, -2, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 ), 4, 4, byrow = TRUE) B D <- diag(c(3, 1, 2, 1)) S <- B %*% D %*% t(B) triDec(S) solve(B)
A simple way to define an undirected graph by means of a single model formula.
UG(f)UG(f)
f |
a single model formula without response |
The undirected graph is defined by a set of nodes and a set of pairs .
The set of pairs is defined by the set of
interactions in the formula. Interactions define complete
subgraphs (not necessarily maximal) of the UG. The best way
is to specify interactions that match the cliques of the
undirected graph. This is the standard way to
define graphical models for contingency tables.
Remember that some hierarchical models are not graphical,
but they nevertheless imply the same graph.
The function returns the adjacency matrix of the
graph, i.e., a symmetric Boolean matrix of order equal
to the number of nodes of the graph and a 1 (or a 10) in
position and if there is an edge from
to and 0 otherwise. By default, this matrix
has zeros along the main diagonal. The dimnames of
the adjacency matrix are the nodes of the UG.
a Boolean matrix with dimnames, the adjacency matrix of the undirected graph.
Giovanni M. Marchetti
Lauritzen, S. (1996). Graphical models, 2nd ed. Oxford: Oxford University Press.
fitConGraph(), fitCovGraph(), DAG()
# X independent of Y given Z UG(~ X * Z + Y * Z) # The saturated model UG(~ X * Y * Z) # The model without three-way interactions has the same graph UG(~ (X + Y + Z)^2) UG(~ X * Y + Y * Z + Z * X) # Butterfly model defined from the cliques (see # dataset `ggm::marks`) UG(~ mec * vec * alg + alg * ana * sta) # Some isolated nodes UG(~ x * y * z + a + b)# X independent of Y given Z UG(~ X * Z + Y * Z) # The saturated model UG(~ X * Y * Z) # The model without three-way interactions has the same graph UG(~ (X + Y + Z)^2) UG(~ X * Y + Y * Z + Z * X) # Butterfly model defined from the cliques (see # dataset `ggm::marks`) UG(~ mec * vec * alg + alg * ana * sta) # Some isolated nodes UG(~ x * y * z + a + b)
Splits the adjacency matrix of a loopless mixed graph into three components: directed, undirected and bi-directed.
unmakeMG(amat)unmakeMG(amat)
amat |
a square matrix, with dimnames, representing
a loopless mixed graph. The matrix consists of 4
different integers as an
|
The matrices ug, and bg are just
symmetric Boolean matrices.
the inverse of makeMG. It returns the
following components.
dg the adjacency matrix of the directed edges
ug the adjacency matrix of the undirected edges
bg the adjacency matrix of the bi-directed edges
Mathias Drton, Giovanni M. Marchetti, Kayvan Sadeghi
ag <- makeMG(ug = UG(~ y0 * y1), dg = DAG(y4 ~ y2, y2 ~ y1), bg = UG(~ y2 * y3 + y3 * y4)) isAG(ag) unmakeMG(ag)ag <- makeMG(ug = UG(~ y0 * y1), dg = DAG(y4 ~ y2, y2 ~ y1), bg = UG(~ y2 * y3 + y3 * y4)) isAG(ag) unmakeMG(ag)