Skip to content

Instantly share code, notes, and snippets.

View wviechtb's full-sized avatar

Wolfgang Viechtbauer wviechtb

View GitHub Profile
@wviechtb
wviechtb / fun.r
Last active August 10, 2016 08:23
Function that either takes a variable from the data argument or from the parent frame.
fun <- function(x, data) {
if (missing(data))
data <- NULL
if (is.null(data))
data <- sys.frame(sys.parent())
mf <- match.call()
mf.x <- mf[[match("x", names(mf))]]
x <- eval(mf.x, data, enclos=sys.frame(sys.parent()))
temp <- x^2 + 5
return(temp)
@wviechtb
wviechtb / ll.r
Created April 4, 2017 12:46
Function to compute the ML/REML log-likelihood for a standard random-effects model.
ll <- function(mu, tau2, yi, vi, method="ML") {
k <- length(yi)
wi <- 1/(vi + tau2)
if (method == "ML") {
sum(dnorm(yi, mean=mu, sqrt(vi + tau2), log=TRUE))
} else {
-1/2 * (k-1) * log(2*pi) + 1/2 * log(k) - 1/2 * sum(log(vi + tau2)) - 1/2 * log(sum(wi)) - 1/2 * sum(wi * (yi - mu)^2)
}
}
@wviechtb
wviechtb / bivariate_normal_wallpaper.r
Last active April 23, 2023 01:09
Code to generate wallpaper (bivariate normal distribution)
rm(list=ls())
setwd("/home/wviechtb/misc/wallpapers/bivariate_normal")
library(mvtnorm)
library(ellipse)
### specify the correlation for the bivariate normal distribution
rho <- 0.4
@wviechtb
wviechtb / fe_coverage.r
Last active July 5, 2017 15:29
Show that FE model has nominal coverage for conditional inferences.
rm(list=ls())
library(metafor)
iters <- 1000
### number of studies/effects
k <- 10
### SD of the true effects
@wviechtb
wviechtb / fe_coverage_fixed_theta
Created July 5, 2017 18:41
Show that FE model has nominal coverage for conditional inferences (fixed theta scenario).
rm(list=ls())
library(metafor)
iters <- 1000
### number of studies/effects
k <- 10
### SD of the true effects
@wviechtb
wviechtb / rmat.r
Last active July 12, 2020 16:49
Function to compute var-cov matrix of correlations (raw or r-to-z transformed)
rmat <- function(x, n, upper=TRUE, simplify=TRUE, rtoz=FALSE, data) {
if (inherits(x, "formula")) {
options(na.action = "na.pass")
if (missing(data))
stop("Must specify 'data' argument when 'x' is a formula.")
if (!is.data.frame(data))
@wviechtb
wviechtb / gist:e87ee35ea5544a3a5f875f61e270cd18
Created June 2, 2018 11:32
Show density of product-moment correlation as a function of sample size
devtools::install_github("wviechtb/metafor")
library(metafor)
rs <- seq(-1,1,length=1001)
ns <- c(seq(10, 100, 10), 150, 200, 300)
ys <- lapply(ns, function(n) metafor:::.dcor(rs, n, rho=0))
plot(NA, xlim=range(rs), ylim=range(ys), xlab="Correlation", ylab="Density")
invisible(lapply(ys, function(y) lines(rs, y)))
@wviechtb
wviechtb / Multivariate meta-analysis
Created July 6, 2018 15:15
Some literature on multivariate meta-analysis and other methods to account for dependency in multiple estimates
Kalaian, H. A., & Raudenbush, S. W. (1996). A multivariate mixed linear model for meta-analysis. Psychological Methods, 1(3), 227-235.
Berkey, C. S., Hoaglin, D. C., Antczak-Bouckoms, A., Mosteller, F., & Colditz, G. A. (1998). Meta-analysis of multiple outcomes by regression with random effects. Statistics in Medicine, 17(22), 2537-2550.
van Houwelingen, H. C., Arends, L. R., & Stijnen, T. (2002). Advanced methods in meta-analysis: Multivariate approach and meta-regression. Statistics in Medicine, 21(4), 589-624.
Nam, I.-S., Mengersen, K., & Garthwaite, P. (2003). Multivariate meta-analysis. Statistics in Medicine, 22(14), 2309-2333.
Hedges, L. V., Tipton, E., & Johnson, M. C. (2010). Robust variance estimation in meta-regression with dependent effect size estimates. Research Synthesis Methods, 1(1), 39-65.
@wviechtb
wviechtb / bifurcation_diagram_logistic_map.r
Created February 21, 2020 17:04
Bifurcation diagram for the logistic map
iters <- 1000
ns <- iters - 500
nl <- iters - ns + 1
rmin <- 2.4
rmax <- 4
rn <- 5000
x <- rep(0,iters)
@wviechtb
wviechtb / install_packages.r
Last active January 23, 2024 09:56
Code to install packages needed during the 'Introduction to R' course.
############################################################################
# During the course, we will make use of a number of add-on packages for R.
# Ideally, you should install these packages before the course (then you don't
# have to bother with installing packages during the course). The code below
# can be used to install each of the packages one by one. So, just run each
# command below (e.g., by copy-pasting it into R) line by line.
# If you are asked to create a 'personal library', choose 'yes'. If you are
# asked to choose a 'CRAN mirror', just select the default one (which is