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
#' 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
## 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
## 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
#' 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
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
## 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) | |
} |
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 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. |
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
## 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 |
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
## 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. |