Skip to content

Exploring reference class objects in R

January 16, 2013

Most R users know about S3 methods. Many have been frustrated when they find out that the package that they’re learning uses S4 methods (although see here to help reduce frustration). Now there’s R5 methods, which, if you ever run into them, can be very frustrating if you’re used to using S3 or S4. I’d try to avoid R5 if you can, unless you’re a very smart developer or you are trying to figure out how a package works, which is what I’m doing with the development version of lme4 (note that this is at present different from the version of lme4 on cran.

This post is about making life less frustrating for S3 people wanting to understand how an R5 package works. Note that the little tip that follows will not completely eliminate the frustration.

Before the tip, first the main frustrating thing about R5. With S3 and S4, we are used to passing arguments to functions and then receiving a return value. For example,

x <- 1:4
y <- sum(x)

In this example, we pass the value stored in x to the sum function and store the result in y. This is sometimes called a ‘pass by value’ paradigm.

But with R5 methods its more complicated. This system uses a ‘pass by reference’ paradigm. I’m not going to explain it here, mostly because I don’t completely understand it. But the dangerous thing with R5 is that when an R5 object is operated on, the effects of the operation can cause changes in both the return value and ‘other things’ that are difficult to keep track of. So if you don’t really know what you’re doing, it is very easy to do things that you don’t want to do when operating on an R5 object.

But what if you want to understand what an R5 package is doing anyways? I’ve learned that its very important to understand what statistical software is ‘doing’ to the data, especially if you are in a position where you are recommending the software to others. Here’s a good tip to keep in mind when trying to figure out analyses in R5. Suppose that x is R5 (e.g. a model fit object). R5 objects can be ‘converted’ to a standard R list with the following command:

x_as_list <- x$copy()

You can now explore this new object as you would explore any R list.

No comments yet

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: