Skip to content

Instantly share code, notes, and snippets.

@Templier
Templier / extractmerTables.r
Created August 18, 2017 17:40 — forked from russellpierce/extractmerTables.r
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
@Templier
Templier / pairedSamplest-testEffectSize.rmd
Created August 18, 2017 17:40 — forked from russellpierce/pairedSamplest-testEffectSize.rmd
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
@Templier
Templier / errorInSSCalcDueToRounding.r
Created August 18, 2017 17:40 — forked from russellpierce/errorInSSCalcDueToRounding.r
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()))
@Templier
Templier / athena.R
Created August 18, 2017 17:39 — forked from russellpierce/athena.R
Connect to AWS Athena using R (with the option to use IAM credentials)
#repsych is on github and is here only for the glibrary idiom
library(repsych)
#install and load the following packages
glibrary(whisker, lubridate, magrittr, rappdirs, awsjavasdk, rJava)
if (!aws_sdk_present()) {
install_aws_sdk()
}
load_sdk()
@Templier
Templier / send_slack_message.R
Created August 18, 2017 17:39 — forked from russellpierce/send_slack_message.R
Use Zapier to Send a Slack Message
#' Send a message to Slack
#'
#' To use, set options(zapier_slack_webhook="yourZapierPostWebhookHere")
#'
#' @param message
#' @param channel
#' @param bot_name
#' @param bot_icon_url
#' @param image_url
#' @param emoji
@Templier
Templier / dropLeading.r
Created August 18, 2017 17:39 — forked from russellpierce/dropLeading.r
dropLeading
#drops all numbers prior to the decimal place
dropLeading <- function(num) {
sub("^0\\.", ".",as.character(num))
}
@Templier
Templier / getPlottedVals.r
Created August 18, 2017 17:39 — forked from russellpierce/getPlottedVals.r
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))
@Templier
Templier / summarizeComments.js
Created August 18, 2017 17:39 — forked from russellpierce/summarizeComments.js
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({
@Templier
Templier / forkable_JDBC.R
Created August 18, 2017 17:39 — forked from russellpierce/forkable_JDBC.R
Connect to a JDBC db in R via a single node cluster
# Very raw dump of prototype code that allows for connection to a JDBC db via a ad-hoc one node cluster. This demo code was designed to connect to redshift using Amazon's JDBC drivers - because sometimes you just want to do things the hard way
# To make this work, you'll need to update the classPath, driverPath, and of course, the host settings.
#library(plyr); needs to be available, but doesn't need to be attached
library(whisker)
library(R6)
library(logging)
library(R.utils) #tempvar
library(parallel) #makeCluster and friends
@Templier
Templier / parallelRDS.R
Created August 18, 2017 17:39 — forked from russellpierce/parallelRDS.R
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")