52
Introducing the STAT- JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Embed Size (px)

Citation preview

Page 1: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Introducing the STAT-JR software package

Professor William Browne, School of Veterinary Science,

University of Bristol

Page 2: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Acknowledgements

• Jon Rasbash for his vision on which Stat-JR was built• All the team behind programming the Stat-JR software – Chris, Danius, Luc, ZZ, Richard, Camille, Bruce, Huanjia and Alex.• Harvey Goldstein, Fiona Steele, George Leckie and all my other CMM colleagues past and present.• ESRC for much funding over the years!

Page 3: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

What will we cover ?

• A little on the history of software at the centre• Some background to MLwiN and why we are developing new software• Ideas behind STAT-JR• Interoperability• Software comparison

Page 4: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

HISTORY

Page 5: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

So where did it all begin?

Q: How does one start writing a statistics package?A: Build it on top of an existing software package!The NANOStat package developed in 1981 by Prof. Mike

Healy was a Minitab clone written in RATFOR (Fortran) as something to do with his new computer!

Mike worked at LSHTM but also at Rothamsted with Fisher!

Recounted writing programs to invert matrices.

Page 6: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

NANOStat (1981)

• Data represented as a set of columns• Commands took columns, numbers and boxes as arguments• Commands strung together so output from 1 command acts

as input to another (became MLwiN macro language)• Software consisted of functions to create columns and several

hundred commands. Still sits under MLwiN today!

Page 7: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

ML2, ML3 and MLN• Jon worked for Mike at LSHTM and then transferred to Harvey

at Institute of Education. • Harvey wrote his IGLS paper in 1986• ML2 came out in 1988 (written in Fortran)• ML3 followed in 1990 (converted to C code)• MLN followed in 1995 (with new N level algorithms written in

C++)• Algorithms very fast for hierarchical models – good matrix

routines for block diagonal structures.• Bob Prosser completed the team.

Page 8: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Moving towards MLwiN - Jon• MLwiN was another step change, this time to a Windows

based program.• ML2/3/N worked in a sequential way with each command

performing an action and then the next etc.… Graphics were limited.

• MLwiN in contrast consists of a GUI front-end (written in Visual Basic) and all the objects are dynamic i.e. changing one window should change the contents of other windows.

• Jon worked with Bruce Cameron setting up this architecture and Bruce is still involved in STAT-JR.

Page 9: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Moving towards MLwiN – Bill in Bath

• I did my MSc. (Comp Stats) in 1995 and used S-Plus and C and BUGS to fit models for my dissertation.

• I then did my PhD. (Stats) between 1995 and 1998 supervised by David Draper. My PhD. included much comparison work between methods of fitting multilevel models (Bayesian & frequentist)

• I did this using both BUGS and MLwiN• An artefact of the process was writing (limited) MCMC

functionality into MLwiN!

Page 10: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

MLWIN

Page 11: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

MLwiN in 1998

What was good / What was new?• Interactive Equations window• Interactive Graphics• Interactive Trajectories window• Choice of estimation methods• Adaptive Metropolis algorithms

Page 12: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interactive Equations Window

This is the 2011 version but the 1998 version had many of the same features.Could toggle between estimates and symbols. Numbers changing from blue to green on convergence. Clicking on the window would allow model construction – see X variable window.

Page 13: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interactive GraphicsGraphics can instantly update as column content changes.

Highlighting of data points passed between graphs.

Highlighting can be done at different levels of the data structure

Page 14: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interactive Trajectories WindowTrajectories plot particularly useful for MCMC estimation

First software (to my knowledge) to have dynamic chains although they appeared in WinBUGS soon after.

Used to joke about taking a coffee break while MCMC ran and chains make you look busy! Click on graph to get diagnostics

Page 15: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Sixway diagnostic plotPlot of chain plus kernel density – code from my MSc!

Kernel would show informative priors as well.

Time series plots and originally MCSE and summary in bottom 2 boxes.

Expanded to 7 boxes later.

Page 16: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

MCMC functionality

• In 1998 we had implemented Normal, Binomial and Poisson N level models by shoe horning my stand-alone C code into MLwiN in a couple of manic visits to London.

• For Normal responses used Gibbs sampling, for others used a mixture of Gibbs and random walk Metropolis including my ad-hoc adapting scheme that persists today.

• BUGS used AR sampling instead of MH at the time.• Code was very model specific and optimised so far faster than

BUGS as it still is today!

Page 17: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Changes to MLwiN in my time at IOE (1998-2003)

• Better handling of missing data, categorical variables.• Lots more data manipulation windows.• MCMC functionality for:XC/MM/Spatial CAR models (Browne et al. 2001a)Multilevel Factor Analysis (Goldstein & Browne 2002,2005)Measurement Error (Browne et al 2001b)Multivariate/Mixed responses/Complex variation (Browne 2006)

Page 18: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Documentation

Part of the success of MLwiN is due to the quality and quantity of the accompanying documentation:

• Users Guide in 1998 (Goldstein et al. ~130 pages)By 2003 • Users Guide (Rasbash et al. ~ 250 pages) & MCMC Guide

(Browne et al.) & Command guideToday: User’s Guide (314 pages) Supplement (114 pages) MCMC

Guide (439 pages) Command guide (114 pages)Huge effort by colleagues in centre on web-based training

materials (over 6,000 registered users)

Page 19: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

MLwiN (2003-)

We still develop MLwiN even after Jon Rasbash’s death:• The Users guide supplement shows Jon & Chris’s work on

improved model specification, model comparison, customised predictions and auto-correlated error modelling.

• The last 5 chapters of the MCMC guide shows my work on MCMC algorithm improvements (SMVN, SMCMC, hierarchical centring, parameter expansion and orthogonal parameterisations) which we covered in the last lecture. See also Browne et al. (2009)

Page 20: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

STAT-JR

Page 21: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Jon Rasbash’s big vision

• Jon had been thinking hard on where the software went next. • The frequentist IGLS algorithm was hard to extend further.• WinBUGS showed that MCMC as an algorithm could be

extended easily but the difficulty in MLwiN was in extending my MCMC code and possibly relying on the personnel!

• The big vision was an all-singing all-dancing system where expert users could add functionality easily and which interoperates with other software. Bruce was developing an underpinning algebra system.

• The ESRC LEMMA II and E-STAT grants would enable this to be achieved. Work continues in LEMMA III

Page 22: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

STAT-JR

Jon identified 3 groups of users:• Novice practitioners who want to use statistical software that

is user friendly and maybe tailored to their discipline• Advanced practitioners who are the experts in their fields and

also want to develop tools for the novice practitioners• Algorithm Developers who want their algorithms used by

practitioners.• See

http://www.cmm.bristol.ac.uk/research/NCESS-EStat/news.shtml for details of documentation for STAT-JR and several meeting presentations.

Page 23: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

STAT-JR component based approachBelow is an early diagram of how we envisioned the system. Here you will see

boxes representing components some of which are built into the STAT-JR system. The system is written in Python with currently a VB.net algebra processing system. A team of coders (currently me, Chris, Danius, Camille Zhengzheng, Huanjia, Richard and Bruce) work together on the system.

Page 24: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Templates

Consist of a set of code sections for advanced users to write.For a model template it consists of at least:• an invars method which specifies inputs and types• An outbug method that creates (BUGS like) model code for

the algebra system• An (optional) outlatex method can be used for outputting

LaTeX code for the model.Other optional functions required for more complex templates

Page 25: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Regression 1 Examplefrom EStat.Templating import *from mako.template import Template as MakoTemplateimport re

class Regression1(Template): 'A model template for fitting 1 level Normal multiple

regression model in E-STAT only. To be used in documentation.'

tags = [ 'Model', '1-Level', 'e-STAT', 'Normal' ] engines = ['eSTAT']

invars = '''y = DataVector('response: ')tau = ParamScalar()sigma = ParamScalar()sigma2 = ParamScalar()x = DataMatrix('explanatory variables: ')beta = ParamVector(parents=[x], as_scalar=True)'''

outbug = '''model{ for (i in 1:length(${y})) { ${y}[i] ~ dnorm(mu[i], tau) mu[i] <- ${mmult(x, 'beta', 'i')} } # Priors % for i in range(0, x.ncols()): beta${i} ~ dflat() % endfor tau ~ dgamma(0.001000, 0.001000) sigma2 <- 1 / tau sigma <- 1 / sqrt(tau)} ''' outlatex = r'''\begin{aligned} \mbox{${y}}_i & \sim \mbox{N}(\mu_i, \sigma^2) \\ \mu_i & = ${mmulttex(x, r'\beta', 'i')} \\ %for i in range(0, len(x)):\beta_${i} & \propto 1 \\ %endfor\tau & \sim \Gamma (0.001,0.001) \\ \sigma^2 & = 1 / \tau\end{aligned} '''

Page 26: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

An example of STAT-JR – setting up a model

Page 27: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

An example of STAT-JR – setting up a model

Page 28: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Equations for model and model code

Page 29: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Equations for model and model code

• Note: Equations use MATHJAX and so underlying LaTeX can be copied and paste. The model code is based around the WinBUGS language with some variation.

Page 30: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Model code in detailmodel{ for (i in 1:length(normexam)) { normexam[i] ~ dnorm(mu[i], tau) mu[i] <- cons[i] * beta0 + standlrt[i] * beta1 } # Priors beta0 ~ dflat() beta1 ~ dflat() tau ~ dgamma(0.001000, 0.001000) sigma2 <- 1 / tau }

For this template the code is, aside from the length function, standard WinBUGS model code.

Page 31: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Bruce’s Algebra system steps

Page 32: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Bruce’s Algebra system steps

Here the first line is what is returned by Bruce’s algebra system – which works solely on the model code.The second line is what can be calculated when values are added for constants etc.

Page 33: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Output of generated C++ code (old screen)

The package can output C++ code that can then be taken away by software developers and modified.

Page 34: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Output of generated C++ code (new screen)

// Update beta1{ beta1 = dnorm((0.000249799765395*(2382.12631198+(beta0*(-

7.34783096611)))),(4003.20632175*tau)); }// Update beta0 { beta0 = dnorm((((-0.462375992909)+((-

7.34783096611)*beta1))*0.000246366100025),(tau*4059.0));}• Note now that the code includes the actual data in place of constants and

so looks less like the familiar algebraic expressions

Page 35: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Output from the E-STAT engine

Estimates and the DIC diagnostic can be viewed for the model fitted.

Page 36: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Output from the E-STAT engine

E-STAT offers multiple chains so that we can use multiple chain diagnostics to aid convergence checking.

Otherwise the graphs are borrowed from the MLwiN 6-way plotting.

Page 37: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

INTEROPERABILITY

Page 38: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interoperability with WinBUGS (Regression 2)

This template offers the choice of many software packages for fitting a regression model.

STAT-JR checks what is installed on the machine and only offers packages that are installed.Here we choose WinBUGS.

Interoperability in the user interface is obtained via a few extra inputs. In fact in the template code user written functions are required for all packages apart from WinBUGS, OpenBUGS and JAGS. The transfer of data between packages is however generic.

Page 39: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interoperability with WinBUGS (Regression 2)

Here we can view the files required to run WinBUGS in the left hand pane (script file shown but model, inits and data also available)

The model can be run by press of a button.

Page 40: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interoperability with RR can be chosen as another alternative. In fact here we have 2 choices – MASS or MCMCglmm.

You will see in the left pane the data file ready for input to R

Page 41: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Interoperability with RIf written in to the code in the template – graphics from other software can be extracted.

Here for example is a residual plot associated with the R fit of the model.

Page 42: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Other templates - XYplotThere are also templates for plotting.

For example here is a plot using the Xyplot template.

The left pane gives the Python script and the right pane the plot.

Page 43: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Different forms of STAT-JR and E-books

• Webtest - the format we have demonstrated up to now. Allows user to investigate 1 template and 1 dataset. A dataset can be output from 1 template and then used by the next.

• Cmdtest – this format involves the use of a Python script and allows the template to be called from within a script. Helpful for our test suite and potential for tasks like simulations.

• E-book – mixing up templates with textboxes to make executable books – this will be covered by Richard in next talk

Page 44: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Comparing software packages through interoperability

Page 45: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Comparing MCMC engines• Example taken from section 4.4.4 of Beginner’s guide to Stat-

JR (Browne et al. 2012) available to download at http://www.bristol.ac.uk/cmm/research/estat/downloads/index.html

• Model is a two level logistic regression model fitted to a demographic dataset on contraceptive use in Bangladesh

• Fitted model in 5 packages through Stat-JR: It’s own e-STAT engine, WinBUGS, OpenBUGS, JAGS and MCMC in MLwiN

• For each package used multiple chains with 3 chains each with a burnin of 2,500 and main run of 2,500

• Note MCMCglmm another alternative but not with multiple chains.

Page 46: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Settings for 2LevelCat template

Page 47: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Model description

Page 48: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Methods

• WinBUGS / OpenBUGS uses multivariate updating method for the fixed effects in a GLMM, as developed by Gamerman (1997)

• JAGS possibly uses MH Sampling though not clear• E-Stat and MLwiN use single-site MH Sampling with adapting

to tune proposals• Also include for E-Stat orthogonal parameterisation method

of Browne et al. (2009) which transforms data before fitting and then transforms resulting parameters back to original parameterisation.

Page 49: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Results – first part Parameter e-STAT WinBUGS OpenBUGS JAGS MLwiN e-STAT

orthogonal Beta0 -0.800

(0.176) -0.789

(0.170) -0.790

(0.173) -0.797

(0.183) -0.832

(0.175) -0.772

(0.169) Beta0 ESS 110 396 2607 213 94 975 Beta1 0.0067

(0.0093) 0.0068

(0.0090) 0.0066

(0.0091) 0.0061

(0.0094) 0.0051

(0.0091) 0.0067

(0.0091) Beta1 ESS 232 953 5055 485 244 1880 Beta2 -0.0048

(0.00072) -0.0048

(0.00072) -0.0048

(0.00073) -0.0048

(0.00073) -0.0047

(0.00073) -0.0048

(0.00070) Beta2 ESS 337 1288 5037 728 332 1743 Beta3 0.783

(0.164) 0.779

(0.160) 0.782

(0.162) 0.787

(0.166) 0.797

(0.163) 0.783

(0.163) Beta3 ESS 271 1117 5291 457 250 1621 Beta4 0.826

(0.190) 0.822

(0.181) 0.826

(0.186) 0.833

(0.189) 0.855

(0.187) 0.820

(0.184) Beta4 ESS 209 780 5205 337 192 1588

Page 50: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Results – part 2Parameter e-STAT WinBUGS OpenBUGS JAGS MLwiN e-STAT

orthogonal Beta5 0.828

(0.189) 0.824

(0.180) 0.827

(0.184) 0.837

(0.193) 0.861

(0.183) 0.825

(0.184) Beta5 ESS 151 547 4446 270 128 1801 Sigma2u 0.324

(0.103) 0.318

(0.100) 0.317

(0.101) 0.317

(0.102) 0.317

(0.099) 0.320

(0.099) Sigma2u ESS

737 1764 1771 1401 770 955

Pd 42.77 42.44 42.67 42.16 42.49 42.12 DIC 2393.29 2393.35 2394.0 2393.55 2393.65 2393.63 Time (s) 129 350 260 421 35 150

Note MLwiN run in parallel and so really should be 3x35 = 105s!

Page 51: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Conclusions

• All methods, as one would hope, give similar answers give or take Monte Carlo errors - interest in ESS and time

• Gamerman (1997) method very effective on this example but puzzling why works much better for OpenBUGS than WinBUGS?

• JAGS appears to perform better than MLwiN and eSTAT in terms of ESS – for other examples better than BUGS.

• Both MLwiN and eSTAT faster than other software• Orthogonal parameterisation works well and makes eSTAT

competitive with OpenBUGS – it’s also available in MLwiN.

Page 52: Introducing the STAT-JR software package Professor William Browne, School of Veterinary Science, University of Bristol

Final Remarks and Questions?

• Covered history and new developments• Shown screenshots of the software and how it works• Shown interoperability and how it can be used for model

comparison• Free to download into beta form for (renewable) 30 day

licences from http://www.bristol.ac.uk/cmm/research/estat/downloads/index.html

• We have a stand where we will be happy to show other features

• And now onwards to Richard and E-books!