Skip to content

Instantly share code, notes, and snippets.

View shalizi-quick-sim.R
library(Matrix)
library(tidyverse)
# need these two packages that are not on CRAN
# if something goes wrong, let me know, i wrote them and will fix
# to install:
#
# > install.packages("pak")
# > pak::pkg_install("RoheLab/fastRG")
View multinomial-partial-dependence-for-anna.R
library(nnet)
library(pdp)
library(tidyverse)
library(broom)
augment.multinom <- function(object, newdata) {
newdata <- as_tibble(newdata)
class_probs <- predict(object, newdata, type = "prob")
View subspace_distance.R
``` r
set.seed(27)
n <- 100
k <- 5
A <- matrix(rnorm(n * n), nrow = n, ncol = n)
s <- svd(A, k, k)
# generate two n x k orthonormal bases
View date-interval-thing.R
``` r
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
# As an example, let's say I have a dataframe of birthdays:
View furrr-rsplit-hell.R
library(tidyverse)
library(here)
library(skimr)
library(lubridate)
library(rsample)
library(furrr)
library(arrow)
plan(sequential)
plan(multisession, workers = parallel::detectCores() - 1)
View kruger-implied-regression.R
library(tidyverse)
regress <- function(x_bar, y_bar, s_x, s_y, rho, n, alpha = 0.05) {
beta_hat <- rho * s_y / s_x
alpha_hat <- y_bar - beta_hat * x_bar
ssr <- (1 - rho^2) * s_y^2 * (n - 1)
sigma_sq_hat <- ssr / (n - 2)
View katharine-snippet.
d_trial_only <- discrim %>%
select(contains("d_trial")) %>%
as.matrix()
pos <- max(d_trial_only, 0) # might need to use pmax instead of max
discrim$score <- colSums(pos)
View matrix-to-seriate.R
x <- structure(c(-0.000380334706954992, -2.44590466188477e-06, 3.04119377729188e-05,
-1.21695609451638e-05, -0.000166346113348224, -0.000182613611523365,
0.000315614668722388, -1.68796521972763e-05, -8.94153911270233e-05,
-3.61775757252454e-05, 4.55131652127818e-05, 3.35394483493792e-05,
-4.01546825890245e-05, -0.000298022226648099, 4.73504524881937e-05,
-0.000136942519443143, 0.000176140638158973, 0.00013546140743366,
-9.91744212417299e-05, -7.39274572106565e-05, -3.80026989391578e-05,
1.96661709232902e-05, 1.06148384569098e-05, 0.000266761732350429,
-9.84713447945633e-05, -0.000104111311719871, 4.46840881651868e-08,
1.98555791286171e-05, -4.51081398420943e-06, 3.01691284341858e-05,
View left-padded-integer-sequence.R
left_padded_sequence <- function(x) {
original <- withr::with_options(
c(scipen = 999),
as.character(x)
)
max_digits <- max(vapply(original, nchar, integer(1)))
formatC(x, width = max_digits, format = "d", flag = "0")
}
View varimax-log-log-scaling.R
library(scales)
y_long <- fa %>%
get_varimax_y() %>%
pivot_longer(
names_to = "factor",
cols = contains("y"),
values_to = "loading"
)