Mclogit
Mclogit
Mclogit
URL http://mclogit.elff.eu,https://github.com/melff/mclogit/
BugReports https://github.com/melff/mclogit/issues
RoxygenNote 7.1.2
NeedsCompilation no
Repository CRAN
Date/Publication 2022-10-27 10:02:37 UTC
1
2 dispersion
R topics documented:
dispersion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
electors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
getSummary-methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
mblogit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
mclogit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
mclogit.control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
mclogit.fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
simulate.mclogit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Transport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Index 19
Description
The function dispersion() extracts the dispersion parameter from a multinomial logit model or
computes a dispersion parameter estimate based on a given method. This dispersion parameter can
be attached to a model using update(). It can also given as an argument to summary().
Usage
dispersion(object,method, ...)
## S3 method for class 'mclogit'
dispersion(object,method=NULL, ...)
Arguments
References
Afroz, Farzana, Matt Parry, and David Fletcher. (2020). "Estimating Overdispersion in Sparse
Multinomial Data." Biometrics 76(3): 834-842. doi:10.1111/biom.13194.
electors 3
Examples
library(MASS) # For 'housing' data
dispersion(house.mblogit,method="Afroz")
dispersion(house.mblogit,method="Deviance")
summary(house.mblogit)
summary(update(house.mblogit, dispersion="Afroz"))
Description
This is an artificial data set on electoral choice as influenced by class and party positions.
Usage
data(electors)
Format
A data frame containing the following variables:
Examples
data(electors)
summary(mclogit(
cbind(Freq,interaction(time,class))~econ.left+welfare+auth,
data=electors))
summary(mclogit(
cbind(Freq,interaction(time,class))~econ.left/class+welfare/class+auth/class,
data=electors))
summary(mclogit(
cbind(Freq,interaction(time,class))~econ.left/(class*time)+welfare/class+auth/class,
random=~1|party.time,
data=within(electors,{
party.time <-interaction(party,time)
econ.left.sq <- (econ.left-mean(econ.left))^2
})))
## End(Not run)
Description
getSummary methods for use by mtable
Usage
## S3 method for class 'mblogit'
getSummary(obj,
alpha=.05,
...)
## S3 method for class 'mclogit'
getSummary(obj,
alpha=.05,
rearrange=NULL,
getSummary-methods 5
...)
## S3 method for class 'mmblogit'
getSummary(obj,
alpha=.05,
...)
## S3 method for class 'mmclogit'
getSummary(obj,
alpha=.05,
rearrange=NULL,
...)
Arguments
obj an object returned by mblogit or mclogit
alpha level of the confidence intervals; their coverage should be 1-alpha/2
rearrange an optional named list of character vectors. Each element of the list designates
a column in the table of estimates, and each element of a character vector refers
to a coefficient. Names of list elements become column heads and names of the
character vector elements become coefficient labels.
... further arguments; ignored.
Examples
## Not run:
summary(classd.model <- mclogit(cbind(Freq,choice.set)~
(econdim1.sq+nonmatdim1.sq+nonmatdim2.sq)+
(econdim1+nonmatdim1+nonmatdim2)+
(econdim1+nonmatdim1+nonmatdim2):classd,
data=mvoteint.classd,random=~1|mvoteint/eb,
subset=classd!="Farmers"))
myGetSummary.classd <- function(x)getSummary.mclogit(x,rearrange=list(
"Econ. Left/Right"=c(
"Squared effect"="econdim1.sq",
"Linear effect"="econdim1",
" x Intermediate/Manual worker"="econdim1:classdIntermediate",
" x Service class/Manual worker"="econdim1:classdService class",
" x Self-employed/Manual worker"="econdim1:classdSelf-employed"
),
"Lib./Auth."=c(
"Squared effect"="nonmatdim1.sq",
"Linear effect"="nonmatdim1",
" x Intermediate/Manual worker"="nonmatdim1:classdIntermediate",
" x Service class/Manual worker"="nonmatdim1:classdService class",
" x Self-employed/Manual worker"="nonmatdim1:classdSelf-employed"
),
"Mod./Trad."=c(
"Squared effect"="nonmatdim2.sq",
"Linear effect"="nonmatdim2",
" x Intermediate/Manual worker"="nonmatdim2:classdIntermediate",
6 mblogit
library(memisc)
mtable(classd.model,getSummary=myGetSummary.classd)
# Output would look like so:
# ==================================================================================
# Econ. Left/Right Lib./Auth. Mod./Trad.
# ----------------------------------------------------------------------------------
# Squared effect 0.030 0.008 -0.129**
# (0.081) (0.041) (0.047)
# Linear effect -0.583*** -0.038 0.137**
# (0.063) (0.041) (0.045)
# x Intermediate/Manual worker 0.632*** -0.029 -0.015
# (0.026) (0.020) (0.019)
# x Service class/Manual worker 1.158*** 0.084** 0.000
# (0.040) (0.032) (0.030)
# x Self-employed/Manual worker 1.140*** 0.363*** 0.112***
# (0.035) (0.027) (0.026)
# Var(mvoteint) 1.080***
# (0.000)
# Var(mvoteint x eb) 0.118***
# (0.000)
# ----------------------------------------------------------------------------------
# Dispersion 1.561
# Deviance 15007.0
# N 173445
# ==================================================================================
## End(Not run)
Description
The function mblogit fits baseline-category logit models for categorical and multinomial count
responses with fixed alternatives.
Usage
mblogit(
formula,
data = parent.frame(),
random = NULL,
catCov = c("free", "diagonal", "single"),
subset,
mblogit 7
weights = NULL,
na.action = getOption("na.action"),
model = TRUE,
x = FALSE,
y = TRUE,
contrasts = NULL,
method = NULL,
estimator = c("ML", "REML"),
dispersion = FALSE,
start = NULL,
from.table = FALSE,
groups = NULL,
control = if (length(random)) mmclogit.control(...) else mclogit.control(...),
...
)
Arguments
formula the model formula. The response must be a factor or a matrix of counts.
data an optional data frame, list or environment (or object coercible by as.data.frame
to a data frame) containing the variables in the model. If not found in data,
the variables are taken from environment(formula), typically the environment
from which glm is called.
random an optional formula or list of formulas that specify the random-effects structure
or NULL.
catCov a character string that specifies optional restrictions on the covariances of ran-
dom effects between the logit equations. "free" means no restrictions, "diago-
nal" means that random effects pertinent to different categories are uncorrelated,
while "single" means that the random effect variances pertinent to all categories
are identical.
subset an optional vector specifying a subset of observations to be used in the fitting
process.
weights an optional vector of weights to be used in the fitting process. Should be NULL
or a numeric vector.
na.action a function which indicates what should happen when the data contain NAs. The
default is set by the na.action setting of options, and is na.fail if that is
unset. The ‘factory-fresh’ default is na.omit. Another possible value is NULL,
no action. Value na.exclude can be useful.
model a logical value indicating whether model frame should be included as a compo-
nent of the returned value.
x, y logical values indicating whether the response vector and model matrix used in
the fitting process should be returned as components of the returned value.
contrasts an optional list. See the contrasts.arg of model.matrix.default.
method NULL or a character string, either "PQL" or "MQL", specifies the type of the
quasilikelihood approximation to be used if a random-effects model is to be
estimated.
8 mblogit
Details
The function mblogit internally rearranges the data into a ’long’ format and uses mclogit.fit to
compute estimates. Nevertheless, the ’user data’ are unaffected.
Value
mblogit returns an object of class "mblogit", which has almost the same structure as an object
of class "glm". The difference are the components coefficients, residuals, fitted.values,
linear.predictors, and y, which are matrices with number of columns equal to the number of
response categories minus one.
References
Agresti, Alan. 2002. Categorical Data Analysis. 2nd ed, Hoboken, NJ: Wiley. doi:10.1002/
0471249688
Breslow, N.E. and D.G. Clayton. 1993. "Approximate Inference in Generalized Linear Mixed Mod-
els". Journal of the American Statistical Association 88 (421): 9-25. doi:10.1080/01621459.1993.10594284
See Also
The function multinom in package nnet also fits multinomial baseline-category logit models, but
has a slightly less convenient output and does not support overdispersion or random effects. How-
ever, it provides some other options. Baseline-category logit models are also supported by the
package VGAM, as well as some reduced-rank and (semi-parametric) additive generalisations. The
package mnlogit estimates logit models in a way optimized for large numbers of alternatives.
mclogit 9
Examples
library(MASS) # For 'housing' data
library(nnet)
library(memisc)
summary(house.mult)
summary(house.mblogit)
mtable(house.mblogit)
Description
mclogit fits conditional logit models and mixed conditional logit models to count data and individ-
ual choice data, where the choice set may vary across choice occasions.
Conditional logit models without random effects are fitted by Fisher-scoring/IWLS. Models with
random effects (mixed conditional logit models) are estimated via maximum likelihood with a sim-
ple Laplace aproximation (aka PQL).
Usage
Arguments
formula a model formula: a symbolic description of the model to be fitted. The left-
hand side should result in a two-column matrix. The first column contains the
choice counts or choice indicators (alternative is chosen=1, is not chosen=0).
The second column contains unique numbers for each choice set.
The left-hand side can either take the form cbind(choice,set) or (from ver-
sion 0.9.1) choice|set
If individual-level data is used, choice sets correspond to individuals, if aggre-
gated data with choice counts are used, choice sets usually correspond to covari-
ate classes.
The right-hand of the formula contains choice predictors. It should be noted that
constants are deleted from the formula as are predictors that do not vary within
choice sets.
data an optional data frame, list or environment (or object coercible by as.data.frame
to a data frame) containing the variables in the model. If not found in data,
the variables are taken from environment(formula), typically the environment
from which glm is called.
random an optional formula or list of formulas that specify the random-effects structure
or NULL.
weights an optional vector of weights to be used in the fitting process. Should be NULL
or a numeric vector.
offset an optional model offset. Currently only supported for models without random
effects.
subset an optional vector specifying a subset of observations to be used in the fitting
process.
na.action a function which indicates what should happen when the data contain NAs. The
default is set by the na.action setting of options, and is na.fail if that is
unset. The ‘factory-fresh’ default is na.omit. Another possible value is NULL,
no action. Value na.exclude can be useful.
start an optional numerical vector of starting values for the conditional logit parame-
ters. If the model has random effects, the vector should have a "VarCov" attribute
wtih starting values for the random effects (co-)variances. If the random effects
model is estimated with the "PQL" method, the starting values matrix should
also have a "random.effects" attribute, which should have the same structure as
the "random.effects" component of an object returned by mblogit().
model a logical value indicating whether model frame should be included as a compo-
nent of the returned value.
x, y logical values indicating whether the response vector and model matrix used in
the fitting process should be returned as components of the returned value.
contrasts an optional list. See the contrasts.arg of model.matrix.default.
method NULL or a character string, either "PQL" or "MQL", specifies the type of the
quasilikelihood approximation to be used if a random-effects model is to be
estimated.
mclogit 11
Value
mclogit returns an object of class "mclogit", which has almost the same structure as an object of
class "glm".
Note
Covariates that are constant within choice sets are automatically dropped from the model formula
specified by the formula argument of mclogit.
If the model contains random effects, these should
• either vary within choice sets (e.g. the levels of a factor that defines the choice sets should not
be nested within the levels of factor)
• or be random coefficients of covariates that vary within choice sets.
In earlier versions of the package (prior to 0.6) it will lead to a failure of the model fitting algorithm
if these conditions are not satisfied. Since version 0.6 of the package, the function mclogit will
complain about such model a misspecification explicitely.
References
Agresti, Alan (2002). Categorical Data Analysis. 2nd ed, Hoboken, NJ: Wiley. doi:10.1002/
0471249688
Breslow, N.E. and D.G. Clayton (1993). "Approximate Inference in Generalized Linear Mixed
Models". Journal of the American Statistical Association 88 (421): 9-25. doi:10.1080/01621459.1993.10594284
Elff, Martin (2009). "Social Divisions, Party Positions, and Electoral Behaviour". Electoral Studies
28(2): 297-308. doi:10.1016/j.electstud.2009.02.002
McFadden, D. (1973). "Conditionial Logit Analysis of Qualitative Choice Behavior". Pp. 105-135
in P. Zarembka (ed.). Frontiers in Econometrics. New York: Wiley. https://eml.berkeley.edu/
reprints/mcfadden/zarembka.pdf
12 mclogit
See Also
Conditional logit models are also supported by gmnl, mlogit, and survival. survival supports con-
ditional logit models for binary panel data and case-control studies. mlogit and gmnl treat condi-
tional logit models from an econometric perspective. Unlike the present package, they focus on the
random utility interpretation of discrete choice models and support generalisations of conditional
logit models, such as nested logit models, that are intended to overcome the IIA (indipendence from
irrelevant alterantives) assumption. Mixed multinomial models are also supported and estimated us-
ing simulation-based techniques. Unlike the present package, mixed or random-effects extensions
are mainly intended to fit repeated choices of the same individuals and not aggregated choices of
many individuals facing identical alternatives.
Examples
data(Transport)
summary(mclogit(
cbind(resp,suburb)~distance+cost,
data=Transport
))
# New syntactic sugar:
summary(mclogit(
resp|suburb~distance+cost,
data=Transport
))
# Party-level random intercepts and random slopes varying over time points
summary(mclogit(
Freq|time.class~econ.left/class+welfare/class+auth/class,
random=list(~1|party,~econ.left+0|time),
data=electors))
## End(Not run)
mclogit.control 13
Description
mclogit.control returns a list of default parameters that control the fitting process of mclogit.
Usage
mclogit.control(epsilon = 1e-08,
maxit = 25, trace=TRUE)
mmclogit.control(epsilon = 1e-08,
maxit = 25, trace=TRUE,
trace.inner=FALSE,
avoid.increase = FALSE,
break.on.increase = FALSE,
break.on.infinite = FALSE,
break.on.negative = FALSE)
Arguments
epsilon positive convergence tolerance ; the iterations converge when |dev−devold |/(|dev|+
0.1) < .
maxit integer giving the maximal number of IWLS or PQL iterations.
trace logical indicating if output should be produced for each iteration.
trace.inner logical; indicating if output should be produced for each inner iteration of the
PQL method.
avoid.increase logical; should an increase of the deviance be avoided by step truncation?
break.on.increase
logical; should an increase of the deviance be avoided by stopping the algo-
rithm?
break.on.infinite
logical; should an infinite deviance stop the algorithm instead of leading to step
truncation?
break.on.negative
logical; should a negative deviance stop the algorithm?
Value
A list.
14 mclogit.fit
Description
These functions are exported and documented for use by other packages. They are not intended for
end users.
Usage
mclogit.fit(y, s, w, X,
dispersion=FALSE,
start = NULL, offset = NULL,
control = mclogit.control())
mmclogit.fitPQLMQL(y, s, w, X, Z, d,
start = NULL,
start.Phi = NULL,
start.b = NULL,
offset = NULL, method=c("PQL","MQL"),
estimator = c("ML","REML"),
control = mmclogit.control())
Arguments
y a response vector. Should be binary.
s a vector identifying individuals or covariate strata
w a vector with observation weights.
X a model matrix; required.
dispersion a logical value or a character string; whether and how a dispersion parameter
should be estimated. For details see dispersion.
Z the random effects design matrix.
d dimension of random effects. Typically $d=1$ for random intercepts only, $d>1$
for models with random intercepts.
start an optional numerical vector of starting values for the coefficients.
offset an optional model offset. Currently only supported for models without random
effects.
start.Phi an optional matrix of strarting values for the (co-)variance parameters.
start.b an optional list of vectors with starting values for the random effects.
method a character string, either "PQL" or "MQL", specifies the type of the quasilikeli-
hood approximation.
estimator a character string; either "ML" or "REML", specifies which estimator is to be
used/approximated.
control a list of parameters for the fitting process. See mclogit.control
predict 15
Value
A list with components describing the fitted model.
Description
The predict() methods allow to obtain within-sample and out-of-sample predictions from models
fitted with mclogit() and mblogit().
For models with random effecs fitted using the PQL-method, it is possible to obtain responses that
are conditional on the reconstructed random effects.
Usage
Arguments
object an object in class "mblogit", "mmblogit", "mclogit", or "mmclogit"
newdata an optional data frame with new data
type a character string specifying the kind of prediction
se.fit a logical value; whether predictions should be accompanied with standard errors
conditional a logical value; whether predictions should be made conditional on the random
effects (or whether they are set to zero, i.e. their expectation). This argument
is consequential only if the "mmblogit" or "mmclogit" object was created with
method="PQL".
... other arguments, ignored.
Value
The predict methods return either a matrix (unless called with se.fit=TRUE) or a list with two
matrix-valued elements "fit" and "se.fit".
16 simulate.mclogit
Examples
library(MASS)
(house.mblogit <- mblogit(Sat ~ Infl + Type + Cont,
data = housing,
weights=Freq))
str(predict(mcre))
str(predict(mcre,type="response"))
str(predict(mcre,se.fit=TRUE))
str(predict(mcre,type="response",se.fit=TRUE))
Description
The simulate() methods allow to simulate responses from models fitted with mclogit() and
mblogit(). Currently only models without random effects are supported for this.
Usage
## S3 method for class 'mblogit'
simulate(object, nsim = 1, seed = NULL, ...)
## S3 method for class 'mclogit'
simulate(object, nsim = 1, seed = NULL, ...)
Arguments
object an object from the relevant class
nsim a number, specifying the number of simulated responses for each observation.
seed an object specifying if and how the random number generator should be initial-
ized (’seeded’). The interpetation of this argument follows the default method,
see link[stats]{simulate}
... other arguments, ignored.
Value
The result of the simulate method for objects created by mclogit is a data frame with one variable
for each requested simulation run (their number is given by the nsim= argument). The contents
of the columns are counts (or zero-one values), with group-wise multinomial distribution (within
choice sets) just like it is assumed for the original response.
The shape of the result of the simulate method for objects created by mblogit is also a data frame.
The variables within the data frame have a mode or shape that corresponds to the response to which
the model was fitted. If the response is a matrix of counts, then the variables in the data frame
are also matrices of counts. If the response is a factor and mblogit was called with an argument
from.table=FALSE, the variables in the data frame are factors with the same factor levels as the
response to which the model was fitted. If instead the function was called with from.table=TRUE,
the variables in the data frame are counts, which represent frequency weights that would result from
applying as.data.frame to a contingency table of simulated frequency counts.
Examples
library(MASS)
(house.mblogit <- mblogit(Sat ~ Infl + Type + Cont,
data = housing,
weights=Freq,
from.table=TRUE))
sm <- simulate(house.mblogit,nsim=7)
str(sm)
str(sml)
str(smm)
18 Transport
head(smm[[1]])
Description
This is an artificial data set on choice of means of transport based on cost and walking distance.
Usage
data(Transport)
Format
A data frame containing the following variables:
19