diff --git a/DESCRIPTION b/DESCRIPTION index 75b860d..53dbbe4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,4 +25,4 @@ Imports: parglm Suggests: ggplot2, did Encoding: UTF-8 -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/R/fastdid.R b/R/fastdid.R index ed43ab4..6645045 100644 --- a/R/fastdid.R +++ b/R/fastdid.R @@ -225,6 +225,7 @@ coerce_dt <- function(dt, p){ return(list(dt = dt, time_change = list(time_step = time_step, max_time = max(time_periods), + last_treated_cohort = ifelse(p$control_option == "notyet", dt[!is.infinite(G),max(G)], dt[,max(G)]), time_offset = time_offset))) } @@ -352,8 +353,10 @@ convert_targets <- function(results, result_type, t){ } else if (result_type == "group_time"){ - results[, cohort := floor((target-1)/t$max_time)] - results[, time := (target-cohort*t$max_time)] + max_avail_time <- min(t$max_time, t$last_treated_cohort-1) + + results[, cohort := floor((target-1)/max_avail_time)] + results[, time := (target-cohort*max_avail_time)] #recover the time results[, cohort := recover_time(cohort, t$time_offset, t$time_step)] diff --git a/R/plot_event_dynamics.R b/R/plot_event_dynamics.R index b338b8b..9fb984d 100644 --- a/R/plot_event_dynamics.R +++ b/R/plot_event_dynamics.R @@ -12,7 +12,7 @@ #' @export plot_did_dynamics <-function(dt, - graphname = "event study plot", note = "", base_time = -1, significance_level = 0.05#, + graphname = "event study plot", note = "", significance_level = 0.05#, #stratify_offset =0.1 ){ @@ -22,7 +22,12 @@ plot_did_dynamics <-function(dt, return(NULL) } - + + #find the base_period + et_range <- min(dt[, event_time]):max(dt[, event_time]) + base_time <- et_range[!et_range %in% dt[, unique(event_time)]] + if(length(base_time)!=1){stop("missing more then one period")} + #add the base period if("outcome" %in% names(dt)){ base_row <- data.table(att = 0, se = 0, event_time = base_time, outcome = dt[, unique(outcome)]) diff --git a/README.md b/README.md index 1f003cd..29ddf72 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ Since **fastdid** is not on CRAN yet, it needs to be converted to R scripts to b - add filtervar 0.9.3.1 (2024/5/24): fix the bug with `univar == clustervar` (TODO: address problems with name-changing and collision) +0.9.3.2 (2024/7/17): fix group_time result when using `control_type = "notyet"` and make the base period in plots adapt to anticipation. ## 0.9.2 (2023/12/20) diff --git a/development/playground.R b/development/playground.R index 88ebfb5..33cfa5a 100644 --- a/development/playground.R +++ b/development/playground.R @@ -1,4 +1,4 @@ -setwd("~/GitHub/fastdid") +setwd("~/Documents/GitHub/fastdid") library(devtools) library(tinytest) diff --git a/inst/tinytest/test_1_fastdid.R b/inst/tinytest/test_1_fastdid.R index 207f497..c369081 100644 --- a/inst/tinytest/test_1_fastdid.R +++ b/inst/tinytest/test_1_fastdid.R @@ -77,6 +77,11 @@ expect_silent(fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit",ou base_period = "varying"), info = "baseperiod vary") +#plot +expect_silent(plot_did_dynamics(fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit",outcomevar = "y", result_type = "dynamic")), + info = "simple plot") + + # filtervar ------------------------------------------ dt2 <- copy(dt) diff --git a/inst/tinytest/test_2_compare_gt.R b/inst/tinytest/test_2_compare_gt.R index 479a8b2..fa18ec6 100644 --- a/inst/tinytest/test_2_compare_gt.R +++ b/inst/tinytest/test_2_compare_gt.R @@ -30,6 +30,19 @@ expect_equal(est_diff_ratio(result, did_result), c(0,0), tolerance = tol, info = "simple") rm(result, did_result) +# simple notyet only ---------------------- + +result <- fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit",outcomevar = "y", result_type = "group_time", control_option = "notyet") +did_result <- did::att_gt(yname = "y",gname = "G",idname = "unit",tname = "time",data = dt[!is.infinite(G)],base_period = "universal",est_method = "ipw",cband = FALSE, + #xformla = ~x, + control_group = "notyettreated", + clustervars = NULL, + bstrap = FALSE) + +expect_equal(est_diff_ratio(result, did_result), c(0,0), tolerance = tol, + info = "notyet") +rm(result, did_result) + # never ------------------- result <- fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit",outcomevar = "y", result_type = "group_time", diff --git a/man/plot_did_dynamics.Rd b/man/plot_did_dynamics.Rd index 7619904..77b0597 100644 --- a/man/plot_did_dynamics.Rd +++ b/man/plot_did_dynamics.Rd @@ -8,7 +8,6 @@ plot_did_dynamics( dt, graphname = "event study plot", note = "", - base_time = -1, significance_level = 0.05 ) } @@ -19,9 +18,9 @@ plot_did_dynamics( \item{note}{A character string for adding additional notes or comments to the plot (default is empty).} -\item{base_time}{The time point representing the base period (default is -1).} - \item{significance_level}{The significance level for confidence intervals (default is 0.05).} + +\item{base_time}{The time point representing the base period (default is -1).} } \value{ A ggplot2 object representing the event study plot.