Skip to content

Commit

Permalink
0.9.3.2: fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
TsaiLintung committed Jul 17, 2024
1 parent d105ddf commit 25d234d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Imports:
parglm
Suggests: ggplot2, did
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
7 changes: 5 additions & 2 deletions R/fastdid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

}
Expand Down Expand Up @@ -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)]
Expand Down
9 changes: 7 additions & 2 deletions R/plot_event_dynamics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
){

Expand All @@ -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)])
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion development/playground.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
setwd("~/GitHub/fastdid")
setwd("~/Documents/GitHub/fastdid")

library(devtools)
library(tinytest)
Expand Down
5 changes: 5 additions & 0 deletions inst/tinytest/test_1_fastdid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions inst/tinytest/test_2_compare_gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions man/plot_did_dynamics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25d234d

Please sign in to comment.