Skip to content

Instantly share code, notes, and snippets.

View JonasMoss's full-sized avatar

Jonas Moss JonasMoss

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / generator_prototype.R
Last active February 19, 2018 12:31
A generator variant of "i:j".
## 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 / functors_in_R.R
Last active February 19, 2018 10:48
C++-style functors in R.
## A simple example of a functor in R.
## A simple function of one argument. Since y is neither defined inside the function body or
## given as an argument, R will search the enclosing environment for y. Since R is lazy, it
## won't do this when the function is defined, but only when the function is called (and y
## is needed.)
f = function(x) x^2 + y^2
## This changes the enclosing environment of f to a new environment. It was the global
## environment before, and we don't want that.
@JonasMoss
JonasMoss / functor_memoization.R
Created February 18, 2018 18:12
Using functors to memoize a function in R.
## Functor example 1: Memoization.
## We memoize a function g with up to N values.
## The function to memoize: It is slow to calculate, so we'll gain from
## storing its values.
g = function(x) {
Sys.sleep(0.1)
print("Processesing ...")
x^2
@JonasMoss
JonasMoss / kde_sampling_gaussian_kernel.R
Last active February 13, 2018 17:39
Sampling from a Gaussian kernel density estimate in R.
## Make a function rkde that samples from a kernel density
#' Sample from a kernel density.
#'
#' @param n Number of observations to sample.
#' @param x The data from which the estimate is to be computed.
#' @param bw Desired bandwidth.
#' @return A numeric vector with n sampled data points from the kernel
#' density estimator.