R‎ > ‎

R

This blog is mostly notes to myself about using R for problems in computational social science. Because most of my work is done in a Bayesian framework, I also include tips and tricks for working with JAGS. I used to use WinBUGS and OpenBUGS, but JAGS is a much better option. It is cross-platform and includes some extra distributions (e.g. the t-distribution) not included in Win/OpenBUGS.

Functions, Objects and Dataframes

posted Mar 24, 2014, 7:16 AM by David Eagle

When working with data frames in R, there can be some unexpected behavior when calling functions. It is not always clear if/what data frame R will reference when using a function. Also, it would be nice to write functions that do not need to be referenced with a specific data frame, but have one passed to them. 

Enter the command match.call(), a function that according to the ever-so-helpful R help "returns a call in which all of the specified arguments are specified by their full names." Well, whatever the case, this function can be used to attach an object/dataframe that to call another function.

For example (thanks stack overflow!):

#Makes a generic function attach to a specific data frame to another function

x=2

d=data.frame(NA)

d$x=10

#create generic function:

fn <- function(dat, expr) {

  mf <- match.call() # makes expr an expression that can be evaluated

  eval(mf$expr, envir = dat)

}

#see it in action

P <- function(x) x+1

fn(d,P(x)) #returns 11

P(x) #returns 3

Running WinBUGS on MacOSX via Wine

posted Nov 12, 2010, 1:25 AM by David Eagle   [ updated Oct 17, 2013, 7:50 AM ]

WinBUGS is a program that allows for relatively efficient methods of estimating Bayesian models using Gibbs and other related sampling techniques. However, it does not run natively on MacOSX. OpenBUGS, the open source alternative, does not have all of the same feature set as WinBUGS. 

What is a Mac Owner to do? Run WinBUGS under Wine (an open-source Windows emulator). While WinBUGS can be run as a stand-alone package, it is most efficient to call from R. Here's how:

A. INSTALL XQuartz (the open source X-Windows System needed to run Windows Programs)
1. Go to the X-Quartz website and download and install their latest version of XQuartz http://xquartz.macosforge.org/.
2. Log in/Log out of your computer to activate the new X11 environment.
3. Go to step B

B. Install MacPorts, Freetype, and Wine
1. Go to www.macports.org and install the macports software. This enables you to install open source linux programs easily, without self-compiling. THIS REQUIRES XCODE to work, so get it from apple.com if you haven't installed Xcode.
2. Once MacPorts is installed, open an X window terminal and type the following. This will install freetype, a good font renderer for Xserver.

sudo port install freetype

3. Symbolically link the freetype directory to /usr/lib to allow X windows to find it (the period in the last line is needed).

cd /usr/lib
sudo ln -s /opt/local/lib/libfree* .

4. Install the latest version of Wine by issuing the command. This will create a <USERNAME>/.wine directory, which will become the default directory in which Wine is installed. Detailed Wine installation instructions here.

sudo port install wine-devel

You may also want to type this command to save disk space:

sudo port clean all

6. Make sure wine is installed. Open an XQuartz terminal window and type:

wine --version
 
6. Download WinBUGS14.exe from here. Copy the entire directory to ~/.wine/drive_c/Program Files.
7. Verify that WinBUGS is running by opening a XQuartz terminal window and typing:

wine "C:\Program Files\WinBUGS14\WinBUGS14.exe"

C. UPDATE WinBUGS
1. Download the current patch from: http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/contents.shtml. These should be .txt files.
2. Open WinBUGS, click File Open, choose ".txt" for type of file and find the patch key and open it.
3. Go to Tools > Decode, then click Decode All.
4. Quit WinBUGS, reopen WinBUGS.
5. Download the immortality key from the link in 1. Again, just like in steps 2-4, decode this patch and restart WinBUGS

D. RUNNING WINBUGS FROM R
1. First, if you don't have a ~/.Renviron file create it by:
2. Open a text editor and enter the following into it:
WINE="/opt/local/bin/wine"
WINEPATH="/opt/local/bin/winepath"
BUGS.DIR="/Users/<USERNAME>/.wine/drive_c/Program Files/WinBUGS14/"
3. Save this file as ~/.Renviron
4. Before running a WinBUGS program through R, start XQuartz.app 
5. Run a sample file to test if it works:

# An example model file is given in:
library(R2WinBUGS)
model.file <- system.file(package = "R2WinBUGS", "model", "schools.txt")
# Let's take a look:
file.show(model.file)
# Some example data (see ?schools for details):
data(schools)
schools
J <- nrow(schools)
y <- schools$estimate
sigma.y <- schools$sd
data <- list ("J", "y", "sigma.y")
inits <- function(){
    list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0, 100),
         sigma.theta = runif(1, 0, 100))
}
parameters <- c("theta", "mu.theta", "sigma.theta")
## Not run: 
## both write access in the working directory and package BRugs required:
schools.sim <- bugs(data, inits, parameters, model.file, n.chains=3, n.iter=1000,n.thin=10,n.burnin=10,bin=1,DIC=F,debug=F)
print(schools.sim)
plot(schools.sim)

1-2 of 2