Skip to content

Commit

Permalink
fix an odd bug caused by floating point
Browse files Browse the repository at this point in the history
  • Loading branch information
TsaiLintung committed Aug 5, 2024
1 parent cc126fa commit e289c9d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: fastdid
Type: Package
Title: lightning-fast staggered Difference-in-Difference estimators
Version: 0.9.2
Date: 2023-12-20
Version: 0.9.4
Date: 2024-08-05
Authors@R: c(
person("Lin-Tung","Tsai",
role = c("aut", "cre"),
Expand Down
3 changes: 1 addition & 2 deletions R/estimate_gtatt.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ estimate_gtatt_outcome <- function(y, aux, p, caches) {
names(cohort_did) <- c("D", "post.y", "pre.y", "weights")

# estimate --------------------

result <- tryCatch(estimate_did(dt_did = cohort_did, covvars, p,
last_coef, caches[[gt_name]]),
error = function(e){stop("DiD estimation failed for group-", recover_time(g, p$time_offset, p$time_step) ,
" time-", recover_time(t, p$time_offset, p$time_step), ": ", e)})

# post process --------------------

#collect the result
Expand Down
10 changes: 1 addition & 9 deletions R/fastdid.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ fastdid <- function(data,
setnames(dt, c(timevar, cohortvar, unitvar), c("time", "G", "unit"))
dt <- validate_dt(dt, p)


# preprocess -----------------------------------------------------------

#make dt conform to the WLOG assumptions of fastdid
Expand Down Expand Up @@ -127,14 +126,7 @@ get_exper_default <- function(exper){
}

coerce_dt <- function(dt, p){

#change to int before sorting
if(!is.numeric(dt[, G])){
dt[, G := as.numeric(G)]
}
if(!is.numeric(dt[, time])){
dt[, time := as.numeric(time)]
}


#chcek if there is availble never-treated group
if(!is.infinite(dt[, max(G)]) & p$control_option != "notyet"){
Expand Down
7 changes: 7 additions & 0 deletions R/validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ validate_dt <- function(dt, p){
varnames <- unlist(p[str_ends(names(p), "var")], recursive = TRUE) #get all the argument that ends with "var"
varnames <- varnames[!varnames %in% c(p$timevar, p$unitvar, p$cohortvar)]

#change to int
uniquecols <- c("G", "time", "unit")
for(col in uniquecols){
if(!dt[, is.numeric(get(col))]){stop(col, " needs to be numeric.")}
dt[!is.infinite(get(col)), c(col) := as.integer(get(col))] #yeah sometimes floating point can be annoying
}

raw_unit_size <- dt[, uniqueN(unit)]
raw_time_size <- dt[, uniqueN(time)]

Expand Down
24 changes: 11 additions & 13 deletions development/fastdid_0_9_4.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#2024-08-03
message('loading fastdid source ver. ver: 0.9.4 date: 2024-08-03')
#2024-08-05
message('loading fastdid source ver. ver: 0.9.4 date: 2024-08-05')
require(data.table);
require(stringr);
require(BMisc);
Expand Down Expand Up @@ -625,12 +625,11 @@ estimate_gtatt_outcome <- function(y, aux, p, caches) {
names(cohort_did) <- c("D", "post.y", "pre.y", "weights")

# estimate --------------------

result <- tryCatch(estimate_did(dt_did = cohort_did, covvars, p,
last_coef, caches[[gt_name]]),
error = function(e){stop("DiD estimation failed for group-", recover_time(g, p$time_offset, p$time_step) ,
" time-", recover_time(t, p$time_offset, p$time_step), ": ", e)})

# post process --------------------

#collect the result
Expand Down Expand Up @@ -833,7 +832,6 @@ fastdid <- function(data,
setnames(dt, c(timevar, cohortvar, unitvar), c("time", "G", "unit"))
dt <- validate_dt(dt, p)


# preprocess -----------------------------------------------------------

#make dt conform to the WLOG assumptions of fastdid
Expand Down Expand Up @@ -870,14 +868,7 @@ get_exper_default <- function(exper){
}

coerce_dt <- function(dt, p){

#change to int before sorting
if(!is.numeric(dt[, G])){
dt[, G := as.numeric(G)]
}
if(!is.numeric(dt[, time])){
dt[, time := as.numeric(time)]
}


#chcek if there is availble never-treated group
if(!is.infinite(dt[, max(G)]) & p$control_option != "notyet"){
Expand Down Expand Up @@ -1357,6 +1348,13 @@ validate_dt <- function(dt, p){
varnames <- unlist(p[str_ends(names(p), "var")], recursive = TRUE) #get all the argument that ends with "var"
varnames <- varnames[!varnames %in% c(p$timevar, p$unitvar, p$cohortvar)]

#change to int
uniquecols <- c("G", "time", "unit")
for(col in uniquecols){
if(!dt[, is.numeric(get(col))]){stop(col, " needs to be numeric.")}
dt[!is.infinite(get(col)), c(col) := as.integer(get(col))] #yeah sometimes floating point can be annoying
}

raw_unit_size <- dt[, uniqueN(unit)]
raw_time_size <- dt[, uniqueN(time)]

Expand Down

0 comments on commit e289c9d

Please sign in to comment.