Reputation: 183
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)
Upvotes: 1
Views: 848
Reputation: 43
Beyond coercing the data as a dataframe, you also need to use the complete
function to complete the statdistcons3
and year
combinations. Below is the code:
house_ab <- house_ab %>%
complete(statdistcons3, year) %>% as.data.frame()
Upvotes: 1
Reputation: 1
My best guess is that the issue lies with the Stata version of the exported data. In your case, that would be 'faads_women_09.dta'. I encountered the same problem yesterday.
My solution to this problem was, firstly, to change the version to Version (12) in Stata, then load it in R. Secondly, I changed the unit id and time id to factors. With these changes, I was able to successfully generate the PanelMatch and DisplayTreatment commands. I hope this helps a bit.
Upvotes: 0
Reputation: 26
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)
Upvotes: 1