Skip to content

Instantly share code, notes, and snippets.

@bvancil
bvancil / compositional-pca.qmd
Created March 28, 2023 19:59
Compositional PCA
---
title: "Compositional PCA"
---
## Problem
From [https://sciencemastodon.com/\@Alex_Koiter/109909545205270622](https://sciencemastodon.com/@Alex_Koiter/109909545205270622), "Question for the [#soil](https://fosstodon.org/tags/soil) and [#stats](https://fosstodon.org/tags/stats) peeps. Can you include the percent sand, silt, and clay in to a PCA? The reason I am asking is that the three variables always sum to 100% My lurking on stack exchange etc seems to suggest that it is ok, but some how it doesn't feel right."
Let's check this for three variables. We don't have a proof but suspect different eigenvalues to $X^TX$ . Let's try to shoot for a negative case.
@bvancil
bvancil / as_tribble.R
Last active November 12, 2022 15:01
copy or print a dataframe as tribble code
as_tribble_string <- function(df, delim = "|") {
headers <- df |>
names() |>
purrr::map(as.name) |>
paste0(a = "~", b = _, collapse = ", ")
# write contents to read back
output_file <- tempfile()
readr::write_delim(
df,
file = output_file,
@bvancil
bvancil / telescoping.R
Last active July 10, 2021 16:28
a-telescoping discrete distribution
#' Generate random numbers from an \eqn{a}-telescoping
#' discrete distribution
#'
#' Let \eqn{a(n)} be a sequence (a function defined on the
#' natural numbers) that is non-decreasing and such that
#' \eqn{a(0)=1}. Then the a-telescoping discrete
#' distribution has a probability mass function (PMF) given
#' by
#'
#' \deqn{p(n) = \frac{1}{a(n)} - \frac{1}{a(n+1)}}
@bvancil
bvancil / spiralaterals.py
Created June 30, 2020 21:23
Python code to try on trinket.io to draw spiralaterals
import turtle
def draw_list(tortuga, ns, step = 30, angle = 90):
tortuga.home()
while True:
for n in ns:
tortuga.forward(n * step)
tortuga.right(angle)
if tortuga.position() == (0, 0):
@bvancil
bvancil / map_seq_int.R
Last active May 7, 2020 00:10
purrr::map includes all arguments from a builtin when using purrr::as_mapper making .f complain about missing arguments
library(purrr)
library(rlang)
# "closure"s work for .f in map()
print(typeof(seq))
print(map(c(1L, 3L), seq, to = 5L, by = 1L))
# "builtin"s do not work for .f in map()
print(typeof(seq.int))
# Generates an error:
# Error in .f(.x[[i]], ...) :
@bvancil
bvancil / Status: Primary Language Spoken at Home - Other.c
Last active June 8, 2017 17:37
Status: Primary Language Spoken at Home - Other
IF [Que21 Primary Language Spoken A 2034] = "8" THEN // Other
IIF(ISNULL([Que22 Other 2035]), "Missing", "Completed")
END
@bvancil
bvancil / index.html
Last active June 29, 2016 20:45
Pixel Programmer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pixel Programmer</title>
</head>
<body>
<div>
<div id="pixel"></div>
@bvancil
bvancil / README.md
Created September 28, 2015 19:03
OMG Particles!
@bvancil
bvancil / ProductGrabber.js
Created May 28, 2015 13:01
Product Grabber Bookmarklet for E-Commerce Website ArborSci.com
javascript: (function(e, a, g, h, f, c, b, d) {
if (!(f = e.jQuery) || g > f.fn.jquery || h(f)) {
c = a.createElement("script");
c.type = "text/javascript";
c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
c.onload = c.onreadystatechange = function() {
if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
h((f = e.jQuery).noConflict(1), b = 1);
f(c).remove()
}
@bvancil
bvancil / markovChainFun.html
Last active August 29, 2015 14:20
Markov Chain Fun
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Markov Chain Fun</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style type="text/css">