# MarginalModelFit

##### MarginalModelFit

Fits marginal models, by default using maximum likelihood.

- Keywords
- univar

##### Usage

```
MarginalModelFit(dat, model, ShowSummary = TRUE, MaxSteps = 1000, MaxStepSize = 1,
MaxError = 1e-20, StartingPoint = "Automatic", MaxInnerSteps = 2,
ShowProgress = TRUE, CoefficientDimensions="Automatic", Labels="Automatic",
ShowCoefficients = TRUE, ShowParameters = FALSE, ParameterCoding = "Effect",
ShowCorrelations = FALSE, Method = "ML", Title = "Summary of model fit")
```

##### Arguments

- dat
vector of frequencies or data frame

- model
list specified eg as

`list(bt,coeff,at)`

- ShowSummary
Whether or not to execute

`summary()`

of the output- MaxSteps
integer: maximum number of steps of the algorithm

- MaxStepSize
number greater than 0 and at most 1: step size

- MaxError
numeric: maximum error term

- StartingPoint
vector of starting frequencies corresponding to all cells in the manifest table

- MaxInnerSteps
nonnegative integer: only used for latent variable models, indicates number of steps in M step of EM algorithms

- ShowProgress
boolean or integer: FALSE for no progress information, TRUE or 1 for information at every step, an integer k for information at every k-th step

- CoefficientDimensions
numeric vector of dimensions of the table in which the coefficient vector is to be arranged

- Labels
list of characters or numbers indicating labels for dimensions of table in which the coefficient vector is to be arranged

- ShowCoefficients
boolean, indicating whether or not the coefficients are to be displayed

- ShowParameters
boolean, indicating whether or not the parameters (computed from the coefficients) are to be displayed

- ParameterCoding
Coding to be used for parameters, choice of

`"Effect"`

,`"Dummy"`

and`"Polynomial"`

- ShowCorrelations
boolean, indicating whether or not to show the correlation matrix for the estimated coefficients

- Method
character, choice of "ML" for maximum likelihood or "GSK" for the GSK method

- Title
title of computation to appear at top of screen output

##### Details

The data can be a data frame or vector of frequencies. `MarginalModelFit`

converts a data frame `dat`

to a vector of
frequencies using `c(t(ftable(dat)))`

.

The model specification is fairly flexible. We first describe the most typical way to specify the model.
The model itself should typically first be written in the form of a constraint vector as
\(B'\theta(A'\pi) = 0\)
where *B'* is a contrast matrix, *A'* is matrix, normally of zeroes and ones, such that *A'pi* gives a vector of marginal probabilities, and the function theta yields
a list of (marginal) *coefficients*. The model is then specified as `model=list(bt,coeff,at)`

where `bt`

is the matrix *B'*, `at`

is the matrix *A'*, and `coeff`

represents the vector of coefficients using the generalized exp-log notation. For most of the models in the book, `bt`

can be obtained directly using `ConstraintMatrix`

,
`coeff`

can be obtained directly using `SpecifyCoefficient`

, and `at`

can be obtained directly using `MarginalMatrix`

.

Note that CMM does not permit the *C* and *X* matrix in the model
\(C'\theta(A'\pi) = X\beta\)
to be specified for use in the programme. The model has to be rewritten in terms of constraints as above, which is normally straightforward to do with the use of `ConstraintMatrix`

.
For many purposes, estimates and standard errors for a beta vector as in the equation above can still be obtained using the optional argument `ShowParameters=TRUE`

.

There are two ways to specify `coeff`

. The first is using the generalized exp-log notation, in which case `coeff[[1]]`

should be a list of matrices, and
`coeff[[2]]`

should be a list of predefined functions of the same length. The second is to set `coeff`

equal to a predefined function; for example, marginal loglinear models
are obtained by setting `coeff="log"`

.

The model can be specified in various other ways: as `model=list(coeff,at)`

, `model=list(bt,coeff)`

, `model=at`

, or even just `model=coeff`

.
Furthermore, the model
\(B'\theta(A'\pi) = d\)
with *d* a nonzero vector is specified in the form `model=list(bt,coeff,at,d)`

.

To specify the simultaneous model
\(B'\theta(A'\pi) = 0\\ \log\pi=X\beta\)
the extended model specification `model=list(margmodel,x)`

should be used, where `margmodel`

has one of the above forms, and `x`

is a design matrix,
which can be obtained using `DesignMatrix`

. Fitting is often more efficient by specifying a loglinear model for the joint distribution in this way rather than
using constraints.

The default on-screen output when running `fit=MarginalModelFit(...)`

is given by `summary(fit)`

. Important here is the distinction between coefficients and parameters, briefly
described above. Standard output gives the coefficients. These are that part of `model`

without the `bt`

matrix, eg if the model is `list(bt,coeff,at)`

then the coefficients are `list(coeff,at)`

. If other coefficients are needed, `ModelStatistics`

can be used.

Latent variable models can be specified: if the size of the table for which `model`

is specified is a multiple of the the size of the
observed frequencies specified in `dat`

, it is assumed this is due to the presence of latent variables. With respect to vectorization,
the latent variables are assumed to change their value fastest.

Convergence may not always be achieved with `MaxStepSize=1`

and a lower value may need to be used, but not too low or convergence is slow. If the step size is too large,
a typical error message is "system is computationally singular: reciprocal condition number = 1.35775e-19"

##### Value

Most of the following are included in any output. Use `summary()`

to get a summary of output.

Vector of fitted frequencies for the full table (including any latent variables).

Fitting method used (currently maximum likelihood, GSK or minimum discrimination information)

p-value based on asymptotic chi-square approximation for likelihood ratio test statistic

##### References

Bergsma, W. P. (1997).
*Marginal models for categorical data*.
Tilburg, The Netherlands: Tilburg University Press.
http://stats.lse.ac.uk/bergsma/pdf/bergsma_phdthesis.pdf

##### See Also

##### Examples

```
# NOT RUN {
# see also the built-in data sets
data(NKPS)
# Fit the model asserting Goodman and Kruskal's gamma is zero for
# Child's attitude toward sex role's (NKPS[,3], three categories) and
# parent's attitude toward sex role's (NKPS[,4], three categories).
coeff = SpecifyCoefficient("GoodmanKruskalGamma",c(3,3))
fit = MarginalModelFit(NKPS[,c(3,4)], coeff )
# Marginal homogeneity (MH) in a 3x3 table AB
# Note that MH is equivalent to independence in the 2x3 table of marginals IR, in which
# the row with I=1 gives the A marginal, and the row with I=2 gives the B marginal
n <- c(1,2,3,4,5,6,7,8,9)
at <- MarginalMatrix(c("A","B"),list(c("A"),c("B")),c(3,3))
bt <- ConstraintMatrix(c("I","R"),list(c("I"),c("R")),c(2,3))
model <- list( bt, "log", at)
fit <- MarginalModelFit(n,model)
#Output can be tidied up:
fit <- MarginalModelFit(n,model,CoefficientDimensions=c(2,3))
# }
```

*Documentation reproduced from package cmm, version 0.12, License: GPL (>= 2)*