Skip to content

Commit

Permalink
fix namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
TsaiLintung committed Oct 9, 2023
1 parent 976cab1 commit 96f5700
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 49 deletions.
22 changes: 11 additions & 11 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ Type: Package
Title: lightning-fast staggered Difference-in-Difference estimators
Version: 0.9
Date: 2023-10-06
Author@R: c(person(given = "Lin-Tung",
family = "Tsai",
role = "aut",
person(given = "Maxwell",
family = "Kellogg",
role = "aut"),
person(given = "Kuan-Ju",
family = "Tseng",
role = "aut"))
Authors@R: c(
person("Lin-Tung","Tsai",
role = c("aut", "cre"),
email = "[email protected]"),
person(given = "Maxwell",
family = "Kellogg", ,
role = "aut"),
person(given = "Kuan-Ju",
family = "Tseng", ,
role = "aut"))
Maintainer: Lin-Tung Tsai <[email protected]>
Description: **fastdid** is a lightning-fast implementation of Callaway and Sant'Anna's (2021)<https://www.sciencedirect.com/science/article/pii/S0304407620303948> staggered Difference-in-Differences (DiD) estimators that speeds up the process by magnitudes. With fastdid You can run DiD designs for millions of units in just seconds, not hours.
License: GPL (>= 2)
Expand All @@ -21,8 +22,7 @@ Imports:
BMisc,
collapse,
dreamerr,
speedglm,
stringr
speedglm
Suggests: ggplot2, did
Encoding: UTF-8
RoxygenNote: 7.2.3
9 changes: 8 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Generated by roxygen2: do not edit by hand

export(fastdid)
export(plot_did_dynamics)
export(sim_did)
import(BMisc)
import(collapse)
import(data.table)
import(dreamerr)
import(ggplot2)
import(speedglm)
import(stringr)
importFrom(stats,binomial)
importFrom(stats,fitted)
importFrom(stats,qnorm)
importFrom(stats,quantile)
importFrom(stats,rnorm)
importFrom(stats,sd)
importFrom(stats,vcov)
2 changes: 0 additions & 2 deletions R/aggregate_gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ aggregate_gt <- function(gt_result, cohort_sizes,
#aggregated att
agg_att <- weights %*% gt_att



#get the influence from weight estimation
#this needs to be optimized!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
inf_weights <- sapply(asplit(weights, 1), function (x){
Expand Down
4 changes: 2 additions & 2 deletions R/estimate_did.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ estimate_did <- function(dt_did, last_coef = NULL){
#the intercept is essential
if(ipw){
prop_score_est <- suppressWarnings(speedglm(dt_did[,D] ~ as.matrix(dt_did[,.SD, .SDcols = covvars]),
family = binomial(), fitted = TRUE, start = last_coef,
family = stats::binomial(), fitted = TRUE, start = last_coef,
weights = dt_did[, weights]))
logit_coef <- prop_score_est$coefficients
logit_coef[is.na(logit_coef)|abs(logit_coef) > 1e10] <- 0 #put extreme value and na to 0
Expand Down Expand Up @@ -55,7 +55,7 @@ estimate_did <- function(dt_did, last_coef = NULL){
# influence from ipw
if(ipw){
score_ps <- as.matrix(dt_did[, weights*(D-ps)*.SD, .SDcols = covvars]) |> reverse_col()
hess <- vcov(prop_score_est) * n
hess <- stats::vcov(prop_score_est) * n
asym_linear_ps <- score_ps %*% hess
M2 <- dt_did[, .(cont_ipw_weight*(delta_y-weighted_cont_delta)*.SD), .SDcols = covvars][, lapply(.SD, mean), .SDcols = covvars] |>
as.matrix()|> reverse_col()
Expand Down
20 changes: 13 additions & 7 deletions R/fastdid.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#' @param copy whether to copy the dataset before processing, set to true if the original dataset is to be re-used.
#' @param validate whether to validate the dataset before processing.
#'
#' @import data.table speedglm stringr collapse dreamerr BMisc
#'
#' @import data.table speedglm stringr collapse dreamerr BMisc
#' @importFrom stats quantile vcov sd binomial fitted qnorm rnorm
#' @return A data.table containing the estimated treatment effects and standard errors.
#' @export
#'
Expand All @@ -28,15 +28,21 @@
#' dt <- simdt$dt
#'
#' #basic call
#' result <- fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit", outcomevar = "y", result_type = "group_time")
#' result <- fastdid(dt, timevar = "time", cohortvar = "G",
#' unitvar = "unit", outcomevar = "y",
#' result_type = "group_time")
#'
#' #control for covariates
#' result2 <- fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit", outcomevar = "y", result_type = "group_time",
#' covariatesvar = c("x", "x2"))
#' result2 <- fastdid(dt, timevar = "time", cohortvar = "G",
#' unitvar = "unit", outcomevar = "y",
#' result_type = "group_time",
#' covariatesvar = c("x", "x2"))
#'
#' #bootstrap and clustering
#' result3 <- fastdid(dt, timevar = "time", cohortvar = "G", unitvar = "unit", outcomevar = "y", result_type = "group_time",
#' boot = TRUE, clustervar = "x")
#' result3 <- fastdid(dt, timevar = "time", cohortvar = "G",
#' unitvar = "unit", outcomevar = "y",
#' result_type = "group_time",
#' boot = TRUE, clustervar = "x")
#'
#' @keywords difference-in-differences fast computation panel data estimation did
fastdid <- function(dt,
Expand Down
6 changes: 3 additions & 3 deletions R/get_se.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ get_se <- function(inf_matrix, boot, biters, cluster) {
top_quant <- 0.75
bot_quant <- 0.25
if(!is.null(cluster)){
cluster_n <- aggregate(cluster, by=list(cluster), length)[,2]
cluster_n <- stats::aggregate(cluster, by=list(cluster), length)[,2]
inf_matrix <- fsum(inf_matrix, cluster) / cluster_n #the mean without 0 for each cluster of each setting
}

boot_results <- BMisc::multiplier_bootstrap(inf_matrix, biters = biters) %>% as.data.table()

boot_top <- boot_results[, lapply(.SD, function(x) quantile(x, top_quant, type=1, na.rm = TRUE)),]
boot_bot <- boot_results[, lapply(.SD, function(x) quantile(x, bot_quant, type=1, na.rm = TRUE)),]
boot_top <- boot_results[, lapply(.SD, function(x) stats::quantile(x, top_quant, type=1, na.rm = TRUE)),]
boot_bot <- boot_results[, lapply(.SD, function(x) stats::quantile(x, bot_quant, type=1, na.rm = TRUE)),]

dt_se <- rbind(boot_bot, boot_top) %>% transpose()
names(dt_se) <- c("boot_bot", "boot_top")
Expand Down
19 changes: 19 additions & 0 deletions R/global.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NULL

if(FALSE){
text <- ". agg_weight att att_cont att_treat attgt cohort cohort_size
conf_lwb conf_upb const cont_ipw_weight count delta_y element_rect
element_text event_time pg placeholder post.y pre.y ps s se target
tau time_fe treat_ipw_weight treat_latent type unit unit_fe weight x
x2 x_trend y y0 y1 y2 time"
text <- text |> str_remove_all("\\\n") |>str_split(" ") |> unlist()
text <- text[text!=""]
text <- text |> str_flatten(collapse = "','")
text <- paste0("c('", text, "')")
}

# quiets concerns of R CMD check re: the .'s that appear in pipelines and data.table variables
utils::globalVariables(c('.','agg_weight','att','att_cont','att_treat','attgt','cohort','cohort_size','conf_lwb','conf_upb',
'const','cont_ipw_weight','count','delta_y','element_rect','element_text','event_time','pg','placeholder',
'post.y','pre.y','ps','s','se','target','tau','time_fe',
'treat_ipw_weight','treat_latent','type','unit','unit_fe','weight','x','x2','x_trend','y','y0','y1','y2', 'time', 'weights'))
31 changes: 18 additions & 13 deletions R/plot_event_dynamics.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
#' @param significance_level The significance level for confidence intervals (default is 0.05).
#'
#' @return A ggplot2 object representing the event study plot.
#'
#' @import ggplot2
#' @export

plot_did_dynamics <-function(dt,
graphname = "event study plot", note = "", base_time = -1, significance_level = 0.05,
stratify_offset =0.1
graphname = "event study plot", note = "", base_time = -1, significance_level = 0.05#,
#stratify_offset =0.1
){


if (!requireNamespace("ggplot2", quietly = TRUE)) {
warning("The ggplot2 package must be installed to use plotting functions")
#Either exit or do something without rgl
return(NULL)
}


#add the base period
base_row <- data.table(att = 0, se = 0, event_time = base_time)
Expand All @@ -32,18 +37,18 @@ plot_did_dynamics <-function(dt,
# }

figure <- dt |>
ggplot() +
geom_hline(yintercept = 0, linetype = 'dashed', col = 'red')
ggplot2::ggplot() +
ggplot2::geom_hline(yintercept = 0, linetype = 'dashed', col = 'red')

# if("stratify" %in% names(dt)){
# figure <- figure + geom_line(aes(x = event_time, y = att, color = stratify)) +
# geom_point(aes(x = event_time, y = att, color = stratify)) +
# geom_errorbar(aes(x = event_time, ymin = conf_lwb, ymax = conf_upb),
# width = 0.1, linetype = "dashed")
# } else {
figure <- figure + geom_line(aes(x = event_time, y = att), color = "black") +
geom_point(aes(x = event_time, y = att), color = "black") +
geom_errorbar(aes(x = event_time, ymin = conf_lwb, ymax = conf_upb),
figure <- figure + ggplot2::geom_line(ggplot2::aes(x = event_time, y = att), color = "black") +
ggplot2::geom_point(ggplot2::aes(x = event_time, y = att), color = "black") +
ggplot2::geom_errorbar(ggplot2::aes(x = event_time, ymin = conf_lwb, ymax = conf_upb),
width = 0.1, linetype = "dashed")
# }

Expand All @@ -55,14 +60,14 @@ plot_did_dynamics <-function(dt,
# }

figure <- figure +
theme_classic() +
theme(legend.position = "bottom",
ggplot2::theme_classic() +
ggplot2::theme(legend.position = "bottom",
legend.background = element_rect(linetype = "dashed", color = "black"),
legend.box = "horizontal",
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0)) +
labs(title = graphname, subtitle = note)
ggplot2::labs(title = graphname, subtitle = note)

return(figure)

Expand Down
3 changes: 1 addition & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


set_max_thread <- function(){
setDTthreads(0)
data.table::setDTthreads(0)
options(kit.nThread = getDTthreads())
setFixest_nthreads(getDTthreads())
}

reverse_col <- function(x){
Expand Down
2 changes: 1 addition & 1 deletion R/validate_did.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ validate_did <- function(dt, covariatesvar){
}

#check covaraites is not constant
if(any(sapply(dt[1:raw_unit_size, .SD, .SDcols = covariatesvar], sd) == 0)){stop("some covariates have no variation")}
if(any(sapply(dt[1:raw_unit_size, .SD, .SDcols = covariatesvar], stats::sd) == 0)){stop("some covariates have no variation")}
}

#check balanced panel
Expand Down
5 changes: 5 additions & 0 deletions interactive/test_all.R → interactive/pkg_dev.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ if(FALSE){
test_all()
}

if(FALSE){
roxygenise()
build()
check()
}

if(FALSE){
use_github_actions()
Expand Down
16 changes: 11 additions & 5 deletions man/fastdid.Rd

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

3 changes: 1 addition & 2 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 96f5700

Please sign in to comment.