Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonas Moss JonasMoss

View GitHub Profile
@JonasMoss
JonasMoss / generator_prototype.R
Last active Feb 19, 2018
A generator variant of "i:j".
View generator_prototype.R
## These are generic functions for extracting and assigning values to the enclosing
## environment of a function. It needs class(f) = "function" to work.
`$.function` = function(f, y) environment(f)[[y]]
`$<-.function` = function(f, y, value) {
environment(f)[[y]] = value
invisible(f)
}
@JonasMoss
JonasMoss / weird_list_behaviour.R
Created Feb 21, 2018
Some weird list behaviour in R.
View weird_list_behaviour.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 Mar 19, 2018
An alist function that works with '...' objects.
View alist2.R
#' 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 Mar 25, 2018
Small demonstration of power.
View power_plot.R
## 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 Mar 25, 2018
Idealistic simulation of true powers in psychology.
View power_simulation.R
## 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 Apr 23, 2018
A function that allows the arguments in function calls to be self-referential.
View R.R
#' 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 Apr 25, 2018
A function that evaluates a call as if it was defined in a specified environment.
View S.R
#' 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 Apr 28, 2018
Reproducible simulations for 'optional_stopping_streaks'.
View optional_stopping_streaks.R
#' 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 Apr 30, 2018
Illustration of negative binomial.
View negative_binomial.R
#' 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 Dec 19, 2018
An example of strange R squared values.
View strange_rsq.R
# 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.