Skip to content

Instantly share code, notes, and snippets.

@jepusto
jepusto / Simulate Welch t-test
Last active December 29, 2015 14:49
A simulation evaluating the confidence interval coverage for a difference in means, allowing for unequal variances, based on Welch's approximation for the degrees of freedom. The code demonstrates the basic structure of a simulation study in R.
#----------------------------------------------
# data-generating model
#----------------------------------------------
two_group_data <- function(iterations, n, p, var_ratio, delta) {
Group <- c(rep("C", n * p), rep("T", n * (1 - p)))
Y_C <- matrix(rnorm(iterations * n * p, mean = 0, sd = 1), n * p, iterations)
Y_T <- matrix(rnorm(iterations * n * (1 - p), mean = delta, sd = sqrt(var_ratio)), n * (1 - p), iterations)
dat <- data.frame(Group, rbind(Y_C, Y_T))
return(dat)
@jepusto
jepusto / Simulate Welch t-test in parallel
Last active December 31, 2015 23:28
An adaptation of the Welch t-test simulation, for running in parallel on the Stampede server of the Texas Advanced Computing Cluster.
#----------------------------------------------
# data-generating model
#----------------------------------------------
two_group_data <- function(iterations, n, p, var_ratio, delta) {
Group <- c(rep("C", n * p), rep("T", n * (1 - p)))
Y_C <- matrix(rnorm(iterations * n * p, mean = 0, sd = 1), n * p, iterations)
Y_T <- matrix(rnorm(iterations * n * (1 - p), mean = delta, sd = sqrt(var_ratio)), n * (1 - p), iterations)
dat <- data.frame(Group, rbind(Y_C, Y_T))
return(dat)
@jepusto
jepusto / RVE-hierarchical.R
Last active August 29, 2015 14:00
Hierarchical effects example from Tanner-Smith & Tipton (2013)
# robumeta calculations
library(grid)
library(robumeta)
data(hierdat)
HTJ <- robu(effectsize ~ males + binge,
data = hierdat, modelweights = "HIER",
studynum = studyid,
var.eff.size = var, small = FALSE)
@jepusto
jepusto / metafor-sandwich.R
Created April 21, 2014 14:16
Calculate sandwich covariance estimators for multi-variate meta-analysis models
require(Formula)
require(metafor)
require(sandwich)
require(zoo)
require(lmtest)
#-----------------------------------------------
# Functions for making sandwich standard errors
#-----------------------------------------------
@jepusto
jepusto / RVE-correlated.R
Created April 21, 2014 16:10
Correlated effects example from Tanner-Smith & Tipton (2013)
# robumeta calculations
library(grid)
library(robumeta)
data(corrdat)
rho <- 0.8
HTJ <- robu(effectsize ~ males + college + binge,
data = corrdat,
modelweights = "CORR", rho = rho,
studynum = studyid,
@jepusto
jepusto / metafor-BRL.R
Created April 25, 2014 20:33
Bias-reduced linearization covariance estimator and degrees of freedom for multi-variate meta-analysis models
require(Formula)
require(metafor)
require(sandwich)
require(zoo)
require(lmtest)
#-----------------------------------------------
# Identify outer-most clustering variable
#-----------------------------------------------
@jepusto
jepusto / Austin crashes - annual data figures
Last active August 29, 2015 14:27
Code for figures displaying annual number of fatal automobile crashes, fatalities, etc. in Austin and Travis County, 2003-2015
library(tidyr)
library(dplyr)
library(stringr)
library(ggplot2)
#--------------------------------
# format the data for graphing
#--------------------------------
crash_dat <- read.csv("http://blogs.edb.utexas.edu/pusto/files/2015/08/Yearly_crash_data_Austin_and_Travis_County.csv")
library(tidyr)
library(dplyr)
library(stringr)
library(ggplot2)
cities_select <- c("HOUSTON","SAN ANTONIO","DALLAS","AUSTIN","FORT WORTH","EL PASO")
#----------------------------------------
# get population estimates
#----------------------------------------
# Demonstrate the problem with gls model
library(nlme)
data(Ovary)
gls_raw <- gls(follicles ~ sin(2*pi*Time) + cos(2*pi*Time), data = Ovary,
correlation = corAR1(form = ~ 1 | Mare),
weights = varPower())
@jepusto
jepusto / simulation-in-R-2016
Created September 28, 2016 18:16
Rmd for my presentation on simulation studies in R, Quant Methods brownbag colloquium 2016/09/28
---
title: "Designing simulation studies in R"
author: "James E. Pustejovsky"
date: "September 28, 2016"
output:
ioslides_presentation:
css: custom.css
widescreen: true
transition: faster
---