rpanel-data

PanelMatch R Errors: "please convert time id to consecutive integers" and "please convert unit id column to integer or numeric"


When using the PanelMatch package in R, I'm getting a couple errors. Any guidance on how to resolve them would be greatly appreciated.

First, when running the DisplayTreatment() function, I get the following error: "please convert time id to consecutive integers". I've converted the time id to integer class with as.integer(year), and I'm pretty sure they are consecutive---I've run unique(year) and can see that there are no gaps---but the error persists.

Second, when running the PanelMatch() function, I get this error: "please convert unit id column to integer or numeric". Again, I've tried converting the unit id to integer class, but the error persists.

The data can be downloaded here: faads_women_09.dta.zip. Here is the code necessary to replicate the issue:

house_ab <- read_dta("faads_women_09.dta")

house_ab$year <- as.integer(house_ab$year)
house_ab$statdistcons3 <- as.integer(house_ab$statdistcons3)

DisplayTreatment(unit.id = "statdistcons3", time.id = "year", legend.position = "none", xlab = "year", ylab = "CD", treatment = "female", data = house_ab)

PM.results.AB <- PanelMatch(lag = 4, time.id = "year", unit.id = "statdistcons3", treatment = "female",
                            refinement.method = "CBPS.match", data = house_ab, match.missing = TRUE, 
                            covs.formula = ~ party + unified_govt + terms + ln_statecapital + margin + ln_population + ln_age65 + ln_black + ln_constrct + ln_school + ln_farmer + ln_foreign + ln_manuf + ln_median_income + ln_unemployed + ln_miltpop + ln_urban,
                            size.match = 5, qoi = "att", outcome.var = "high_lnoutlays_cpi", forbid.treatment.reversal = FALSE)

Solution

  • I've been vexed by the same problem for quite a while but finally figured it out. I looked up the error logic in the PanelMatch code

      if(!"data.frame" %in% class(data)) stop("please convert data to data.frame class")
      
      if(!class(data[, unit.id]) %in% c("integer", "numeric")) stop("please convert unit id column to integer or numeric")
      if(class(data[, time.id]) != "integer") stop("please convert time id to consecutive integers")
    

    If we run class(house_ab[,"year"] we get:

    "tbl_df"    "tbl"    "data.frame"
    

    This means the data are stored as a tibble, rather than an ordinary (base) dataframe. Solution is to convert from a tibble to a base dataframe:

    house_ab <- as.data.frame(house_ab)
    DisplayTreatment(unit.id = "statdistcons3", time.id = "year", legend.position = "none", xlab = "year", ylab = "CD", treatment = "female", data = house_ab)
    
    PM.results.AB <- PanelMatch(lag = 4, time.id = "year", unit.id = "statdistcons3", treatment = "female",
                                refinement.method = "CBPS.match", data = house_ab, match.missing = TRUE, 
                                covs.formula = ~ party + unified_govt + terms + ln_statecapital + margin + ln_population + ln_age65 + ln_black + ln_constrct + ln_school + ln_farmer + ln_foreign + ln_manuf + ln_median_income + ln_unemployed + ln_miltpop + ln_urban,
                                size.match = 5, qoi = "att", outcome.var = "high_lnoutlays_cpi", forbid.treatment.reversal = FALSE)