insongkim / PanelMatch

111 stars 34 forks source link

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

Closed rssteel closed 1 year ago

rssteel commented 1 year ago

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.

Similarly, 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.

I'm attaching the data. 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)

faads_women_09.dta.zip

hjdruc commented 1 year ago

The source code may check all columns, so load only those columns what you actual need. For example:

base.cols <- c(time.id,unit.id, treatment) concern.cols <- c(outcome.var, x.var) check.cols <-c(base.cols,concern.cols)

data <- data[,check.cols]

The error may come from your ['member'] or ['outlay~i '] column.

adamrauh commented 1 year ago

Hi @rssteel thanks for using the package! And sorry for the delay in getting back to you. I think I have figured out the problem.

The read_dta function is loading the data as a tibble -- the package relies on data frames as input and we currently are not throwing a very helpful error. For what it's worth, the errors are basically being thrown because the package is expecting a vector after subsetting a column of the data to check the integer/consecutive requirements, but its getting a tibble. So, the errors are being thrown, but the message isn't helpful.

I was able to get the code to run just by adding house_ab <- as.data.frame(read_dta("faads_women_09.dta"))

We'll improve the error checking in a future update! thanks for finding this. Let us know if you have other questions.