Skip to content

Instantly share code, notes, and snippets.

View russellpierce's full-sized avatar

Russell S. Pierce russellpierce

View GitHub Profile
#The base version on 9/23/2013 still dies on WUA_yldata_for TRB_contest Sep 10 2013.xlsx as a file name
xlsxToR <- function(file, keep_sheets = NULL, header = FALSE) {
require(XML)
require(plyr)
require(pbapply)
suppressWarnings(file.remove(tempdir()))
file.copy(file, tempdir())
new_file <- list.files(tempdir(), full.name = TRUE, pattern = basename(file))
@russellpierce
russellpierce / errorInSSCalcDueToRounding.r
Created July 31, 2013 00:11
What is the error in SS when rounding takes place prior to the calculation? A simulation to address a comment here: http://stats.stackexchange.com/questions/65220/exception-for-sum-of-deviations-from-mean-being-0/65232#comment127480_65232
sim <- function(places=3,N=20,FUN=round,...) {
SSr <- function(x) {sum((FUN(x,places)-mean(FUN(x,places)))^2)}
SS <- function(x) {sum((x-mean(x))^2)}
y <- rnorm(N,...)
return(abs(SSr(y)-SS(y)))
}
mean(replicate(20000,sim()))
@russellpierce
russellpierce / pairedSamplest-testEffectSize.rmd
Created August 1, 2013 14:40
Describing the relationship between t, r, and F for paired samples t-tests and repeated measures ANOVA with two factor levels.
# Paired Sample t-tests: Yes, Virginia there is an effect size for that!
There are a plethora of options, Cohen’s d, a tweaked version of Glass’ $\Delta$ (1976): $\Delta_{RM}$ (Gibbons et al., 1993), dIGPP (Becker, 1988), generalized $\eta^2$ (Bakeman, 2005; Olejnik & Algina, 2003), and r (which in this context is a square rooted alias for partial eta squared). Of these I prefer generalized $\eta^2$ or r. Why r I hear you say. I’ve never seen r used before in this context.
A paired samples t is just a one sample t-test of the differences. The advantage of using r is that it is well known and amenable to meta-analysis. The interpretation does take a couple steps to think through, but I thought I’d try to help point you in the right direction. You’ll recall that a paired samples t-test reduces to a within samples ANOVA with 1 df in the numerator. Just as r is the square root of the proportion explained, so too is r (in this context) the square root of the proportion of variance explained by estimatin
@russellpierce
russellpierce / cloneFolder.vbs
Last active January 22, 2021 14:22
Visual basic script to clone directories using robocopy as a back-end; code adapted from code originally developed by Rob van der Woude; Modification requires that grep be available at the launch location (earlier version does not: https://gist.github.com/drknexus/6132223/693faf00fa6a7a228a176c52446dc600f44f0aa0)
Option Explicit
Dim SD
Dim DD
Dim CMD, CMDRUN, CMDRUNPersist
Dim oShell, oShellOut
Dim Resp
Set oShell = WScript.CreateObject("WScript.Shell")
SD = BrowseFolder( "%userprofile%\documents", True, "Select source directory")
DD = BrowseFolder( "%userprofile%\documents", True, "Select destination directory")
@russellpierce
russellpierce / extractmerTables.r
Last active August 18, 2017 17:40
Several accessor functions for pulling out data of interest from mer objects. Not tested, not generalizable. Not safe.
#will have to decide about the html later.
#probably need to make digits work in a sane sort of way.
fixedEffectFrame <- function(x,digits=c(2,2,2)) {
data.frame("Coefficient"=round(fixef(x),digits[1]),"Std. Error"=round(sqrt(diag(vcov(x))),digits[2]),"t value"=abs(round(fixef(x)/sqrt(diag(vcov(x))),digits[3])),check.names=FALSE)
}
varcortau <- function(lme4.model,groupLevel="WAISID",digits=2) {
model.var <- VarCorr(lme4.model)[[groupLevel]]
model.var[lower.tri(model.var)] <- attr(model.var,"correlation")[lower.tri(attr(model.var,"correlation"))]
REmat <- summary(lme4.model)@REmat
@russellpierce
russellpierce / summarizeComments.js
Last active August 18, 2017 17:39
Summarize comments for Adobe PDF
//Modified from: http://forums.adobe.com/message/4496260#4496260 to list comments in page number order
//Open PDF
//Control-J
//Control-A (select the junk already in the console)
//Control-V (paste in code)
//Control-A
//Control-Enter
//It will freeze for a while while it does the work
this.syncAnnotScan();
var a = this.getAnnots({
@russellpierce
russellpierce / getPlottedVals.r
Last active August 18, 2017 17:39
Get all values along a given axis from a ggplot2 object (including layers).
# Get all variables plotted along a given axis in a ggplot.
getPlottedVals <- function(ggplotObj,axis=c("x","y"),includeLayers=TRUE)
{
if (is.null(axis)) {stop("In getPlotted: Argument axis not specified.")}
getValAxis <- function(.q,axis=c("x","y")) {
as.data.table(.q$data)[,eval(.q$mapping[[axis]])]
}
getLayerAxis <- function(tmpLayer,axis=c("x","y")) {
if (!any(class(tmpLayer$data) %in% "waiver") & (any(class(tmpLayer$data)=="data.frame"))) {
return(getValAxis(tmpLayer,axis))
@russellpierce
russellpierce / dropLeading.r
Created September 15, 2013 13:42
dropLeading
#drops all numbers prior to the decimal place
dropLeading <- function(num) {
sub("^0\\.", ".",as.character(num))
}
@russellpierce
russellpierce / parallelRDS.R
Created May 29, 2015 09:05
Provided the right tools are installed, i.e. xz and pigz, will offload the compression handling to an external program and leave R free to do the data import. This ends up being quite a bit more efficient for large files. Some tweaks may be needed for operating systems other than Ubuntu; there may be additional dependencies on the github repo dr…
library(parallel)
saveRDS.xz <- function(object,file,threads=parallel::detectCores()) {
pxzAvail <- any(grepl("(XZ Utils)",system("pxz -V",intern=TRUE)))
if (pxzAvail) {
con <- pipe(paste0("pxz -T",threads," > ",file),"wb")
base::saveRDS(object, file = con)
close(con)
} else {
saveRDS(object,file=file,compress="xz")
@russellpierce
russellpierce / require_package_version_github.R
Last active August 18, 2017 17:35
Guarantee that the required version (or higher) of a package is installed and load it; otherwise get from github
#' Require and install package version
#'
#' Guarantee that the required version (or higher) is installed.
#' This function will install from github in the desired version isn't found on CRAN or the current machine version is insufficient.
#'
#' @param packageName A quoted string, e.g. 'lubridate'
#' @param githubLoc e.g., 'hadley/lubridate'
#' @param requiredVersion e.g. '1.4'
#'
#' @return boolean ; TRUE if successful, FALSE otherwise