This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 30 | |
param_loo = vector("list", n) | |
print(length(param_loo)) | |
# [1] 30 | |
for (i in 1:n) { | |
param_loo[[i]] = NULL | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Create an \code{alist}. | |
#' | |
#' An alternative to \code{base::alist} that works with \code{...} objects | |
#' inside a function. | |
#' | |
#' An \code{alist} is a list containing possibly unevaluated arguments. The | |
#' standard implementation of \code{alist} does not work with \code{...} objects | |
#' inside a function, but this one does. Uses of this function includes partial | |
#' function application and easier handling of generics that make use of | |
#' \code{...}, for instance \code{plot}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## A small demonstration of statistical power for a one-sided z-test. The point | |
## is to show that powers close to zero or one are not reasonable to assume a | |
## priori. | |
n = 80 | |
theta = seq(-0.3, 1, by = 0.01) | |
plot(theta, 1 - pnorm(1.96 - sqrt(n)*theta), type = "l", bty = "l", | |
xlab = expression(theta), ylab = "Power", | |
main = paste0("Power when n = ", n)) | |
grid() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## A small simulation of how powers could be distributed in psychology. | |
## 'sn' is the skew-normal distribution, which I suppose is useful in this case. | |
## The package is available from CRAN: | |
## install.packages("sn") | |
set.seed(313) | |
N = 100000 | |
thetas = sn::rsn(N, xi = 0.05, omega = 0.15, alpha = 2) # Sample of true thetas. | |
## I assume the effect sizes (thetas) are sampled from the following |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Allow self-referential arguments in functions. | |
#' | |
#' @param call A function call. | |
#' @param quote Logical; if \code{TRUE}, the supplied \code{call} is interpreted | |
#' as a quote, so \code{substitute} is applied. | |
#' @return The evaluated function call with the self-refering arguments | |
#' evaluated. | |
#' @examples | |
#' R(plot(y = 1:10, x = y^2)) | |
#' R(plot(x = y^2, y = 1:10)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Evaluates a call as if its function was defined in a specified environment | |
#' | |
#' When a name is encountered in the definition of a function, the search path | |
#' for that name is given by the defining environment of the function. This is | |
#' good behaviour, since it allows simple reasoning about how a function should | |
#' behave: If two calls to a function defined in a constant environment \code{e} | |
#' yield different results, this must be because they are given different | |
#' arguments. | |
#' | |
#' Sometimes, a function is defined to make messy code more readable, but is |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Find the cumulative maximal streak length in a vector of bools. | |
#' | |
#' @param bools Logical vector. | |
#' @return An integer vector. The \code{i}th element is the maximal streak | |
#' length in \code{x[1:i]}. | |
#' @example | |
#' bools1 = c(FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE) | |
#' streaks(bools1) [1] 0 1 1 1 2 3 3 | |
#' | |
#' bools2 = c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Graph of number of tries needed to obtain K successes. | |
#' @param K number of studies. | |
#' @return NULL. | |
plotter = function(K){ | |
kk = 0:(K*70) | |
plot(kk + K, dnbinom(kk, K, 0.05), bty = "l", type = "b", pch = 20, | |
xlab = "Number of studies", | |
ylab = "Probability", | |
main = paste0("Number of studies before ", K, " successes")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a covariance matrix for the covariates. | |
rho12 = -0.1 | |
rho13 = 0.65 | |
rho23 = -0.3 | |
covariance = matrix(c(1, rho12, rho13, | |
rho12, 1, rho23, | |
rho13, rho23, 1), nrow = 3) | |
# Simulate a linear regression with all betas equal to 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Hide non-function variables from function. | |
#' | |
#' @param ... Named functions and function definitions. | |
#' @return Nothing. | |
H = function(...) { | |
function_names = names(as.list(substitute((...)))[-1]) | |
function_defs = list(...) | |
envir = parent.env(parent.frame()) |