Giter VIP home page Giter VIP logo

pretrends's Introduction

pretrends

The pretrends package provides tools for power calculations for pre-trends tests, and visualization of possible violations of parallel trends. Calculations are based on Roth (2020). (Please cite the paper if you enjoy the package!)

If you’re not an R user, you may also be interested in the associated Shiny app.

Installation

You can install the released version of pretrends from Github using the devtools package:

# install.packages("devtools") #install devtools if not installed
devtools::install_github("jonathandroth/pretrends")

Application to He and Wang (2017)

We illustrate how to use the package with an application to He and Wang (2017). The analysis will be based on the event-study in Figure 2A, which looks like this:

He and Wang Plot.

Load the package and data

We first load the pretrends package.

library(pretrends)

Next we load the estimation results used for the event-plot, namely the coefficients (beta), the variance-covariance matrix (sigma), and the corresponding event-times (t). In this case, these coefficients come from a two-way fixed effects regression, but the pretrends package can accommodate an event-study from any asymptotically normal estimator, including Callaway and Sant’Anna (2020) and Sun and Abraham (2020).

#Load the coefficients, covariance matrix, and time periods
beta <- pretrends::HeAndWangResults$beta
sigma <- pretrends::HeAndWangResults$sigma
tVec <- pretrends::HeAndWangResults$tVec
referencePeriod <- -1 #This is the omitted period in the regression
data.frame(t = tVec, beta = beta)
#>    t        beta
#> 1 -4 -0.08942866
#> 2 -3 -0.08939153
#> 3 -2 -0.06860359
#> 4  0  0.09308024
#> 5  1  0.18774731
#> 6  2  0.18067996
#> 7  3  0.09727819

Using the package

The pretrends package has two main functions. The first is slope_for_power(), which calculates the slope of a linear violation of parallel trends that a pre-trends test would detect a specified fraction of the time. (By detect, we mean that there is any significant pre-treatment coefficient.)

#Compute slope that gives us 50% power
slope50 <-
slope_for_power(sigma = sigma,
                targetPower = 0.5,
                tVec = tVec,
                referencePeriod = referencePeriod)
slope50
#> [1] 0.06992116

The package’s second (and main) function is pretrends(), which enables power analyses and visualization given the results of an event-study and a user-hypothesized difference in trends. We illustrate this using the linear trend against which pre-tests have 50 percent power, computed above.

pretrendsResults <- 
  pretrends(betahat = beta, 
            sigma = sigma, 
            tVec = tVec, 
            referencePeriod = referencePeriod,
            deltatrue = slope50 * (tVec - referencePeriod))

The pretrends function returns a list of objects, which we examine in turn. First, we can visualize the event-plot and the hypothesized trend.

pretrendsResults$event_plot

Next, df_power displays several useful statistics about the power of the pre-test against the hypothesized trend:

  • Power The probability that we would find a significant pre-trend under the hypothesized pre-trend. (This is 0.50 by construction in our example).

  • Bayes Factor The ratio of the probability of ‘’passing’’ the pre-test under the hypothesized trend relative to under parallel trends.

  • Likelihood Ratio The ratio of the likelihood of the observed coefficients under the hypothesized trend relative to under pararallel trends.

pretrendsResults$df_power
#>       Power Bayes.Factor Likelihood.Ratio
#> 1 0.4996759    0.5672377        0.8588128

Next, df_eventplot contains the data used to make the event-plot. It also includes a column meanAfterPretesting, which shows the expected value of the coefficients conditional on passing the pre-test under the hypothesized trend.

pretrendsResults$df_eventplot
#>    t     betahat   deltatrue         se meanAfterPretesting
#> 1 -4 -0.08942866 -0.20976347 0.12228981         -0.12246120
#> 2 -3 -0.08939153 -0.13984231 0.10185069         -0.07513433
#> 3 -2 -0.06860359 -0.06992116 0.07761490         -0.03059641
#> 4 -1  0.00000000  0.00000000 0.00000000          0.00000000
#> 5  0  0.09308024  0.06992116 0.07494899          0.09017880
#> 6  1  0.18774731  0.13984231 0.10177085          0.16448752
#> 7  2  0.18067996  0.20976347 0.10727871          0.22996451
#> 8  3  0.09727819  0.27968463 0.15696300          0.29193119

Finally, the plot event_plot_pretest adds the meanAfterPretesting to the original event-plot.

pretrendsResults$event_plot_pretest

Although our example has focused on a linear violation of parallel trends, the package allows the user to input an arbitrary non-linear hypothesized trend. For instance, here is the event-plot from a quadratic trend.

  pretrends(betahat = beta, 
            sigma = sigma, 
            tVec = tVec, 
            referencePeriod = referencePeriod,
            deltatrue = -0.05 * (tVec - referencePeriod)^2)$event_plot_pretest

pretrends's People

Contributors

jonathandroth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.