Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile
@kylebutts
kylebutts / febinsreg.R
Last active September 1, 2024 08:51
Learning internals of `binsreg`
# %%
library(tidyverse)
library(fixest)
library(binsreg)
library(patchwork)
set.seed(20240829)
x <- runif(500)
w <- rnorm(n = 500, mean = x, sd = 1)
y <- sin(x) + w * 2 + rnorm(500, mean = 0, sd = 1)
@kylebutts
kylebutts / 0.md
Last active August 27, 2024 20:15

This code shows the problem of post-selection inference following the review article Post-Selection Inference

library(tidyverse)
library(fixest)

Data generation process:

  • $X = (X_1, X_2, X_3)'$ is multi-variate normal with non-diagonal covaraince
% https://brand.uark.edu/graphic-identity/official-colors.php
\definecolor{cardinal_red}{HTML}{9D2235}
\definecolor{apple_blossom}{HTML}{FFFFFF}
\definecolor{quartz}{HTML}{F2F2F4}
\definecolor{gray_squirrel}{HTML}{C7C8CA}
\definecolor{spoofers_stone}{HTML}{424242}
\definecolor{black_whetstone}{HTML}{000000}
\definecolor{diana_butterfly}{HTML}{2B5269}
\definecolor{ozark_mountains}{HTML}{3F7F7F}
\definecolor{birdsfoot_violet}{HTML}{2F1332}
@kylebutts
kylebutts / _about.md
Last active July 7, 2024 21:40
spin v2

I love knitr::spin() and code cells for my dev experience. However, there are a few edge-cases that made the function insufficient.

  1. #' roxygen-style documentation would create problems since they would be interpreted as markdown.
  2. With ark, jupytext style notebooks will be advantageous and as such, supporting # %% [markdown] would be beneficial.
  3. If # %% is on the start of a line in a string, this would cause problems (unlikely, but still)

This function streamlines this proceess by rewriting the code from first-principles:

  1. The function parses the source code using tree-sitter (see history for using R's parse function)
  2. The code is iterated line-by-line and uses a state-machine to properly parse everything.

You can see a demo with temp.R which I've intentionally writen to highlight difficulties that knitr::spin has

@kylebutts
kylebutts / attach_vcov_ex.md
Created May 15, 2024 16:15
"Attach" vcov to fixest object
library(fixest)
library(sandwich)

est <- feols(mpg ~ hp + i(cyl) | am, mtcars, vcov = "hc1")
vcov_bs <- sandwich::vcovBS(est, R = 500)

### attach new vcov to est
est_bs <- summary(est, vcov = vcov_bs)
###
@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(