xuyiqing / tjbal

Trajectory Balancing
MIT License
11 stars 10 forks source link

Unit index must not be named "id" #12

Open owmork opened 2 months ago

owmork commented 2 months ago

Hi Yiqing,

I noticed the following behavior: when the unit-index is called "id", tjbal() throws an error. I think the root of the error is covar.tmp <- ddply(X.pre, .(unit = X.pre[, id]), numcolwise(mean), na.rm = TRUE)[,-1] in tjbal.default(). Please see the attached example.

BR and thanks for the package! Oliver

library(tjbal)
#> ## Syntax has changed since v.0.4.0.
#> ## See http://bit.ly/tjbal4r for more info.
#> ## Comments and suggestions -> yiqingxu@stanford.edu.
data(tjbal)

# Works:
# unit index other name than "id"
out2.mbal <- tjbal(
  data = germany,
  Y = "gdp",
  D = "treat",
  Y.match.time = c(1981:1982),
  X = c("trade"),
  X.avg.time = list(c(1981:1990)),
  index = c("country", "year"),
  demean = TRUE
)
#> Too few treated unit(s). Uncertainty estimates not provided.
#> Seek balance on:
#> gdp.dm1981, gdp.dm1982, trade 
#> 
#> Optimization:
#> bias.ratio = 0.7006; num.dims = 3 + 0 (mbal + kbal)
#> 
#> Balance Table
#>              mean.tr mean.co.pre mean.co.pst diff.pre diff.pst
#> gdp.dm1981 3548.5484   3020.6452   3548.5484   0.1488        0
#> gdp.dm1982 4194.5484   3664.8327   4194.5484   0.1263        0
#> trade        56.7778     59.8313     56.7778  -0.0538        0

# Does not work:
# unit index named "id"
germany$id <- germany$country

out2.mbal <- tjbal(
  data = germany,
  Y = "gdp",
  D = "treat",
  Y.match.time = c(1981:1982),
  X = c("trade"),
  X.avg.time = list(c(1981:1990)),
  index = c("id", "year"), # INDEX NAME CHANGED
  demean = TRUE
)
#> Too few treated unit(s). Uncertainty estimates not provided.
#> Error in `[.data.frame`(X.pre, , id): undefined columns selected

Created on 2024-07-04 with reprex v2.0.2