Learn R Programming

R2MLwiN (version 0.8-1)

write.MCMC: Writes MLwiN macros to fit models using Markov chain Monte Carlo (MCMC) methods

Description

write.MCMC is an internal function which creates an MLwiN macro file to fit models using MCMC.

Usage

write.MCMC(indata, dtafile, oldsyntax = FALSE, resp, levID, expl, rp,
  D = "Normal", nonlinear = c(0, 1), categ = NULL, notation = NULL,
  nonfp = NULL, clre = NULL, Meth = 1, merr = NULL, carcentre = FALSE,
  maxiter = 20, convtol = 2, seed = 1, iterations = 5000,
  burnin = 500, scale = 5.8, thinning = 1, priorParam = "default",
  refresh = 50, fixM = 1, residM = 1, Lev1VarM = 1, OtherVarM = 1,
  adaption = 1, priorcode = 1, rate = 50, tol = 10, lclo = 0,
  mcmcOptions, fact = NULL, xc = NULL, mm = NULL, car = NULL,
  BUGO = NULL, mem.init = "default", optimat = FALSE, modelfile, initfile,
  datafile, macrofile, IGLSfile, MCMCfile, chainfile, MIfile, resifile,
  resi.store = FALSE, resioptions, resichains, FACTchainfile,
  resi.store.levs = NULL, debugmode = FALSE, startval = NULL,
  dami = NULL, namemap = sapply(colnames(indata), as.character))

Arguments

indata
A data.frame object containing the data to be modelled.
dtafile
The file name of the dataset to be imported into MLwiN, which is in Stata format (i.e. with extension .dta).
oldsyntax
Specified as FALSE if new syntax has been used in Formula object, else specified as TRUE (to enable backcompatibility).
resp
A character string (vector) of response variable(s).
levID
A character string (vector) of the specified level ID(s). The ID(s) should be sorted in the descending order of levels (e.g. levID = c('level2', 'level1') where 'level2' is the higher level).
expl
A character string (vector) of explanatory (predictor) variable(s).
rp
A character string (vector) of random part of random variable(s).
D
A character string/vector specifying the type of distribution to be modelled, which can include 'Normal' (the default), 'Binomial', 'Poisson', 'Unordered Multinomial', 'Ordered Multinomial'
nonlinear
A character vector specifying linearisation method for IGLS starting values for discrete response models (see Chapter 9 of Rasbash et al 2012, and Goldstein 2011). N = 0 specifies marginal quasi-likelihood linearization (MQL), whilst
categ
Specifies categorical variable(s) as a matrix. Each column corresponds to a categorical variable; the first row specifies the name(s) of variable(s); the second row specifies the name(s) of reference group(s), NA(s) if no reference group;
notation
Specifies the model subscript notation to be used in the MLwiN equations window. 'class' means no multiple subscripts, whereas 'level' has multiple subscripts.
nonfp
Removes the fixed part of random variable(s). NA if no variable is removed.
clre
A matrix used to estimate some, but not all, of the variances and covariances for a set of coefficients at a particular level. Remove from the random part at level the covariance matrix element(s) defined by the pair(s) of rows
Meth
Specifies the maximum likelihood estimation method to be used when generating starting values via (R)IGLS. If Meth = 0 estimation method is set to RIGLS. If Meth = 1 estimation method is set to IGLS (the default setting). If <
merr
A vector which sets-up measurement errors on predictor variables. The first element N defines the number of variables that have measurement errors. Then, for each variable with measurement error, a pair of inputs is required: the first of
carcentre
If CAR model (i.e. if car is non-NULL), carcentre = TRUE mean-centres all random effects at that level.
maxiter
When generating starting values via (R)IGLS, a numeric value specifying the total number of iterations, from the start, before IGLS estimation halts (if startval = NULL).
convtol
When generating starting values via (R)IGLS, a numeric value specifying the IGLS convergence criterion, as specified in the tol option within estoptions, where startval = NULL) (see
seed
An integer specifying the random seed in MLwiN.
iterations
An integer specifying the number of iterations after burn-in.
burnin
An integer specifying length of the burn-in.
scale
An integer specifying the scale factor of proposed variances; this number will be multiplied by the estimated parameter variance (from IGLS/RIGLS) to give the proposal distribution variance.
thinning
An integer specifying the frequency with which successive values in the Markov chain are stored. By default thinning = 1.
priorParam
A vector specifying the informative priors used, as output from prior2macro.
refresh
An integer specifying how frequently the parameter estimates are refreshed on the screen during iterations; only applies if debugmode = TRUE in estoptions: see runMLwiN.
fixM
Specifies the fixed effect method: 1 for Gibbs Sampling, 2 for univariate MH Sampling and 3 for multivariate MH Sampling.
residM
Specifies the residual method: 1 for Gibbs Sampling, 2 for univariate MH Sampling and 3 for multivariate MH Sampling.
Lev1VarM
Specifies the level 1 variance method: 1 for Gibbs Sampling, 2 for univariate MH Sampling and 3 for multivariate MH Sampling.
OtherVarM
Specifies the variance method for other levels: 1 for Gibbs Sampling, 2 for univariate MH Sampling and 3 for multivariate MH Sampling.
adaption
adaption = 1 indicates adaptation is to be used; 0 otherwise.
priorcode
An integer indicating which default priors are to be used for the variance parameters. This parameter takes the value 1 for Gamma priors or 0 for Uniform on the variance scale priors. See the section on 'Priors' in the MLwiN h
rate
An integer specifying the acceptance rate (as a percentage); this command is ignored if adaption = 0.
tol
An integer specifying tolerance (as a percentage) for the acceptance rate.
lclo
This command toggles on/off the possible forms of complex level 1 variation when using MCMC. lclo = 0 expresses the level 1 variation as a function of the predictors, whereas lclo = 1 expresses the log of the level 1 precision
mcmcOptions
A list of other MCMC options used. See `Details' below.
fact
A list of objects specified for factor analysis. See `Details' below.
xc
Indicates whether model is cross-classified (TRUE) or nested (FALSE). xc = NULL by default (corresponding to FALSE), unless either mm or car are not null, in which case
mm
Specifies the structure of a multiple membership model. Can be a list of variable names, a list of vectors, or a matrix (e.g. see df2matrix). In the case of the former, each element of the list corresp
car
A list specifying structure of a conditional autoregressive (CAR) model. Each element of the list corresponds to a level (classification) of the model, in descending order. If a level is not a spatial classification, then NA is specified.
BUGO
If non-NULL uses BUGS for MCMC estimation using files specified in modelfile, initfile and datafile.
mem.init
A vector which sets and displays worksheet capacities for the current MLwiN session according to the value(s) specified. By default, the number of levels is nlev+1; worksheet size in thousands of cells is 6000; the number of columns is 250
optimat
This option instructs MLwiN to limit the maximum matrix size that can be allocated by the (R)IGLS algorithm. Specify optimat = TRUE if MLwiN gives the following error message 'Overflow allocating smatrix'. This error message arises if one
modelfile
A file name where the BUGS model will be saved in .txt format.
initfile
A file name where the BUGS initial values will be saved in .txt format.
datafile
A file name where the BUGS data will be saved in .txt format.
macrofile
A file name where the MLwiN macro file will be saved.
IGLSfile
A file name where the IGLS estimates will be saved.
MCMCfile
A file name where the MCMC estimates will be saved.
chainfile
A file name where the MCMC chains will be saved.
MIfile
A file name where the missing values will be saved.
resifile
A file name where the residual estimates will be saved.
resi.store
A logical value to indicate if residuals are to be stored (TRUE) or not (FALSE).
resioptions
A string vector to specify the various residual options. The 'variance' option calculates the posterior variances instead of the posterior standard errors; the 'standardised' option calculates standardised residuals.
resichains
A file name where the residual chains will be saved.
FACTchainfile
A file name where the factor chains will be saved.
resi.store.levs
An integer vector indicating the levels at which the residual chains are to be stored.
debugmode
A logical value determining whether MLwiN is run in the background or not. The default value is FALSE: i.e. MLwiN is run in the background. If TRUE the MLwiN GUI is opened, and then pauses after the model has been set-up, allo
startval
A list of numeric vectors specifying the starting values when using MCMC. FP.b corresponds to the estimates for the fixed part; FP.v specifies the variance/covariance estimates for the fixed part; RP.b specifies t
dami
This command outputs a complete (i.e. including non-missing responses) response variable y. If dami = c(0, , ,...) then the response variables returned will be the value of y at the iterations quoted (as integers
namemap
A mapping of column names to DTA friendly shorter names

Value

  • Outputs a modified version of namemap containing newly generated short names.

code

fact

itemize

  • nfact: Specifies the number of factors
  • lev.fact: Specifies the level/classification for the random part of the factor for each factor.
  • nfactcor: Specifies the number of correlated factors
  • factcor: a vector specifying the correlated factors: the first element corresponds to the first factor number, the second to the second factor number, the third element corresponds to the starting value for the covariance and the fourth element to whether this covariance is constrained (1) or not (0). If more than one pair of factors is correlated, then repeat this sequence for each pair.
  • loading: A matrix specifying the starting values for the factor loadings and the starting value of the factor variance. Each row corresponds to a factor.
  • constr: A matrix specifying indicators of whether the factor loadings and the factor variance are constrained (1) or not (0).

Details

A list of other MCMC options as used in the argument mcmcOptions:
  • orth: Iforth = 1, orthogonal fixed effect vectors are used; zero otherwise.
  • hcen: An integer specifying the level where we use hierarchical centering.
  • smcm: Ifsmcm = 1, structured MCMC is used; zero otherwise.
  • smvn: Ifsmvn = 1, the structured MVN framework is used; zero otherwise.
  • paex: A matrix of Nx2; in each row, if the second digit is1, parameter expansion is used at level .
  • mcco: This command allows the user to have constrained settings for the lowest level variance matrix in a multivariate Normal model. If value is0, it estimates distinct variances for each residual error and distinct covariances for each residual error pair. Four other settings are currently available: ll{1 fits stuctured errors with a common correlation paramater and a common variance parameter; 2 fits AR1 errors with a common variance parameter; 3 fits structured errors with a common correlation parameter and independent variance parameters; 4 fits AR1 errors with independent variance parameters.

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

write.IGLS