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. |
Authors: | Giovanni M. Marchetti [aut, cre], Mathias Drton [aut], Kayvan Sadeghi [aut] |
Maintainer: | Giovanni M. Marchetti <[email protected]> |
License: | GPL-2 |
Version: | 2.5.1 |
Built: | 2024-11-20 03:49:34 UTC |
Source: | https://github.com/stathin/ggm |
Transforms the “edge matrix” of a graph into the adjacency matrix.
adjMatrix(A)
adjMatrix(A)
A |
a square matrix representing the edge matrix of a graph. |
Given the edge matrix of a graph, this can be transformed into
an adjacency matrix
with the formula
.
E |
the adjacency matrix of the graph. |
Giovanni M. Marchetti
amat <- DAG(y ~ x+z, z~u+v) E <- edgematrix(amat) adjMatrix(E)
amat <- DAG(y ~ x+z, z~u+v) E <- edgematrix(amat) 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 that can be of class |
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
A logical value, |
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
A matrix that is the adjacency matrix of the generated graph.
It 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.
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 a graph. |
a matrix with two columns. Each row of the matrix is a pair of
indices indicating an edge of the graph. If the graph is undirected,
then 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
data(anger)
data(anger)
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 |
a square 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 set (which may be empty).
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).
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.
## 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)
Inverts a marginal log-linear parametrization.
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 marginal log-linear link is defined by . See
Bartolucci et al. (2007).
A vector of probabilities p
.
From a Matlab function by A. Forcina, University of Perugia, Italy.
Antonio Forcina, Giovanni M. Marchetti
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
blodiag(1:10, blo = c(2, 3, 5)) blodiag(1:10, blo = c(3,4,0,1))
blodiag(1:10, blo = c(2, 3, 5)) blodiag(1:10, blo = c(3,4,0,1))
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)
cmpGraph(amat)
amat |
the adjacency matrix of an undirected graph |
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. (1996). Graphical models. Oxford: Clarendon 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 a graph.
conComp(amat, method)
conComp(amat, method)
amat |
a square matrix with dimnames, 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. (1996). Graphical models. Oxford: Clarendon Press.
## three connected components conComp(UG(~a*c+c*d+e+g*o*u)) ## a 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)) ## a 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)
correlations(x)
x |
a square symmetric matrix, a covariance matrix, or a data.frame for n observations and p variables. |
a square correlation matrix with marginal correlations (lower triangle) and partial correlations (upper triangle).
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)
## See Table 6.1 in Cox & Wermuth (1996) data(glucose) correlations(glucose)
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 of 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. Oxford: Clarendon 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) ## 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) ## 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.
data(derived)
data(derived)
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
Sys
Systolic blood pressure, in mm Hg
Dia
Diastolic blood pressure, in mm Hg
Age
Age of the patient, in years
Hei
Height, in cm
Wei
Weight, in kg
The following is the description of the variables
for the covariance matrix S
.
Y
Derived variable Y=log(Sys/Dia)
X
Derived variables X=log(Dia)
Z
Body mass index Z=Wei/(Hei/100)^2
W
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 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.
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. (1996). Graphical models. Oxford: Clarendon 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 DG(y ~ x, x ~ y) ## 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 DG(y ~ x, x ~ y) ## 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
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 = layout.auto)
drawGraph(amat, coor = NULL, adjust = FALSE, alpha = 1.5, beta = 3, lwd = 1, ecol = "blue", bda = 0.1, layout = layout.auto)
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 (default: "blue"). |
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 graphs, with a rudimentary interface for moving nodes and edges of a given graph and adjusting the final plot. For better displays use dynamicGraph or Rgraphviz package in Bioconductor project.
The function plots 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
dynamicGraph, Rgraphwiz, https://www.bioconductor.org.
GraphViz, Graph Visualization Project. AT&T Research. https://www.graphviz.org.
## A directed acyclic graph d <- DAG(y1 ~ y2+y6, y2 ~ y3, y3 ~ y5+y6, y4 ~ y5+y6) ## Not run: drawGraph(d) ## An undirected graph g <- UG(~giova*anto*armo + anto*arj*sara) ## Not run: drawGraph(d) ## 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) ## An undirected graph g <- UG(~giova*anto*armo + anto*arj*sara) ## Not run: drawGraph(d) ## 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 a d-separated by a third set of nodes.
dSep(amat, first, second, cond)
dSep(amat, first, second, cond)
amat |
a Boolean matrix with dimnames, representing the adjacency matrix of a directed
acyclic graph. The function does not check if this is the case. See
the function |
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. 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. (1996). Graphical models. Oxford: Clarendon Press.
DAG
, shipley.test
,
inducedCovGraph
## 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"))
Transforms the adjacency matrix of a graph into an “edge matrix”.
edgematrix(E, inv=FALSE)
edgematrix(E, inv=FALSE)
E |
a square matrix, representing the adjacency matrix of a graph. |
inv |
a logical value. |
In some matrix computations for graph objects the adjacency matrix
of the graph is transformed into an “edge matrix”. Briefly,
if is the adjacency matrix of the
graph, the edge matrix is
.
Thus,
has ones along the diagonal
and if the graph has no edge between nodes
and
the entries
and
are both zero.
If there is an arrow from
to
and
. If there is an undirected edge, both
.
A |
the edge matrix of the graph.
If |
Giovanni M. Marchetti
Wermuth, N. (2003). Analysing social science data with graphical Markov models. In: Highly Structured Stochastic Systems. P. Green, N. Hjort & T. Richardson (eds.), 47–52. Oxford: Oxford University Press.
amat <- DAG(y ~ x+z, z~u+v) amat edgematrix(amat) edgematrix(amat, inv=TRUE)
amat <- DAG(y ~ x+z, z~u+v) amat edgematrix(amat) edgematrix(amat, inv=TRUE)
Find the essential graph from a given directed acyclic graph.
essentialGraph(dagx)
essentialGraph(dagx)
dagx |
a square binary matrix, 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).
returns the adjacency matrix of the essential graph.
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.
dag = DAG(U ~ Y+Z, Y~X, Z~X) essentialGraph(dag)
dag = DAG(U ~ Y+Z, Y~X, Z~X) 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. |
en |
an integer, the ending node. |
path |
a vector of integers, used in recursive calls. At the
beginning is |
a vector of integers, the sequence of nodes of a path,
starting from st
to en
. In some graphs
(spanning trees) there is only one path between two nodes.
This function is not intended to be directly called by the user.
Giovanni M. Marchetti, translating the original Python code (see references).
Python Softftware 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 col names, the sample covariance matrix. |
n |
the sample size, a positive integer. |
tol |
a small positive number indicating the tolerance used in convergence checks. |
In the Gaussian case, the models can be parameterized
using precision parameters, regression coefficients, and error
covariances (compare 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.
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 said |
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 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"))) ## The following should give the same fit. ## Fit an ancestral graph y -> x <-> z <- u fitAncestralGraph(ag1 <- makeMG(dg=DAG(x~y,z~u), bg = UG(~x*z)), S, n=100) ## Fit an ancestral graph y <-> x <-> z <-> u fitAncestralGraph(ag2 <- makeMG(bg= UG(~y*x+x*z+z*u)), S, n=100) ## Fit the same graph with fitCovGraph fitCovGraph(ag2, S, n=100) ## Another example for the mathematics marks data data(marks) S <- var(marks) mag1 <- makeMG(bg=UG(~mechanics*vectors*algebra+algebra*analysis*statistics)) fitAncestralGraph(mag1, S, n=88) mag2 <- makeMG(ug=UG(~mechanics*vectors+analysis*statistics), dg=DAG(algebra~mechanics+vectors+analysis+statistics)) fitAncestralGraph(mag2, S, n=88) # Same fit as above
## 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"))) ## The following should give the same fit. ## Fit an ancestral graph y -> x <-> z <- u fitAncestralGraph(ag1 <- makeMG(dg=DAG(x~y,z~u), bg = UG(~x*z)), S, n=100) ## Fit an ancestral graph y <-> x <-> z <-> u fitAncestralGraph(ag2 <- makeMG(bg= UG(~y*x+x*z+z*u)), S, n=100) ## Fit the same graph with fitCovGraph fitCovGraph(ag2, S, n=100) ## Another example for the mathematics marks data data(marks) S <- var(marks) mag1 <- makeMG(bg=UG(~mechanics*vectors*algebra+algebra*analysis*statistics)) fitAncestralGraph(mag1, S, n=88) mag2 <- makeMG(ug=UG(~mechanics*vectors+analysis*statistics), dg=DAG(algebra~mechanics+vectors+analysis+statistics)) fitAncestralGraph(mag2, S, n=88) # Same fit as above
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 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. and 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 Booloean 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.
If |
dual.alg |
And integer equal to 1 or 2. It is used if
|
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. |
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 model. |
df |
the degrees of freedom. |
it |
the 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 the DAG |
S |
a symmetric positive definite matrix, the sample covariance matrix |
n |
an integer > 0, the sample size |
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
|
Dhat |
a vector containing the partial variances of each variable given the parents. |
dev |
the ‘deviance’ of the 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.
If |
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 maxit iterations the algorithms stops and a warning message is returned. |
tol |
a small real value, denoting the tolerance used in testing convergence. |
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 |
Ahat |
a square matrix of the fitted regression coefficients. The entry
|
Dhat |
a vector containing the partial variances of each
variable given the parents. If |
dev |
the ‘deviance’ of the 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.
fitmlogit(..., C = c(), D = c(), data, mit = 100, ep = 1e-80, acc = 1e-04)
fitmlogit(..., C = c(), D = c(), data, mit = 100, ep = 1e-80, acc = 1e-04)
... |
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 cosntraints. |
data |
A data frame containing the responses and the explanatory variables. |
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 |
See Evans and Forcina (2011).
LL |
The maximized log-likelihood. |
be |
The vector of the Maximum likelihood 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, data = surdata) out1$beta out2 <- fitmlogit(A ~X, B ~ Z, cbind(A, B) ~ 1, data = surdata) out2$beta
data(surdata) out1 <- fitmlogit(A ~X, B ~ Z, cbind(A, B) ~ X*Z, data = surdata) out1$beta out2 <- fitmlogit(A ~X, B ~ Z, cbind(A, B) ~ 1, data = 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 an error message. |
All the cycles in an UG can be obtained from combination (ring sum) of the set of fundamental cycles.
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
.
This function is used by cycleMatrix
and isGident
.
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))
ggm
: summary information
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
, cliques
,
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.
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
Data on glucose control of diabetes patients.
data(glucose)
data(glucose)
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
generates the associated adjacency matrix to a given graph.
grMAT(agr)
grMAT(agr)
agr |
A graph that can be a |
A matrix that consists 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
## 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)
## 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)
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
## 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.
inducedCovGraph(amat, sel = rownames(amat), cond = NULL) inducedConGraph(amat, sel = rownames(amat), cond = NULL) inducedRegGraph(amat, sel = rownames(amat), cond = NULL) inducedChainGraph(amat, cc=rownames(amat), cond = NULL, type="LWF") inducedDAG(amat, order, cond = NULL)
inducedCovGraph(amat, sel = rownames(amat), cond = NULL) inducedConGraph(amat, sel = rownames(amat), cond = NULL) inducedRegGraph(amat, sel = rownames(amat), cond = NULL) inducedChainGraph(amat, cc=rownames(amat), cond = NULL, type="LWF") 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. |
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 |
cond |
a character vector representing the variables on which you
want to condition. |
cc |
a list of character vectors specifying the chain components for the chain graph. |
type |
a string indicating the interpretation of the chain graph. It can be either "LWF" (Lauritzen, Wermuth, Frydenberg interpretation), "AMP" (Andersson, Madigan, Perlman interpretation) or "MRG" (Multivariate regression graph interpretation). |
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 marginalizingover 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 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
is NULL
the functions return the null matrix.
If cond
is NULL
, the conditioning set is empty and the
functions inducedConGraph
and inducedCovGraph
return the overall induced covariance or concentration
matrices of the selected variables. If 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, Part 3, 687-717.
## 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")
## 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: (i) no undirected edge meets an arrowhead; (ii) no directed cycles;
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)) 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 must hold: (i) no undirected edge meets an arrowhead; (ii) no directed cycles; (iii) 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)) 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)) 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
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)
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 that can be a |
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
A logical value, |
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
This function uses the functions AG
and Max
.
A matrix that consists 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
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.
ex<-matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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)
ex<-matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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 the directed, undirected and undirected 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. |
A loopless mixed graph is a mixed graph with three types of edges: undirected,
directed and bi-directed edges.
Note that the three adjacency matrices must have labels and
may be defined using the functions DG
, DAG
or 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
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)) 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(a1) a3 <- makeMG(ug = UG(~ a*c), dg=DAG(b ~ a, d~c), bg=UG(~ b*d)) # Fig. 3 (b2) p.969 a5 <- makeMG(bg=UG(~alpha*beta+gamma*delta), dg=DAG(alpha~gamma, delta~beta)) # Fig. 6 p. 973 ## Another Example a4 <- makeMG(ug=UG(~y0*y1), dg=DAG(y4~y2, y2~y1), bg=UG(~y2*y3+y3*y4)) ## 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) ) ## 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))
## Examples from Richardson and Spirtes (2002) a1 <- makeMG(dg=DAG(a~b, b~d, d~c), bg=UG(~a*c)) 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(a1) a3 <- makeMG(ug = UG(~ a*c), dg=DAG(b ~ a, d~c), bg=UG(~ b*d)) # Fig. 3 (b2) p.969 a5 <- makeMG(bg=UG(~alpha*beta+gamma*delta), dg=DAG(alpha~gamma, delta~beta)) # Fig. 6 p. 973 ## Another Example a4 <- makeMG(ug=UG(~y0*y1), dg=DAG(y4~y2, y2~y1), bg=UG(~y2*y3+y3*y4)) ## 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) ) ## 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))
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).
C |
Matrix of constrasts (the first |
M |
Marginalization matrix with elements 0 and 1. |
G |
Corresponding design matrix for the corresponding log-linear model. |
Assumes that the vector of probabilities is in inv lex order. The interactions are returned in order of dimension, like e.g., 1, 2, 3, 12, 13, 23, 123.
Francesco Bartolucci, Antonio Forcina, Giovanni M. Marchetti
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.
marg.param(c(3,3), c("l", "g"))
marg.param(c(3,3), c("l", "g"))
MarkEqMag
determines whether two MAGs are Markov equivalent.
MarkEqMag(amat, bmat)
MarkEqMag(amat, bmat)
amat |
An adjacency matrix of a MAG, or a graph that can be a |
bmat |
The same as |
The function checks whether the two graphs have the same skeleton and colliders with order.
"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)
MarkEqMag
determines whether two RCGs (or subclasses of RCGs) are
Markov equivalent.
MarkEqRcg(amat, bmat)
MarkEqRcg(amat, bmat)
amat |
An adjacency matrix of an RCG or a graph that can be a |
bmat |
The same as |
The function checks whether the two graphs have the same skeleton and unshielded colliders.
"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.
data(marks)
data(marks)
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, (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
and M
of e 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 margins. For instance
|
The power set is 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.
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 that can be a |
Max
looks for non-adjacent pais of nodes that are connected by
primitive inducing paths, and connect such pairs by an appropriate edge.
A matrix that consists 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
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 that can be a |
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
A logical value, |
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
This function uses the functions RG
and Max
.
A matrix that consists 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
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.
ex <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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)
ex <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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(a, alpha, beta, C = c())
msep(a, alpha, beta, C = c())
a |
An adjacency matrix, or a graph that can be a |
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 |
A logical value. TRUE
if alpha
and beta
are m-separated given C
.
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())
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())
MAG
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 of a MAG, or a graph that can be a |
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
A logical value, |
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
This function uses the functions SG
and Max
.
A matrix that consists 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
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.
ex<-matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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)
ex<-matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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
is zero.
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
, ~~~
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 the
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.
### 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 > 1. The first two integers are the indices of variables the correlation of which must be computed. The rest of the vector is the conditioning set. |
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
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)
r |
a partial correlation coefficient, computed by |
q |
the number of variables in the conditioning set. |
n |
integer > 0, the sample size. |
tval |
The Student's t-test statistic. |
df |
The degrees of freedom |
pvalue |
The P-value, assuming a two-sided alternative. |
Giovanni M. Marchetti
## 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)
Plots a mixed graph from an adjacency matrix,
a graphNEL
object, an igraph
object, or
a descriptive vector.
plotGraph(a, dashed = FALSE, tcltk = TRUE, layout = layout.auto, directed = FALSE, noframe = FALSE, nodesize = 15, vld = 0, vc = "gray", vfc = "black", colbid = "FireBrick3", coloth = "black", cex = 1.5, ...)
plotGraph(a, dashed = FALSE, tcltk = TRUE, layout = layout.auto, directed = FALSE, noframe = FALSE, nodesize = 15, vld = 0, vc = "gray", vfc = "black", colbid = "FireBrick3", coloth = "black", cex = 1.5, ...)
a |
An adjacency matrix: a matrix that consists of 4 different integers as an |
dashed |
A logical value. If |
tcltk |
A logical value. If |
layout |
The name of a function used to compute the (initial) layout of the graph. The default is |
directed |
A logical value. If |
noframe |
A logical value. If |
nodesize |
An integer denoting the size of the nodes (default 15). It can be increased to accommodate larger labels. |
vld |
An integer defining the distance between a vertex and its label. Defaults to 0. |
vc |
Vertex color. Default is "gray". |
vfc |
Vertex frame color. Default is "black". |
colbid |
Color of the bi-directed edges. Default is "FireBrick3". |
coloth |
Color of all the other edges. Default is "black". |
cex |
An integer (defaults to 1) to adjust the scaling of the font of the labels. |
... |
Further arguments to be passed to |
plotGraph
uses plot
and tkplot
in igraph package.
Plot of the associated graph and returns invisibly a list with two slots:
tkp.id
,
graph
, the input graph as an igraph
object.
The id can be used to get the layout of the adjusted graph.
The bi-directed edges are plotted in red.
Kayvan Sadeghi, Giovanni M. Marchetti
grMAT
, tkplot
, drawGraph
,
plot.igraph
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) ############################################ amat<-matrix(c(0,11,0,0,10,0,100,0,0,100,0,1,0,0,1,0),4,4) plotGraph(amat) 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 = TRUE) # 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 with longer labels plotGraph(makeMG(bg = UG(~Love*Constraints+ Constraints*Reversal+ Abuse*Distress), dg = DAG(Love ~ Abuse + Distress, Constraints ~ Distress, Reversal ~ Distress, Abuse ~ Fstatus, Distress ~ Fstatus), ug = UG(~Fstatus*Schooling+ Schooling*Age)), dashed = TRUE, 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)
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) ############################################ amat<-matrix(c(0,11,0,0,10,0,100,0,0,100,0,1,0,0,1,0),4,4) plotGraph(amat) 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 = TRUE) # 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 with longer labels plotGraph(makeMG(bg = UG(~Love*Constraints+ Constraints*Reversal+ Abuse*Distress), dg = DAG(Love ~ Abuse + Distress, Constraints ~ Distress, Reversal ~ Distress, Abuse ~ Fstatus, Distress ~ Fstatus), ug = UG(~Fstatus*Schooling+ Schooling*Age)), dashed = TRUE, 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 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 |
If sort == FALSE
the sets are in inverse lexicographical order.
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 == FALSE
or
the adjacency matrix of the generated graph, if
verify == TRUE
. In this case it 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.
Kayvan Sadeghi
Sadeghi, K. (2011). Markov equivalences for subclasses of loopless mixed graphs. Submitted, 2011.
MarkEqMag
, MarkEqRcg
, RepMarDAG
,
RepMarUG
H<-matrix(c(0,10,0,0,10,0,0,0,0,1,0,100,0,0,100,0),4,4) RepMarBG(H)
H<-matrix(c(0,10,0,0,10,0,0,0,0,1,0,100,0,0,100,0),4,4) RepMarBG(H)
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 == FALSE
or
the adjacency matrix of the generated graph, if
verify == TRUE
. In this case it 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.
Kayvan Sadeghi
Sadeghi, K. (2011). Markov equivalences for subclasses of loopless mixed graphs. Submitted, 2011.
MarkEqMag
, MarkEqRcg
, RepMarBG
,
RepMarUG
H<-matrix(c(0,10,0,0,10,0,0,0,0,1,0,100,0,0,100,0),4,4) RepMarBG(H)
H<-matrix(c(0,10,0,0,10,0,0,0,0,1,0,100,0,0,100,0),4,4) RepMarBG(H)
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 |
RepMarBG
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 == FALSE
or
the adjacency matrix of the generated graph, if
verify == TRUE
. In this case it 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.
Kayvan Sadeghi
Sadeghi, K. (2011). Markov equivalences for subclasses of loopless mixed graphs. Submitted, 2011.
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 that can be a |
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
A logical value, |
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
A matrix that consists 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
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.
ex <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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)
ex <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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 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
S = solve(A) %*% diag(Delta) %*% t(solve(A))
.
Giovanni M. Marchetti
Cox, D. R. & Wermuth, N. (1996). Multivariate dependencies. London: Chapman & Hall.
## 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
## 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 d-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 d-vector
of a sample from a multivariate 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 that can be a |
M |
A subset of the node set of |
C |
Another disjoint subset of the node set of |
showmat |
A logical value. |
plot |
A logical value, |
plotfun |
Function to plot the graph when |
... |
Further arguments passed to |
A matrix that consists 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. (2013). Stable mixed graphs. Bernoulli 19(5B), 2330–2358.
Wermuth, N. (2011). Probability distributions with summary graph structure. Bernoulli, 17(3),845-879.
ex <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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)
ex <- matrix(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ##The adjacency matrix of a DAG 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 model defined according to a directed acyclic graph, based on the sample covariance matrix.
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. |
The test statistic is where
are the
p-values of tests of conditional independence in the basis set
computed by
basiSet(A)
. The p-values are independent
uniform variables on and the statistic has exactly a
chi square distribution on
degrees of freedom where
is the number of elements of the basis set.
Shipley (2002) calls this test Fisher's C test.
ctest |
Test statistic |
df |
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).
The operators return 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.
## 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)
## 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)
Stress data
data(stress)
data(stress)
A covariance matrix for the following variables.
See Cox and Wermuth (1996).
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.
data(surdata)
data(surdata)
A data frame with 600 observations on the following 4 variables.
A
a numeric response vector
B
a numeric response vector
X
a numeric vector
Z
a numeric vector with codes 1
and -1
for a binary variables.
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 covariance matrix of the residuals.
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 very simple example V <- matrix(c(10, 1, 1, 2), 2, 2) swp(V, 2)
## A very simple example V <- matrix(c(10, 1, 1, 2), 2, 2) swp(V, 2)
topOrder
returns the topological order of a directed
acyclic graph (parents, before children). topSort
permutates
the adjacency matrix according to the topological order.
topSort(amat) topOrder(amat)
topSort(amat) topOrder(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. The function is a translation of
the Matlab function topological_sort
in 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. (1996). Graphical models. Oxford: Clarendon Press.
## A simple example dag <- DAG(a ~ b, c ~ a + b, d ~ c + b) dag topOrder(dag) topSort(dag)
## 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
|
B |
the inverse of |
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 imply the same graph.
The function returns the edge matrix of the 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. By default this matrix
has ones along the main diagonal. For UGs this matrix is symmetric.
The dimnames of the edge 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. Oxford: Clarendon Press.
## 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 + Y*Z + Z*X) UG(~ (X + Y + Z)^2) ## Butterfly model defined from the cliques 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 + Y*Z + Z*X) UG(~ (X + Y + Z)^2) ## Butterfly model defined from the cliques 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.
It is the inverse of makeAG
. 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
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)
Functions used internally.
Kayvan Sadeghi, Giovanni M. Marchetti
unique
,setdiff
, is.element