Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (19.3%) to scientific vocabulary
Last synced: 6 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: yo5uke
  • License: other
  • Language: R
  • Default Branch: main
  • Size: 418 KB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created about 1 year ago · Last pushed 8 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



# fixes 



```{r, include=FALSE}
rcompendium::add_cran_badge(quiet = FALSE)
```
[![CRAN status](https://www.r-pkg.org/badges/version/fixes)](https://CRAN.R-project.org/package=fixes)
[![R-CMD-check](https://github.com/yo5uke/fixes/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/yo5uke/fixes/actions/workflows/R-CMD-check.yaml)


## Overview

> **Note**  
> By default, the `fixes` package assumes time is a regularly spaced numeric variable (e.g., year = 1995, 1996, ...).  
> If your time variable is irregular or non-numeric (e.g., `Date` type), set `time_transform = TRUE` to automatically convert it to a sequential index within each unit.  
> For unit-specific treatment timing, set `staggered = TRUE`.

The `fixes` package is designed for convenient event study analysis and plotting, particularly useful for visualizing parallel trends and dynamic effects in two-way fixed effects (TWFE) difference-in-differences (DID) research.

**Key Functions:**

1. `run_es()` — Takes a data frame, generates lead/lag dummies, and fits the event study regression. Supports fixed effects, covariates, clustering, staggered timing, weights, custom baseline, and multiple confidence intervals.
2. `plot_es()` — Plots event study results using `ggplot2` with flexible options: ribbon or error bars, choice of CI level, and theme customization.

## Installation

Install from CRAN:

```r
install.packages("fixes")
```

Or with **pak**:

```r
pak::pak("fixes")
```

For the latest development version from GitHub:

```r
pak::pak("yo5uke/fixes")
```

## How to use

First, load the library.

```r
library(fixes)
```

### Data frame requirements

`run_es()` expects a panel data frame with at least:

- Unit identifier (e.g., individual, firm, region)
- Treatment indicator (0/1 or TRUE/FALSE)
- Time variable (numeric or `Date`)
- Outcome variable (continuous)

For **staggered adoption** (`staggered = TRUE`), include a variable specifying unit-specific treatment timing (e.g., "treatment_year").

#### Example data

Widely used panel datasets include:

- `did::sim_dt()`: simulated panel for DiD tutorials
- `fixest::base_stagg`: a built-in dataset for staggered adoption

```{r}
df1 <- fixest::base_did      # Basic DiD
df2 <- fixest::base_stagg    # Staggered treatment
```

```{r, echo=FALSE}
head(df1) |>
  knitr::kable()
head(df2) |>
  knitr::kable()
```

### `run_es()`

The main event study function. All key arguments below:

| Argument        | Description |
|-----------------|-------------|
| `data`          | Data frame to be used. |
| `outcome`       | Outcome variable. Can be specified as a raw variable or a transformation (e.g., `log(y)`). Provide it unquoted. |
| `treatment`     | Dummy variable indicating the treated units. Provide it unquoted. Accepts both `0/1` and `TRUE/FALSE`. |
| `time`          | Time variable. Provide it unquoted. |
| `timing`        | The time at which the treatment occurs. If `staggered = FALSE`, this should be a scalar (e.g., `2005`). If `staggered = TRUE`, provide a variable (column) indicating the treatment time for each unit. |
| `fe`            | Fixed effects to control for unobserved heterogeneity. **Must be a one-sided formula** (e.g., `~ id + year`). |
| `lead_range`    | Number of pre-treatment periods to include (e.g., 3 = `lead3`, `lead2`, `lead1`). Default is `NULL`, which automatically uses the maximum available lead range. |
| `lag_range`     | Number of post-treatment periods to include (e.g., 2 = `lag0` (the treatment period), `lag1`, `lag2`). Default is `NULL`, which automatically uses the maximum available lag range. |
| `covariates`    | Additional covariates to include in the regression. **Must be a one-sided formula** (e.g., `~ x1 + x2`). |
| `cluster`       | Specifies clustering for standard errors. Can be a **character vector** (e.g., `c("id", "year")`) or a **formula** (e.g., `~ id + year`, `~ id^year`). |
| `weights`       | Optional weights to be used in the regression. Provide as a one-sided formula (e.g., `~ weight`). |
| `baseline`      | Relative time value to be used as the reference category. The corresponding dummy is excluded from the regression. **Must be within the specified lead/lag range.** |
| `interval`      | Time interval between observations (e.g., `1` for yearly data, `5` for 5-year intervals). |
| `time_transform`| Logical. If `TRUE`, converts the `time` variable into a sequential index (1, 2, 3, ...) within each unit. Useful for irregular time (e.g., Date). Default is `FALSE`. |
| `unit`          | Required if `time_transform = TRUE`. Specifies the panel unit identifier (e.g., `firm_id`). |
| `staggered`     | Logical. If `TRUE`, allows for unit-specific treatment timing (staggered adoption). Default is `FALSE`. |
| `conf.level`    | Numeric vector of confidence levels (e.g., `c(0.90, 0.95, 0.99)`; default: `0.95`). |

#### Example: basic event study

```r
event_study <- run_es(
  data       = df1,
  outcome    = y,
  treatment  = treat,
  time       = period,
  timing     = 6,
  fe         = ~ id + period,
  lead_range = 5,
  lag_range  = 4,
  cluster    = ~ id,
  baseline   = -1,
  interval   = 1,
  conf.level = c(0.90, 0.95, 0.99)
)
```

- `fe` must be a one-sided formula (e.g., `~ firm_id + year`).
- `cluster` can be a one-sided formula or a character vector.

#### With covariates

```r
event_study <- run_es(
  data       = df1,
  outcome    = y,
  treatment  = treat,
  time       = period,
  timing     = 6,
  fe         = ~ id + period,
  lead_range = 5,
  lag_range  = 4,
  covariates = ~ cov1 + cov2 + cov3,
  cluster    = ~ id,
  baseline   = -1,
  interval   = 1
)
```

#### Using irregular time data (`Date`), with `time_transform`

```r
df_alt <- df1 |>
  dplyr::mutate(
    year = rep(2001:2010, times = 108),
    date = as.Date(paste0(year, "-01-01"))
  )

event_study_alt <- run_es(
  data           = df_alt,
  outcome        = y,
  treatment      = treat,
  time           = date,
  timing         = 9,  # Use index, not the original Date
  fe             = ~ id + period,
  lead_range     = 3,
  lag_range      = 3,
  cluster        = ~ id,
  baseline       = -1,
  time_transform = TRUE,
  unit           = id
)
```

> **Note:**  
> When `time_transform = TRUE`, specify `timing` as an index (e.g., 9 = 9th observation in unit).  
> Currently, `time_transform = TRUE` *cannot* be combined with `staggered = TRUE` (future versions may support this).

### `plot_es()`

`plot_es()` visualizes results using `ggplot2`. By default, it plots a ribbon for the 95% CI, but supports error bars, CI level selection, and multiple themes.

| Argument     | Description                                |
|--------------|--------------------------------------------|
| data         | Data frame from `run_es()`                 |
| ci_level     | Confidence interval (default: 0.95)        |
| type         | "ribbon" (default) or "errorbar"           |
| vline_val    | X for vertical line (default: 0)           |
| vline_color  | Color for vline (default: "#000")          |
| hline_val    | Y for horizontal line (default: 0)         |
| hline_color  | Color for hline (default: "#000")          |
| linewidth    | Line width (default: 1)                    |
| pointsize    | Point size (default: 2)                    |
| alpha        | Ribbon transparency (default: 0.2)         |
| barwidth     | Errorbar width (default: 0.2)              |
| color        | Point/line color (default: "#B25D91FF")    |
| fill         | Ribbon color (default: "#B25D91FF")        |
| theme_style  | Theme: "bw" (default), "minimal", "classic"|

#### Example usage

```r
plot_es(event_study)
plot_es(event_study, type = "errorbar")
plot_es(event_study, type = "ribbon", ci_level = 0.9, theme_style = "minimal")
plot_es(event_study, type = "errorbar", ci_level = 0.99) + ggplot2::ggtitle("Event Study, 99% CI")
```

Further customization with `ggplot2` is fully supported:

```r
plot_es(event_study, type = "errorbar") + 
  ggplot2::scale_x_continuous(breaks = seq(-5, 5, by = 1)) + 
  ggplot2::ggtitle("Result of Event Study")
```

## Planned Features

- Support for `staggered = TRUE` with `time_transform = TRUE`
- Allow `timing` to accept original time values (e.g., `Date`), not just index

## Debugging and Contributions

If you find an issue or want to contribute, please use the [GitHub Issues page](https://github.com/yo5uke/fixes/issues).

---

Happy analyzing!🥂

Owner

  • Name: Yosuke Abe
  • Login: yo5uke
  • Kind: user
  • Location: Osaka
  • Company: Osaka School of International Public Policy

GitHub Events

Total
  • Push event: 33
  • Create event: 2
Last Year
  • Push event: 33
  • Create event: 2

Packages

  • Total packages: 1
  • Total downloads:
    • cran 336 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 6
  • Total maintainers: 1
cran.r-project.org: fixes

Tools for Creating and Visualizing Fixed-Effects Event Study Models

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 336 Last month
Rankings
Dependent packages count: 27.0%
Dependent repos count: 33.2%
Average: 49.1%
Downloads: 87.0%
Maintainers (1)
Last synced: 7 months ago

Dependencies

DESCRIPTION cran
  • R >= 4.0.0 depends
  • broom * imports
  • dplyr * imports
  • fixest * imports
  • ggplot2 * imports
  • tibble * imports