Marc in the box - 1 year ago 41

R Question

I'm stuck on how to calculate error for a parameter fitting when the scale of the parameter is cyclic in nature.

For example, parameter "A" can vary between 0 and 1, which represent fraction of a year (i.e. 0 = Jan 1st, 0.25 = Apr 1st, 0.5 = Jul 1st, 0.75 = Oct 1st, and 1.0 = Jan 1, etc.).

How can I measure difference from a reference point. In this case, I would like difference to be min/max +- 0.5.

Here's an example where reference point is Apr 1st:

`ref <- 0.25 # Apr 1st`

val <- c(0.1, 0.3, 0.7, 0.95) # what is the difference from these values to ref

minval <- 0

maxval <- 1

# expected output

c(-0.15, 0.05, 0.45, -0.3)

Answer Source

Here's an idea:

```
((val-ref + .5) %% 1) - .5
# [1] -0.15 0.05 0.45 -0.30
```