markmfredrickson/RItools — Issue #8: Funny scoping error
Author: jwbowers
Created: 2011-10-11 18:34:32+00:00
State: open

Here is a funny scoping error. Why would adding a new column to a data frame (and evening changing its name) cause this scoping error?

```
> data(nuclearplants)
> xb1<- xBalance(pr~ date + t1 + t2 + cap + ne + ct + bw + cum.n,
+ strata=list(pt=~pt),
+ data=nuclearplants,
+ report=c("chisquare.test"))
> print(xb1)
---Overall Test---
chisquare df p.value
pt 10.8 8 0.215
---
Signif. codes: 0 '***' 0.001 '** ' 0.01 '* ' 0.05 '. ' 0.1 ' ' 1
> ## A very lazy way to get the enumerated distribution of the overall chisq test from xBalance
> library(MASS) ##for the generalized inverse function b/d I'm lazy
> Omega <- with(nuclearplants, produceRandomizations(pr, pt, 1000) )
> Omega <- as.data.frame(apply(Omega, 2, function(idx) {
+ x <- numeric(nrow(nuclearplants))
+ x[idx] <- 1
+ return(x)
+ }))
> xBalance(pr~ date + t1 + t2 + cap + ne + ct + bw + cum.n, data=nuclearplants,strata=list(pt=~pt),report="adj.mean.diffs")
strata pt
stat adj.diff
vars
date 0.0979
t1 0.9535
t2 9.3721 *
cap 66.5698
ne -0.0233
ct -0.1105
bw -0.0465
cum.n -0.8198
> junk<-nuclearplants[,c("date","t1","t2","cap","ne","ct","bw","cum.n")]
> junk$pr<-Omega[,1]
> xBalance(pr~ date + t1 + t2 + cap + ne + ct + bw + cum.n, data=junk,strata=list(pt=~pt),report="adj.mean.diffs")
Error in unique.default(x) : unique() applies only to vectors
> xBalance(pr~ date + t1 + t2 + cap + ne + ct + bw + cum.n,
+ data=data.frame(nuclearplants[,c("date","t1","t2","cap","ne","ct","bw","cum.n")],
+ pr=Omega[,10]),
+ strata=list(pt=~pt),report="adj.mean.diffs")
Error in unique.default(x) : unique() applies only to vectors
>
```