Re: [R] importing data from BUGS format to R?

From: Martyn Plummer <plummer_at_iarc.fr>
Date: Fri 24 Feb 2006 - 20:27:21 EST

On Fri, 2006-02-24 at 08:48 +0100, Uwe Ligges wrote:
> Gabor Grothendieck wrote:
> > Just source the file:
> >
> > source("mywinbugsfile.R")
> > head(y)
>
>
> ... and don't forget to transpose the matrix afterwards, if this was
> BUGS code.
>
> Uwe Ligges

If this were a WinBUGS data file (or initial values file), the whole expression would be wrapped in a call to list(). In this case, you would need to assign the output from source(), or use the dget() function, e.g. for the EPILEPSY example:

> mydata <- dget("epildata.txt")
> names(mydata)

[1] "N" "T" "y" "Trt" "Base" "Age" "V4"
> mydata$T

[1] 4

But, as Uwe points out, arrays will need to be reorganized, as WinBUGS fills matrices by row rather than column.

> head(mydata$y) #wrong!

     [,1] [,2] [,3] [,4]

[1,]    5    9    9    7
[2,]    3   11    4    6
[3,]    3    0    0    7
[4,]    3    0    4    1
[5,]    3    5    3    1
[6,]    5    0    0    2

I wrote a function called "bugs2jags", which you will find in the coda package, for converting WinBUGS data files into the data format used by JAGS which is, by no coincidence, the format used by the R function dump().

> library(coda)

Loading required package: lattice
> bugs2jags("epildata.txt","epildata.R") #Creates file "epildata.R"
> source("epildata.R")
> ls()

[1] "Age" "Base" "N" "T" "Trt" "V4" "y"

This creates objects in your work space, and sorts out the arrays

> head(y) #right

     [,1] [,2] [,3] [,4]

[1,]    5    3    3    3
[2,]    3    5    3    3
[3,]    2    4    0    5
[4,]    4    4    1    4
[5,]    7   18    9   21
[6,]    5    2    8    7

Martyn

>
> >
> > On 2/23/06, Jeffrey Moore <jemoore@duke.edu> wrote:
> >
> >>For those who use WinBUGS (or for those who are just familar with this
> >>format), I have a text file that looks like this (which is how R would
> >>export data if you used the "structure" function):
> >>
> >>y= structure(.Data= c(-6.93310E+01, 4.32870E+01, -6.96600E+01, 4.35730E+01,
> >>-6.90150E+01, 4.35870E+01, -5.81060E+01, 4.52890E+01, -6.65590E+01,
> >>4.34600E+01, -6.61850E+01, 4.35000E+01, -6.54130E+01, 4.31940E+01,
> >>-6.42790E+01, 4.34780E+01, -6.35520E+01, 4.38070E+01, -6.32980E+01,
> >>4.39520E+01, -6.25690E+01, 4.41760E+01, -6.20810E+01, 4.40800E+01,
> >>-6.14280E+01, 4.46210E+01, -6.10530E+01, 4.48050E+01, -6.00300E+01,
> >>4.50480E+01, -5.88110E+01, 4.50280E+01, -5.83660E+01, 4.50400E+01,
> >>-5.83140E+01, 4.48780E+01, -5.87330E+01, 4.50340E+01, -5.87430E+01,
> >>4.51630E+01, -5.88170E+01, 4.54030E+01, -5.89380E+01, 4.53260E+01,
> >>-5.89110E+01, 4.55260E+01, NA, NA, -5.91250E+01, 4.56070E+01,
> >>-5.90140E+01, 4.59690E+01, -5.89830E+01, 4.64640E+01, -5.89240E+01,
> >>4.66300E+01, -5.93770E+01, 4.66810E+01, -5.90010E+01, 4.65640E+01,
> >>-5.91230E+01, 4.67780E+01, -5.92350E+01, 4.70000E+01, -5.92310E+01,
> >>4.68350E+01, NA, NA, -5.88530E+01, 4.68560E+01, NA, NA,
> >>NA, NA, NA, NA, -5.83550E+01, 4.65300E+01, -5.83270E+01,
> >>4.64970E+01, -5.86210E+01, 4.65320E+01, NA, NA, -5.82720E+01,
> >>4.65060E+01, -5.81480E+01, 4.64490E+01, -5.83350E+01, 4.63650E+01, NA,
> >>NA, -5.84800E+01, 4.63340E+01, -5.83980E+01, 4.63040E+01, -5.83390E+01,
> >>4.62030E+01, -5.82170E+01, 4.62620E+01, NA, NA, -5.80420E+01,
> >>4.61940E+01, -5.80360E+01, 4.57280E+01, -5.80590E+01, 4.55420E+01,
> >>-5.83010E+01, 4.54730E+01, -5.83710E+01, 4.55010E+01, -5.86540E+01,
> >>4.52870E+01, -5.87020E+01, 4.51740E+01, -5.87400E+01, 4.52620E+01,
> >>-5.88330E+01, 4.53190E+01, -5.89740E+01, 4.53410E+01, NA, NA,
> >>-5.85240E+01, 4.54970E+01, -5.81710E+01, 4.56200E+01, -5.79070E+01,
> >>4.58370E+01, NA, NA, -5.73610E+01, 4.62660E+01, -5.71820E+01,
> >>4.60770E+01, -5.70540E+01, 4.59920E+01, NA, NA, NA, NA,
> >>-5.58460E+01, 4.51830E+01, -5.53690E+01, 4.52890E+01, -5.49260E+01,
> >>4.53340E+01, -5.40070E+01, 4.53670E+01, -5.35510E+01, 4.54510E+01, NA,
> >>NA, -5.15130E+01, 4.63060E+01, -5.15000E+01, 4.63280E+01, -5.08410E+01,
> >>4.67780E+01, -4.99400E+01, 4.69670E+01, -4.88440E+01, 4.72810E+01,
> >>-4.87250E+01, 4.76880E+01, -4.70460E+01, 4.87420E+01, -5.17870E+01,
> >>4.83990E+01, -4.68830E+01, 4.97030E+01, -4.73700E+01, 5.03350E+01,
> >>-4.75990E+01, 5.10690E+01, -5.15050E+01, 5.05110E+01, -4.80640E+01,
> >>5.19200E+01, -4.83890E+01, 5.27580E+01, NA, NA, -4.85200E+01,
> >>5.41250E+01, -4.87630E+01, 5.53650E+01, NA, NA, -4.84790E+01,
> >>5.70560E+01, -4.82690E+01, 5.77990E+01, -4.77870E+01, 5.87570E+01,
> >>-4.74070E+01, 5.96700E+01, -4.76990E+01, 6.02020E+01, -4.82110E+01,
> >>6.03410E+01, -4.90240E+01, 6.05510E+01, -4.89050E+01, 6.06780E+01,
> >>-4.80660E+01, 6.05380E+01, -4.61030E+01, 6.01290E+01, NA, NA,
> >>-4.59880E+01, 6.02070E+01, -4.55240E+01, 5.99680E+01, -4.59540E+01,
> >>5.97650E+01, -4.58830E+01, 5.98200E+01, -4.64730E+01, 6.00690E+01,
> >>-4.64660E+01, 6.00730E+01, -4.59630E+01, 5.99330E+01, -4.63940E+01,
> >>6.01380E+01, -4.64370E+01, 6.02270E+01, -4.67750E+01, 6.04410E+01,
> >>-4.68020E+01, 6.04700E+01, -4.57440E+01, 5.97720E+01, -4.48480E+01,
> >>5.96590E+01, -4.50540E+01, 5.97830E+01), .Dim=c(120, 2))
> >>
> >>I would like to read this into R as an 120x2 array (matrix or data frame).
> >>How would I do so?
> >>
> >>I know I can just copy/paste all the values bound by c( ), and then just do
> >>something like this:
> >>
> >>y <- matrix(c(pasted values), nrow=120, ncol = 2)
> >>
> >>But, I get errors messages when I try to copy/paste the whole dataset
> >>because I reach line limits in the R console, such that certain values get
> >>split, and then R doesn't know what to do with something like "0E+01, -".
> >>So, I can instead try to copy/paste in a piecemeal fashion (which I've done,
> >>and it works fine), but I'm sure there's a better way.
> >>
> >>Thanks
> >>Jeff
> >>
> >>******************************************
> >>Jeffrey Moore, Ph.D.
> >>Postdoctoral Research Scientist
> >>Duke Center for Marine Conservation
> >>Duke University Marine Laboratory
> >>135 Duke Marine Lab Road
> >>Beaufort, NC 28516
> >>Phone: (252) 504-7653
> >>Fax: (252) 504-7689
> >>Email: jemoore@duke.edu
> >>*****************************************
> >>
> >>
> >> [[alternative HTML version deleted]]
> >>
> >>______________________________________________
> >>R-help@stat.math.ethz.ch mailing list
> >>https://stat.ethz.ch/mailman/listinfo/r-help
> >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >>
> >
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



This message and its attachments are strictly confidential. ...{{dropped}}

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Feb 24 20:36:16 2006

This archive was generated by hypermail 2.1.8 : Sat 25 Feb 2006 - 00:08:47 EST