Friday, September 14, 2007

Extract value of a R-bugs object that has a special charater as the variable name

If you have "()" as the variable name in a bugs object that is created by R2WinBUGS related function, you might find that a standard way of extracting the value of such variable might not work.

# example
library (arm)
fm1 <- lmer(Reaction ~ Days + (DaysSubject), sleepstudy)
fm1.sim <- mcsamp(fm1)
attach.bugs(fm1.sim)

You will find that you can get the simulated beta for Days by typing Days in R console (no quotation!!). But you cannot get (Intercept) by typing (Intercept) in R console. This is because R does not allow special character like "(" or "_" as a the variable name.

So in order to get (Intercept), we have to use "`".

`(Intercept)`

2 comments:

Hadley Wickham said...

You probably shouldn't refer to this as slot access, as slots have a specific meaning with respect to S4.

Note that you can also access the columns using character subsetting:

fm1.sim[, "(Intercept)"]
fm1.sim[ "(Intercept)"]]

Yu-Sung Su said...

Thanks!

I think I meant variable names. So in this case, since fm1.sim is an bugs object, the standard trick of using "" won't work.