172 views

Uploaded by sommukh

- UT Dallas Syllabus for stat7330.0u1.08u taught by Michael Baron (mbaron)
- Int__Computacional_2016___Ex__1__Linear_Regression.pdf
- ERIC Multivariate Analysis Commresearcj
- 10072015_Annexure-63
- Example Class One
- Regression
- Educational Psychology
- Critical Reviews 32
- visit2
- p41.pdf
- Log Explanation
- Urban Area Microzonation as Prevention in Mаnaging Earthquake Risk
- Full Text 262010
- Analysis of Lead Time Delays in Supply Chain a Case Study
- Datascience Online Training in Hyderabad
- Measuring the Level of Competition in the Loan
- Climate Change Research Methodology
- R-Code for various Regressions
- 30313-33155-1-PB
- Report Format

You are on page 1of 3

http://people.duke.edu/~neelo003/r/

R Examples

This page contains examples of R programs for running standard Bayesian regression models (linear regression, logistic/probit regression, random effects models, latent class models, etc.). A basic working knowledge of R and Bayesian computation is assumed. Note: Several of the programs below require you to download R packages such as mvtnorm, MCMCpack, etc. General Disclaimer: These programs are meant as heuristic aids for newcomers to Bayesian inference, not as exemplars of great code! Please email me if you find errors or have questions...thanks!

Useful References Bayesian Data Analysis by Andrew Gelman et al. A First Course in Bayesian Statistical Methods by Peter Hoff Bayesian Computation with R by Jim Albert Finite Mixture and Markov Switching Models by Sylvia Fruhwirth-Schnatter

R Programs Normal and Linear Regression Models 1. Normal Mean Gibbs.r: Gibbs sampler for normal model with unknown mean and variance 2. Normal Mean Metropolis.r: Normal mean model with Metropolis update 3. Bivariate Normal.zip: Two approaches to fitting a bivariate normal linear regression model. BivNorm condtional.r updates regression parameters from their univariate full conditionals. BinNorm Joint.r upates Y1,Y2 jointly -- this is easier code to follow, but much slower. 4. Random Intercept Mixed Model.r: Gibbs sampler for balanced-design mixed model with random intercept 5. Random Slope Mixed Model.r: Gibbs sampler for random slope model with unbalanced design. Centers out the random effects to improve mixing of fixed effects parameters, beta. Here is another version which updates beta after marginalizing out the random effects to further improve mixing. See Chib and Carlin (1999) Statistics and Computing for more information. Note: both programs are slow as n increases, due to inversion of large block diagonals. You may want to try a sparse matrix algorithm to improve mixing. Or, more crudely, you can simply update random effects one-at-a-time via a loop, as in this program. This is acutally a bit faster than the matrix-inversion programs for n>200 or so. But it's not the best solution. Random Slope Using MH: Uses MH for updating the random effects and is faster than the previous programs (although still not great). MH Function.r is the Metropolis function for udpating the random slopes, and Random Slope MH.r generates data and runs the MCMC. Logistic, Multinomial and Probit Regression Models 1. Probit Model for Binary Data.r: Gibbs sampler for probit regression using Albert and Chib (1993) data-augmentation method 2. Fixed Effects Logistic Regression.zip: Files for running basic logistic regression using Metropolis step. Contains two files: 1. Update Beta Fixed.r: function for updating beta (run this first) 2. Log1.r: Generates data and runs MCMC (either source in "Update Beta" function or run "Update Beta"

1 of 3

1/11/2013 4:02 AM

http://people.duke.edu/~neelo003/r/

first) 3. Multinomial Logit Model.r: Gibbs sampler for multinomial logit model using Metropolis algorithm. 4. Probit Random Intercept Model.r: Gibbs sampler for probit random intercept model using data-augmentation approach 5. Random Intercept Logistic Regression.zip: Files for running random intercept logistic regression using Gibbs and Metropolis steps. Contains three files: 1. Update Beta Random.r: function for updating beta (run this first) 2. Update b.r: function for updating random intercept b (run this second) 3. Log2.r: Generates data and runs MCMC (either source in "Update Beta" and "Update b" functions or run those first) 6. Bivariate Probit Model.zip: Various MCMC programs for fixed-effects bivariate (correlated) probit regression model. See "Read Me" file for details 7. Bivariate Probit Model with Random Effects.r: MCMC for random-intercept bivariate probit regression model. Count Models 1. Zero Inflated Poisson.r: Fixed effects zero-inflated Poisson (ZIP) regression using MH. 2. Spatial Poisson Hurdle Model.zip: R2WinBUGS and BUGS code for simulating and fitting the spatial Poisson hurdle model described here. See README.txt for file discriptions. Bayesian Two-Part Latent Class Model 1. Bayesian Two-Part.zip: R code for running latent-class two-part model as described in Neelon et al. (2011), with three latent classes and random intercepts within classes. Contains 8 files: 1. Three Class MCMC.r: This is the main program for implementing the MCMC algorithm. It calls the other programs, but, of course, you'll need to change the file path names; 2. Update Gamma.r, Update Alpha.r, etc: These are the five functions used in the Gibbs and MH steps; 3. simdata.txt: Data file 4. Make simdata.r: Creates simdata.txt 2. Growth Mixture Model for Blood Pressure and Birth Outcomes: Contains R code for simulating and fitting the Bayesian growth mixture model (GMM) desribed in Neelon et al. (2011) A Bayesian Growth Mixture Model to Examine Maternal Hypertension and Birth Outcomes.See README.txt for file discriptions. Finite Mixture and Dirichlet Process Models 1. Two-Component Finite Mixture.r: Gibbs sampler for two-component (or "two-class") finite mixture normal model with class-specific means and variances. Dirichlet prior on class-membership probabilities. The program also illustrates the so-called "label-switching" problem and uses order-constraint solution to resolve the issue. See Fruhwirth-Schnatter (2006) for a more general discussion. 2. Two-Class Bivariate Normal Mixture.zip: Zip file contains 5 programs: 1) makedata.r creates two-class bivariate normal data with a class-level covariate ("male"); 2) data.txt is the data file; 3) MCMC.r runs the mcmc using the conditional udpating scheme described above in BivNorm.zip; programs 4 and 5 are Update C.r and Update Gamma.r---these are functions for updating the latent class indicators and the class-specific regression parameters (gamma's) via MH. 3. Three-Component Dirichlet Process.r: Uses stick-breaking, Dirichlet process model to estimate three-component normal mixture density. For a nice review of DPs, and Bayesian nonparametrics in general, see this paper by David Dunson.

2 of 3

1/11/2013 4:02 AM

http://people.duke.edu/~neelo003/r/

Latent Factor Model Bayesian Latent Factor Model.r: Three-factor linear model with M=6 manifest variables. See Lopes and West (Statistica Sinica, 2004) for details. Bayesian Isotonic Regression 1. Bayesian Order-Restricted Inference for Categorical Predictors: Contains files for implemented Bayesian orderrestricted inference on categorical predictors, as described in Dunson and Neelon (2003). The ZIP file contains two R programs: the first is the minmax function used for imposing the order restriction, and the second simulates a linear model with three categorical covariates and applies the minmax monotonicity restriction as part of a Gibbs sampling algorithm. 2. Bayesian Isotonic Regression for Continuous Predictors: The program first generates normal data with a montone-increasing sigmoidal mean function. It then fits a Bayesian, piecewise-linear isotonic regression model with a fixed number of "free" knots (i.e., unknown knot locations). This approach provides a reasonable estimate the mean function, but it assumes that the number of knots is known; to account for uncertainty in the number of knots (K), one can re-fit the model under different choices of K and use model averaging. Alternatively, one can use reversible jump MCMC or a model comparison criterion, such as BIC or DIC, to choose the "optimal" number of knots. The method is outlined in this working paper. See Denison, Mallick and Smith (1998) and Holmes and Heard (2003) for a related approaches. Spatial Analysis for Areal Data 1. Normal ICAR Model.r: Generates and fits data for spatially varying normal data under intrinsic CAR (ICAR) model. To generate data, we set spatial smoothing parameter close to 1 (but not equal to 1) to ensure that the joint covariance of the spatial random effects is non-singular. The full condtionals are given here. 2. Bivariate CAR (biCAR) Model.zip: Generates and fits data for spatially varying bivariate normal data under intrinsic bivariate CAR (biCAR) model. The Gibbs sampling algorithm and data generation are in bicar.r, and the function for updating the spatial effects phi are in update phi.r. The full condtionals are given here. 3. Spatial Bivariate Probit (Biprobit) Model.zip: Generates and fits data for correlated binary data as described in Neelon et al. (2012) A spatial bivariate probit model for correlated binary data with application to adverse birth outcomes. More to come...

3 of 3

1/11/2013 4:02 AM

- UT Dallas Syllabus for stat7330.0u1.08u taught by Michael Baron (mbaron)Uploaded byUT Dallas Provost's Technology Group
- Int__Computacional_2016___Ex__1__Linear_Regression.pdfUploaded byRodrigo De Santis
- ERIC Multivariate Analysis CommresearcjUploaded byIala Raolona
- 10072015_Annexure-63Uploaded bysaurabhmc
- Example Class OneUploaded byas5155
- RegressionUploaded bymirelaasman
- Educational PsychologyUploaded byIoana Basarab
- Critical Reviews 32Uploaded byNadia Jimena C A
- visit2Uploaded byshivam chugh
- p41.pdfUploaded byshaikh a n
- Log ExplanationUploaded byMarkWeber
- Urban Area Microzonation as Prevention in Mаnaging Earthquake RiskUploaded bySEP-Publisher
- Full Text 262010Uploaded bySertaç Karaman
- Analysis of Lead Time Delays in Supply Chain a Case StudyUploaded byKumar Vinit
- Datascience Online Training in HyderabadUploaded byMohan R
- Measuring the Level of Competition in the LoanUploaded byAl Ihsan
- Climate Change Research MethodologyUploaded byMarissa Adraincem
- R-Code for various RegressionsUploaded byMujahid Khan
- 30313-33155-1-PBUploaded byWiiznu Scremo Xen Xluet
- Report FormatUploaded byYousouf Iqbal
- 118-355-1-PB (1)Uploaded byJefferson Ullegue
- Gender, Class, And Social DesirabilityUploaded bydonnakishot
- Statistical Analysis and Predictive Modeling for Optimization of Geriatrics Healthcare ProcessesUploaded byRafael San Miguel Carrasco
- Degree of Competetiveness in India AutomobileUploaded bysukanta89
- Impact of Oil and Gas Technology on Nigeria ( the Niger Delta Experience) for PublicationUploaded byNewman Enyioko
- jkan-43-154Uploaded byDnyanesh Ambhore
- Family Of Dirichlet DistributionsUploaded byYragosnhoj3118
- Factors that contribute to social media influence within an Internal Medicine Twitter learning communityUploaded byNephrology On-Demand
- Sestelo Roca Report 2010Uploaded byMarta Sestelo
- Correlation of Limestone Properties with Bit Performance Variables for LIMROCKWARE2010 DevelopmentUploaded bySEP-Publisher

- 214.7 (MH)Uploaded bysommukh
- Section 6 is Far Too LongUploaded bysommukh
- Appendix 2Uploaded bysoumya
- Text_S1Uploaded bysommukh
- ZipUploaded bysommukh
- Ac SignalingUploaded bysommukh
- General Correlated Ranefs Bayes Jags.rUploaded bysommukh
- Chan Jeliazkov 2009Uploaded bysommukh
- e Ticket 2014 FebUploaded bysommukh
- A Dynamic Quality Ladder Model With Entry and Exit- Exploring TheUploaded bysommukh
- amUploaded bysommukh
- Signalling Value of Registration_2Uploaded bysommukh
- PhD HyekyungJungUploaded bysommukh
- Doc1Uploaded bysommukh
- Chan Jeli Az Kov 2009Uploaded bysommukh
- OrdinalUploaded bysommukh
- 189Uploaded bysommukh
- sbanerjee_07.pdfUploaded bysommukh
- 3216811Uploaded bysommukh
- 3216811Uploaded bysommukh
- SelectionUploaded byHector Garcia
- art-3A10.1007-2FBF00162520Uploaded bysommukh
- emetwp87Uploaded bysommukh
- Consumer Learning From Online ReviewsUploaded bysommukh
- gibbs regime 2Uploaded bysommukh
- DocumentUploaded bysommukh
- mcmcUploaded bysommukh
- nzsm,Uploaded bysommukh
- p297Uploaded bysommukh
- xtsurHelpUploaded bysommukh

- Database Design With UML and SQLUploaded byRomi Dellarose
- Presentation on Visual AidsUploaded byAbhishekGupta
- 25 HydraulicsAndFloodplainModeling-HECRASAnalysisUploaded byMacari Adrian
- Regression Analysis in R languageUploaded byKamakshaiah Musunuru
- glspec14Uploaded byapi-3714151
- 8.hypothesisUploaded byAkwasi Adjei Marfo
- Verification and Validation of Simulation ModelsUploaded byTosin Amuda
- Data Ware Housing ConceptsUploaded bysrivardan
- Lab 05 ManualUploaded bykarrar
- Lec431_6Uploaded bytrevin
- Relational AlgebraUploaded byPraja Saptaak
- CRAN - Package SpatstatUploaded byWalid Mohanna
- Statistical Inference ExercisesUploaded byognobogvoshno
- Others 3Uploaded byKahHoeLim
- Softuri CADUploaded byBăra Sebastian
- StarUMLUploaded byKwadwo Boateng
- SQL BasicsUploaded byVipin Tiwari
- Uml Lab ManualUploaded byAnil Kumar
- Real World WorkflowsUploaded bynervagus
- gravity10_thus3Uploaded byMohd Azruan Abdullah
- Database Management SYSTemsUploaded byamina
- macro 1 - bootstrapUploaded byapi-297722278
- Data Modeling 1Uploaded byBishnupriya Panda
- Bim for Interior Design Jan07 1Uploaded bynaassomv7975
- Entity Relationship ModelUploaded byabhay1990
- Building Information Modeling (BIM) Trends,BenUploaded byWeishuang Xie
- NormalizationUploaded byvijayantony81
- Lin Reg AssignmentUploaded byYusef Vazirabad
- E2_LinearModelUploaded byRocio Gill
- Computer Graphics File (3rd Semester)Uploaded byKetan Rajpal