Easy lambdas

R already has anonymous functions. Just write function(x) do_someting and you’re done. This post is for all of us who are not satisified with this solution. For one, it’s exhausting to write to long function(x) instead of, say, L(x), but that’s not all of it! Take the following example, a calculation of the maximum likelihood estimate of a gamma distribution: set.seed(313) x = rgamma(100, 2, 4) nlm(f = function(p) -mean(dgamma(x, shape = p[1], rate = p[2], log = TRUE)), p = c(1, 1))$estimate ## [1] 2.

Continue reading

The Problem of Side Effects When I am writing \(\mathtt{R}\) code, I often do stuff in the body of my script that creates undesired side effects. # n and x constants I wish to use later n = 100 x = pi^2/6 # lots of code # ... # I suddenly wish to plot something n = 1:1000 x = 0.1 plot(n, pnorm(sqrt(n)*x), type = "l") Notice that n and x have been rewritten.

Continue reading

Author's picture

Jonas Moss

Meta-contrarian statistician who wants to save science.

Ph.d. student in statistics, Universitas Osloensis

Oslo in Norway