Skip to content

Instantly share code, notes, and snippets.

View scrogster's full-sized avatar

Michael Scroggie scrogster

View GitHub Profile
@scrogster
scrogster / missing_data_in_JAGS.R
Created January 12, 2022 23:08
imputing missing values in JAGS
#simple missing data imputation
#1. simulate some fake data from a simple linear regression
beta0<-5
beta1<--2
sigma<- 2
n=100
x1<-runif(n, 0, 10)
y_bar<- beta0 +beta1*x1
y_obs<-rnorm(n, y_bar, sigma)
@scrogster
scrogster / logo_add.r
Last active April 13, 2021 11:38
Adding a png logo to an R plot
library(png)
library(RCurl)
#I got these free png silhouettes of red fox and rabbit from phylopic.org
foxurl<-"http://phylopic.org/assets/images/submissions/51b1b6e4-129d-41a6-bbbd-c3fab459c25f.1024.png"
raburl<-"http://phylopic.org/assets/images/submissions/1e15411c-5394-4a9d-a209-76c8ac0c331d.1024.png"
fox_logo <- readPNG(getURLContent(foxurl))
rab_logo <- readPNG(getURLContent(raburl))
#utility function for embedding png images at specified fractional sizes in R plots
#places the logo centred on a specified fraction of the the usr space,
@scrogster
scrogster / convert_align.R
Created April 20, 2018 14:33
Rmarkdown numbered equation helper
#I write papers and reports using Rmarkdown, knitting to pdf.
#My co-authors use word, so I usually knit a version to docx for them to edit.
#This mostly works flawlessly, except when I use \begin{align} and \end{align} to delimit display equations.
#Using \begin{align} and \end{align} results in nicely centered and numbered equations in pdf, BUT
#the equations don't render at all when I try to make a docx.
#As a quick hack so I can make a passable docx to share with my co-authors, the following script replaces all instances
#of \begin{align} and \end{align} with "$$". The modified version of the original Rmarkdown file knits to docx just fine,
# albeit without equation numbers.
#function to substitute vowels with Maori macrons into text strings in R
#there's probably a fancier way to do this, but it works OK.
#just precede vowels requiring a macron with '@' and the function will substitute the appropriate vowel with macron.
#Handy for graph labelling etc.
maorify<-function(x){
x<-gsub("@a","\u0101", x) #macron a
x<-gsub("@e","\u0113", x) #macron e
x<-gsub("@i","\u012B", x) #macron i
x<-gsub("@o","\u014D", x) #macron o