Thomas Roh
2017-Jan-31 16:39 UTC
[Rd] rnbinom Returns Error that says optional argument is missing
I am trying to reset the default arguments in the rnbinom function with the following example code: params <- c("size" = 1, "mu" = 1) formals(rnbinom)[names(params)] <- params rnbinom(n = 10) It returns the following: Error in rnbinom(n = 10) : argument "prob" is missing, with no default If I set the defaults with this code: params <- c("size" = 1, "prob" = .5) formals(rnbinom)[names(params)] <- params rnbinom(n = 10) The function works correctly. The documentation specifies that you can set mu or prob with size. I understand that the problem lies in default arguments are evaluated as missing, but it seems unintentional that setting "prob" and "size" defaults will actually evaluate. Here is the function call: function (n, size, prob, mu) { if (!missing(mu)) { if (!missing(prob)) stop("'prob' and 'mu' both specified") .Call(C_rnbinom_mu, n, size, mu) } else .Call(C_rnbinom, n, size, prob) } -- Thomas Roh thmsroh at gmail.com [[alternative HTML version deleted]]
Joris Meys
2017-Jan-31 17:14 UTC
[Rd] rnbinom Returns Error that says optional argument is missing
Hi Thomas, This seems fully expected behaviour. Obviously unspecified arguments are evaluated as missing regardless of a default value. So if you set mu as a default, the function will call C_rnbinom with n, size and prob. As prob is not specified you get the error one would expect. Specifying a default value for prob also makes rnbinom call C_rnbinom, but in this case there is a prob value so it works. I don't know what you consider "unintentional", but everything works as expected and imho as intended as well. Changing formals to a function comes with no guarantees, and setting a default value for an argument that previously had none, comes with the risk of breaking things (like you noticed) If you want to use a default value for mu, you have to change the body of the function as well, eg:> formals(rnbinom)[c('size','mu')] <- c(1,1) > body(rnbinom) <- quote(.Call(C_rnbinom_mu, n, size, mu)) > rnbinom(10)[1] 0 4 2 0 3 0 4 0 0 2 That's really hacking away and something I would never suggest to people, but it works. Hope this explains Cheers Joris On Tue, Jan 31, 2017 at 5:39 PM, Thomas Roh <thmsroh at gmail.com> wrote:> I am trying to reset the default arguments in the rnbinom function with the > following example code: > > params <- c("size" = 1, "mu" = 1) > formals(rnbinom)[names(params)] <- params > rnbinom(n = 10) > > It returns the following: > > Error in rnbinom(n = 10) : argument "prob" is missing, with no default > > If I set the defaults with this code: > > params <- c("size" = 1, "prob" = .5) > formals(rnbinom)[names(params)] <- params > rnbinom(n = 10) > > The function works correctly. The documentation specifies that you can set > mu or prob with size. I understand that the problem lies in default > arguments are evaluated as missing, but it seems unintentional that setting > "prob" and "size" defaults will actually evaluate. > > Here is the function call: > > function (n, size, prob, mu) > > { > > if (!missing(mu)) { > > if (!missing(prob)) > > stop("'prob' and 'mu' both specified") > > .Call(C_rnbinom_mu, n, size, mu) > > } > > else .Call(C_rnbinom, n, size, prob) > > } > > > > > > -- > Thomas Roh > thmsroh at gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Mathematical Modelling, Statistics and Bio-Informatics tel : +32 (0)9 264 61 79 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]