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 / is_numeric.md
Created January 27, 2021 14:49
if_numeric for database tables
Sys.setenv(PGHOST="192.168.1.192", PGUSER="igow", PGPORT=5434L, PGDATABASE="crsp")
library(dplyr, warn.conflicts = FALSE)
library(dbplyr, warn.conflicts = FALSE)

starwars_db <- tbl_memdb(starwars %>% select(-films, -vehicles, -starships))

is_numeric <- function(df) {
  df %>%
 collect(n = 1) %&gt;%
@iangow
iangow / averages.md
Last active January 8, 2021 04:48
Test averages
library(rvest)
#> Loading required package: xml2
library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(tidyr)

df <-
    read_html("https://stats.espncricinfo.com/ci/content/records/227046.html") %>%
    html_nodes("table") %>%
@iangow
iangow / ilink.md
Last active January 7, 2021 22:14
ilink.sas replication
@iangow
iangow / install_numpy.md
Last active May 18, 2021 09:27
Install numpy on M1 Mac

The following is what I needed to do to get Python going on my M1 Mac mini to the extent that I could run code from Chapter 2 of Introduction to Machine Learning with Python.

wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh
bash Miniforge3-MacOSX-arm64.sh
conda install scipy matplotlib ipython scikit-learn pandas pillow
conda install -c conda-forge jupyterlab   
@iangow
iangow / replicate_wrds_schema.ipynb
Last active November 22, 2020 00:52
Replicate WRDS schema
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@iangow
iangow / make_ibes.sql
Created November 22, 2020 00:00
Code to add foreign schema
DROP SCHEMA tr_ibes CASCADE;
CREATE SCHEMA tr_ibes;
ALTER SCHEMA tr_ibes OWNER TO ibes;
IMPORT FOREIGN SCHEMA tr_ibes
FROM SERVER wrds
INTO tr_ibes;
@iangow
iangow / pipit.sh
Created November 3, 2020 21:49
PyPI commands
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
@iangow
iangow / get_election_2016_data.R
Last active October 27, 2020 14:35
Code to get data for the 2016 US presidential election
library(rvest)
library(dplyr, warn.conflicts = FALSE)
library(dataverse)
library(readr)
library(tidyr)
Sys.setenv("DATAVERSE_SERVER" = "dataverse.harvard.edu")
tmp <- tempfile(fileext = ".tab")
f <- get_file("1976-2016-president.tab", "doi:10.7910/DVN/42MVDX")
@iangow
iangow / winsor.md
Last active October 15, 2020 20:04
Winsorizing in R to match SAS

Here is a SAS macro for winsorizing data that seems to be commonly used in accounting and finance research. This code comes from replication materials provided by Fang, Huang and Karpoff ("FHK") here. Below I provide an R function that produces equivalent results.

%MACRO winsorize(var, var_out, by_var, left, right, input, output);
	%LET var_number = 1; 
	%LET var&var_number = %QSCAN(&var, &var_number, %STR( ));
	%DO %WHILE (&&var&var_number NE);
		%LET var_number = %EVAL(&var_number + 1);
@iangow
iangow / get_ff_ind.md
Last active November 23, 2022 18:54
Get Fama-French industry data

Note that this requires dplyr, readr and tidyr packages to be installed.

library(dplyr, warn.conflicts = FALSE)
    
ff_ind_nums <- c(5, 10, 12, 17, 30, 38, 48, 49)

get_ff_ind <- function(num = 48) {
    t <- tempfile(fileext = ".zip")