synth-inference / synthdid

Synthetic difference in differences
https://synth-inference.github.io/synthdid
BSD 3-Clause "New" or "Revised" License
261 stars 97 forks source link

panel.matrices throws error if panel argument is a tidyverse tibble #75

Open skranz opened 2 years ago

skranz commented 2 years ago

Just a short notice. The panel.matrices function seems to throw an error if the passed panel object is a tibble (the tidyverse equivalent of a data frame). Here is a simple example with resulting error message:

data('california_prop99')
library(dplyr)
setup = panel.matrices(as_tibble(california_prop99))

Error in panel.matrices(as_tibble(california_prop99)) : 
  There is no variation in treatment status.

Since tidyverse is very popular and certain dplyr operations tend to convert data frames into tibbles, this error may be encountered by some users. A simple fix would probably be to start the panel.matrices function with a call panel = as.data.frame(panel). At least the following code works fine:

data('california_prop99')
library(dplyr)
setup = panel.matrices(as.data.frame(as_tibble(california_prop99)))
yang-yuchuan commented 2 years ago

THANKS A LOT!!! Your comment really helps me!

MatthieuStigler commented 1 year ago

The problem comes from unique(panel[, treatment]), which has a different implicit behavior with respect to the implicit drop argument

This is why length(unique(panel[, treatment])) returns a false positive warning, compare:

df <- data.frame(treatment =c(0,1))
tib <- tibble::tibble(treatment =c(0,1))

length(unique(df[, "treatment"]))
#> [1] 2
length(unique(tib[, "treatment"]))
#> [1] 1

Created on 2022-09-12 with reprex v2.0.2

siminl commented 3 months ago

Thank you so much! This is so helpful!!