Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile
@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(
@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