Skip to content

Instantly share code, notes, and snippets.

View bwiernik's full-sized avatar

Brenton M. Wiernik bwiernik

View GitHub Profile
@bwiernik
bwiernik / s3_tutorial.R
Created March 8, 2021 22:09
S3 dispatch examples for R packages
library(psychmeta)
ma_obj <- ma_r(rxyi = rxyi, n = n, rxx = rxxi, ryy = ryyi,
construct_x = x_name, construct_y = y_name, sample_id = sample_id,
moderators = moderator, data = data_r_meas_multi)
summary(ma_obj)
class(ma_obj)
inherits(ma_obj, "ma_psychmeta")
inherits(ma_obj, "ma_table")
@bwiernik
bwiernik / sim.rma.uni.R
Created February 16, 2021 14:14
Posterior (predictive) simulation for metafor rma models
# Copyright (2021) Brenton M. Wiernik
# Licensed under GPL-3.0
#
sim.rma.uni <- function(object, n.sims = object$k, sim.type = c("coef", "sample"), newdata = NULL, control = NULL, ...) {
sim.type <- match.arg(sim.type)
if (!inherits(object, "rma.uni"))
stop(mstyle$stop("Argument 'object' must be an object of class \"rma.uni\"."))
if (inherits(object, "robust.rma"))
stop(mstyle$stop("Method not available for objects of class \"robust.rma\"."))
if (inherits(object, "rma.ls"))
@bwiernik
bwiernik / compute_smd.R
Created December 6, 2020 13:42
R functions to compute adjusted SMD (Cohen's d) from a regression or ANCOVA model
.cmicalc <- function (df) {
cmi <- ifelse(df <= 1, NA,
exp(lgamma(df/2) - log(sqrt(df/2)) - lgamma((df - 1)/2))
)
return(cmi)
}
smd_stats <-
function(diff = NULL,
se_diff = NULL,
@bwiernik
bwiernik / apa_references.yaml
Created November 18, 2020 18:23
APA Manual references
---
references:
- id: 1917a
archive: 'Manuscripts and Archives, Yale University Library'
archive_location: 'Robert Mearns Yerkes Papers (Box 137, Folder 2292)'
archive-place: 'New Haven, CT'
issued:
- year: 1917
- year: 1954
circa: true
@bwiernik
bwiernik / refernces.yaml
Created November 18, 2020 18:16
Example CSL YAML
---
references:
- id: BalsamAffirmativecognitivebehavior2019
accessed:
- year: 2019
month: 10
day: 17
author:
- family: Balsam
given: Kimberly F.
@bwiernik
bwiernik / complete_list.R
Last active November 7, 2020 15:27
R function to complete lists and make missing elements explicitly NA
complete_list <- function(.list, .names) {
if (missing(.names)) return(.list)
.list[.names[! (.names %in% names(.list)) ]] <- NA
return(.list)
}
a <- list(b = 1, c = 2)
a <- complete_list(a, c("a", "b", "c"))
dplyr::coalesce(a$a, a$b, a$c, 0)
@bwiernik
bwiernik / change_plot.R
Created October 30, 2020 20:12
ggplot example individual + group change plot
library(tidyverse)
ind_data <- psych::bfi %>%
na.omit() %>%
filter(education %in% c(2, 4, 5)) %>%
select(A1:A5, education) %>%
group_by(education) %>%
slice(sample(1:n(), 40)) %>%
ungroup() %>%
rowid_to_column(".id") %>%
@bwiernik
bwiernik / pp_check.lm.R
Last active September 4, 2020 07:43
Posterior predictive checks for lm() models
## Functions to draw samples from distributions implied by lm() models
##
## Arguments for simulate_model correspond to arguments in:
## a) stats::predict.lm();
## b) stats::simulate();
## c) nsamples is an alias for nsim;
## d) dist_type specifies the type of distribution to sample from
## 1) "ml"; from the maximum likelihood distribution;
## the distribution implied by estimated model parameters;
## (equivalent to stats::simulate());
@bwiernik
bwiernik / chicago-with-cite-group.csl
Created August 4, 2020 06:27
CSL Chicago author-date with cite-group-delimiter
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="display-and-sort" page-range-format="chicago">
<info>
<title>Chicago Manual of Style 17th edition (author-date)</title>
<id>http://www.zotero.org/styles/chicago-author-date</id>
<link href="http://www.zotero.org/styles/chicago-author-date" rel="self"/>
<link href="http://www.chicagomanualofstyle.org/tools_citationguide.html" rel="documentation"/>
<author>
<name>Julian Onions</name>
<email>julian.onions@gmail.com</email>
>>===== MODE =====>>
bibliography
<<===== MODE =====<<
>>===== DESCRIPTION =====>>
set delimiter
<<===== DESCRIPTION =====<<
>>===== RESULT =====>>
<div class="csl-bib-body">