Archive for October, 2009

h1

How-to: VIM, GNU R, and LaTeX

October 24, 2009

Recently I switched from Emacs to VIM. Mostly because I’ve felt that the default keybindings are more sane. The first thing I needed to do was to find a plugin for VIM that worked for R. Well here is that plugin. The follow the directions there for installation. (If you need help leave me a comment). VIM will automatically detect that it is in R mode when opening a *.R file.

Two quick tips to get you started:

# To open R
\rf
# To send code to R
\d

Also, if you need a LaTeX plugin for VIM here it is. It also has very sane keybindings. I recommend not using the Debian package as it didn’t work for me. To get LaTeX to compile in PDF mode by default I added the following to ~/.vimrc:


" Set pdf as default target
let g:Tex_DefaultTargetFormat='pdf'

That’s it. And in complete disclosure, I am really using the GTK-front end of VIM known as GVIM.

h1

SEM in WinBUGS

October 24, 2009

For my MITER research, I have been working with a faculty member in the University of Minnesota’s Institute of Child Development. I have been involved on a couple of projects with this group. One project involved longitudinal modeling of work competency (from childhood to adulthood) using structural equation modeling (SEM). This project examined how childhood competence in externalizing behaviors/conduct, academics, and socializing behaviors predict work as an adult. If you’re interested in this work, it has just been accepted and should be in press soon. (Feel free to email me for a copy of this manuscript and I’ll see if I can get a copy).

To do the SEM, we initially tried the sem package in R. This proved to be to basic for us as it was unable to do a multi-group analysis. So we settled on MPlus. Even in MPlus, we were unable to run the models we really wanted to because the number of integration points were too many and MPlus would just error out. We dealt with it the best we could and I think our manuscript is strong and transparent about our computational problems.

After this paper was submitted, I become more interested in examining what sort of alternatives exist. Is there a Bayesian book on SEM? Well apparently there is. In the Bayesian framework, latent constructs are just dealt with as missing data problems, making them more computationally solvable. Additionally, using Bayesian you work directly from your observed data and not from a covariance matrix. Because of this you can create more highly complex, developmentally appropriate models that are infeasible in traditional software. Even Bollen is writing about this.

To help you get started. I am going to paste the code that I have used to replicate our models in WinBUGS. Unfortunately I can not share the data. But if you have specific data you can share then I’d be happy to modify the code as appropriate.


model {
for(i in 1:N){
##########################
# measurement equation model
##########################
for(j in 1:P){
y[i,j]~dnorm(mu[i,j],psi[j])
}
##########################
# alp[i] corresponds to the intercepts
##########################
mu[i,1]<-xi[i,1]+alp[1] ## Ext1
mu[i,2]<-lam[1]*xi[i,1]+alp[2]
mu[i,3]<-lam[2]*xi[i,1]+alp[3]
mu[i,4]<-xi[i,2]+alp[4] ## Soc1
mu[i,5]<-lam[3]*xi[i,2]+alp[5]
mu[i,6]<-lam[4]*xi[i,2]+alp[6]
mu[i,7]<-xi[i,3]+alp[7] ## Aca1
mu[i,8]<-lam[5]*xi[i,3]+alp[8]
mu[i,9]<-lam[6]*xi[i,3]+alp[9]
mu[i,10]<-xi[i,4]+alp[10] ## Ext2
mu[i,11]<-lam[7]*xi[i,4]+alp[11]
mu[i,12]<-lam[8]*xi[i,4]+alp[12]
mu[i,13]<-xi[i,5]+alp[13] ## Soc2
mu[i,14]<-lam[9]*xi[i,5]+alp[14]
mu[i,15]<-lam[10]*xi[i,5]+alp[15]
mu[i,16]<-xi[i,6]+alp[16] ## Aca2
mu[i,17]<-lam[11]*xi[i,6]+alp[17]
mu[i,18]<-lam[12]*xi[i,6]+alp[18]
mu[i,19]<-xi[i,7]+alp[19] ## Work2
mu[i,20]<-lam[13]*xi[i,7]+alp[20]
mu[i,21]<-lam[14]*xi[i,7]+alp[21]
##########################
# structural equation model
##########################
xi[i,1:3]~dmnorm(u[1:3],phi[1:3,1:3]) ## factor loadings are multivariate normal
xi[i,4]~dnorm(ext2[i],psd)
xi[i,5]~dnorm(soc2[i],psd)
xi[i,6]~dnorm(aca2[i],psd)
xi[i,7]~dnorm(work2[i],psd)
##########################
# continuity Paths
##########################
ext2[i]<-gam[1]*xi[i,1]
soc2[i]<-gam[2]*xi[i,2]
aca2[i]<-gam[3]*xi[i,3]
work2[i]<-gam[4]*xi[i,1] + gam[5]*xi[i,1] + gam[6]*xi[i,1]
} #end of i
##########################
# priors on intercepts
##########################
for(j in 1:21){alp[j]~dnorm(0.0, 1.0)}
##########################
# priors on loadings and coefficients
##########################
lam[1]~dnorm(0.8,psi[1])
lam[2]~dnorm(0.8,psi[2])
lam[3]~dnorm(0.8,psi[3])
lam[4]~dnorm(0.8,psi[4])
lam[5]~dnorm(0.8,psi[5])
lam[6]~dnorm(0.8,psi[6])
lam[7]~dnorm(0.8,psi[7])
lam[8]~dnorm(0.8,psi[8])
lam[9]~dnorm(0.8,psi[9])
lam[10]~dnorm(0.8,psi[10])
lam[11]~dnorm(0.8,psi[11])
lam[12]~dnorm(0.8,psi[12])
lam[13]~dnorm(0.8,psi[13])
lam[14]~dnorm(0.8,psi[14])
for(j in 1:6){gam[j]~dnorm(0.5, psd)}
##########################
#priors on precisions
##########################
for(j in 1:P){
psi[j]~dgamma(9.0, 4.0)
sgm[j]<-1/psi[j]
}
psd~dgamma(9.0, 4.0)
sgd<-1/psd
phi[1:3,1:3]~dwish(R[1:3,1:3], 5)
phx[1:3,1:3]<-inverse(phi[1:3,1:3])
} #end of model

I’d be happy to answer any questions about the code. Note this is just the model, not the starting values. I usually let WinBUGS generate them for me. Bad practice I know, but as long as my chains mix I am not worried.

h1

WinBUGS and OpenBUGS on Debian GNU/Linux Lenny

October 24, 2009

A quick how-to for installing WinBUGS and OpenBUGS on Debian GNU/Linux Lenny. I am sure this will work on other distributions. The only issue might be that certain distributions ship different versions of wine . I am use wine 1.0.1, with other versions of wine YMMV.

First, install wine

su -
aptitude install wine

Second, go here and download the WinBUGS and OpenBUGS binaries. It is important to note that OpenBUGS is the development version of WinBUGS and it’s open-source.

I also encourage you to check out JAGS. I have created a 64-bit package of JAGS that is available here for Debian. If you’re a R user and do a lot of multilevel modeling, I recommend MCMCglmm .

Third, assuming you’ve downloaded these files to ~/Downloads, for WinBUGS do the following:

unzip ~/Downloads/winbugs14.zip

And for OpenBUGS do the following:


mkdir -p ~/Downloads/openbugs
mv ~/Downloads/OpenBUGS.zip ~/Downloads/openbugs
unzip ~/Downloads/openbugs/OpenBUGS.zip

Now everything is ready to go. To launch WinBUGS:

wine ~/Downloads/WinBUGS14/WinBUGS14.exe

To launch OpenBUGS:

wine ~/Downloads/openbugs/winbugs.exe

Note the first line of code is for WinBUGS and the second line is for OpenBUGS.

Don’t forget to add the immortality key and the

h1

Bayesian Guide for Education

October 24, 2009

I have been working on a gentle, very applied introduction to Bayesian aimed at educational researchers. I’ve uploaded the guide. Feel free to download it and if you do please provide feedback.

Click here for the Bayesian guide