Rev 68948 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/loglin.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2014 R Core Team% Distributed under GPL 2 or later\name{loglin}\alias{loglin}\title{Fitting Log-Linear Models}\usage{loglin(table, margin, start = rep(1, length(table)), fit = FALSE,eps = 0.1, iter = 20, param = FALSE, print = TRUE)}\description{\code{loglin} is used to fit log-linear models to multidimensionalcontingency tables by Iterative Proportional Fitting.}\arguments{\item{table}{a contingency table to be fit, typically the output from\code{table}.}\item{margin}{a list of vectors with the marginal totals to be fit.(Hierarchical) log-linear models can be specified in terms of thesemarginal totals which give the \sQuote{maximal} factor subsets containedin the model. For example, in a three-factor model,\code{list(c(1, 2), c(1, 3))} specifies a model which containsparameters for the grand mean, each factor, and the 1-2 and 1-3interactions, respectively (but no 2-3 or 1-2-3 interaction), i.e.,a model where factors 2 and 3 are independent conditional on factor1 (sometimes represented as \sQuote{[12][13]}).The names of factors (i.e., \code{names(dimnames(table))}) may beused rather than numeric indices.}\item{start}{a starting estimate for the fitted table. This optionalargument is important for incomplete tables with structural zerosin \code{table} which should be preserved in the fit. In thiscase, the corresponding entries in \code{start} should be zero andthe others can be taken as one.}\item{fit}{a logical indicating whether the fitted values should bereturned.}\item{eps}{maximum deviation allowed between observed and fittedmargins.}\item{iter}{maximum number of iterations.}\item{param}{a logical indicating whether the parameter values shouldbe returned.}\item{print}{a logical. If \code{TRUE}, the number of iterations andthe final deviation are printed.}}\value{A list with the following components.\item{lrt}{the Likelihood Ratio Test statistic.}\item{pearson}{the Pearson test statistic (X-squared).}\item{df}{the degrees of freedom for the fitted model. There is noadjustment for structural zeros.}\item{margin}{list of the margins that were fit. Basically the sameas the input \code{margin}, but with numbers replaced by nameswhere possible.}\item{fit}{An array like \code{table} containing the fitted values.Only returned if \code{fit} is \code{TRUE}.}\item{param}{A list containing the estimated parameters of themodel. The \sQuote{standard} constraints of zero marginal sums(e.g., zero row and column sums for a two factor parameter) areemployed. Only returned if \code{param} is \code{TRUE}.}}\details{The Iterative Proportional Fitting algorithm as presented inHaberman (1972) is used for fitting the model. At most \code{iter}iterations are performed, convergence is taken to occur when themaximum deviation between observed and fitted margins is less than\code{eps}. All internal computations are done in double precision;there is no limit on the number of factors (the dimension of thetable) in the model.Assuming that there are no structural zeros, both the LikelihoodRatio Test and Pearson test statistics have an asymptotic chi-squareddistribution with \code{df} degrees of freedom.Note that the IPF steps are applied to the factors in the order givenin \code{margin}. Hence if the model is decomposable and the ordergiven in \code{margin} is a running intersection property orderingthen IPF will converge in one iteration.Package \CRANpkg{MASS} contains \code{loglm}, a front-end to\code{loglin} which allows the log-linear model to be specified andfitted in a formula-based manner similar to that of other fittingfunctions such as \code{lm} or \code{glm}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.Haberman, S. J. (1972)Log-linear fit for contingency tables---Algorithm AS51.\emph{Applied Statistics}, \bold{21}, 218--225.Agresti, A. (1990)\emph{Categorical data analysis}.New York: Wiley.}\author{Kurt Hornik}\seealso{\code{\link{table}}.\code{\link[MASS]{loglm}} in package \CRANpkg{MASS} for auser-friendly wrapper.\code{\link{glm}} for another way to fit log-linear models.}\examples{## Model of joint independence of sex from hair and eye color.fm <- loglin(HairEyeColor, list(c(1, 2), c(1, 3), c(2, 3)))fm1 - pchisq(fm$lrt, fm$df)## Model with no three-factor interactions fits well.}\keyword{category}\keyword{models}