Skip to content

Instantly share code, notes, and snippets.

@sTeamTraen
sTeamTraen / f_range.R
Created June 1, 2026 15:48
Function to display the possible ranges of the F or t statistic from a one- or two-way ANOVA.
library(rpsychi)
# Function to display the possible ranges of the F or t statistic from a one- or two-way ANOVA.
f_range <- function (m, s, n, title=FALSE, show.t=FALSE, dp.p=-1, labels=c()) {
m.ok <- m
if (class(m.ok) == "matrix") {
func <- ind.twoway.second
useF <- c(3, 2, 4)
default_labels <- c("col F", "row F", "inter F")
}
# Find words (in a dictionary file) that are made up of other words run together.
# e.g.: "deliberating" = "deli" + "berating"
# e.g.: "categorising" = "cat" + "ego" + "rising"
# By Nick Brown (nicholasjlbrown@gmail.com), June 2025.
# Some parameters. I hope their meaning is reasonably obvious.
min.split.words <- 2
max.split.words <- 3
shortest.single.word <- 3
longest.single.word <- 8
@sTeamTraen
sTeamTraen / Rizk-BMI.R
Last active April 21, 2025 13:28
Simulation of BMI values from Figure 1 of Rizk et al. 10.1016/j.fertnstert.2004.07.960
# Simulation of BMI values from Figure 1 of Rizk et al. 10.1016/j.fertnstert.2004.07.960
exact.rnorm <- function(n, m, sd) {
x <- rnorm(n)
x <- (x - mean(x)) / sd(x) # standardize to mean 0, sd 1
x <- x * sd + m # rescale to target mean and sd
return(x)
}
# Mean and SD of height and weight from Table 1
# Compare multiple correlations to see if they are similar.
# See http://home.ubalt.edu/ntsbarsh/business-stat/opre504.htm#rmulticorr
# By Nick Brown (nicholasjlbrown@gmail.com), September 2022. Licence: CC-0.
r_to_z <- function (r) {
(log(1 + r) - log(1 - r)) / 2
}
many_corr <- function (vec.z, vec.n) {
tot1 <- 0
@sTeamTraen
sTeamTraen / chi-square-null.R
Last active February 17, 2022 22:10
Demonstrates the chi-square distribution under the null for DFs 1 through 6
# Demonstration of chi-square distribution from chi-square tests under the null
# By Nick Brown, February 2022.
# Licence: CC-0
set.seed(2)
df.range <- 1:6 # range of DFs for which to generate plots
loops <- 10000 # number of cases to generate (more gives better precision)
N <- 5000 # range of numbers in tables
@sTeamTraen
sTeamTraen / Matta - eTable7.R
Created February 7, 2022 17:38
Analysis of pooled effects across 17 symptoms (excluding anosmia) in eTable7 of Matta et al., 10.1001/jamainternmed.2021.6454
# Matta et al., 10.1001/jamainternmed.2021.6454
# See Altman, 2011, 10.1136/bmj.d2304 for calculation of z-scores
# See https://en.wikipedia.org/wiki/Fisher%27s_method#Relation_to_Stouffer's_Z-score_method for Stouffer's method
eTable7 <- data.frame(name="", N=0, OR.m6=0.0, CIlo.m6=0.00, CIhi.m6=0.00, OR.m7=0, CIlo.m7=0.00, CIhi.m7=0.00)
eTable7 <- rbind(eTable7, c("Sleep problems", 54, 0.76, 0.42, 1.36, 0.92, 0.51, 1.67))
eTable7 <- rbind(eTable7, c("Joint pain", 35, 1.38, 0.69, 2.73, 1.40, 0.64, 3.03))
eTable7 <- rbind(eTable7, c("Back pain", 38, 1.58, 0.80, 3.09, 1.24, 0.60, 2.55))
eTable7 <- rbind(eTable7, c("Digestive track problems", 29, 0.61, 0.27, 1.35, 1.16, 0.50, 2.49))
@sTeamTraen
sTeamTraen / Matta-T2.R
Created December 15, 2021 00:02
Matta et al., 10.1001/jamainternmed.2021.6454, Table 2
# Matta et al., 10.1001/jamainternmed.2021.6454
# Table 2
n.s0b0 <- 25271
n.s0b1 <- 461
n.s1b0 <- 638
n.s1b1 <- 453
symptoms <- list(
@sTeamTraen
sTeamTraen / Cadegiani-Table1s.R
Created October 2, 2021 19:20
Analysis of Table 1 from two (or three) articles by Cadegiani et al.
# Code to analyse Table 1 from some articles about the use of anti-androgens to treat Covid-19.
# By Nick Brown, October 2021.
# Licence: CC-0.
# See https://steamtraen.blogspot.com/2021/10/applying-john-carlisles-table-1.html
# Compute the chi-square p value for a given analysis modality (see the blog post).
mychi <- function (s1, s2, n1, n2, analysis)
{
mat <- matrix(c(s1, (n1 - s1), s2, (n2 - s2)), ncol=2)
# Simple demonstration of mean absolute correlation with random data.
# By Nick Brown, February 2020. License: CC-0.
library(ggplot2)
set.seed(1)
iter <- 1000
# Create data frame to contain correlation results, excluding CI (which is a list)
ct.names <- names(cor.test(1:3, 3:1)) # little hack: correlation is perfect, so no CI
@sTeamTraen
sTeamTraen / Laird-VitD.R
Last active January 29, 2021 15:40
Reanalysis of Laird et al. (2020) https://pubmed.ncbi.nlm.nih.gov/32603576/
# Reanalysis of Laird et al. (2020) https://pubmed.ncbi.nlm.nih.gov/32603576/
# By Nick Brown, January 2021.
# Licence: CC-0.
# Laird et al. did not state how they went from four (Italy) or three (Spain) measurements
# of Vitamin D levels to a single figure.
# When I calculate the unweighted mean of their samples, this seems to correspond to the
# Y-axis values in their figure 1, but I have included the relevant numbers from their
# Table 1 here to allow a weighted average to be calculated.