Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile
@kylebutts
kylebutts / installation.md
Last active March 10, 2025 12:50
Dev Build of Positron

Dev

  1. Download the files with gh repo clone posit-dev/positron in some directory
  2. npm i from the main directory to install packages
  3. npm run watch from the main directory to compile client and extensions (watch-client and watch-extensions). This will run continuously in the background, so you need a second terminal window

Running tests

Run tests with yarn test-extension -l EXTENSION

  • Add -g "" to filter tests by a regex string (grep).
  • The -l flag seems to only work with yarn for some reason.
library(tidyverse)
library(spatstat)
#> Loading required package: spatstat.data
#> Loading required package: spatstat.univar
#> spatstat.univar 3.1-1
#> Loading required package: spatstat.geom
#> spatstat.geom 3.3-5
#> Loading required package: spatstat.random
#> spatstat.random 3.3-2
@kylebutts
kylebutts / Poisson_Limit_Theorem.md
Created February 18, 2025 00:56
Simulations showing the Poisson Limit Theorem

Law of Rare Events / Poisson Limit Theorem

This famous theorem states that when you have:

  • A large number of independent trials ($n \to \infty$)
  • Each trial has a small probability of success ($p \to 0$)
  • The expected number of successes ($\lambda = np$) remains constant

$\implies$ the number of successes $X$ follows a Poisson distribution with parameter $\lambda$

@kylebutts
kylebutts / log.do
Created October 26, 2024 12:14
`pyfixest` #672
* https://github.com/py-econometrics/pyfixest/issues/672
. use "~/Downloads/census2000_5pc.dta", clear
(5% Extract from 2000 US Census (from Abadie et al., 2023))
.
. // set 1
. b1x2 ln_earnings, x1all(educ) x2all(hours)
Number of obs = 2632838
@kylebutts
kylebutts / 0.md
Created October 4, 2024 12:18
Plot multiple columns of a data frame and have a legend
library(ggplot2)
beta <- seq(-0.2, 0.2, by = 0.001)
exp_minus_1 <- exp(beta) - 1
(plot_pct_change_appxoimation <- ggplot() +
  geom_line(
 aes(x = beta, y = beta, color = "approx"),
@kylebutts
kylebutts / 0.md
Created September 16, 2024 13:46
Moving Hawaii and Alaska onto the US map
# Modified to use `sf` from this article:
# https://rud.is/b/2014/11/16/moving-the-earth-well-alaska-hawaii-with-r/
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tigris)
#> To enable caching of data, set `options(tigris_use_cache = TRUE)`
#> in your R script or .Rprofile.
library(rmapshaper)
library(ggplot2)
@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