runMLwiN {R2MLwiN}R Documentation

Calls MLwiN from R.

Description

This function executes MLwiN and then brings results back to R.

Usage

runMLwiN(
  Formula,
  levID = NULL,
  D = "Normal",
  data = NULL,
  estoptions = list(EstM = 0),
  BUGO = NULL,
  MLwiNPath = NULL,
  stdout = "",
  stderr = "",
  workdir = tempdir(),
  checkversion = TRUE,
  allowcontrast = FALSE,
  indata = NULL,
  saveworksheet = NULL
)

Arguments

Formula

A formula object specifying the model formula. See Formula.translate (Formula.translate.compat details back-compatible functionality for deprecated syntax used in versions of R2MLwiN prior to 0.8-0) and also ‘Details’ below.

levID

A character vector specifying the level ID(s). Deprecated syntax: by default this is NULL and level ID(s) are specified in the Formula object.

D

A character string/vector specifying the type of distribution to be modelled, which can include 'Normal' (the default), 'Binomial', 'Poisson', 'Negbinom', 'Unordered Multinomial', 'Ordered Multinomial', 'Multivariate Normal', or 'Mixed'. In the case of the latter, 'Mixed' precedes the response types which also need to be listed in D, e.g. c('Mixed', 'Normal', 'Binomial'); these need to be be listed in the same order to which they are referred to in the Formula object (see Formula.translate, Formula.translate.compat). For (R)IGLS estimation (i.e. EstM = 0 in estoptions) 'Mixed' combinations can consist of 'Normal' and 'Binomial' or 'Normal' and 'Poisson'; for MCMC estimation (i.e. EstM = 0), on the other hand, only a combination of 'Normal' and 'Binomial' is available.

data

A data.frame object containing the data to be modelled. Optional (but recommended): if empty, data taken from environment of formula.

estoptions

A list of options used for estimating the model. See ‘Details’ below.

BUGO

A vector specifying BUGS options. If non-null, then WinBUGS/OpenBUGS, in conjunction with MLwiN, are used for modelling. Non-null only applicable if EstM = 1. See ‘Details’, below.

MLwiNPath

A path to the MLwiN folder. By default, MLwiNPath = NULL and path set by options('MLwiN_path'), the default for which can be changed via options(MLwiN_path = 'path/to/MLwiN vX.XX/')).

stdout

See system2; '' by default (i.e. output to stdout sent to R console).

stderr

See system2; '' by default (i.e. output to stderr sent to R console).

workdir

A path to the folder where the outputted files are to be saved. If the folder specified does not exist, a new folder of that name is created; workdir = tempdir() by default.

checkversion

If TRUE (default), returns version number unless (a) version detected is unknown or newer than MLwiN version available when current version of R2MLwiN was released, in which case returns text to this effect, or (b) version detected > 1 year older than MLwiN version available when current version of R2MLwiN was released, in which case function call stopped and user invited to update via usual channels. Can disable via FALSE e.g. if slowing execution time down (for example in a simulation).

allowcontrast

If TRUE, factor variables will follow the R behaviour when creating contrast variables. If FALSE (default) factor variables will be converted into a series of zero/one dummies.

indata

A data.frame object containing the data to be modelled. Deprecated syntax: by default this is NULL and the data.frame is instead referenced via data.

saveworksheet

A file name (or list of file names if more than one chain is specified) used to store the MLwiN worksheet after the model has been estimated.

Details

With regard to runMLwiN's Formula object, see formula for notes on general usage, noting the following differences:

Further details are as follows.

The random part of the model is specified in sets of parentheses arranged in descending order with respect to their hierarchy. E.g. in the case of a 3-level model, the variable containing the level 3 ID is specified first, then the variable containing the level 2 ID, etc. Note that the variable containing the level 1 ID also needs to be explicitly specified unless it is a discrete response model (in which case you should not specify it).

The table below summarises the options for the Formula argument in R2MLwiN. They assume an intercept is added (via ~ 1; for alternative specifications see formula). <link> denotes the link function, <y1>, <y2>, etc. represent response variables, <denom> denotes the denominator, <offs> the offset (optional), <L2>, <L1>, etc. the variables containing the level 2 and level 1 identifying codes, and <ref_cat> represents the reference category of a categorical response variable (optional: if unspecified the lowest level of the factor is used as the reference category). Explanatory variables are specified as e.g. <x1> + <x2>. For 'Ordered Multinomial', 'Multivariate Normal' and 'Mixed' responses, [<common>] indicates a common coefficient (i.e. the same for each category) is to be fitted; here <common> takes the form of a numeric identifier indicating the responses for which a common coefficient is to be added (e.g. [1:5] to fit a common coefficient for categories 1 to 5 of a 6-point ordered variable, [1] to fit a common coefficient for the response variable specified first in the Formula object for a 'Mixed' response model, etc.) Otherwise a separate coefficient (i.e. one for each category) is added. For 'Mixed' response models, the Formula arguments need to be grouped in the order the distributions are listed in D.

* denotes IGLS only in the table below.

Distribution Format of Formula object Where <link> can equal...
'Normal' <y1> ~ 1 + <x1> + (1|<L2>) + (1|<L1>) + ... (identity link assumed)
'Poisson' <link>(<y1>) ~ 1 + offset(<offs>) + <x1> + (1|<L2>) + ... log
'Negbinom' <link>(<y1>) ~ 1 + offset(<offs>) + (1|<L2>) + ... log
'Binomial' <link>(<y1>, <denom>) ~ 1 + <x1> + (1|<L2>) + ... logit,probit,cloglog
'Unordered Multinomial' <link>(<y1>, <denom>, <ref_cat>) ~ 1 + <x1> + (1|<L2>) + ... logit
'Ordered Multinomial' <link>(<y1>, <denom>, <ref_cat>) ~ 1 + <x1> + <x2>[<common>] + (1[<common>]|<L3>) + (1|<L2>) + ... logit,probit,cloglog
'Multivariate Normal' c(<y1>, <y2>, ...) ~ 1 + <x1> + <x2>[<common>] + (1[<common>]|<L3>) + (1|<L2>) + (1|<L1>) + ... (identity link assumed)
c('Mixed', 'Normal', 'Binomial') c(<y1>, ..., <link> (<y2>, <denom>), ...) ~ 1 + <x1> + <x2>[<common>] + (1[<common>]|<L3>) + (1|<L2>) + (1|<L1>) + ... logit*,probit,cloglog*
c('Mixed', 'Normal', 'Poisson')* c(<y1>, ..., <link>(<y2>, <offset>), ...) ~ 1 + <x1> + <x2>[<common>] + (1[<common>]|<L3>) + (1|<L2>) + (1|<L1>) + ... log

The argument estoptions is a list which can contain the following options used for estimating the model:

The argument BUGO is a vector specifying BUGS options as follows:

Value

If BUGO is non-NULL then the output is an mcmc.list object.

If the IGLS algorithm is used (i.e., EstM = 0), then returns mlwinfitIGLS-class object; if MCMC estimation used (i.e., EstM = 1), then returns mlwinfitMCMC-class object.

Author(s)

Zhang, Z., Charlton, C.M.J., Parker, R.M.A., Leckie, G., and Browne, W.J. (2016) Centre for Multilevel Modelling, University of Bristol.

References

Goldstein, H. (2011) Multilevel Statistical Models. 4th Edition. London: John Wiley and Sons.

Rasbash, J., Steele, F., Browne, W.J. and Goldstein, H. (2012) A User's Guide to MLwiN Version 2.26. Centre for Multilevel Modelling, University of Bristol.

See Also

formula, Formula.translate, Formula.translate.compat, write.IGLS, write.MCMC

Examples


## The R2MLwiN package includes scripts to replicate all the analyses in
## Rasbash et al (2012) A User's Guide to MLwiN Version 2.26 and
## Browne, W.J. (2012) MCMC estimation in MLwiN Version 2.26.
## The MLwiN manuals are available online, see:
## http://www.bristol.ac.uk/cmm/software/mlwin/download/manuals.html

## Not run: 
library(R2MLwiN)
# NOTE: if MLwiN not saved in location R2MLwiN defaults to, specify path via:
# options(MLwiN_path = 'path/to/MLwiN vX.XX/')
# If using R2MLwiN via WINE, the path may look like this:
# options(MLwiN_path = '/home/USERNAME/.wine/drive_c/Program Files (x86)/MLwiN vX.XX/')

## For a list of demo titles
demo(package = 'R2MLwiN')

## Take MCMCGuide03 as an example
## To view file
file.show(system.file('demo', 'MCMCGuide03.R', package='R2MLwiN'))

## To run the demo
demo(MCMCGuide03)

## End(Not run)


[Package R2MLwiN version 0.8-9 Index]