% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/assoc_graph.R
\name{assoc_graph}
\alias{assoc_graph}
\alias{assoc_graph.list}
\alias{assoc_graph.loglm}
\alias{assoc_graph.glm}
\alias{print.assoc_graph}
\title{Association Graph for a Loglinear Model}
\usage{
assoc_graph(x, ...)

\method{assoc_graph}{list}(x, result = c("igraph", "matrix", "edge_list"), ...)

\method{assoc_graph}{loglm}(x, result = c("igraph", "matrix", "edge_list"), ...)

\method{assoc_graph}{glm}(
  x,
  result = c("igraph", "matrix", "edge_list"),
  measure = c("none", "chisq", "cramer"),
  ...
)

\method{print}{assoc_graph}(x, ...)
}
\arguments{
\item{x}{An object specifying the model. This can be:
\itemize{
\item A \code{list} of character vectors (a margin/generating class list, as produced by
\code{\link{joint}}, \code{\link{conditional}}, etc.)
\item A fitted \code{\link[MASS]{loglm}} object
\item A fitted \code{\link[stats]{glm}} object (poisson family loglinear model)
}}

\item{\dots}{Additional arguments (currently unused).}

\item{result}{Type of result to return: \code{"igraph"} (default) returns an
\code{\link[igraph:igraph-package]{igraph}} object; \code{"matrix"} returns the
adjacency matrix; \code{"edge_list"} returns a two-column character matrix of edges.}

\item{measure}{Type of association measure for edge weights (only for \code{glm} method):
\code{"none"} (default) produces an unweighted graph;
\code{"chisq"} computes partial chi-squared statistics (deviance change when each
edge is removed from the model);
\code{"cramer"} computes Cramer's V from the marginal two-way table for each edge.}
}
\value{
Depending on \code{result}:
\itemize{
\item \code{"igraph"}: An \code{igraph} undirected graph object of class
\code{c("assoc_graph", "igraph")}, with vertex names corresponding to
the variable names. When \code{measure != "none"}, edge weights are
stored as \code{E(g)$weight} and the measure name as \code{g$measure}.
\item \code{"matrix"}: A symmetric adjacency matrix with variable names as
row and column names. Contains 0/1 when unweighted, or association
strength values when \code{measure} is specified.
\item \code{"edge_list"}: When unweighted, a two-column character matrix (from, to).
When \code{measure} is specified, a data frame with columns from, to, and weight.
}
}
\description{
Construct an undirected graph representing the associations in a loglinear model.
Nodes represent variables and edges represent pairwise associations fitted in the model.
If two variables are not connected by an edge, they are conditionally independent
given the other variables.
}
\details{
Each high-order term (margin) in a hierarchical loglinear model defines a clique
in the association graph. For example, the term \code{c("A", "B", "C")} generates
edges A--B, A--C, and B--C. Single-variable terms (as in mutual independence)
yield isolated nodes with no edges.

For \code{loglm} objects, the margins are extracted from the \code{$margin} component.
For \code{glm} objects, the interaction terms are extracted from the model formula.
}
\examples{
# Structural graphs from margin lists (3-way: A, B, C)
mutual(3, factors = c("A", "B", "C"))      |> assoc_graph()
joint(3, factors = c("A", "B", "C"))       |> assoc_graph()
conditional(3, factors = c("A", "B", "C")) |> assoc_graph()
saturated(3, factors = c("A", "B", "C"))   |> assoc_graph()

# Adjacency matrix form
conditional(3, factors = c("A", "B", "C")) |> assoc_graph(result = "matrix")

# From a fitted loglm model (Berkeley admissions)
\dontrun{
mod <- MASS::loglm(~ (Admit + Gender) * Dept, data = UCBAdmissions)
assoc_graph(mod)
plot(assoc_graph(mod), main = "Berkeley: [AD] [GD]")
}

# From glm models (Dayton Survey: cigarette, alcohol, marijuana, sex, race)
data(DaytonSurvey)

# Mutual independence + sex*race: one edge only
mod.SR <- glm(Freq ~ . + sex*race, data = DaytonSurvey, family = poisson)
assoc_graph(mod.SR)
plot(assoc_graph(mod.SR), main = "Mutual indep. + [SR]")

# [AM][AC][MC][AR][AS][RS]: {race, sex} indep {marijuana, cigs} | alcohol
mod.cond <- glm(Freq ~ (cigarette + alcohol + marijuana)^2 +
                        (alcohol + sex + race)^2,
                data = DaytonSurvey, family = poisson)

# define groups for the model
gps <- list(c("cigarette", "marijuana"),
            "alcohol",
            c("sex", "race"))

assoc_graph(mod.cond)
plot(assoc_graph(mod.cond),
     groups = gps,
     layout = igraph::layout_nicely,
     main = "{R,S} indep {M,C} | A")

# Weighted graph: partial chi-squared
g <- assoc_graph(mod.cond, measure = "chisq")
g
plot(g, edge.label = TRUE,
     groups = gps,
     layout = igraph::layout_nicely,
     main = "Partial chi-squared weights")

# Cramer's V (marginal)
g2 <- assoc_graph(mod.cond, measure = "cramer")
g2
plot(g2, edge.label = TRUE,
     groups = gps,
     layout = igraph::layout_nicely,
     main = "Cramer's V weights")

}
\references{
Khamis, H. J. (2011). \emph{The Association Graph and the Multigraph for Loglinear Models}.
SAGE Publications. \doi{10.4135/9781452226521}

Darroch, J. N., Lauritzen, S. L., & Speed, T. P. (1980). Markov Fields and Log-Linear
Interaction Models for Contingency Tables. \emph{The Annals of Statistics}, 8(3), 522--539.
\doi{10.1214/aos/1176345006}

Whittaker, J. (1990). \emph{Graphical Models in Applied Multivariate Statistics}.
John Wiley & Sons, Chichester.
}
\seealso{
\code{\link{joint}}, \code{\link{conditional}}, \code{\link{mutual}},
\code{\link{saturated}}, \code{\link{loglin2string}}, \code{\link{seq_loglm}},
\code{\link{plot.assoc_graph}}

Other loglinear models:
\code{\link[=get_model]{get_model()}},
\code{\link[=glmlist]{glmlist()}},
\code{\link[=joint]{joint()}},
\code{\link[=plot.assoc_graph]{plot.assoc_graph()}},
\code{\link[=seq_loglm]{seq_loglm()}}
}
\concept{loglinear models}
