mages / ChainLadder

Claims reserving models in R
https://mages.github.io/ChainLadder/
75 stars 62 forks source link

Creation of triangle objects #46

Closed vigou3 closed 5 years ago

vigou3 commented 6 years ago

Another enhancement proposal I'm willing to work on.

Currently, one creates triangle objects using as.triangle on matrices or data frames. This works very well for imported data, but I think we could do better for triangles created from the command line (or in a script file, which amounts to the same).

Just like there are matrix and as.matrix, or data.frame and as.data.frame, we could have a function triangle to directly create triangle objects from vectors of data. The added benefit would be that one would not have to supply NA values for the lower triangle. Think of an interface similar to this to create a 4 x 4 triangle:

triangle(c(1:4), c(1:3), c(1:2), 1, byrow = TRUE)

Any interest?

Cheers

trinostics commented 6 years ago

Not sure I understand the UI. This interpretation based on as.triangle doesn't make sense ... triangle(origin = c(1:4), dev = c(1:3), value = c(1:2), 1, byrow = TRUE) Please clarify. Thanks.

mages commented 6 years ago

My understanding of Vincent's proposal is that triangle(c(1:4), c(1:3), c(1:2), 1, byrow = TRUE) would produces a triangle that looks like this:

1 2 3 4
1 2 3 NA
1 2 NA NA
1 NA NA NA
trinostics commented 6 years ago

Ah, of course. Thanks, Markus. byrow, based on a ragged array of vectors identifying rows, where the first entry of every vector is assumed to be as of the same age. NA in that entry would be possible, presumably. byrow = FALSE would be equivalent to 'bycol'? Or would there be a 'bydiag' (diagonal) capability, like in Schedule P of the US annual statement blank?

vigou3 commented 6 years ago

The understanding of Markus is correct. Sorry if I wasn't clear enough, I put together a toy example pretty quickly.

Dan: your bydiag idea is great! The only drawback UI wise would be that this would mean three arguments (byrow, bycol, bydiag) instead of a toggle (TRUE/FALSE).

Do you think it's worth working on this?

trinostics commented 6 years ago

As I thought about how one might fill a more generalized data.frame-Triangle with, say, names=c("origin","dev","value"), it seemed clear that the vectors would be strung out in the "value" column. How to fill the other two columns is more problematic.

My takeaway is that you are creating matrix-Triangles, not data.frame-Triangles. Since there are only two dimensions in that case, your byrow=T/F is best by far.

If your vectors have names, will they be used to fill the matrix or ignored?