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 / wavelength_to_rgb.R
Created October 24, 2016 19:50
Convert wavelength of color to RGB
#' Wavelength to RGB
#'
#' This function converts a given wavelength of light to an
#' approximate RGB color value.
#'
#' @param wavelength A wavelength value, in nanometers, in the human visual range from 380 nm through 750 nm.
#' These correspond to frequencies in the range from 789 THz through 400 THz.
#' @param gamma The \eqn{\gamma} correction for a given display device. The linear RGB values will require
#' gamma correction if the display device has nonlinear response.
#' @return a color string, corresponding to the result of \code{\link[grDevices]{rgb}} on the
@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 / 3d-demo.R
Created September 30, 2016 14:58
3D demonstrations of linear transformations and matrix inverse
#' ---
#' title: "3D demonstrations of linear transformations and matrix inverse"
#' author: "Michael Friendly"
#' date: "30 Sep 2016"
#' ---
#' Start with a unit cube, representing the identity matrix. Show its transformation
#' by a matrix $A$ as the corresponding transformation of the cube.
#'
#' This also illustrates the determinant, det(A), as the volume of the transformed
@friendly
friendly / minard-plot.R
Last active April 7, 2022 11:30
Reproduce Minard's March on Moscow Graphic using ggplot2
#' # Reproduce Minard's March on Moscow Graphic using ggplot2
#' This gist extends the examples given in the HistData package. It explicitly
#' shows the construction of the plot as layers, and adds the plot of temperature
#' vs. longitude below the plot of troop strength.
#' ## Load data and required packages
data(Minard.troops, package="HistData")
data(Minard.cities, package="HistData")
data(Minard.temp, package="HistData")