Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am stonegold546 on github.
  • I am jamesuanhoro (https://keybase.io/jamesuanhoro) on keybase.
  • I have a public key ASDQwPwPar3wn53oAgVAk6oGl6VZsS7Xlucm8AE_Rkgx3wo

To claim this, I am signing this object:

@stonegold546
stonegold546 / Web sequence diagram
Created July 23, 2016 15:40
Web sequence diagram for canvas visualisation API
title Using our API: details at /api/v1/routes
note over API: LOGIN
note right of API: GET /api/v1/client_id
Dev->API: Request for Google OAuth Client ID
API->Dev: Returns Google OAuth Client ID as URL Dev can call
Dev->*Google: Request callback code from Google
Google-->Dev: Returns callback code
destroy Google
note right of API: GET /api/v1/use_callback_code
get_gci_fcn <- function(mymeansqsvec, MC = 100000, B, L, R, alpha = 0.05) {
# Precondition: mymeansqsvec is a vector of mean squares from
# the ANOVA fit of a two-way crossed models. B>2
# is the number of levels of the first factor and
# L>2 is the number of levels of the second factor and
# R>0 is the number of replicates per cell. A two-way
# balanced layout without interaction is assumed.
# MC is the number of Monte Carlo used to construct the
# GCI, with default one-hundred thousand.
@stonegold546
stonegold546 / icc_fml.R
Last active February 15, 2017 00:07
R code for Full maximum likelihood calculation of ICC
# require(lme4)
mymod <- function(data) {
result <- lme4::lmer(y ~ 1 + (1 | x), data = data, REML = FALSE)
sumy <- summary(result)
varb <- sumy$varcor$x[1]
varw <- sumy$sigma ^ 2
icc <- varb / (varb + varw)
c(icc, varb, varw)
}
# mymod(data)
@stonegold546
stonegold546 / cfa.R
Last active April 8, 2019 05:14
CFA with Stan
library(lavaan)
library(rstan)
library(rethinking)
library(loo)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
dat <- dplyr::select(HolzingerSwineford1939, x1:x9)
@stonegold546
stonegold546 / sum_s.R
Last active August 10, 2019 16:34
Sum or factor score
library(lavaan)
library(psych)
sim.fun <- function (lv, lambda, nrep = 2e3) {
np <- nrow(lv)
t(replicate(nrep, {
X <- lv %*% lambda +
matrix(rnorm(np * length(lambda), 0, sqrt(1 - lambda ^ 2)), np, byrow = TRUE)
# summary(cfa(paste("F =~", paste0("V", 1:length(lambda), collapse = " + ")), X, std.lv = TRUE))
@stonegold546
stonegold546 / 0_hs_gaus.R
Last active August 16, 2019 13:57
Holzinger Swineford Bayesian CFA example
library(lavaan)
summary(m0 <- cfa(
"F1 =~ x1 + x2 + x3 + x9\n F2 =~ x4 + x5 + x6\n F3 =~ x7 + x8 + x9\n x3 ~~ x5\n x2 ~~ x7\n x4 ~~ x7",
HolzingerSwineford1939, std.lv = TRUE, meanstructure = TRUE))
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
@stonegold546
stonegold546 / dmdv.stan
Last active September 26, 2019 03:04
Practical significance t test
data {
real<lower = 0> sd_m;
real<lower = 0> sd_m_diff;
real<lower = 0> sd_st;
real<lower = 0> sd_st_r;
int<lower = 0, upper = 1> nu_choice;
int<lower = 0> N;
vector<lower = 0, upper = 1>[N] x;
vector[N] y;
}
@stonegold546
stonegold546 / progesterone.R
Last active October 2, 2019 21:13
Two group logistic model
progesterone <- c(1513, 2025)
placebo <- c(1459, 2013)
(tab <- as.data.frame(rbind(progesterone, placebo)))
names(tab) <- c("pass", "total")
tab$fail <- tab$total - tab$pass
tab$treatment <- c(1, 0)
chisq.test(tab[c("pass", "fail")], correct = FALSE)
# chisq.test(long.tab$treatment, long.tab$pass, correct = FALSE)
summary(glm(cbind(pass, fail) ~ treatment, binomial, tab))
@stonegold546
stonegold546 / morning_call.R
Last active September 3, 2020 22:01
measure ICC
library(lavaan)
X <- HolzingerSwineford1939[, paste0("x", 1:9)]
colMeans(X)
apply(X, 2, sd)
# min-max scaling version
X.p <- as.data.frame(apply(X, 2, function (x) (x - min(x)) / (max(x) - min(x))))
colMeans(X.p)
apply(X.p, 2, sd)