Skip to content

Instantly share code, notes, and snippets.

View iangow's full-sized avatar
🏠
Working from home

Ian Gow iangow

🏠
Working from home
View GitHub Profile
@iangow
iangow / get_vcovCL.md
Created May 12, 2021 19:02
Code for two-way cluster-robust standard errors
vcovCL <- function(fm, cluster) {
  M <- length(unique(cluster))
  u  <- apply(estfun(fm), 2, function(x) tapply(x, cluster, sum, na.rm = TRUE))
  M/(M-1) * sandwich(fm, meat = crossprod(u)/nobs(fm))
}

vcovCL2 <- function(fm, cluster1, cluster2 = NULL) {
  
 vc1 &lt;- vcovCL(fm, cluster1)
@iangow
iangow / drop_role.R
Created April 26, 2021 13:18
Some code to drop roles from PostgreSQL
library(DBI)
library(dplyr, warn.conflicts = FALSE)
pg <- dbConnect(RPostgres::Postgres(), dbname="crsp")
roles <- tbl(pg, sql("SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
@iangow
iangow / gym_rotation.R
Created April 5, 2021 12:40
Checks gymnastics rotations
library(googlesheets4)
library(dplyr, warn.conflicts = FALSE)
df <- read_sheet("1q4Pd6J4olH1qKfN1e0Hl7Oq_Zps8vD3WyXJ3Pw2D-OU")
roster <-
df %>%
inner_join(df, by = "Gymnast") %>%
filter(Order.y > Order.x) %>%
mutate(diff = Order.y - Order.x) %>%
@iangow
iangow / df_to_pg.md
Last active March 31, 2021 20:33
Illustration of function to "write" table to PosgreSQL in read-only transaction
library(dplyr, warn.conflicts = FALSE)
library(DBI)

pg <- dbConnect(RPostgres::Postgres(), bigint = "integer", sslmode='require')

url <- "https://github.com/tidyverse/dbplyr/files/6239289/temp.RDS.zip"
t <- tempfile(fileext = ".zip")
download.file(url, t)
temp <- readRDS(unzip(t))
@iangow
iangow / Chapter_2.ipynb
Last active April 3, 2025 19:46
Benchmark notebook
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@iangow
iangow / mult_permnos.md
Created February 14, 2021 20:35
multiple_permnos
library(dbplyr)
library(dplyr, warn.conflicts = FALSE)
library(DBI)
pg <- dbConnect(RPostgres::Postgres(), 
                bigint = "integer", 
                check_interrupts = TRUE)
ccmxpf_lnkhist <- tbl(pg, sql("SELECT * FROM crsp.ccmxpf_lnkhist")) 
stocknames <- tbl(pg, sql("SELECT * FROM crsp.stocknames"))
@iangow
iangow / ccm_links.md
Created February 13, 2021 00:43
Comparison of three CRSP-Compustat link tables
@iangow
iangow / state_changes.md
Last active February 11, 2021 17:41
R code to track companies' states over time
library(dplyr, warn.conflicts = FALSE)
library(dbplyr)
#> 
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#> 
#>     ident, sql
library(DBI)
library(ggplot2)
@iangow
iangow / trading_profits.md
Last active August 19, 2021 12:16
R code to calculate trading profits for insiders
library(dplyr, warn.conflicts = FALSE)
library(DBI)
library(parallel)

pg <- dbConnect(RPostgres::Postgres(), bigint = "integer")

table1 <- tbl(pg, sql("SELECT * FROM tfn.table1"))
stocknames <- tbl(pg, sql("SELECT * FROM crsp.stocknames"))
@iangow
iangow / by_tag_year.csv
Created February 4, 2021 23:57
Data for "Introduction to R" chapter of Financial Accounting Research.
We can't make this file beautiful and searchable because it's too large.
year,tag,number,year_total
2008,.htaccess,54,58390
2008,.net,5910,58390
2008,.net-2.0,289,58390
2008,.net-3.5,319,58390
2008,.net-4.0,6,58390
2008,.net-assembly,3,58390
2008,.net-core,1,58390
2008,2d,42,58390
2008,32-bit,19,58390