# Results

`DiffinDiffsBase.AbstractDIDResult`

— Type`AbstractDIDResult{TR<:AbstractTreatment} <: StatisticalModel`

Interface supertype for all types that collect estimation results for difference-in-differences with treatment of type `TR`

.

**Interface definition**

Required method | Default definition | Brief description |
---|---|---|

`coef(r)` | `r.coef` | Vector of point estimates for all coefficients including covariates |

`vcov(r)` | `r.vcov` | Variance-covariance matrix for estimates in `coef` |

`vce(r)` | `r.vce` | Covariance estimator |

`confint(r)` | Based on t or normal distribution | Confidence interval for estimates in `coef` |

`treatment(r)` | `r.tr` | Treatment specification |

`nobs(r)` | `r.nobs` | Number of observations (table rows) involved in estimation |

`outcomename(r)` | `r.yname` | Name of the outcome variable |

`coefnames(r)` | `r.coefnames` | Names (`Vector{String}` ) of all coefficients including covariates |

`treatcells(r)` | `r.treatcells` | `Tables.jl` -compatible tabular description of treatment coefficients in the order of `coefnames` (without covariates) |

`weights(r)` | `r.weights` | Name of the column containing sample weights (if specified) |

`ntreatcoef(r)` | `size(treatcells(r), 1)` | Number of treatment coefficients |

`treatcoef(r)` | `view(coef(r), 1:ntreatcoef(r))` | A view of treatment coefficients |

`treatvcov(r)` | `(N = ntreatcoef(r); view(vcov(r), 1:N, 1:N))` | A view of variance-covariance matrix for treatment coefficients |

`treatnames(r)` | `coefnames(r)[1:ntreatcoef(r)]` | Names (`Vector{String}` ) of treatment coefficients |

Optional methods | ||

`parent(r)` | `r.parent` or `r` | Result object from which `r` is generated |

`dof_residual(r)` | `r.dof_residual` or `nothing` | Residual degrees of freedom |

`responsename(r)` | `outcomename(r)` | Name of the outcome variable |

`coefinds(r)` | `r.coefinds` or `nothing` | Lookup table (`Dict{String,Int}` ) from `coefnames` to integer indices (for retrieving estimates by name) |

`ncovariate(r)` | `length(coef(r)) - ntreatcoef(r)` | Number of covariate coefficients |

`DiffinDiffsBase.AggregatedDIDResult`

— Type`AggregatedDIDResult{TR,P<:DIDResult} <: AbstractDIDResult{TR}`

Supertype for all types that collect estimation results aggregated from a `DIDResult`

of type `P`

with treatment of type `TR`

.

`DiffinDiffsBase.DIDResult`

— Type`DIDResult{TR} <: AbstractDIDResult{TR}`

Supertype for all types that collect estimation results directly obtained from `DiffinDiffsEstimator`

with treatment of type `TR`

.

`DiffinDiffsBase.DiffinDiffsEstimator`

— Type`DiffinDiffsEstimator{A,T} <: AbstractStatsProcedure{A,T}`

Specify the estimation procedure for difference-in-differences.

`DiffinDiffsBase.ExportFormat`

— Type`ExportFormat`

Supertype for all types representing the format for exporting an `AbstractDIDResult`

.

`DiffinDiffsBase.StataPostHDF`

— Type`StataPostHDF <: ExportFormat`

Export an `AbstractDIDResult`

for Stata module `posthdf`

.

`DiffinDiffsBase.SubDIDResult`

— Type`SubDIDResult{TR,P<:AbstractDIDResult,I,TI} <: AbstractDIDResult{TR}`

A view into a DID result of type `P`

with indices for all coefficients of type `I`

and indices for treatment coefficients of type `TI`

. See also `view(r::AbstractDIDResult, inds)`

.

`DiffinDiffsBase.TransSubDIDResult`

— Type`TransSubDIDResult{TR,P,M,I,TI} <: AbstractDIDResult{TR}`

Estimation results obtained from a linear transformation of a subset of coefficient estimates from `DIDResult`

. See also `TransformedDIDResult`

, `lincom`

and `rescale`

.

**Parameters**

`P`

: type of the result that is transformed.`M`

: type of the matrix representing the linear transformation.`I`

: type of indices for all coefficients.`TI`

: type of indices for treatment coefficients.

`DiffinDiffsBase.TransformedDIDResult`

— Type`TransformedDIDResult{TR,P,M} <: AbstractDIDResult{TR}`

Estimation results obtained from a linear transformation of all coefficient estimates from `DIDResult`

. See also `TransSubDIDResult`

, `lincom`

and `rescale`

.

**Parameters**

`P`

: type of the result that is transformed.`M`

: type of the matrix representing the linear transformation.

`Base.parent`

— Method`parent(r::AbstractDIDResult)`

Return the `AbstractDIDResult`

from which `r`

is generated.

`Base.view`

— Method`view(r::AbstractDIDResult, inds)`

Return a `SubDIDResult`

that lazily references elements from `r`

at the given index or indices `inds`

without constructing a copied subset.

`DiffinDiffsBase._treatnames`

— Method`_treatnames(treatcells)`

Generate names for treatment coefficients. Assume `treatcells`

is compatible with the `Tables.jl`

interface.

`DiffinDiffsBase.agg`

— Method`agg(r::DIDResult)`

Aggregate difference-in-differences estimates and return a subtype of `AggregatedDIDResult`

. The implementation depends on the type of `r`

.

`DiffinDiffsBase.checktreatindex`

— Method`checktreatindex(inds, tinds)`

Check whether all indices for treatment coefficients `tinds`

are positioned before any other index in `inds`

. This is required to be true for methods such as `treatcoef`

and `treatvcov`

to work properly. If the test fails, an `ArgumentError`

exception is thrown.

`DiffinDiffsBase.coefinds`

— Method`coefinds(r::AbstractDIDResult)`

Return the map from coefficient names to integer indices for retrieving estimates by name.

`DiffinDiffsBase.didspec`

— Method`didspec(args...; kwargs...)`

Construct a `StatsSpec`

for difference-in-differences with the specified arguments.

`DiffinDiffsBase.getexportformat`

— Method`getexportformat()`

Return the default `ExportFormat`

for `post!`

.

`DiffinDiffsBase.lincom`

— Function`lincom(r::AbstractDIDResult, linmap::AbstractMatrix{<:Real}, subset=nothing)`

Linearly transform the coefficient estimates from DID result `r`

through a matrix `linmap`

. The number of columns of `linmap`

must match the total number of coefficients from `r`

. If `linmap`

is not square (with fewer rows than columns), `subset`

must be specified with indices representing coefficients that remain after the transformation. See also `rescale`

.

`DiffinDiffsBase.ncovariate`

— Method`ncovariate(r::AbstractDIDResult)`

Return the number of covariate coefficients.

`DiffinDiffsBase.ntreatcoef`

— Method`ntreatcoef(r::AbstractDIDResult)`

Return the number of treatment coefficients.

`DiffinDiffsBase.outcomename`

— Method`outcomename(r::AbstractDIDResult)`

Return the name of outcome variable generated by `StatsModels.coefnames`

. See also `responsename`

.

`DiffinDiffsBase.parse_didargs!`

— Method`parse_didargs!(args::Vector{Any}, kwargs::Dict{Symbol,Any})`

Return a `Dict`

that is suitable for being passed to `valid_didargs`

for further processing.

Any `TreatmentTerm`

in `args`

is decomposed. Any collection of terms is converted to `TermSet`

. Keys are assigned to all positional arguments based on their types. An optional `name`

for `StatsSpec`

can be included in `args`

as a string. The order of positional arguments is irrelevant.

This function is required for `@specset`

to work properly.

`DiffinDiffsBase.post!`

— Method`post!(f, r; kwargs...)`

Export result `r`

in a default `ExportFormat`

.

The default format can be retrieved via `getexportformat`

and modified via `setexportformat!`

. The keyword arguments that can be accepted depend on the format and the type of `r`

.

`DiffinDiffsBase.post!`

— Method`post!(f, ::StataPostHDF, r::AbstractDIDResult; kwargs...)`

Export result `r`

for Stata module `posthdf`

. A subset of field values from `r`

are placed in `f`

by setting key-value pairs, where `f`

can be either an `HDF5.Group`

or any object that can be indexed by strings.

**Keywords**

`model::String=repr(typeof(r))`

: name of the model.`fields::Union{AbstractVector{<:Union{Symbol, Pair{String,Symbol}}}, Nothing}=nothing`

: additional fields to be exported; alternative names can be specified with`Pair`

s.`at::Union{AbstractVector{<:Real}, Bool, Nothing}=nothing`

: post the`at`

vector in Stata.

`DiffinDiffsBase.rescale`

— Function```
rescale(r::AbstractDIDResult, scale::AbstractVector{<:Real}, subset=nothing)
rescale(r::AbstractDIDResult, by::Pair, subset=nothing)
```

Rescale the coefficient estimates from DID result `r`

. The order of elements in `scale`

must match the order of coefficients. If the length of `scale`

is smaller than the total number of coefficient, `subset`

must be specified with indices representing coefficients that remain after the transformation. Alternatively, if `by`

is specified in the same way for `apply`

, the scales can be computed based on values in `treatcells(r)`

. In this case, only treatment coefficients are transformed even if `subset`

is not specified. See `lincom`

for more general transformation.

`DiffinDiffsBase.setexportformat!`

— Method`setexportformat!(format::ExportFormat)`

Set the default `ExportFormat`

for `post!`

.

`DiffinDiffsBase.treatcells`

— Method`treatcells(r::AbstractDIDResult)`

Return a `Tables.jl`

-compatible tabular description of treatment coefficients in the order of coefnames (without covariates).

`DiffinDiffsBase.treatcoef`

— Method`treatcoef(r::AbstractDIDResult)`

Return a view of treatment coefficients.

`DiffinDiffsBase.treatindex`

— Method`treatindex(ntcoef::Int, I)`

Extract indices referencing the treatment coefficients from `I`

based on the total number of treatment coefficients `ntcoef`

.

`DiffinDiffsBase.treatment`

— Method`treatment(r::AbstractDIDResult)`

Return the treatment specification.

`DiffinDiffsBase.treatnames`

— Method`treatnames(r::AbstractDIDResult)`

Return a vector of names for treatment coefficients.

`DiffinDiffsBase.treatvcov`

— Method`treatvcov(r::AbstractDIDResult)`

Return a view of variance-covariance matrix for treatment coefficients.

`DiffinDiffsBase.valid_didargs`

— Method`valid_didargs(args::Dict{Symbol,Any})`

Return a tuple of objects that can be accepted by the constructor of `StatsSpec`

. If no `DiffinDiffsEstimator`

is found in `args`

, try to select one based on other information.

This function is required for `@specset`

to work properly.

`DiffinDiffsBase.vce`

— Method`vce(r::AbstractDIDResult)`

Return the covariance estimator used to estimate variance-covariance matrix.

`StatsAPI.coef`

— Method```
coef(r::AbstractDIDResult, name::String)
coef(r::AbstractDIDResult, name::Symbol)
coef(r::AbstractDIDResult, i::Int)
coef(r::AbstractDIDResult, inds)
```

Retrieve a point estimate by name (as in `coefnames`

) or integer index. Return a vector of estimates if an iterable collection of names or integers are specified.

Indexing by name requires the method `coefinds(r)`

. See also `AbstractDIDResult`

.

`StatsAPI.coef`

— Method`coef(r::AbstractDIDResult, bys::Pair...)`

Return a vector of point estimates for treatment coefficients selected based on the specified functions that return either `true`

or `false`

.

Depending on the argument(s) accepted by a function `f`

, it is specified with argument `bys`

as either `column_index => f`

or `column_indices => f`

where `column_index`

is either a `Symbol`

or `Int`

for a column in `treatcells`

and `column_indices`

is an iterable collection of such indices for multiple columns. `f`

is applied elementwise to each specified column to obtain a `BitVector`

for selecting coefficients. If multiple `Pair`

s are provided, the results are combined into one `BitVector`

through bit-wise `and`

.

This method only selects estimates for treatment coefficients. Covariates are not taken into account.

`StatsAPI.coef`

— Method`coef(r::AbstractDIDResult)`

Return the vector of point estimates for all coefficients.

`StatsAPI.coefnames`

— Method`coefnames(r::AbstractDIDResult)`

Return a vector of coefficient names.

`StatsAPI.confint`

— Method`confint(r::AbstractDIDResult; level::Real=0.95)`

Return a confidence interval for each coefficient estimate. The returned object is of type `Tuple{Vector{Float64}, Vector{Float64}}`

where the first vector collects the lower bounds for all intervals and the second one collects the upper bounds.

`StatsAPI.dof_residual`

— Method`dof_residual(r::AbstractDIDResult)`

Return the residual degrees of freedom.

`StatsAPI.nobs`

— Method`nobs(r::AbstractDIDResult)`

Return the number of observations (table rows) involved in estimation.

`StatsAPI.responsename`

— Method`responsename(r::AbstractDIDResult)`

Return the name of outcome variable generated by `StatsModels.coefnames`

. This method is an alias of `outcomename`

.

`StatsAPI.vcov`

— Function```
vcov(r::AbstractDIDResult, name1::Union{String, Symbol}, name2::Union{String, Symbol}=name1)
vcov(r::AbstractDIDResult, i::Int, j::Int=i)
vcov(r::AbstractDIDResult, inds)
```

Retrieve the covariance between two coefficients by name (as in `coefnames`

) or integer index. Return the variance if only one name or index is specified. Return a variance-covariance matrix for selected coefficients if an iterable collection of names or integers are specified.

Indexing by name requires the method `coefinds(r)`

. See also `AbstractDIDResult`

.

`StatsAPI.vcov`

— Method`vcov(r::AbstractDIDResult, bys::Pair...)`

Return a variance-covariance matrix for treatment coefficients selected based on the specified functions that return either `true`

or `false`

.

Depending on the argument(s) accepted by a function `f`

, it is specified with argument `bys`

as either `column_index => f`

or `column_indices => f`

where `column_index`

is either a `Symbol`

or `Int`

for a column in `treatcells`

and `column_indices`

is an iterable collection of such indices for multiple columns. `f`

is applied elementwise to each specified column to obtain a `BitVector`

for selecting coefficients. If multiple `Pair`

s are provided, the results are combined into one `BitVector`

through bit-wise `and`

.

This method only selects estimates for treatment coefficients. Covariates are not taken into account.

`StatsAPI.vcov`

— Method`vcov(r::AbstractDIDResult)`

Return the variance-covariance matrix for all coefficient estimates.

`StatsAPI.weights`

— Method`weights(r::AbstractDIDResult)`

Return the column name of the weight variable. Return `nothing`

if `weights`

is not specified for estimation.

`DiffinDiffsBase.@did`

— Macro`@did [option option=val ...] "name" args... kwargs...`

Conduct difference-in-differences estimation with the specified arguments. The order of the arguments is irrelevant.

**Arguments**

`[option option=val ...]`

: optional settings for @did including keyword arguments passed to an instance of`StatsSpec`

.`name::AbstractString`

: an optional name for the`StatsSpec`

.`args... kwargs...`

: a list of arguments to be processed by`parse_didargs!`

and`valid_didargs`

.

**Notes**

When expanded outside `@specset`

, a `StatsSpec`

is constructed and then estimated by calling this instance. Options for [`StatsSpec`

] can be provided in a bracket `[...]`

as the first argument after `@did`

with each option separated by white space. For options that take a Boolean value, specifying the name of the option is enough for setting the value to be true. By default, only a result object that is a subtype of `DIDResult`

is returned.

When expanded inside `@specset`

, `@did`

informs `@specset`

the methods for processing the arguments. Any option specified in the bracket is ignored.

Options for the behavior of `@did`

can be provided in a bracket `[...]`

as the first argument with each option separated by white space. For options that take a Boolean value, specifying the name of the option is enough for setting the value to be true.

The following options are available for altering the behavior of `@did`

:

`noproceed::Bool=false`

: return the constructed`StatsSpec`

without conducting the procedure.`verbose::Bool=false`

: print the name of each step when it is called.`keep=nothing`

: names (of type`Symbol`

) of additional objects to be returned.`keepall::Bool=false`

: return all objects generated by procedures along with arguments from the`StatsSpec`

s.`pause::Int=0`

: break the iteration over`StatsStep`

s after finishing the specified number of steps (for debugging).

`InteractionWeightedDIDs.RegDIDResultOrAgg`

— Type`RegDIDResultOrAgg{TR,Haslsweights}`

Union type of `RegressionBasedDIDResult`

and `AggregatedRegDIDResult`

.

`InteractionWeightedDIDs.AggregatedRegDIDResult`

— Type`AggregatedRegDIDResult{TR,Haslsweights,P<:RegressionBasedDIDResult,I} <: AggregatedDIDResult{TR,P}`

Estimation results aggregated from a `RegressionBasedDIDResult`

. See also `agg`

.

**Fields**

`parent::P`

: the`RegressionBasedDIDResult`

from which the results are generated.`inds::I`

: indices of the coefficient estimates from`parent`

used to generate the results.`coef::Vector{Float64}`

: coefficient estimates.`vcov::Matrix{Float64}`

: variance-covariance matrix for the estimates.`coefweights::Matrix{Float64}`

: coefficient weights used to aggregate the coefficient estimates from`parent`

.`treatweights::Vector{Float64}`

: sum of`treatweights`

from`parent`

over combined`treatcells`

.`treatcounts::Vector{Int}`

: sum of`treatcounts`

from`parent`

over combined`treatcells`

.`coefnames::Vector{String}`

: coefficient names.`coefinds::Dict{String, Int}`

: a map from`coefnames`

to integer indices for retrieving estimates by name.`treatcells::VecColumnTable`

: cells combined from the`treatcells`

from`parent`

.`lsweights::Union{TableIndexedMatrix, Nothing}`

: cell-level least-square weights.`cellymeans::Union{Vector{Float64}, Nothing}`

: cell-level averages of the outcome variable.`cellweights::Union{Vector{Float64}, Nothing}`

: total sample weights for each cell.`cellcounts::Union{Vector{Int}, Nothing}`

: number of observations for each cell.

`InteractionWeightedDIDs.ContrastResult`

— Type`ContrastResult{T,M,R,C} <: AbstractMatrix{T}`

Matrix type that holds least-square weights obtained from one or more `RegDIDResultOrAgg`

s computed over the same set of cells and cell-level averages. See also `contrast`

.

The least-square weights are stored in a `Matrix`

that can be retrieved with property name `:m`

, where the weights for each treatment coefficient are stored columnwise starting from the second column and the first column contains the cell-level averages of outcome variable. The indices for cells can be accessed with property name `:r`

; and indices for identifying the coefficients can be accessed with property name `:c`

. The `RegDIDResultOrAgg`

s used to generate the `ContrastResult`

can be accessed by calling `parent`

.

`InteractionWeightedDIDs.RegressionBasedDIDResult`

— Type`RegressionBasedDIDResult{TR,CohortInteracted,Haslsweights} <: DIDResult{TR}`

Estimation results from regression-based difference-in-differences.

**Fields**

`coef::Vector{Float64}`

: coefficient estimates.`vcov::Matrix{Float64}`

: variance-covariance matrix for the estimates.`vce::CovarianceEstimator`

: variance-covariance estiamtor.`tr::TR`

: treatment specification.`pr::AbstractParallel`

: parallel trend assumption.`treatweights::Vector{Float64}`

: total sample weights from observations for which the corresponding treatment indicator takes one.`treatcounts::Vector{Int}`

: total number of observations for which the corresponding treatment indicator takes one.`esample::BitVector`

: indicator for the rows from`data`

involved in estimation.`nobs::Int`

: number of observations involved in estimation.`dof_residual::Int`

: residual degree of freedom.`F::Float64`

: F-statistic for overall significance of regression model.`p::Float64`

: p-value corresponding to the F-statistic.`yname::String`

: name of the outcome variable.`coefnames::Vector{String}`

: coefficient names.`coefinds::Dict{String, Int}`

: a map from`coefnames`

to integer indices for retrieving estimates by name.`treatcells::VecColumnTable`

: a tabular description of cells where a treatment indicator takes one.`treatname::Symbol`

: column name for the variable representing treatment time.`yxterms::Dict{AbstractTerm, AbstractTerm}`

: a map from all specified terms to concrete terms.`yterm::AbstractTerm`

: the specified term for outcome variable.`xterms::Vector{AbstractTerm}`

: the specified terms for covariates and fixed effects.`contrasts::Union{Dict{Symbol, Any}, Nothing}`

: contrast coding to be processed by`StatsModels.jl`

.`weightname::Union{Symbol, Nothing}`

: column name of the sample weight variable.`fenames::Vector{String}`

: names of the fixed effects.`nfeiterations::Union{Int, Nothing}`

: number of iterations for the fixed effect solver to reach convergence.`feconverged::Union{Bool, Nothing}`

: whether the fixed effect solver has converged.`nfesingledropped::Int`

: number of singleton observations for fixed effects that have been dropped.`lsweights::Union{TableIndexedMatrix, Nothing}`

: cell-level least-square weights.`cellymeans::Union{Vector{Float64}, Nothing}`

: cell-level averages of the outcome variable.`cellweights::Union{Vector{Float64}, Nothing}`

: total sample weights for each cell.`cellcounts::Union{Vector{Int}, Nothing}`

: number of observations for each cell.

`DiffinDiffsBase.agg`

— Function`agg(r::RegressionBasedDIDResult{<:DynamicTreatment}, names=nothing; kwargs...)`

Aggregate coefficient estimates from `r`

by values taken by the columns from `r.treatcells`

indexed by `names`

with weights proportional to `treatweights`

within each relative time.

**Keywords**

`bys=nothing`

: columnwise transformations over`r.treatcells`

before grouping by`names`

.`subset=nothing`

: subset of treatment coefficients used for aggregation.

`DiffinDiffsBase.post!`

— Function`post!(gl, gr, gd, ::StataPostHDF, cr::ContrastResult, left::Int=2, right::Int=3; kwargs...)`

Export the least-square weights for coefficients indexed by `left`

and `right`

from `cr`

for Stata module `posthdf`

. The contribution of each cell to the difference between two coefficients are computed and also exported. The weights and contributions are stored as coefficient estimates in three groups `gl`

, `gr`

and `gd`

respectively. The groups can be `HDF5.Group`

s or objects that can be indexed by strings.

**Keywords**

`lefttag::String=string(left)`

: name to be used as`depvar`

in Stata after being prefixed by`"l_"`

for the coefficient indexed by`left`

.`righttag::String=string(right)`

: name to be used as`depvar`

in Stata after being prefixed by`"r_"`

for the coefficient indexed by`right`

.`model::String="InteractionWeightedDIDs.ContrastResult"`

: name of the model.`eqnames::Union{AbstractVector, Nothing}=nothing`

: equation names prefixed to coefficient names in Stata.`colnames::Union{AbstractVector, Nothing}=nothing`

: column names used as coefficient names in Stata.`at::Union{AbstractVector{<:Real}, Nothing}=nothing`

: the`at`

vector in Stata.

`FixedEffectModels.has_fe`

— Method`has_fe(r::RegressionBasedDIDResult)`

Test whether any fixed effect is involved in regression.

`InteractionWeightedDIDs.contrast`

— Method`contrast(r1::RegDIDResultOrAgg, rs::RegDIDResultOrAgg...; kwargs)`

Construct a `ContrastResult`

by collecting the computed least-square weights from each of the `RegDIDResultOrAgg`

.

**Keywords**

`subset=nothing`

: indices for cells to be included (rows in output).`coefs=nothing`

: indices for coefficients from each result to be included (columns in output).

`InteractionWeightedDIDs.has_lsweights`

— Method`has_lsweights(r::RegDIDResultOrAgg)`

Test whether `r`

contains computed least-sqaure weights (`r.lsweights!==nothing`

).