-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a396266
commit b7ab9f9
Showing
348 changed files
with
10,469 additions
and
472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#' Panel Estimate Function | ||
#' | ||
#' This function computes estimates and standard errors for panel data using selected estimators. | ||
#' It allows the user to select specific estimators and set parameters for Monte Carlo replications and seed. | ||
#' | ||
#' @param setup A list containing matrices Y, N0, and T0 for panel data analysis. | ||
#' @param selected_estimators A character vector specifying which estimators to use. | ||
#' Defaults to all available estimators. | ||
#' @param mc_replications The number of Monte Carlo replications for computing standard errors. | ||
#' Applicable if the 'mc' estimator is used. Defaults to 200. | ||
#' @param seed An integer value to set the random seed for reproducibility. Defaults to 1. | ||
#' | ||
#' @return A list where each element corresponds to an estimator and contains its estimate and standard error. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' data('california_prop99') | ||
#' setup = panel.matrices(california_prop99) | ||
#' results_all = panel_estimate(setup) | ||
#' results_selected = panel_estimate(setup, selected_estimators = c("did", "sc")) | ||
#' summary(results_selected$did$estimate) | ||
#' } | ||
#' | ||
#' @export | ||
panel_estimate <- | ||
function(setup, | ||
selected_estimators = names(panel_estimators), | ||
mc_replications = 200, | ||
seed = 1) { | ||
set.seed(seed) | ||
# Subset the list of estimators based on user selection | ||
estimators_to_use = panel_estimators[selected_estimators] | ||
|
||
# Compute estimates | ||
estimates = lapply(estimators_to_use, function(estimator) { | ||
estimator(setup$Y, setup$N0, setup$T0) | ||
}) | ||
|
||
# Compute standard errors | ||
standard.errors = mapply(function(estimate, name) { | ||
if (name == 'mc') { | ||
mc_placebo_se(setup$Y, setup$N0, setup$T0, replications = mc_replications) | ||
} else { | ||
sqrt(vcov(estimate, method = 'placebo')) | ||
} | ||
}, estimates, names(estimators_to_use)) | ||
|
||
# Combine estimates and standard errors | ||
results = Map(function(est, se) | ||
list(estimate = est, std.error = se), | ||
estimates, | ||
standard.errors) | ||
|
||
# Name the results with estimator names | ||
names(results) = names(estimators_to_use) | ||
|
||
return(results) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#' Process Panel Estimate | ||
#' | ||
#' This function processes the output from `panel_estimate()` for panel estimates and returns a formatted data frame. | ||
#' It takes a list of results, each corresponding to a different method, and combines them into a single data frame. | ||
#' The data frame includes the method name, estimate, and standard error for each method. | ||
#' | ||
#' @param results_selected A list of results from `panel_estimate()`. Each element in the list should be an object | ||
#' containing the results for a particular estimation method. Each object must have an `estimate` and a `std.error` attribute. | ||
#' | ||
#' @return A data frame with columns `Method`, `Estimate`, and `SE`, representing the method name, the estimate value, | ||
#' and the standard error, respectively. The data frame is formatted using `causalverse::nice_tab()`. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' library(synthdid) | ||
#' setup = synthdid::panel.matrices(synthdid::california_prop99) | ||
#' results_selected = panel_estimate(setup, selected_estimators = c("did", "sc")) | ||
#' results_table = process_panel_estimate(results_selected) | ||
#' print(results_table) | ||
#' } | ||
#' | ||
#' @export | ||
process_panel_estimate <- function(results_selected) { | ||
# Create the data frame from the results | ||
results_df <- do.call(rbind, lapply(names(results_selected), function(name) { | ||
data.frame( | ||
Method = toupper(name), | ||
Estimate = as.numeric(results_selected[[name]]$estimate), | ||
SE = as.numeric(results_selected[[name]]$std.error) | ||
) | ||
})) |> | ||
causalverse::nice_tab() | ||
|
||
return(results_df) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.