Skip to content

Instantly share code, notes, and snippets.

View timothyslau's full-sized avatar
💭
There's data in my veins.

Timothy Lau timothyslau

💭
There's data in my veins.
View GitHub Profile
@timothyslau
timothyslau / A-Pen-by-Timothy-Lau.markdown
Created January 10, 2014 21:23
A Pen by Timothy Lau.
@timothyslau
timothyslau / Installing metaSEM on MacOSX.R
Last active August 29, 2015 14:02
How to install metaSEM on Mac OSX 10.9, R 3.0.3, OpenMX 1.4-3059
#install metaSEM package----
install.packages(pkgs=c("metafor","devtools","ellipse","MASS"),dependencies=T)
source('http://openmx.psyc.virginia.edu/getOpenMx.R')
library(metafor)
library(OpenMx)
library(devtools)
library(ellipse)
library(MASS)
#first download the package source from metaSEM from: http://courses.nus.edu.sg/course/psycwlm/internet/metaSEM/#sec-3) then load the package with the script below
install.packages(pkgs=file.choose(), repos=NULL, type="source")
@timothyslau
timothyslau / totalmds
Created June 29, 2015 20:55
Categorical Distribution Data
totalmds <- c(31L, 11L, 56L, 83L, 3L, 5L, 17L, 160L, 6L, 10L, 16L, 1L, 31L,
12L, 14L, 54L, 4L, 6L, 5L, 95L, 8L, 247L, 24L, 82L, 7L, 6L, 7L,
3L, 6L, 6L, 7L, 18L, 188L, 5L, 1L, 35L, 50L, 187L, 21L, 3L, 16L,
29L, 2L, 336L, 6L, 25L, 1L, 34L, 23L, 77L, 7L, 3L, 29L, 21L,
973L, 87L, 19L, 5L, 8L, 22L, 2L, 15L, 18L, 9L, 7L, 377L, 22L,
4L, 53L, 127L, 18L, 91L, 25L, 94L, 1004L, 8L, 39L, 116L, 2938L,
15L, 59L, 50L, 31L, 3L, 9L, 9L, 73L, 38L, 16L, 4L, 13L, 12L,
16L, 1L, 20L, 13L, 44L, 14L, 630L, 3L, 1L, 7L, 5L, 8L, 3L, 4L,
75L, 10L, 2L, 2L, 0L, 4L, 25L, 67L, 115L, 15L, 10L, 129L, 8L,
7L, 184L, 5L, 42L, 15L, 1L, 7L, 12L, 9L, 12L, 14L, 31L, 0L, 48L,
@timothyslau
timothyslau / BPmedian
Last active August 29, 2015 14:28
Breakdown Point Demonstration
# Mean v. Median
# Tim's point (break down point of the mean is 0 and median is 0.5)
x0 <- c(1:10)
mean(x0)
median(x0)
x1 <- c(1:9, 1000)
mean(x1)
median(x1)
@timothyslau
timothyslau / within-group sum of cross products
Created August 26, 2015 15:34
A function for computing the within-group sum of cross products (SCPW)
# data from p.645 Explaining Psychological Statistics
dat <- data.frame(initial = c(1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7), final = c(3, 4, 3, 5, 7, 7, 6, 9, 6, 8, 2, 6, 5, 8, 6, 9, 8, 6, 2, 4, 3, 5, 3, 7, 5, 7, 4, 9, 8, 6), condition = factor(x = c(rep(x = "psycho", times = 10), rep(x = "behav", times = 8), rep(x = "cont", times = 12)), levels = c("cont", "behav", "psycho")))
# function
scpw <- function(condition, ){
n <- length(dat[dat$condition == "cont", "initial"]) - 1
sdx <- sd(dat[dat$condition == "cont", "initial"])
sdy <- sd(dat[dat$condition == "cont", "final"])
r <- cor(dat[dat$condition == "cont", "final"], dat[dat$condition == "cont", "initial"])
@timothyslau
timothyslau / ANCOVAadjmean
Last active August 26, 2015 19:20
Adjusted mean for an ANCOVA
# My attempt to make Cohen's example into a formula (the correct calculations for the example in the textbook start on line 40)
adjmeans <- function(IV, DV, CV, data){
ano1 <- lm(formula = getElement(object = data, name = DV) ~ getElement(object = data, name = CV))
ano2 <- lm(formula = getElement(object = data, name = IV) ~ getElement(object = data, name = CV))
SSYer <- anova(ano1)$"Sum Sq"[2]
SSXer <- anova(ano2)$"Sum Sq"[2]
@timothyslau
timothyslau / Orange Tree Models
Last active December 20, 2015 01:46
Orange: Linear v. Additive v. Nonlinear Single and Multilevel Models
###################################################
### linear v additive v nonlinear models
###################################################
# necessary packages
library(ggplot2)
library(lme4)
library(mgcv)
# rough idea of relationship
cor(Orange$age, Orange$circumference)
@timothyslau
timothyslau / type_cnvt.py
Created March 11, 2022 23:03
Auto Downcast DataFrame dtypes
def type_cnvt(df, col, to, copy=True, errors="raise", bool_true=None, fillna=None):
import numpy as np
num_list = ["int8", "int16", "int32", "int64", "float16", "float32", "float64", "float128"]
# error can be "raise" or "ignore" or "coerce"
i8_min, i8_max = np.iinfo(np.int8)._min_vals.get("i8"), np.iinfo(np.int8)._max_vals.get("i8")
i16_min, i16_max = np.iinfo(np.int16)._min_vals.get("i16"), np.iinfo(np.int16)._max_vals.get("i16")
i32_min, i32_max = np.iinfo(np.int32)._min_vals.get("i32"), np.iinfo(np.int32)._max_vals.get("i32")
i64_min, i64_max = np.iinfo(np.int64)._min_vals.get("i64"), np.iinfo(np.int64)._max_vals.get("i64")
f16_min, f16_max = np.finfo(np.float16).min, np.finfo(np.float16).max
f32_min, f32_max = np.finfo(np.float32).min, np.finfo(np.float32).max