library(data.table)
library(fixest)
library(did2s)
#> ℹ did2s (v0.7.0). For more information on the methodology, visit <https://www.kylebutts.com/did2s>
#> To cite did2s in publications use:
#>
#> Butts, Kyle (2021). did2s: Two-Stage Difference-in-Differences
#> Following Gardner (2021). R package version 0.7.0.
#>
This file contains hidden or 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
| library(sf) | |
| library(s2) | |
| nc <- sf::st_read(system.file("shape/nc.shp", package="sf")) | |
| nc_s2 <- s2::as_s2_geography(nc) | |
| ncpts_sf <- sf::st_sample(nc, 1000) | |
| ncpts_s2 <- s2::as_s2_geography(ncpts_sf) | |
| # ---- Distance Matrix --------------------------------------------------------- | |
| bench::mark( | |
| sf::st_distance(ncpts_sf), |
This file has been truncated, but you can view the full file.
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" /> | |
| <meta name="generator" content="distill" /> |
This file contains hidden or 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
| #' Prepares table body from dataframe | |
| #' | |
| #' @description Note this will copy the latex code into your clipboard. This is | |
| #' for my ease of use | |
| #' | |
| #' @param df Dataframe to convert to latex | |
| df_to_table_body <- function(df, col_formatters = NULL) { | |
| # Figure out maximum length of each column (for pretty layout) | |
| max_col_lengths <- rep(0, times = ncol(df)) |
This file contains hidden or 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
| --- | |
| output: | |
| html_document: | |
| highlight: zenburn | |
| self_contained: false | |
| slim_css: TRUE | |
| css: [] | |
| theme: null | |
| --- |
This file contains hidden or 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
| library(did2s) | |
| library(data.table) | |
| data(df_hom) | |
| setDT(df_hom) | |
| # ---- Unit FE by Dummy Variables ---------------------------------------------- | |
| did2s(df_hom, | |
| yname = "dep_var", treatment = "treat", cluster_var = "unit", | |
| first_stage = ~ 0 | unit + year, |
Fast implementation of hatvalues.fixest. This calculates (or approximates) the diagonals of the projection matrix
library(sandwich)
# high-leverage observation #5
X = matrix(
c(
1, 1, 2,
1, 2, 3,
4, 5, 5,
1, 1, 1,
This file contains hidden or 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
| library(Matrix) | |
| #' Given list of matrices, form block sparse matrix | |
| #' | |
| #' @param mats List of sparse matrices `dgCMatrix`. | |
| #' | |
| #' @return A block diagonal `dgCMatrix` with each element of mats | |
| #' along the diagonal. | |
| blockDiagonal <- function(mats) { | |
| mats = lapply(mats, function(mat) { |
This file contains hidden or 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
| # https://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/ | |
| A = matrix(runif(1000 * 1000), nrow = 1000, ncol = 1000) | |
| b = matrix(runif(1000), ncol = 1) | |
| # Solve Ax = b | |
| max(solve(A, b) - solve(A) %*% b) | |
| #> [1] 8.537882e-11 | |
| # Benchmark | |
| bench::mark( |