Bug 17306 - The check for NA in the ncp parameter in stats::rt() does not handle vectors
Summary: The check for NA in the ncp parameter in stats::rt() does not handle vectors
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: 3.4.0
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2017-07-07 16:33 UTC by Long Qu
Modified: 2017-07-09 11:22 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Long Qu 2017-07-07 16:33:57 UTC
When _R_CHECK_LENGTH_1_CONDITION_ is set to true, stats::rt() does not accept a vector of ncp: 

> stats::rt(n=5, df=8, ncp=1:5*5)
Error in if (is.na(ncp)) { : the condition has length > 1


A look at rt() indicates that the check for NA in ncp is unnecessary, because warnings can be generated by the call to rnorm() in the final "else" condition: 

> n=5; df=8
> ncp=c(NA, 1:4)*5
> rnorm(n, ncp)/sqrt(rchisq(n, df)/df)
[1]       NaN  5.069892  8.237059 18.587848 20.144304
Warning message:
In rnorm(n, ncp) : NAs produced
> ncp=c(1:4, NA)*5
> rnorm(n, ncp)/sqrt(rchisq(n, df)/df)
[1]  6.923864 14.138385 18.082062 16.731906       NaN
Warning message:
In rnorm(n, ncp) : NAs produced


Therefore, the 2nd condition that generates this error might be removed without affecting other parts. Moreover, if there is only a single NA in ncp, it makes not sense to report all n NaN's as the result. 

My suggested fix is: 

rt=function(n, df, ncp) 
    if (missing(ncp)) .Call(C_rt, n, df) else
    rnorm(n, ncp)/sqrt(rchisq(n, df)/df)
Comment 1 Duncan Murdoch 2017-07-09 11:22:49 UTC
I see the problem and agree with your fix.  Thanks!  Will commit to R-devel and R-patched.