r-quantities/errors

%*% drops errors without warning

Closed this issue · 5 comments

edzer commented

I found the following unexpected:

> a = set_errors(1:4, .1)
> b = set_errors(1:4, .1)
> f = function(a, b, c) sum(a * b) / sum(c)
> f(a,b,a)
3.00(10)
> f = function(a, b, c) sum(a %*% b) / sum(c)
> f(a,b,a)
3.00(6)

I understand why it happens, but shouldn't the second case come with a warning?

Yes, you're right, it should. The question is what's the best way for doing this, because there are many (matrix-related) methods dropping attributes. For instance, cbind and rbind. And these two are particularly difficult to patch, as you may know.

Solution (see r-quantities/units#54):

2 * set_errors(2, .1)

should give a warning that 2 has no errors, but is assumed to have errors 0, and explicitly require

set_errors(2) * set_errors(2, .1)

to get rid of this warning, which requires a default (see #14).

In the current version, your example throws a warning:

a = set_errors(1:4, .1)
b = set_errors(1:4, .1)
f = function(a, b, c) sum(a %*% b) / sum(c)
f(a,b,a)
#> 3.00(6)
#> Warning message:
#> In Ops.errors(sum(a %*% b), sum(c)) :
#>   first operand automatically coerced to an 'errors' object with zero error

But this is implemented in Ops.errors. But matrix multiplication should throw a warning in the first place, and it doesn't:

a %*% a
#>      [,1]
#> [1,]   30

I tried redefining %*% to show a warning for errors objects, but I'm stuck with this. The problem is that it is S4 generic, not S3. Do you know if it's possible to dispatch S4 methods for S3 classes, @edzer?

edzer commented

Yes, by using

setOldClass("errors")
edzer commented

@Enchufa2 could you pls drop me a direct email?