Alex Rivera | Logout

guidelines for testing a statistical function in R?

Asked 2010-10-14T21:17:53.677
12

Question: I am testing functions in a package that I am developing and would like to know if you can suggest some general guidelines for how to do this. The functions include a large range of statistical modeling, transformations, subsetting, and plotting. Is there a 'standard' or some sufficient test?

An Example: the test that prompted me ask this question,

The function dtheta:

dtheta <- function(x) {
  ## find the quantile of the mean
  q.mean <- mean(mean(x) >= x)
  ## find the quantiles of ucl and lcl (q.mean +/- 0.15)
  q.ucl  <- q.mean + 0.15
  q.lcl  <- q.mean - 0.15
  qs <- c(q.lcl, q.mean, q.ucl)
  ## find the lcl, mean, and ucl of the vector
  c(quantile(x,qs), var(x), sqrt(var(x))/mean(x))
}

Step 1: make test data:

set.seed(100) # per Dirk's recommendation
test <- rnorm(100000,10,1)

Step 2: compare the expected output from the function with the actual output from the function:

 expected <- quantile(test, c(0.35, 0.65, 0.5))
 actual   <- dtheta(test)[1:3]
 signif(expected,2) %in% signif(actual,2)

Step 3: maybe do another test

test2 <- runif(100000, 0, 100)
expected <- c(35, 50, 65)
actual   <- dtheta(test2)
expected %in% signif(actual,2)

Step 4: if true, consider function 'functional'

Edit
Report

2 Answers

5

Nice question.

Besides generalities such as setting a seed, I would recommend that you look at some of the tests in the R sources. The directory tests/ in the source has a wealth of these; some of the packages in R Base (such as tools) also have subdirectory tests/.

answered 2010-10-14T21:58:02.483
3

It's already appeared as a comment, but I'll add it as a bona fidey answer. R does have a few automated testing packages to help with this kind of thing, the main two being Runit and testthat. I've briefly used runit, and recently started using testthat in more depth (I can't really give any good advantages / disadvantages of one over another though !).

Automated testing allows you to setup these test cases, as well as others as suggested above like;

  • Boundary Tests
  • Stress Tests (less need to test for accuracy, just throw data at it and see if it falls over)
  • Dealing with different input
  • Dealing with different underlying platforms / locales
answered 2010-10-15T13:38:46.880

Your Answer