[R] Verify that a grid is uniform

Bert Gunter gunter.berton at gene.com
Tue Apr 7 00:55:37 CEST 2015


Does not min(abs(diff(z))) give you the scaling you need to set a tolerance?

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Mon, Apr 6, 2015 at 2:55 PM, Marc Lamblin <marcgg.lamblin at gmail.com> wrote:
> The first solution with diff works for uniform abscissa only with integer
> values.
>
> z <- seq(0, 10, length=100)
> all(diff(z) == z[2] - z[1] )
> ## FALSE
>
> In this case, as you recommended, I could use signif or round or a tolerance
> for real numbers. In my particular case, in order to set a tolerance, I need
> the scale used and I don't have this information. I prefer to test the "near
> uniformity".
> I didn't know the function zapsmall. It could be useful!
> Thanks Sarah and Bert!!!
>
> Marc
>
>
>
>
>
> 2015-04-06 19:51 GMT+02:00 Bert Gunter <gunter.berton at gene.com>:
>>
>> ... correction: you need to use absolute value for the comparison, of
>> course.
>>
>> all(abs(diff(z) - z[2] + z[1]) < tol)
>>
>> -- Bert
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>> (650) 467-7374
>>
>> "Data is not information. Information is not knowledge. And knowledge
>> is certainly not wisdom."
>> Clifford Stoll
>>
>>
>>
>>
>> On Mon, Apr 6, 2015 at 10:47 AM, Bert Gunter <bgunter at gene.com> wrote:
>> > Perhaps ?diff might be useful here:
>> >
>> > z <- runif(20)
>> > all(diff(z) == z[2] - z[1] )
>> > ## FALSE
>> >
>> > z <- seq_len(10)
>> > all(diff(z) == z[2] - z[1] )
>> > ##TRUE
>> >
>> > You can use signif or round as before to allow for "near uniformity"
>> > or use ?zapsmall or an explicit comparison with a tolerancec instead
>> > of ==, e.g. all(diff(z) - z[2] + z[1] < tol)
>> >
>> > Cheers,
>> > Bert
>> >
>> > Bert Gunter
>> > Genentech Nonclinical Biostatistics
>> > (650) 467-7374
>> >
>> > "Data is not information. Information is not knowledge. And knowledge
>> > is certainly not wisdom."
>> > Clifford Stoll
>> >
>> >
>> >
>> >
>> > On Mon, Apr 6, 2015 at 10:11 AM, Marc Lamblin <marcgg.lamblin at gmail.com>
>> > wrote:
>> >> The aim is to control if a given abscissa/grid is uniform or not.
>> >> Abscissa
>> >> in generic vector of real ordered numbers.
>> >>
>> >> Here a reproducibile code:
>> >>
>> >> # uniform abscissa/grid
>> >> abscissa1 <- seq(0, 1, length=100)
>> >> # non-uniform abscissa/grid
>> >> abscissa2 <- sort(runif(100))
>> >>
>> >> control1 <- all(signif(abscissa1[1:(length(abscissa1) - 1) + 1] -
>> >> abscissa1[1:(length(abscissa1) - 1)]) ==
>> >> signif(rep((range(abscissa1)[2] -
>> >> range(abscissa1)[1])/(length(abscissa1) - 1), length(abscissa1) - 1)))
>> >> control2 <- all(signif(abscissa2[1:(length(abscissa2) - 1) + 1] -
>> >> abscissa2[1:(length(abscissa2) - 1)]) ==
>> >> signif(rep((range(abscissa2)[2] -
>> >> range(abscissa2)[1])/(length(abscissa2) - 1), length(abscissa2) - 1)))
>> >>
>> >> control1
>> >> control2
>> >>
>> >> As expected control1 is TRUE and control2 is FALSE. Actually in this
>> >> code
>> >> it is possible also to use
>> >> diff inside signif.
>> >> Do you mean that the control to perform can be done in this manner
>> >>
>> >> if (length(unique(diff(vec))) == 1) {
>> >>   control <- TRUE
>> >> } else {
>> >>   control <- FALSE
>> >> }
>> >>
>> >> I have tried to apply this control on abscissa1 which is uniform but
>> >> length(unique(diff(abscissa1))) was greater than one; probably, as you
>> >> said, this is due to the fact that in this way I don't take into
>> >> account
>> >> the machine precision.
>> >> What I want to understand is if there is a SAFE solution, even if until
>> >> now
>> >> this control is working correctly. I have seen in the documentation of
>> >> signif that by default the number of digits considered are 6. The
>> >> number of
>> >> digits to consider depends on the scale used. It doesn't make sense to
>> >> increase the number of digits with respect to default because, in this
>> >> case, you are not using an handy scale.
>> >> Maybe it could be better directly to ask user if the abscissa passed as
>> >> argument is uniform or not.
>> >> Thanks a lot for the link!!!
>> >>
>> >> Marc
>> >>
>> >>
>> >>
>> >>
>> >> 2015-04-06 16:32 GMT+02:00 Sarah Goslee <sarah.goslee at gmail.com>:
>> >>
>> >>> Without a reproducible example that includes some sample data (fake is
>> >>> fine), the code you used (NOT in HTML format), and some clear idea of
>> >>> what output you expect, it's impossible to figure out how to help you.
>> >>> Here are some suggestions for creating a good reproducible example:
>> >>>
>> >>>
>> >>> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>> >>>
>> >>> Without knowing what you want, it looks like abscissa is a vector, and
>> >>> so I'm not sure how this defines a grid, but
>> >>> length(unique(diff(vec)))
>> >>> might help. Note that this DOES NOT account for machine precision in
>> >>> any
>> >>> way.
>> >>>
>> >>> Sarah
>> >>>
>> >>> On Mon, Apr 6, 2015 at 7:50 AM, Marc Lamblin
>> >>> <marcgg.lamblin at gmail.com>
>> >>> wrote:
>> >>> > I need to control of a given grid is uniform. This control using
>> >>> > signif
>> >>> > until now works:
>> >>> >
>> >>> > if (all(signif(abscissa[1:(length(abscissa) - 1) + 1] -
>> >>> > abscissa[1:(length(abscissa) - 1)]) ==
>> >>> > signif(rep((range(abscissa)[2] -
>> >>> >          range(abscissa)[1])/(length(abscissa) - 1),
>> >>> > length(abscissa) -
>> >>> > 1)))) {
>> >>> > # other stuff
>> >>> > }
>> >>> >
>> >>> > Does someone have some suggestions to improve this control? Thanks
>> >>> > in
>> >>> > advance!! :)
>> >>> >
>> >>> > Marc
>> >>> >
>> >>> >         [[alternative HTML version deleted]]
>> >>> >
>> >>>
>> >>>
>> >>> --
>> >>> Sarah Goslee
>> >>> http://www.functionaldiversity.org
>> >>>
>> >>
>> >>         [[alternative HTML version deleted]]
>> >>
>> >> ______________________________________________
>> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> PLEASE do read the posting guide
>> >> http://www.R-project.org/posting-guide.html
>> >> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list