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

At the Psychological Methods Discussion group, Ben Ambridge asked the following question: Hi everyone - I was wondering (don’t worry, I haven’t actually done this!) what would be wrong statistically speaking with an approach where you run a frequentist t-test (or whatever) after adding each participant and stop testing participants when the p value has remained below 0.05 (or 0.001 or whatever) for - say - each of the last 20 participants.

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