Exporting data from R into WinBUGS’s “R/S-plus list format”

Users of the world’s favourite Bayesian analysis software WinBUGS will recognise the strange format in which data is supplied inside the GUI, generally in a text file looking something like this:

list(x=c(1,2,3,4,5,6),y=c(4,8,2,6,9,9))

This is of course an R (or strictly speaking, S) language format, but it’s not how R views data, and although you can also supply the data in a fixed column width format, I have occasionally wondered how to export data from a bigger general analysis and data management package like R into that weird list format. If you start to think about it, you soon realise it isn’t simple. It hasn’t been an issue because you can just run WinBUGS from inside R, but this week I found I had to do it. I won’t bore you with details.

I found this webpage from Iowa State University which gives an R function called writeDatafileR. This works very nicely, producing a text file that you can then open in WinBUGS’s own GUI, and I am surprised it isn’t better known or on CRAN. It is apparently written by Terry Elrod but though I searched for Terry on Google and at Iowa State I couldn’t find out any more about him or her. So here’s a Pioneer-10-esque “thank you” into cyberspace that may one day find Terry.

Advertisements

11 Comments

Filed under R

11 responses to “Exporting data from R into WinBUGS’s “R/S-plus list format”

  1. Michael

    Nice! Nowdays pleople are trying to use JAGS and Stan with R rather than WinBUGS as they could be implemented in any platform!

    • Yes I agree – and I must admit I am a bit old fashioned in my BUGS use. I still write and perfect the model in the WinBUGS interface. Then if I have lots to run I’ll do it through R2WinBUGS. I really could run everything including the diagnostics inside R. But I find the tips from BUGS (missing node, unused variable, that sort of thing) about where to find errors in the code quite useful. Do you know – is there any R package that brings those error messages back from BUGS? And you’re right, I think JAGS is more streamlined. When Stan is expanded a bit more it is going to be awesome.

  2. kerrychan

    Great! Thanks for sharing!

  3. Hi Robert. Your note of thanks did indeed finally reach me. It did take 16 months — how far into outer space does that place me?
    Glad the function continues to be of use. And thank you for your marvelous blog.
    Terry

  4. A

    What about bugs.data function in R@OpenBugs, I guess it does what you want!
    Cheers

  5. Eimear Cleary

    Thank you for this function! I’m new to R (and winbugs) and am getting this error message when I run the code:
    Error in cat(formatDataR(DATA), file = towhere, fill = fill) :
    object ‘towhere’ not found
    > formatDataR(DATA)
    Error in formatDataR(DATA) : object ‘DATA’ not found
    > invisible(0)
    > }
    Error: unexpected ‘}’ in “}”
    Can anyone help me out with this?
    Thank you!
    Eimear

    • I wonder what you are entering as the command – it hints at there not being the right arguments (objects named in the brackets after the command). But more generally, I would really recommend using Stan instead of BUGS now. The rstan package can just be installed from cran.

  6. Josh

    Unfortunately I’m getting a message when I save this that says writeDatafile.txt is not available for R version 3.1.1 (or later versions). Is there any way around this?

    • I think it just needs to be brought up to date as a package or at least a .R file with a function in it. I actually rewrote the same thing as part of StataStan (not in R but the logic is there), and it exists in rstan as a dump function, so I know it’s not hard to do, as long as you are prepared to restrict the types of object that can get sent. writeDatafile is more all-encompassing.

      As a work around, what if you copy and paste the contents into R[studio] and run them? Is it happy with that?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s