Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3235678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:32:03+00:00 2026-05-17T17:32:03+00:00

Question: I am testing functions in a package that I am developing and would

  • 0

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’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:32:03+00:00Added an answer on May 17, 2026 at 5:32 pm

    It depends on what exactly you want to test. Next to Dirks recommendations, svUnit or the RUnit package VitoshKa mentioned, I’d like to add a few things :

    • Indeed, set the seed, but make sure you try the function with different seeds as well. Some functions fail only once every ten times you try. Especially when optimization is involved, this becomes crucial. replicate() is a nice function to use in this context.
    • Think very well about the input you want to test. You should test a number of “odd” cases that don’t really resemble the “perfect” dataset. I always test at least 10 (simulated) datasets of different sizes.
    • Fool-proof the function: I also throw in some data types that are not the ones the function is meant for. Wrong type input is likely going to happen at one point, and the last thing you want is a function returning a bogus result without a warning. If you use that function later on in some other code, debugging that code can and will! be hell. Been there, done that, bought the t-shirt…

    An example on extended testing of datasets: what would you like to see as output in these cases? Is this the result you’d expect? Not according to the test you did.

    > test3 <- rep(12,100000) # data with only 1 value
    > expected <- c(12, 12, 12)
    > actual   <- dtheta(test3) 
    Error in quantile.default(x, qs) : 'probs' outside [0,1]
    
    >  test4 <- rbinom(100000,30,0.5) # large dataset with a limited amount of values
    >  expected <- quantile(test4,c(0.35, 0.50, 0.65))
    >  actual   <- dtheta(test4)
    >  expected %in% signif(actual,2)
    [1] FALSE  TRUE  TRUE
    
    > test5 <- runif(100,0,100) # small dataset. 
    > expected <- c(35, 50, 65)
    > actual   <- dtheta(test5)
    > expected %in% signif(actual,2)
    [1] FALSE FALSE FALSE
    

    edit : corrected code so tests are a bit more senseful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question isn't about unit-testing. And it is for a desktop product. This is
Question Can I build a image database/library that has an e-commerce style checkout system
so I decided I would put my few R functions into a package and
I've looked through the various questions on unit testing but can't find one that
I'm writing a simple program that has a couple of functions that look like
I don't know if this is strictly a programming question but here goes. I
I've got a question about testing methods working on strings. Everytime, I write a
While trying to answer a question in the vicinity ' Unit Testing WPF Bindings
Duplicate : https://stackoverflow.com/questions/135651/learning-unit-testing I'm trying to develop some software for my research group to
This is but a curious question. I cannot find any useful links from Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.