Skip to content

Instantly share code, notes, and snippets.

View alexpghayes's full-sized avatar

alex hayes alexpghayes

View GitHub Profile
@alexpghayes
alexpghayes / one-two-ls-durbin-fits.md
Created February 28, 2024 19:02
One and two-stage least squares fits for spatial Durbin models
# pak::pak("gpiras/sphet")

library(fastRG)
#> Loading required package: Matrix
library(sphet)

# simulate from y = alpha + trt * gamma + G trt * delta + lambda G y

set.seed(26)
library(tidyverse)
library(mgcv)
#> Loading required package: nlme
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
#> This is mgcv 1.8-41. For overview type 'help("mgcv-package")'.
library(tidyverse)

p_at_least_one_event <- function(p_event, periods = 50) {
  tibble(
    time = 1:periods,
    p_at_least_one_event = 1 - (1 - p_event)^(1:periods)
  )
}
library(ggplot2)
data(mcycle, package = "MASS")
f_approxfun <- approxfun(mcycle$times, mcycle$accel)
f_splinefun <- splinefun(mcycle$times, mcycle$accel)
mcycle |>
ggplot(aes(times, accel)) +
geom_point() +
``` r
# pak::pkg_install("stillmatic/MNIST")
library(MNIST)
# 1 is the most common digit in train
table(MNIST::mnist_train$y) / nrow(MNIST::mnist_train)
#>
#> 0 1 2 3 4 5 6
#> 0.09871667 0.11236667 0.09930000 0.10218333 0.09736667 0.09035000 0.09863333
#> 7 8 9
library(tidyverse)
library(tidygraph)
library(netmediate)
data <- read.csv("final_regime.IGO.sanction.csv") |>
# remove some columns of missing data to avoid downstream issues & unnnecessary columns
select(-1, -AMCOW, -MOPAN, - year, -version)
node_data <- data |>
select(country, ccode, polity2, democracy, sanction) |>
library(tidyverse)
library(tidygraph)
library(netmediate)
data <- read_csv("final_regime.IGO.sanction.csv") |>
# remove some columns of missing data to avoid downstream issues
select(-1, -AMCOW, -MOPAN)
node_data <- data |>
select(country, ccode, polity2, democracy, sanction)
``` r
library(tidyverse)
library(glue)
library(unglue)
#> Warning: package 'unglue' was built under R version 4.2.3
"~/../Desktop/patty-restaurants.txt" |>
readLines() |>
unglue_data("- [{visited}] {name}") |>
arrange(visited, name) |>
# rotate Y to align it to X
mypro <- function(X, Y) {
XY <- crossprod(X, Y)
s <- svd(XY)
rotation <- s$v %*% t(s$u)
Yrot <- Y %*% rotation
diff <- X - Yrot
colwise_loss <- Matrix::colSums(diff^2)
two_infty_loss <- max(sqrt(Matrix::rowSums(diff^2)))
library(tidyverse)
library(qgam)
library(gratia)
library(furrr)
library(mgcv)
plan(multisession, workers = 5)
hap <- read_csv("https://osf.io/download/kpnjf/")