Dear Ravi, It's already been suggested that you could disable warnings, but that's risky in case there's a warning that you didn't anticipate. Here's a different approach: > kk <- k[k >= -1 & k <= n] > ans <- numeric(length(k)) > ans[k > n] <- 1 > ans[k >= -1 & k <= n] <- pbeta(p, kk + 1, n - kk, lower.tail=FALSE) > ans [1] 0.000000000 0.006821826 0.254991551 1.000000000 BTW, I don't think that you mentioned that p = 0.3, but that seems apparent from the output you showed. I hope this helps, John -- John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada web: socialsciences.mcmaster.ca/jfox On 2021-10-07 12:29 p.m., Ravi Varadhan via R-help wrote:> Hi, > I would like to execute the following vectorized calculation: > > ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), ifelse (k < -1, 0, 1) ) > > For example: > > >> k <- c(-1.2,-0.5, 1.5, 10.4) >> n <- 10 >> ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) ) > Warning message: > In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced >> print(ans) > [1] 0.000000000 0.006821826 0.254991551 1.000000000 > > The answer is correct. However, I would like to eliminate the annoying warnings. Is there a better way to do this? > > Thank you, > Ravi > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thank you to Bert, Sarah, and John. I did consider suppressing warnings, but I felt that there must be a more principled approach. While John's solution is what I would prefer, I cannot help but wonder why `ifelse' was not constructed to avoid this behavior. Thanks & Best regards, Ravi ________________________________ From: John Fox <jfox at mcmaster.ca> Sent: Thursday, October 7, 2021 2:00 PM To: Ravi Varadhan <ravi.varadhan at jhu.edu> Cc: R-Help <r-help at r-project.org> Subject: Re: [R] How to use ifelse without invoking warnings External Email - Use Caution Dear Ravi, It's already been suggested that you could disable warnings, but that's risky in case there's a warning that you didn't anticipate. Here's a different approach: > kk <- k[k >= -1 & k <= n] > ans <- numeric(length(k)) > ans[k > n] <- 1 > ans[k >= -1 & k <= n] <- pbeta(p, kk + 1, n - kk, lower.tail=FALSE) > ans [1] 0.000000000 0.006821826 0.254991551 1.000000000 BTW, I don't think that you mentioned that p = 0.3, but that seems apparent from the output you showed. I hope this helps, John -- John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada web: nam02.safelinks.protection.outlook.com/?url=https://socialsciences.mcmaster.ca/jfox/&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160038474|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=Q33yXm36BwEVKUWO72CWFpSUx7gcEEXhM3qFi7n78ZM=&reserved=0 On 2021-10-07 12:29 p.m., Ravi Varadhan via R-help wrote:> Hi, > I would like to execute the following vectorized calculation: > > ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), ifelse (k < -1, 0, 1) ) > > For example: > > >> k <- c(-1.2,-0.5, 1.5, 10.4) >> n <- 10 >> ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) ) > Warning message: > In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced >> print(ans) > [1] 0.000000000 0.006821826 0.254991551 1.000000000 > > The answer is correct. However, I would like to eliminate the annoying warnings. Is there a better way to do this? > > Thank you, > Ravi > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > nam02.safelinks.protection.outlook.com/?url=https://stat.ethz.ch/mailman/listinfo/r-help&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160048428|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=FXX+4zNT0JHBnDFO5dXBDQ484oQF1EK5/a0dG9P/4k4=&reserved=0 > PLEASE do read the posting guide nam02.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160048428|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=ss2ohzJIY6qj0eAexk4yVzTzbjXxK5VZNors0GpsbA0=&reserved=0 > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Dear Ravi, On 2021-10-08 8:21 a.m., Ravi Varadhan wrote:> Thank you to Bert, Sarah, and John. I did consider suppressing warnings, > but I felt that there must be a more principled approach.? While John's > solution is what I would prefer, I cannot help but wonder why `ifelse' > was not constructed to avoid this behavior.The conditional if () else, which works on an individual logical value, uses lazy evaluation and so can avoid the problem you encountered. My guess is that implementing lazy evaluation for the vectorized ifelse() would incur too high a computational overhead for large arguments. Best, John> > Thanks & Best regards, > Ravi > ------------------------------------------------------------------------ > *From:* John Fox <jfox at mcmaster.ca> > *Sent:* Thursday, October 7, 2021 2:00 PM > *To:* Ravi Varadhan <ravi.varadhan at jhu.edu> > *Cc:* R-Help <r-help at r-project.org> > *Subject:* Re: [R] How to use ifelse without invoking warnings > > ????? External Email - Use Caution > > > > Dear Ravi, > > It's already been suggested that you could disable warnings, but that's > risky in case there's a warning that you didn't anticipate. Here's a > different approach: > > ?> kk <- k[k >= -1 & k <= n] > ?> ans <- numeric(length(k)) > ?> ans[k > n] <- 1 > ?> ans[k >= -1 & k <= n] <- pbeta(p, kk + 1, n - kk, lower.tail=FALSE) > ?> ans > [1] 0.000000000 0.006821826 0.254991551 1.000000000 > > BTW, I don't think that you mentioned that p = 0.3, but that seems > apparent from the output you showed. > > I hope this helps, > ? John > > -- > John Fox, Professor Emeritus > McMaster University > Hamilton, Ontario, Canada > web: > nam02.safelinks.protection.outlook.com/?url=https://socialsciences.mcmaster.ca/jfox/&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160038474|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=Q33yXm36BwEVKUWO72CWFpSUx7gcEEXhM3qFi7n78ZM=&reserved=0 > <nam02.safelinks.protection.outlook.com/?url=https://socialsciences.mcmaster.ca/jfox/&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160038474|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=Q33yXm36BwEVKUWO72CWFpSUx7gcEEXhM3qFi7n78ZM=&reserved=0> > > On 2021-10-07 12:29 p.m., Ravi Varadhan via R-help wrote: >> Hi, >> I would like to execute the following vectorized calculation: >> >>??? ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), ifelse (k < -1, 0, 1) ) >> >> For example: >> >> >>> k <- c(-1.2,-0.5, 1.5, 10.4) >>> n <- 10 >>> ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) ) >> Warning message: >> In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced >>> print(ans) >> [1] 0.000000000 0.006821826 0.254991551 1.000000000 >> >> The answer is correct.? However, I would like to eliminate the annoying warnings.? Is there a better way to do this? >> >> Thank you, >> Ravi >> >> >>?????? [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> nam02.safelinks.protection.outlook.com/?url=https://stat.ethz.ch/mailman/listinfo/r-help&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160048428|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=FXX+4zNT0JHBnDFO5dXBDQ484oQF1EK5/a0dG9P/4k4=&reserved=0 > <nam02.safelinks.protection.outlook.com/?url=https://stat.ethz.ch/mailman/listinfo/r-help&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160048428|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=FXX+4zNT0JHBnDFO5dXBDQ484oQF1EK5/a0dG9P/4k4=&reserved=0> >> PLEASE do read the posting guide nam02.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160048428|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=ss2ohzJIY6qj0eAexk4yVzTzbjXxK5VZNors0GpsbA0=&reserved=0 > <nam02.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=04|01|ravi.varadhan@jhu.edu|fd882e7c4f4349db34e108d989bc6a9f|9fa4f438b1e6473b803f86f8aedf0dec|0|0|637692265160048428|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000&sdata=ss2ohzJIY6qj0eAexk4yVzTzbjXxK5VZNors0GpsbA0=&reserved=0> >> and provide commented, minimal, self-contained, reproducible code. >> >