Skip to content

Instantly share code, notes, and snippets.

@friendly
friendly / diabetes-pca-tsne.R
Created April 3, 2024 13:21
Animation of PCA vs. tSNE dimension reduction
#' ---
#' title: Animate transition from PCA <--> tsne
#' ---
# idea from: https://jef.works/genomic-data-visualization-2024/blog/2024/03/06/akwok1/
#' ## Load packages and data
library(ggplot2)
library(gganimate)
library(Rtsne)
library(patchwork)
@friendly
friendly / measerr_demo.R
Created November 17, 2023 18:09
Measurement error demonstration
#' Demonstrate Effect of Measurement Error in Regression
#'
#' This function takes a variables x and y and demonstrates the effect of adding random normal errors
#' to one or both of the coordinates. For each level of \code{err}, it adds \code{rnorm(0, err * SD)}
#' to the corresponding x and/or y and displays the data points, a data ellipse and the fitted
#' regression line overlaid on the corresponding plot of the un-perturbed data.
#'
#' My thought was to make this general, to be used either in an interactive demonstration or an animated
#' graphic using the \pkg{animate} package. There is something wrong with my logic, because it doesn't
#' work for an interactive demo.
@friendly
friendly / use_data_doc.R
Created September 21, 2023 15:43
Generate Outline Documentation for a Data Set in Roxygen Format
#' Generate Outline Documentation for a Data Set in Roxygen Format
#'
#' Generates a shell of documentation for a data set or other object in roxygen format.
#' This function was created by editing \code{\link[utils]{promptData}} to replace
#' the old style \code{.Rd} formatting with code suitable for processing with \code{\link[devtools]{document}}.
#'
#' @details
#' Unless \code{filename} is \code{NA}, a documentation shell for \code{object} is written to the file specified
#' by \code{filename}, and a message about this is given.
#'
@friendly
friendly / Rpackages_bib.R
Created June 14, 2023 20:37
Generate a BibTeX file of all installed packages
#' Generate a BibTeX file of all installed packages
#'
#' This function finds all packages installed in your R library and calls
#' `citation()` for each, collecting the BibTeX entries and writing these to
#' a file. It handles situations where there are multiple citations for a package.
#'
#' @param filename Name of the .bib file to write to.
#' @param header Include a header giving `sessionInfo()` ?
#' @param preamble Preamble for the BibTeX file
#' @param prefix String to prefix the in the bibtex key
@friendly
friendly / bib-at-end.Rmd
Created June 14, 2023 15:28
Accumulating R package references in a Quarto book
# Accumulating R package references in a Quarto book
In writing a book using Quarto, I was looking for a way to automatically
accumulate the packages used in each chapter and write them out to a `pkgs.bib`
file at the end. (I posted this as a Posit Community question,
https://community.rstudio.com/t/how-to-accumulate-packages-bib-used-in-a-quarto-book/167619 )
I can easily do this with a `knitr` vignette, where the essence
is to include a chunk at the end,
@friendly
friendly / term_info.R
Last active February 12, 2023 16:36
Extract information about two-way terms in a loglinear model
#' Extract information about two-way terms in a loglinear model
#'
#' This function is designed for use with an association graph showing the pairwise
#' dependencies among categorical variables in a loglinear model for frequencies
#' fit using \code{\link[MASS]{loglm}} or \code{\link[stats]{glm}} in a poisson family.
#' The goal is to show
#' an association graph diagram with edges indicating the strength of associations
#' between each pair of variables.
#'
#' The weight for two-way terms can be found using the deviance or LRT or AIC statistic
@friendly
friendly / test-newcommand.html
Created August 15, 2022 18:23
Quarto: Test LaTeX newcommands in HTML doc
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.0.37">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Quarto: Test LaTeX newcommands in HTML doc</title>
@friendly
friendly / clark-lord-paradox.R
Created February 7, 2022 19:08
Demonstrate Lord's Paradox example, following Michael Clarke
# Demonstrate Lord's Paradox example, following Michael Clarke
# https://m-clark.github.io/docs/lord/index.html
library(ggplot2)
library(dplyr)
#' ## Data example: Initial and final weight for boys & girls
#'
#' Make a plot showing the distribution of gain scores perpendicular to the line of slope = 1
#'
@friendly
friendly / phys-models.bib
Created November 28, 2020 17:49
Bibliography for physical analogs of statistical models
@comment{Physical analogs for statistical models}
@article{GainesPalphrey:1932,
ISSN = {00368075, 10959203},
URL = {http://www.jstor.org/stable/1658643},
author = {W. L. Gaines and J. R. Palfrey},
journal = {Science},
number = {1977},
pages = {472--474},
publisher = {American Association for the Advancement of Science},
@friendly
friendly / PartySupport-2019.R
Created November 1, 2019 19:43
Plots of Canadian 2019 election results
# Stacked Barplot of Support for Political Parites by Province in 2019 Canadian Federal Election
# written by Christopher D. Green
# 22 October 2019
# additions by MF 31 Oct 2019
provs<-c("BC","AB","SK","MB","ON","QC","NB","NS","PE","NF") # names of the provinces
pops<-c(5.0,4.3,1.2,1.4,14.4,8.4,0.8,1.0,0.2,0.5) # populations in millions
con<-c(34.1,69.2,64.3,45.4,33.2,16.0,32.8,25.7,27.4,28.0) # % support for Conservatives
lib<-c(26.1,13.7,11.6,26.3,41.5,34.2,37.6,41.3,43.6,44.7) # % support for Liberals