regularise_coord: Define an (optimised) regular coordinate

Description Usage Arguments Examples

View source: R/regularise.R

Description

Based on an original coordinate, define a new one that is regular and as close as possible to the original one, overall.

Usage

1
regularise_coord(x, start = NULL, step = NULL, control = NULL, ...)

Arguments

x

vector containing the original coordinate; there are methods for numeric, POSIXt (date and times), and Date objects.

start

initial guess for the starting point of the regular coordinate. If NULL, set to the current starting point (i.e. the minimum value of x).

step

initial guess for the step of the regular coordinate. If NULL, set so that the new coordinate spans the entire range of x.

control

passed as the control argument of stats::optim(). parscale is already set.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Plot original and regularised coordinates
pc <- function(x, xr, ...) {
  plot(x, rep(1, length(x)), type="n",
       xlim=range(c(x, xr)), ylim=c(0.8, 1.2),
       ylab="", yaxt="n")
  abline(h=1, lty="22")
  segments(x0=x, y0=1, x1=x, y1=1.1)
  segments(x0=xr, y0=1, x1=xr, y1=0.9)
  axis(2, at=c(1.05, 0.95), labels=c("original", "regularised"))
}

# Numeric coordinate
# Already regular coordinate
x <- 1:10
pc(x, regularise_coord(x))

# Random coordinate
set.seed(1)
x <- sort(runif(10))
pc(x, regularise_coord(x))
pc(x, regularise_coord(x, step=0.05))

# Almost regular coordinate
set.seed(1)
x <- jitter(1:10, factor=0.5)
pc(x, regularise_coord(x))

# Regular coordinate with additions
x <- sort(c(1:10, 5.2))
pc(x, regularise_coord(x))

# Regular coordinate with deletions
x <- (1:10)[-c(3)]
pc(x, regularise_coord(x))

# Regular coordinate with outliers
x <- c(1:10, 12.9)
pc(x, regularise_coord(x))
x <- c(1:10, 19.9)
pc(x, regularise_coord(x))
# -> not ideal...

# Times
set.seed(1)
x <- as.POSIXct("2010-01-01 00:00:00", tz="UTC") + jitter(1:10) * 10
pc(x, regularise_coord(x))
x <- as.POSIXct("2010-01-01 00:00:00", tz="UTC") + jitter(1:10) * 3600 * 24
pc(x, regularise_coord(x))

# Dates
set.seed(1)
x <- as.Date("2010-01-01") + round(jitter(1:10)*20)
pc(x, regularise_coord(x))

jiho/castr documentation built on April 5, 2020, 2:12 p.m.