pharmaverse / admiral

ADaM in R Asset Library
https://pharmaverse.github.io/admiral
Apache License 2.0
227 stars 65 forks source link

Feature Request: a function to align the SAS rounding in R? #1872

Closed kaz462 closed 1 year ago

kaz462 commented 1 year ago

Feature Idea

The following function is from a PHUSE paper

ut_round <- function(x, n=0)
{
  # x is the value to be rounded
  # n is the precision of the rounding
  scale <- 10^n
  y <- trunc(x * scale + sign(x) * 0.5) / scale
  # Return the rounded number
  return(y)
}

Similar work has been done as shown in the link below, but I couldn't find the source code: https://rdrr.io/github/mbrothe/brdgr/man/round_sas.html

Relevant Input

No response

Relevant Output

No response

Reproducible Example/Pseudo Code

No response

bundfussr commented 1 year ago

Having an option to use SAS rounding is a good idea.

I wonder why round() implements only one option for rounding. In the documentation of round() it is mentioned that the IEEE 754 standard is used. It has two options for rounding: the R way and the SAS way. I am not sure why only one option was implemented in R. (SAS provides functions for both.)

I don't think we need to implement a new function as implementations are already available like mentioned above or janitor::round_half_up().

At the moment we do not use round() in admiral. Once we do, we need to think about

bms63 commented 1 year ago

As this is an R package meant to build datasets in R for production, I think we should always have the default rounding in R, i.e. round to even.

I do like the option of having the round like SAS available if folks want to use admiral for double programming of SAS datasets, but will we need to have this implemented in admiral or just show users how they can switch his on in a mutate statement or in one of functions that allows for expressions?

rossfarrugia commented 1 year ago

My 2 cents on this... isn't this more something for downstream TLG packages rather than in ADaM? Generally in ADaM we just show the full number and let that be a decision for table formatting. as Stefan says we don't use round() anywhere in admiral. my personal perspective is that part of the move to R is appreciating differences like this, and if HAs are informed we're using R for submission via briefing package then they'd expect such differences. it could come up during QC comparisons if using SAS to double program, but even then that could be explained. really, there's no right or wrong approach, they're just different so i wouldn't go overboard here.

kaz462 commented 1 year ago

Thank you all for the detailed replies! I agree no need to implement a new function as it's already available, and the rounding is not ADaM-specific. The validation rounding discrepancies are not caused by SAS/R, but by different rounding approaches, which should be based on the study SAP instead of the software. (Sorry, the title of this issue may be misleading)

There are situations where rounding is done in ADaM (e.g., CDISC pilot data ADSL.BMIBL). I wonder if we could add examples/documentation on different rounding approaches in R, maybe at admiral or pharmaverse/end_to_end_examples?

bms63 commented 1 year ago

Perhaps a certain blogging site could make a short article??

rossfarrugia commented 1 year ago

@mstackhouse / @MichaelRimler what's the latest on this PHUSE WG: https://advance.phuse.global/pages/viewpage.action?pageId=327874 ? That's where I usually point people towards and know there was a white paper planned for this.

@kaz462 another option could be a page at https://bayer-group.github.io/sas2r/ given it's not admiral or pharmaverse specific? i.e. it's part of general R training.

MichaelRimler commented 1 year ago

Still reading the thread, but AVALC will round in its generation, won't it? @rossfarrugia

The CAMIS working group is focused more on statistical methodology implementations. The rounding issue seemed clear in that (R vs SAS), you could get round-to-even or round_away_from_zero with either. in SAS - round() or rounde(). In R, round() or janitor::round_half_up() {as mentioned above}.

mstackhouse commented 1 year ago

My biostats team has fussed with this as well and this PR to Tplyr is refined from some issues/testing that they did. https://github.com/atorus-research/Tplyr/pull/126

kaz462 commented 1 year ago

Thanks all for your information! @bms63 @rossfarrugia a short article at SAS2R catalog sounds great, issue created, will include the two rounding approaches as mentioned by @bundfussr @MichaelRimler

you could get round-to-even or round_away_from_zero with either. in SAS - round() or rounde(). In R, round() or janitor::round_half_up() {as mentioned above}.

and mention options(tplyr.IBMRounding) for Tplyr

The function from PHUSE paper has precision issues as discussed in https://github.com/atorus-research/Tplyr/pull/126, so I will stick with janitor::round_half_up()

rossfarrugia commented 1 year ago

thanks all for the input! think we can close this one out then. sounds a good outcome - thanks Kangjie!