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 / walker_stats.R
Created September 19, 2024 07:41
Code to produce AUC stats from Walker's data
library(farr)
library(tidyverse)
original <- read_csv("~/Downloads/WalkerDataCodeMar2021/original.csv")
original |>
group_by(fyear) |>
summarize(auc = auc(prob, aaer), .groups = "drop") |>
mutate(avg_auc = mean(auc))
@iangow
iangow / other_packages.R
Created September 19, 2024 04:59
Other packages
install.packages(c("formatR", "markdown", "tinytex", "downlit", "xml2"))
@iangow
iangow / pq_to_pg.R
Last active September 19, 2024 00:02
Code to get a parquet file into PostgreSQL.
library(dplyr, warn.conflicts = FALSE)
library(DBI)
db <- dbConnect(duckdb::duckdb())
dbExecute(db, "ATTACH '' AS pg (TYPE POSTGRES);")
dbExecute(db, "COPY pg.crsp.dsf FROM '/Users/iangow/Library/CloudStorage/Dropbox/pq_data/crsp/dsf.parquet'")
create_view <- function(conn, table, schema = "",
@iangow
iangow / get_players.R
Created September 6, 2024 04:25
Code to get player lists from AFL tables
library(httr2) # request(), req_*(), resp_body_html()
library(rvest) # html_elements(), html_table()
library(tidyverse)
extract_team_ids <- function(link_text) {
matches <- str_match(link_text, '<a href="teams/(.*)_idx.html">(.*)</a>')
team_id <- matches[, 2]
team_name <- matches[, 3]
tibble(team_id, team_name)
}
@iangow
iangow / nnhs.md
Created July 3, 2024 15:19
Destinations
library(tidyverse)
library(DBI)
library(googlesheets4)

options(gargle_oauth_email = TRUE)

gs_2024 <- as_sheets_id("1Qymhw_LGN1Z00ip_qswa29yPeZzWvTymD-u-lMY8b-A")

nnhs_2024 &lt;- read_sheet(gs_2024, na = c("", "N/A"))
@iangow
iangow / summ_table.md
Last active June 19, 2024 08:46
Illustration of a function to make summary tables with totals
library(tidyverse)

regions <- c("USA", "Australia", "China")
years <- 2017:2023L
markets <- letters[1:4]

nn <- 1000
sample_n <- function(var, n = nn) {
  var <- rlang::ensym(var)
@iangow
iangow / port_sorts.md
Created June 9, 2024 21:32
Another version of `port_sorts.qmd`.

Fast Portfolio Sorts Revisited

library(tidyverse)
library(dtplyr)
library(data.table)
library(bench)
library(DBI)
@iangow
iangow / port_sorts.md
Created June 9, 2024 13:35
DuckDB/parquet version of Tidy Finance benchmark.

Fast Portfolio Sorts with DuckDB/parquet

Load a few packages. Here farr provides system_time().

library(tidyverse)
library(DBI)
library(farr)
@iangow
iangow / fig-alt-code.qmd
Created April 26, 2024 18:21
Code to produce a Word document detailing alternative text
---
title: "Alternative text for figures"
author: Ian D. Gow
date: 2024-04-25
date-format: "D MMMM YYYY"
bibliography: book.bib
format: docx
---
```{r}
@iangow
iangow / get_libraries.R
Created February 8, 2024 21:55
Small script to identify needed packages
library(stringr)
library(dplyr)
path = getwd()
file_list <-
tibble(full_path = list.files(path, full.names = TRUE)) |>
filter(str_detect(full_path, "\\.(qmd)$"))
get_libraries <- function(file) {