[R] Confused about a warning message

David Winsemius dwinsemius at comcast.net
Fri Jul 8 04:33:49 CEST 2011


On Jul 7, 2011, at 10:17 PM, Gang Chen wrote:

> Thanks for the help! Are you sure R version plays a role in this  
> case? My R version is 2.13.0

I'm not sure, but my version is 2.13.1

>
> Your suggestion prompted me to look into the help content of ifelse,  
> and a similar example exists there:
>
>      x <- c(6:-4)
>      sqrt(x)  #- gives warning
>      sqrt(ifelse(x >= 0, x, NA))  # no warning

  The x variable gets converted to c( 6:0, NA,NA,NA, NA)

Notice the differences here:
 > sqrt(NA)
[1] NA
 > sqrt(-1)
[1] NaN
Warning message:
In sqrt(-1) : NaNs produced

 > qt(.5, 0)
[1] NaN
Warning message:
In qt(p, df, lower.tail, log.p) : NaNs produced

 > qt(.5, NA)
[1] NA



>      ## Note: the following also gives the warning !
>      ifelse(x >= 0, sqrt(x), NA)
>
> Based on the above example, now I have a solution for my situation:
>
> tConvert2 <- function(tval, DF, fullDF) qt(pt(ifelse(DF>=1, tval,  
> 0), ifelse(DF>=1, DF, 1)), fullDF)
>
> > tConvert2(c(2,3), c(0,12), 12)
> [1] 0 3
>
> However, I feel my solution is a little kludged. Any better idea?
>
> Thanks,
> Gang
>
>
>
> On Thu, Jul 7, 2011 at 9:04 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>
> On Jul 7, 2011, at 8:52 PM, David Winsemius wrote:
>
>
> On Jul 7, 2011, at 8:47 PM, Gang Chen wrote:
>
> I define the following function to convert a t-value with degrees of  
> freedom
> DF to another t-value with different degrees of freedom fullDF:
>
> tConvert <- function(tval, DF, fullDF) ifelse(DF>=1, qt(pt(tval, DF),
> fullDF), 0)
>
> It works as expected with the following case:
>
> tConvert(c(2,3), c(10,12), 12)
> [1] 1.961905 3.000000
>
> However, it gives me warning for the example below although the  
> output is
> still as intended:
>
> tConvert(c(2,3), c(0,12), 12)
> [1] 0 3
> Warning message:
> In pt(q, df, lower.tail, log.p) : NaNs produced
>
> I'm confused about the warning especially considering the fact that  
> the
> following works correctly without such warning:
>
> tConvert(2, 0, 12)
> [1] 0
>
> What am I missing?
>
> The fact that ifelse evaluates both sides of the consequent and  
> alternative.
>
> I also think you should update yur R to the most recent version  
> since a current version does not issue that warning.
>
>
> -- 
> David Winsemius, MD
> West Hartford, CT
>
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list