loglin
is used to fit log-linear models to multidimensional
contingency tables by Iterative Proportional Fitting.
loglin(table, margin, start = rep(1, length(table)), fit = FALSE, eps = 0.1, iter = 20, param = FALSE, print = TRUE)
table
. (Hierarchical) log-linear models can be specified in terms of these
marginal totals which give the maximal factor subsets contained
in the model. For example, in a three-factor model,
list(c(1, 2), c(1, 3))
specifies a model which contains
parameters for the grand mean, each factor, and the 1-2 and 1-3
interactions, respectively (but no 2-3 or 1-2-3 interaction), i.e.,
a model where factors 2 and 3 are independent conditional on factor
1 (sometimes represented as [12][13]).
The names of factors (i.e., names(dimnames(table))
) may be
used rather than numeric indices.
table
which should be preserved in the fit. In this
case, the corresponding entries in start
should be zero and
the others can be taken as one.TRUE
, the number of iterations and
the final deviation are printed.margin
, but with numbers replaced by names
where possible.table
containing the fitted values.
Only returned if fit
is TRUE
.param
is TRUE
.iter
iterations are performed, convergence is taken to occur when the
maximum deviation between observed and fitted margins is less than
eps
. All internal computations are done in double precision;
there is no limit on the number of factors (the dimension of the
table) in the model. Assuming that there are no structural zeros, both the Likelihood
Ratio Test and Pearson test statistics have an asymptotic chi-squared
distribution with df
degrees of freedom.
Note that the IPF steps are applied to the factors in the order given
in margin
. Hence if the model is decomposable and the order
given in margin
is a running intersection property ordering
then IPF will converge in one iteration.
Package \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}MASSMASS contains loglm
, a front-end to
loglin
which allows the log-linear model to be specified and
fitted in a formula-based manner similar to that of other fitting
functions such as lm
or glm
.
Haberman, S. J. (1972) Log-linear fit for contingency tables---Algorithm AS51. Applied Statistics, 21, 218--225.
Agresti, A. (1990) Categorical data analysis. New York: Wiley.
table
. loglm
in package \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}MASSMASS for a
user-friendly wrapper.
glm
for another way to fit log-linear models.
## Model of joint independence of sex from hair and eye color.
fm <- loglin(HairEyeColor, list(c(1, 2), c(1, 3), c(2, 3)))
fm
1 - pchisq(fm$lrt, fm$df)
## Model with no three-factor interactions fits well.
Run the code above in your browser using DataLab