Skip to content

Instantly share code, notes, and snippets.

View JonasMoss's full-sized avatar

Jonas Moss JonasMoss

View GitHub Profile
@JonasMoss
JonasMoss / weird_list_behaviour.R
Created February 21, 2018 13:09
Some weird list behaviour in R.
n = 30
param_loo = vector("list", n)
print(length(param_loo))
# [1] 30
for (i in 1:n) {
param_loo[[i]] = NULL
}
@JonasMoss
JonasMoss / alist2.R
Last active March 19, 2018 08:36
An alist function that works with '...' objects.
#' 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}.
@JonasMoss
JonasMoss / power_plot.R
Created March 25, 2018 20:55
Small demonstration of power.
## 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()
@JonasMoss
JonasMoss / power_simulation.R
Last active March 25, 2018 21:34
Idealistic simulation of true powers in psychology.
## 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
@JonasMoss
JonasMoss / R.R
Last active April 23, 2018 19:17
A function that allows the arguments in function calls to be self-referential.
#' 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))
@JonasMoss
JonasMoss / S.R
Last active April 25, 2018 13:20
A function that evaluates a call as if it was defined in a specified environment.
#' 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
@JonasMoss
JonasMoss / optional_stopping_streaks.R
Created April 28, 2018 08:41
Reproducible simulations for 'optional_stopping_streaks'.
#' 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)
@JonasMoss
JonasMoss / negative_binomial.R
Last active April 30, 2018 13:53
Illustration of negative binomial.
#' 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"))
@JonasMoss
JonasMoss / strange_rsq.R
Last active December 19, 2018 19:48
An example of strange R squared values.
# 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.
@JonasMoss
JonasMoss / H.R
Created July 12, 2019 13:54
Define functions inside enclosing environment.
#' 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())