Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile
@kylebutts
kylebutts / make_mat.R
Created April 25, 2024 18:59
`make_mat()` function for converting vector of rows, cols, and values to a matrix
#' Create matrix from vectors for rows, columns, and values
#'
#' Note the rows and columns can be anything (e.g. strings containing fips).
#' Internally, these are efficiently created to an index using
#' `indexthis::to_index` (with values 1, ..., n_unique).
#'
#' @param i Vector used for the row indices.
#' @param j Vector used for the column indices.
#' @param x Vector used for the values.
#' @param names Logical. If column and row names should be used. These
@kylebutts
kylebutts / 0.md
Created April 15, 2024 16:36
Example random projections for regression
# %% 
library(wooldridge)
library(dqrng)
library(collapse)
#> collapse 2.0.10, see ?`collapse-package` or ?`collapse-documentation`
#> 
#> Attaching package: 'collapse'
#> The following object is masked from 'package:stats':
#> 
@kylebutts
kylebutts / simulation-hot_hand_ecta.jl
Created March 19, 2024 18:01
Simulation of "Surpised by the Hot Hand Fallacy" Econometrica
# %% [markdown]
# ---
# format: gfm
# ---
# %%
using StatsBase, Statistics
using Random
# %%
"""
@kylebutts
kylebutts / simulation-hot_hand_ecta.R
Created March 19, 2024 12:06
Simulation of "Surpised by the Hot Hand Fallacy" Econometrica
library(tidyverse)
# %%
simulation <- function(n, p, k) {
trials = purrr::map_dbl(1:100000, function(b) {
# Take 100 shots and record if basket is made
shots = as.numeric(runif(n) < p)
# Observe streaks
hot_hand_shot_results = c()
@kylebutts
kylebutts / apis.R
Created March 13, 2024 21:10
htmx + R example
library(htmltools)
#* @serializer html
#* @get /
base = function() {
html <- tags$html(
tags$head(
tags$script(src='https://unpkg.com/htmx.org@1.9.10/dist/htmx.js')
),
tags$body(
@kylebutts
kylebutts / tikz_ex.md
Last active January 24, 2024 16:17
Example of `kfbmisc::tikzsave`
library(ggplot2)
library(kfbmisc)

x = seq(-pi, pi, 0.02)
df = data.frame(x = x, y = sin(x))
p = ggplot(df) + 
  geom_line(aes(x = x, y = y), linewidth = 1.5) + 
  labs(x = "$x$", y = "$\\sin(x)$") + 
 scale_x_continuous(
#' ---
#' title: "Demo showing R code chunks, VSCode, and quarto support"
#' format: gfm
#' ---
#' This script shows how to use .Rmd chunks in a plain text file.
#' With new changes to vscode-R extension, this is seamlessly integrated
#' into the dev experience. Additionally, knitr::spin() or quarto
#' can be used to generate a report directly from the .R file.
@kylebutts
kylebutts / README.md
Created November 10, 2023 16:23
CR3 and CR3J vcov using `fixest::est_env()`
library(fixest)
library(dqrng)
library(tictoc) # For timing
data(trade, package = "fixest")

tic("Initial feols")
est = feols(Euros ~ dist_km | Destination + Origin, data = trade)
toc()
#> Initial feols: 0.023 sec elapsed
@kylebutts
kylebutts / ex.md
Created October 30, 2023 18:08
Empirical Bayes Baseball Example
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.1
theme_set(theme_bw())

# Sean 'Lahman' Baseball Database
library(Lahman)

data(Batting, package = "Lahman")
data(Pitching, package = "Lahman")
@kylebutts
kylebutts / reprex.md
Last active October 2, 2023 23:48
NBER style legend
library(ggplot2)

(p1 <- ggplot(mtcars) + 
  geom_point(aes(x = mpg, y = wt, color = as.factor(cyl))) + 
  labs(
    title = "Miles per gallon vs. weight", 
    subtitle = "Data from mtcars", 
    caption = "Source: mtcars", 
 color = NULL,