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 / 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 / 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 / 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 / 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 / color_signals.r
Created April 8, 2022 11:22
Use color for signals in R
message("A message")
warning("A warning")
stop("An error")
globalCallingHandlers(
message = function(c) {
style <- crayon::make_style("white")
c$message <- style(c$message)
message(c)
invokeRestart("muffleMessage")
@wviechtb
wviechtb / output.txt
Last active May 4, 2022 08:25
Comparison of apply() versus for-loop in 3 versions of R
############################################################################
R version 2.5.0 (2007-04-23)
Copyright (C) 2007 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
@wviechtb
wviechtb / output.txt
Created May 4, 2022 09:23
Benchmark comparison of for-loops versus apply()/sapply() in 3 different versions of R (2.5.0, 3.0.0, 4.2.0)
> ############################################################################
>
> # A comparison of for-loops versus apply() and sapply() for 1) computing the
> # row means in a matrix and 2) for computing the means of all elements in a
> # list. For task 1), we can also examine the performance of rowMeans() as a
> # specialized / vectorized function and for task 2), we can also compare
> # sapply() with vapply() (note: vapply() was added in version R-2.12.0). Also,
> # for the for-loop, we can examine what the impact is of pre-allocating the
> # vector in which to store the results versus 'growing' the vector in each
> # iteration.