Skip to content

Instantly share code, notes, and snippets.

View wviechtb's full-sized avatar

Wolfgang Viechtbauer wviechtb

View GitHub Profile
@wviechtb
wviechtb / diamond_plot.r
Created September 26, 2023 10:01
Diamond plot with metafor::forest() and metafor::addpoly()
# inspired by: https://github.com/mjskay/uncertainty-examples/blob/master/diamond_plot.md
# to see if we can abuse metafor::forest() and metafor::addpoly() to create such a plot
library(palmerpenguins)
library(metafor)
set.seed(1234)
dat <- penguins
dat <- dat[!is.na(dat$sex),]
@wviechtb
wviechtb / backcalculate_sd.r
Created June 2, 2023 14:53
Back-calculate the pooled SD from means, group sizes, and the p-value
# means, SDs, and group sizes
m1 <- 52
m2 <- 40
sd1 <- 6
sd2 <- 4
n1 <- 20
n2 <- 40
# pooled SD
sdp <- ((n1-1)*sd1^2 + (n2-1)*sd2^2) / (n1 + n2 - 2)
@wviechtb
wviechtb / journal_name_generator.r
Last active January 14, 2023 09:48
Papermill Journal Name Generator
genjournal <- function() {
x1 <- sample(c("American Journal of", "Austin Journal of", "Annals of", "Archives of", "International Journal of", "Journal of", "Open Journal of", "World Journal of", "Insights Journal of", "Short Reports of", ""), 1)
x2 <- sample(c("Clinical", "Child & Adolescent", "Family", "Current", "Preventive", "Emergency", "Novel", ""), 1)
x3 <- sample(c("Medicine", "Health Care", "Biomedical Science", "Case Reports", "Nursing Studies", "Surgery", "Medical Case Reports", "Obstetrics", "Gynecology", "Family Medicine", "Primary Care", "Nursing", "Psychiatry", "Pulmonology", "Images", "Biology", "Behavioral Science", "Public Health", "Nursing Practice", "Respiratory Care", "Cancer", "Neurology", "Musculoskeletal Disorders", "Epidemiology", "Cardiology", "Vascular Medicine", "Hematology", "Imaging", "Microbiology", "Infectious Diseases", "Oncology", "Neuroinflammation", "Neurodegenerative Diseases", "Gerontology", "Addiction", "Rehabilitation Medicine", "Translational Medicine", "Images
@wviechtb
wviechtb / boot_rma_mv_parallel.r
Last active February 10, 2023 14:06
Bootstrapping rma.mv() model to get CI for pseudo R^2 statistic
############################################################################
library(metafor)
library(boot)
dat <- dat.crede2010
dat <- escalc(measure="ZCOR", ri=ri, ni=ni, data=dat, subset=criterion=="grade")
############################################################################
@wviechtb
wviechtb / rcode_rma_mv_example.r
Last active March 8, 2024 18:45
Model Selection using the glmulti and MuMIn Packages with a rma.mv() Model
############################################################################
library(metafor)
library(ape)
############################################################################
# read the documentation for this dataset
help(dat.moura2021)
@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.
@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 / 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 / install_packages.r
Last active November 3, 2024 16:09
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
@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)