Package 'R2mlwin'
Total Page:16
File Type:pdf, Size:1020Kb
Package ‘R2MLwiN’ February 19, 2015 Version 0.2-1 Date 2014-04-22 Title Running MLwiN from within R Depends R (>= 2.14.0), methods, lattice, coda (>= 0.16-1), foreign (>= 0.8-46), rbugs (>= 0.5-7) Suggests lme4 (>= 0.999999-0) OS_type windows Description R2MLwiN is an R command interface to the MLwiN multilevel modelling software package, allowing users to fit multilevel models using MLwiN from within the R environment. License GPL (>= 2) URL https://r-forge.r-project.org/projects/r2mlwin/, http://www.bristol.ac.uk/cmm/software/mlwin/ SystemRequirements MLwiN, (WinBUGs, OpenBUGs optional) Author Zhengzheng Zhang [aut, cre], Chris Charlton [aut], Richard Parker [aut], George Leckie [aut], William Browne [aut] Maintainer Zhengzheng Zhang <[email protected]> NeedsCompilation no Repository CRAN Date/Publication 2014-04-22 18:47:43 R topics documented: R2MLwiN-package . .2 bang1 . .4 caterpillar . .6 caterpillarR . .8 1 2 R2MLwiN-package double2singlePrecision . .9 Formula.translate . 10 jspmix1 . 12 MacroScript1 . 13 MacroScript2 . 16 mlwin2bugs . 22 mlwinfitIGLS-class . 24 mlwinfitMCMC-class . 25 predCurves . 28 predLines . 29 prior2macro . 31 runMLwiN . 32 sixway . 37 trajectories . 39 tutorial . 40 Untoggle . 42 wage1............................................ 43 ws2foreign . 44 xc .............................................. 45 Index 47 R2MLwiN-package R2MLwiN: Running MLwiN from within R Description R2MLwiN is an R command interface to the MLwiN multilevel modelling software package, al- lowing users to fit multilevel models using MLwiN (and also WinBUGS / OpenBUGS) from within the R environment. MLwiN uses both classical and Bayesian approaches to fitting multilevel models, and can model continuous, binomial, count, multivariate, ordered categorical and unordered categorical responses. The data structures it can model include hierarchical, cross-classified and/or multiple membership. Details Package: R2MLwiN Version: 0.2-1 Date: 2014-04-22 License: GPL (>= 2) To use the package, the following objects should be specified: (1) a data.frame object containing the data to be modelled; (2) a model formula; (3) a character (vector) specifying the level ID(s); R2MLwiN-package 3 (4) a list of options used to estimate the model; (5) a vector specifying the distribution to be modelled; (6) a vector specifying BUGS options. If non-null, WinBUGS/OpenBUGS are used, in conjunction with MLwiN, for modelling. (7) a path to the folder where the MLwiN executable is saved; (8) a path to the folder where the output files are to be saved. By default, the temporary directory (tempdir) is used. Once these objects are specified, the runMLwiN function can be used to call MLwiN from R. After execution, the estimates and other statistics are returned to R and can be displayed as a table in the RGui, and the outputs of all parameter estimates (the chains, residuals and BUGs outputs) are also returned for further post-processing in R, as appropriate. Author(s) Zhang, Z., Charlton, C.M.J., Parker, R.M.A., Leckie, G., and Browne, W.J. (2012) Centre for Mul- tilevel Modelling, University of Bristol. Maintainer: Zhengzheng Zhang <[email protected]> References A User’s Guide to MLwiN Version 2.10. Rasbash, J., Steele, F., Browne, W.J. and Goldstein, H. (2009) Centre for Multilevel Modelling, University of Bristol. MCMC estimation in MLwiN Version 2.25. Browne, W.J. (2012) Centre for Multilevel Modelling, University of Bristol. See Also Formula.translate,ws2foreign,read.dta,runMLwiN,MacroScript1,MacroScript2, caterpillar,predLines,predCurves,sixway Examples ## Not run: library(R2MLwiN) ## Modify the following paths as appropriate. ## MLwiN folder mlwin ="C:/Program Files (x86)/MLwiN v2.29/" ## MLwiN sample worksheet folder wspath=paste(mlwin,"/samples/",sep="") ## MLwiN sample worksheet: tutorial dataset wsfile=paste(wspath,"tutorial.ws",sep="");inputfile=paste(tempdir(),"/tutorial.dta",sep="") ws2foreign(wsfile, foreignfile=inputfile, MLwiNPath=mlwin) library(foreign);indata =read.dta(inputfile) ## Define the model formula="normexam~(0|cons+standlrt)+(2|cons+standlrt)+(1|cons)" levID=c('school','student') ## Choose option(s) for inference 4 bang1 estoptions= list(EstM=1) ## Fit the model (mymodel=runMLwiN(formula, levID, D="Normal", indata, estoptions, MLwiNPath=mlwin)) ## The R2MLwiN package includes scripts to replicate all the analyses in ## Browne, W.J. (2009) MCMC estimation in MLwiN Version 2.13. ## Version 2.27 is available online; download from the following link: ## http://www.bristol.ac.uk/cmm/software/mlwin/download/mcmc-print.pdf ## Centre for Multilevel Modelling, University of Bristol #Contents #01 Introduction to MCMC Estimation and Bayesian Modelling #02 Single Level Normal Response Modelling #03 Variance Components Models #04 Other Features of Variance Components Models #05 Prior Distributions, Starting Values and Random Number Seeds #06 Random Slopes Regression Models #07 Using the WinBUGS Interface in MLwiN #08 Running a Simulation Study in MLwiN #09 Modelling Complex Variance at Level 1 / Heteroscedasticity #10 Modelling Binary Responses #11 Poisson Response Modelling #12 Unordered Categorical Responses #13 Ordered Categorical Responses #14 Adjusting for Measurement Errors in Predictor Variables #15 Cross Classified Models #16 Multiple Membership Models #17 Modelling Spatial Data #18 Multivariate Normal Response Models and Missing Data #19 Mixed Response Models and Correlated Residuals #20 Multilevel Factor Analysis Modelling #21 Using Structured MCMC #22 Using the Structured MVN framework for models #23 Using Orthogonal fixed effect vectors #24 Parameter expansion #25 Hierarchical Centring ## Take Chapter03 as an example ## To find the location of a demo for Chapter03 file.show(system.file("demo", "Chapter03.R", package="R2MLwiN")) ## To run the demo for Chapter03 demo(Chapter03) ## End(Not run) bang1 Sub-sample from the 1989 Bangladesh Fertility Survey bang1 5 Description A subset of data from the 1989 Bangladesh Fertility Survey, consisting of 1934 women across 60 districts. Format A data frame with 1934 observations on the following 11 variables: woman Identifying code for each woman (level 1 unit). district Identifying code for each district (level 2 unit). use Contraceptive use status at time of survey; levels are Not using and Using. lc Number of living children at time of survey; levels correspond to none (nokids), one (onekid), two (twokids), or three or more children (threeplus). age Age of woman at time of survey (in years), centred on sample mean of 30 years. urban Type of region of residence; levels are Rural and Urban. educ Woman’s level of education (1 = None, 2 = Lower primary, 3 = Upper primary, 4 = Secondary and above. hindu Woman’s religion; levels are Muslim and Hindu. d_illit Proportion of women in district who are literate. d_pray Proportion of Muslim women in district who pray every day (a measure of religiosity). cons A column of ones. If included as an explanatory variable in a regression model (e.g. in MLwiN), its coefficient is the intercept. Details The bang1 dataset is one of the sample datasets provided with the multilevel-modelling software package MLwiN (Rasbash et al., 2013), and is a subset of data from the 1989 Bangladesh Fertility Survey (Huq and Cleland, 1990) used by Browne (2012) as an example when fitting logistic models for binary and binomial responses. The full sample was analysed in Amin et al. (1997). Source Amin, S., Diamond, I., Steele, F. (1997) Contraception and religiosity in Bangladesh. In: G. W. Jones, J. C. Caldwell, R. M. Douglas, R. M. D’Souza (eds) The Continuing Demographic Transi- tion, 268–289. Oxford: Oxford University Press. Browne, W. J. (2012) MCMC Estimation in MLwiN Version 2.26. University of Bristol: Centre for Multilevel Modelling. Huq, N. M., Cleland, J. (1990) Bangladesh fertility survey, 1989. Dhaka: National Institute of Population Research and Training (NIPORT). Rasbash, J., Browne, W. J., Healy, M., Cameron, B., Charlton, C. M. J. (2013) MLwiN v2.27. University of Bristol: Centre for Multilevel Modelling. See Also See mlmRev package for an alternative format of the same dataset, with fewer variables. 6 caterpillar Examples ## Not run: # NB: change path as appropriate MLwiN <- "C:/Program Files (x86)/MLwiN v2.29/" data(bang1) bang1$use <- as.numeric(bang1$use) - 1 bang1$urban <- as.numeric(bang1$urban) - 1 # Fit 2-level random coefficient logistic model, using MCMC # cons (constant of ones) as denominator specifies # Bernoulli distribution (0/1 response) F1 = "logit(use, cons) ~ (0|cons + age + lc[nokids] + urban) + (2|cons + urban)" ID = c("district", "woman") binomialMCMC <- runMLwiN(Formula = F1, levID = ID, D = "Binomial", indata = bang1, estoptions = list(EstM = 1), MLwiNPath = MLwiN) ## End(Not run) caterpillar Draws a caterpillar plot (in MLwiN style). Description This function uses the means (or medians), upper-quantiles and lower-quantiles for all the groups to draw a caterpillar plot. Usage caterpillar(y, x, qtlow, qtup, xlab = "", ylab = "", xlim = NULL, ylim = NULL, main = "") Arguments y A numerical vector of means (or medians) used as the y coordinates for the plot. x A numerical vector specifying the group labels as x coordinates that correspond to the y coordinates. qtlow A numerical vector of lower-quantiles to be used to plot error bars. qtup A numerical vector of upper-quantiles to be used to plot error bars. xlab A label for the x axis. This is empty by default. ylab A label for the y axis. This is empty by default. xlim The x limits