Documentation
Documentation for TwoWayFEWeights.jl
TwoWayFEWeights.TwoWayFEWeights — Module
TwowayFEWeightsJulia implementation of two-way fixed effects weight diagnostics.
TwoWayFEWeights.twowayfeweights — Method
Estimation of the weights attached to the two-way fixed effects regressions.
Estimates the weights and measure of robustness to treatment effect heterogeneity attached to two-way fixed effects regressions.
This function estimates the weights attached to the two-way fixed effects regressions studied in de Chaisemartin & D'Haultfoeuille (2020a), as well as summary measures of these regressions' robustness to heterogeneous treatment effects.
Returns an object of type "twowayfeweights", which is a wrapper of the OrderedCollections.OrderedDict type. This wrapper allows for a custom Base.show() method. Included among the slots of the returned list object is a data frame containing the weights attached to each group*time cell.
Arguments
dataA data frame or data matrix.YCharacter string. The dependent variable in the regression. Y is the level of the outcome if one wants to estimate the weights attached to the fixed-effects regression, and Y is the first difference of the outcome if one wants to estimate the weights attached to the first-difference regression. Required.GCharacter string. The variable identifying each group. Required.TCharacter string. The variable identifying each period. Required.DCharacter string. The treatment variable in the regression. D is the level of the treatment if one wants to estimate the weights attached to the fixed-effects regression, and D is the first difference of the treatment if one wants to estimate the weights attached to the first-difference regression. Required.typeCharacter string. The type of estimation strategy. Can take one of four different values, each defining a unique combination of regression strategy (either fixed-effects or first-difference) and inference assumption (either common trends on its own or common trends plus an additional assumption about treatment stability over time).- "feTR" (the default): Fixed-effects regression under the common trends assumption.
- "feS": Fixed-effects regression under common trends and the assumption that groups' treatment effect does not change over time.
- "fdTR": First-difference regression under the common trends assumption.
- "fdS": First-difference regression under common trends and the assumption that groups' treatment effect does not change over time.
D0Character string. Iftype = "fdTR"is specified above, then the function requires a fifth argument,D0.D0is the mean of the treatment in group g and at period t. It should be non-missing at the first period when a group appears in the data (e.g. at t=1 for the groups that are in the data from the beginning), and for all observations for which the first-difference of the group-level mean outcome and treatment are non missing.summary_measuresLogical. Should the complementary results from the computation of the weights be displayed? Specifically, the option outputs: (i) the point estimate of the coefficient on the D variable from a TWFE regression, (ii) the minimum value of the standard deviation of the ATEs compatible with the coefficient from the TWFE regression and ATE across all treated (g,t) cells being equal to zero, (iii) the minimum value of the standard deviation of the ATEs compatible with the coefficient from the TWFE regression and ATE across all treated (g,t) cells having different signs (this is computed only if the sum of negative weights is different from 0). See the FAQ section of the original R package for other details.controlsCharacter string(s). An optional vector of control variables that are included in the regression. Controls should not vary within each groupperiod cell, because the results in in de Chaisemartin & D'Haultfoeuille (2020a) apply to two-way fixed effects regressions with group×period level controls. If a control does vary within a group×period cell, the command will replace it by its average value within each groupperiod cell.other_treatmentsCharacter string(s). An optional vector of other treatment variables that are included in the regression. Note that this option can only be used whentype = "feTR"is specified above. While the results in de Chaisemartin & D'Haultfoeuille (2020a) do not cover two-way fixed effects regressions with several treatments, those in de Chaisemartin & D'Haultfoeuille(2020b) do, so the command follows results from that second paper when other_treatments is specified. When it is specified, the command reports the number and sum of positive and negative weights attached to the treatment, but it does not report the summary measures of the regression's robustness to heterogeneous treatment effects, as these summary measures are no longer applicable when the regression has several treatment variables. The command also reports the weights attached to the other treatments. The weights reported by the command are those in Corollary 1 in de Chaisemartin & D'Haultfoeuille (2020b). See de Chaisemartin & D'Haultfoeuille (2020b) for further details.weightsCharacter string. Specifies a column name in the input data that replaces the default weighting scheme. If the regression is weighted, the weight variable can be specified in weight. Iftype="fdTR"is specified, then the weight variable should be non-missing at the first period when a group appears in the data (e.g. at t=1 for the groups that are in the data from the beginning), and for all observations for which the first-difference of the group-level mean outcome and treatment are non missing.test_random_weightsCharacter string(s). An optional vector that, when specified, will cause the function to estimate the correlation between each variable in the vector and the weights. Testing if those correlations significantly differ from zero is a way to assess whether the weights are as good as randomly assigned to groups and time periods.pathFile path for saving the results in a valid csv file that containing 3 variables (Group, Time, Weight). This option allows the user to see the weight attached to each group*time cell. If the other_treatments option is specified, the weights attached to the other treatments are also saved.
TwoWayFEWeights.fn_ctrl_rename — Method
fn_ctrl_rename(x)Rename variables by writing "ctrl" at their beginning. The `fnctrlrenamefunction is the equivalent of the originalfnctrlrename` function, defined in the original R code such as: fnctrlrename <- function(x) paste("ctrl", x, sep="")
TwoWayFEWeights.fn_random_weight_rename — Method
fn_random_weight_rename(x)Rename a vector of variables by putting "RW" in front of them. The `fnrandomweightrenamefunction is the equivalent of the originalfnrandomweightrename` function, defined in the original R code such as: fnrandomweightrename <- function(x) paste("RW", x, sep="_")
TwoWayFEWeights.fn_treatment_rename — Method
fn_treatment_rename(treatments)
Rename a vector of variable names by placing "OT" in front of it.TwoWayFEWeights.fn_treatment_weight_rename — Method
fn_treatment_weight_rename(x)Rename a vector of variables by putting "weight" in front of them. The `fntreatmentweightrenamefunction is the equivalent of the originalfntreatmentweightrename` function, defined in the original R code such as: fntreatmentweightrename <- function(x) paste("weight_", x, sep = "")
TwoWayFEWeights.get_controls_rename — Method
get_controls_rename(controls)Rename a vector of variable names by writing "ctrl" in front of them. The `getcontrolsrenamefunction is the equivalent of the originalgetcontrolsrename` function, defined in the original R code as: getcontrolsrename <- function(controls) unlist(lapply(controls, fnctrl_rename))
TwoWayFEWeights.get_random_weight_rename — Method
get_random_weight_rename(x)Rename a vector of variables by putting "RW" in front of them. The `fnrandomweightrenamefunction is the equivalent of the originalfnrandomweightrename` function, defined in the original R code such as: # getrandomweightrename <- function(ws) unlist(lapply(ws, fnrandomweight_rename))
TwoWayFEWeights.get_treatments_rename — Method
get_treatments_rename(treatments)Rename a vector of variables by putting "OT" in front of them. The `gettreatmentsrenamefunction is the equivalent of the originalgettreatmentsrename` function, defined in the original R code such as: gettreatmentsrename <- function(treatments) {unlist(lapply(treatments, fntreatment_rename))}
TwoWayFEWeights.twowayfeweights_calculate — Method
Internal funcion for calculating the twoway FE weights.
TwoWayFEWeights.twowayfeweights_filter — Method
twowayfeweights_filter(df, Y, G, T, D, D0, cmd_type, controls, treatments)Description.
TwoWayFEWeights.twowayfeweights_normalize_var — Method
twowayfeweights_normalize_var(df, varname)Internal function to replace a variable by its mean per group and time period.
TwoWayFEWeights.twowayfeweights_rename_var — Method
twowayfeweights_rename_var(df, Y, G, T, D, D0, controls, treatments, random_weights)Return a dataframe with renamed variables.
TwoWayFEWeights.twowayfeweights_result — Method
Internal workhorse function for creating the return object of a twowayfeweights() call.
@param dat A data frame, as per the return object from twowayfeweights_calculate(). @param beta Coefficient value of the treatment variable ("D"), again as per the return object of twowayfeweights_calculate(). @param controls A vector indicating the column names of random weights. @param treatments A vector indicating the column names of other treatments. @returns A list. @details This function is normally run directly after twowayfeweights_calculate(). @importFrom magrittr %>% @noRd
TwoWayFEWeights.twowayfeweights_summarize_weights — Method
twowayfeweights_summarize_weights(df, var_weight)Computes the cardinal and the sum of the sets of the weights in a dataframe if they are positive or negative.
TwoWayFEWeights.twowayfeweights_test_random_weights — Method
twowayfeweights_test_random_weights
String methodTwoWayFEWeights.twowayfeweights_transform — Method
function twowayfeweights_transform(; df::DataFrames.DataFrame, controls::Union{String, Vector{String}}, weights::Union{String, Vector{String}, Nothing}, treatments::Union{String, Vector{String}, Nothing})
Internal function.
TwoWayFEWeights.weighted_mean — Method
Returns a weighted mean for internal computation. Sends a warning if there are any missing values in the weights.