Commit 6030a692 authored by pbac's avatar pbac
Browse files

asct() to act()

parent dba283c4
......@@ -50,7 +50,7 @@
#' fit <- rls_fit(c(lambda=0.99), model, D, returnanalysis=TRUE)
#'
#' # Plot the result, see "?plot_ts.rls_fit"
#' plot_ts(fit, xlim=c(asct("2010-12-20"),max(D$t)))
#' plot_ts(fit, xlim=c(act("2010-12-20"),max(D$t)))
#' # Plot for a short period with peaks
#' plot_ts(fit, xlim=c("2011-01-05","2011-01-07"))
#'
......
......@@ -2,8 +2,8 @@
#library(devtools)
#document()
#load_all(as.package("../../onlineforecast"))
#?asct
#?asct.default
#?act
#?act.default
#' The object is converted into POSIXct with tz="GMT".
#'
......@@ -19,16 +19,16 @@
#'
#'
#' # Create a POSIXct with tz="GMT"
#' asct("2019-01-01")
#' class(asct("2019-01-01"))
#' asct("2019-01-01 01:00:05")
#' act("2019-01-01")
#' class(act("2019-01-01"))
#' act("2019-01-01 01:00:05")
#'
#'
#' # Convert to POSIXct
#' class(asct(as.POSIXlt("2019-01-01")))
#' class(act(as.POSIXlt("2019-01-01")))
#'
#' # To seconds and back again
#' asct(as.numeric(1000, units="sec"))
#' act(as.numeric(1000, units="sec"))
#'
#'
#' # --------
......@@ -45,20 +45,20 @@
#' "2019-03-31 01:30",
#' "2019-03-31 03:00",
#' "2019-03-31 03:30")
#' x <- asct(txt, tz="CET")
#' x <- act(txt, tz="CET")
#' x
#' asct(x, tz="GMT")
#' act(x, tz="GMT")
#'
#' # BE AWARE of this conversion of the 02:00: to 02:59:59 (exact time of shift) will lead to a
#' # wrong conversion
#' txt <- c("2019-03-31 01:30",
#' "2019-03-31 02:00",
#' "2019-03-31 03:30")
#' x <- asct(txt, tz="CET")
#' x <- act(txt, tz="CET")
#' x
#' asct(x, tz="GMT")
#' act(x, tz="GMT")
#' # Which a diff on the time can detect, since all steps are not equal
#' plot(diff(asct(x, tz="GMT")))
#' plot(diff(act(x, tz="GMT")))
#'
#' # --------
#' # Shift to winter time is more problematic
......@@ -68,9 +68,9 @@
#' "2019-10-27 02:30",
#' "2019-10-27 03:00",
#' "2019-10-27 03:30")
#' x <- asct(txt, tz="CET")
#' x <- act(txt, tz="CET")
#' x
#' asct(x, tz="GMT")
#' act(x, tz="GMT")
#'
#' # however, timestamps can be given like this
#' txt <- c("2019-10-27 01:30",
......@@ -80,35 +80,35 @@
#' "2019-10-27 02:30",
#' "2019-10-27 03:00",
#' "2019-10-27 03:30")
#' x <- asct(txt, tz="CET")
#' x <- act(txt, tz="CET")
#' x
#' asct(x, tz="GMT")
#' act(x, tz="GMT")
#' # Again can be detected, since all steps are not equal
#' plot(diff(asct(x, tz="GMT")))
#' plot(diff(act(x, tz="GMT")))
#' # This can be fixed by (note that it can go wrong, e.g. with gaps around convertion etc.)
#' asct(x, tz="GMT", duplicatedadd=3600)
#' act(x, tz="GMT", duplicatedadd=3600)
#'
#' @export
asct <- function(object, ...){
UseMethod("asct")
act <- function(object, ...){
UseMethod("act")
}
#' @rdname asct
#' @rdname act
#' @section Methods:
#' - asct.character: Simply a wrapper for \code{as.POSIXct} with default \code{tz}
#' - act.character: Simply a wrapper for \code{as.POSIXct} with default \code{tz}
#' @export
asct.character <- function(object, tz = "GMT", ...){
act.character <- function(object, tz = "GMT", ...){
as.POSIXct(object, tz=tz, ...)
}
#' @rdname asct
#' @rdname act
#' @param duplicatedadd Seconds to be added to duplicated time stamps, to mitigate the problem of duplicated timestamps at the shift to winter time. So the second time a time stamp occurs (identified with \code{duplicated}) then the seconds will be added.
#' @section Methods:
#' - asct.POSIXct: Changes the time zone of the object if \code{tz} is given.
#' - act.POSIXct: Changes the time zone of the object if \code{tz} is given.
#' @export
asct.POSIXct <- function(object, tz = NA, duplicatedadd = NA, ...){
act.POSIXct <- function(object, tz = NA, duplicatedadd = NA, ...){
if(!is.na(tz)){
attr(object, "tzone") <- tz
}
......@@ -120,18 +120,18 @@ asct.POSIXct <- function(object, tz = NA, duplicatedadd = NA, ...){
return(object)
}
#' @rdname asct
#' @rdname act
#' @section Methods:
#' - asct.POSIXlt: Converts to POSIXct.
#' - act.POSIXlt: Converts to POSIXct.
#' @export
asct.POSIXlt <- function(object, tz = NA, duplicatedadd = NA, ...){
as.POSIXct(asct.POSIXct(object, tz, duplicatedadd), ...)
act.POSIXlt <- function(object, tz = NA, duplicatedadd = NA, ...){
as.POSIXct(act.POSIXct(object, tz, duplicatedadd), ...)
}
#' @rdname asct
#' @rdname act
#' @section Methods:
#' - asct.numeric: Converts from UNIX time in seconds to POSIXct with \code{tz} as GMT.
#' - act.numeric: Converts from UNIX time in seconds to POSIXct with \code{tz} as GMT.
#' @export
asct.numeric <- function(object, ...){
act.numeric <- function(object, ...){
ISOdate(1970, 1, 1, 0, ...) + object
}
......@@ -2,8 +2,8 @@
#library(devtools)
#document()
#load_all(as.package("../../onlineforecast"))
#?aslt
#?aslt.default
#?anlt
#?anlt.default
#' The argument is converted into POSIXlt with tz="GMT".
#'
......@@ -18,56 +18,56 @@
#' #' @examples
#'
#' # Create a POSIXlt with tz="GMT"
#' aslt("2019-01-01")
#' class(aslt("2019-01-01"))
#' aslt("2019-01-01 01:00:05")
#' anlt("2019-01-01")
#' class(anlt("2019-01-01"))
#' anlt("2019-01-01 01:00:05")
#'
#' # Convert between time zones
#' x <- aslt("2019-01-01", tz="CET")
#' aslt(x,tz="GMT")
#' x <- anlt("2019-01-01", tz="CET")
#' anlt(x,tz="GMT")
#'
#' # To seconds and back again
#' aslt(as.numeric(x, units="sec"))
#' anlt(as.numeric(x, units="sec"))
#'
#' @export
aslt <- function(object, ...){
UseMethod("aslt")
anlt <- function(object, ...){
UseMethod("anlt")
}
#' @rdname aslt
#' @rdname anlt
#' @section Methods:
#' - aslt.character: Simply a wrapper for \code{as.POSIXlt}
#' - anlt.character: Simply a wrapper for \code{as.POSIXlt}
#' @export
aslt.character <- function(object, tz = "GMT", ...){
anlt.character <- function(object, tz = "GMT", ...){
as.POSIXlt(object, tz = tz, ...)
}
#' @rdname aslt
#' @rdname anlt
#' @section Methods:
#' - aslt.POSIXct: Converts to POSIXct.
#' - anlt.POSIXct: Converts to POSIXct.
#' @export
aslt.POSIXct <- function(object, tz = NA, ...){
anlt.POSIXct <- function(object, tz = NA, ...){
if(!is.na(tz)){
attr(object, "tzone") <- tz
}
as.POSIXlt(object, ...)
}
#' @rdname aslt
#' @rdname anlt
#' @section Methods:
#' - aslt.POSIXlt: Changes the time zone of the object if tz is given.
#' - anlt.POSIXlt: Changes the time zone of the object if tz is given.
#' @export
aslt.POSIXlt <- function(object, tz = NA, ...){
anlt.POSIXlt <- function(object, tz = NA, ...){
if(!is.na(tz)){
attr(object, "tzone") <- tz
}
return(object)
}
#' @rdname aslt
#' @rdname anlt
#' @section Methods:
#' - aslt.numeric: Converts from UNIX time in seconds to POSIXlt.
#' - anlt.numeric: Converts from UNIX time in seconds to POSIXlt.
#' @export
aslt.numeric <- function(object, ...){
anlt.numeric <- function(object, ...){
as.POSIXlt(ISOdate(1970, 1, 1, 0, ...) + object)
}
......@@ -23,7 +23,7 @@
#' @examples
#' # Put together a data.list
#' # The time vector
#' time <- seq(asct("2019-01-01"),asct("2019-01-02"),by=3600)
#' time <- seq(act("2019-01-01"),act("2019-01-02"),by=3600)
#' # Observations time series (as vector)
#' x.obs <- rnorm(length(time))
#' # Forecast input as data.frame
......
......@@ -9,7 +9,7 @@
#' @return Returns a list of dataframes (two for each i in \code{1:nharmonics}) with same number of columns as X.
#' @examples
#' # Make a data.frame with time of day in hours for different horizons
#' tday <- make_tday(seq(asct("2019-01-01"), asct("2019-01-04"), by=3600), kseq=1:5)
#' tday <- make_tday(seq(act("2019-01-01"), act("2019-01-04"), by=3600), kseq=1:5)
#' # See whats in it
#' str(tday)
#' head(tday)
......
......@@ -48,10 +48,10 @@
in_range <- function(tstart, time, tend=NA) {
if (class(tstart)[1] == "character")
tstart <- asct(tstart)
tstart <- act(tstart)
if (is.na(tend))
tend <- time[length(time)]
if (class(tend)[1] == "character")
tend <- asct(tend)
asct(tstart) < time & time <= asct(tend)
tend <- act(tend)
act(tstart) < time & time <= act(tend)
}
......@@ -18,7 +18,7 @@
#' @keywords hourofday lags data.frame
#' @examples
#' # Create a time sequence
#' tseq <- seq(asct("2019-01-01"), asct("2019-02-01 12:00"), by=1800)
#' tseq <- seq(act("2019-01-01"), act("2019-02-01 12:00"), by=1800)
#'
#' # Make the time of day sequence
#' make_tday(tseq, 1:10)
......
......@@ -32,7 +32,7 @@
#' @examples
#'
#' # Generate some test data with 10 minutes sampling frequency for one day
#' X <- data.frame(t=seq(asct("2019-01-01 00:10"),asct("2019-01-02"), by=10*60))
#' X <- data.frame(t=seq(act("2019-01-01 00:10"),act("2019-01-02"), by=10*60))
#'
#' # A single sine over the day
#' X$val <- sin(as.numeric(X$t)/3600*2*pi/(24))
......@@ -73,8 +73,8 @@ resample.data.frame <- function(object, ts, tstart=NA, tend=NA, timename="t", fu
# ----------------------------------------------------------------
# Convert to POSIXct
tstart <- asct(tstart)
tend <- asct(tend)
tstart <- act(tstart)
tend <- act(tend)
# ----------------------------------------------------------------
# Cut out the time period
......
......@@ -2,7 +2,7 @@
#library(devtools)
#document()
#load_all(as.package("../../onlineforecast"))
#?aslt
#?anlt
#' Setting \code{\link{par}()} plotting parameters to a set of default values
#'
......@@ -18,7 +18,7 @@
#' @examples
#'
#' # Make some data
#' D <- data.frame(t=seq(asct("2020-01-01"),asct("2020-01-10"),len=100), x=rnorm(100), y=runif(100))
#' D <- data.frame(t=seq(act("2020-01-01"),act("2020-01-10"),len=100), x=rnorm(100), y=runif(100))
#'
#' # Generate two stacked plots with same x-axis
#' setpar("ts", mfrow=c(2,1))
......
......@@ -16,7 +16,7 @@ load_all(pack)
# Importing data # First unzip to get the .csv system('unzip
# ../data/DataSoenderborg.zip')
data_or <- fread("data_soenderborg.csv", sep = ",", header = TRUE)
data_or[, `:=`(t, asct(data_or$t))]
data_or[, `:=`(t, act(data_or$t))]
setDF(data_or)
names(data_or)[names(data_or) == "Ig.obs"] <- "I.obs"
......@@ -56,7 +56,7 @@ data[["sunElevation"]] <- data_or[, "sunElevation.obs"]
# # The time of day
# ncol <- ncol(data$Ta)
# tmp <- aslt(data$t)$hour
# tmp <- anlt(data$t)$hour
# tmp <- matrix(tmp, nrow = length(tmp), ncol = ncol)
# tmp <- data.frame(t(t(tmp) + (0:(ncol - 1))))
# names(tmp) <- pst("k", 0:(ncol - 1))
......
......@@ -43,6 +43,7 @@ library(roxygen2)
#use_test("newtest")
# # Run all tests
document()
test()
# # Run the examples
......
......@@ -447,7 +447,7 @@ A pairs plot with residuals and inputs to see if patterns are left:
```{r plotpairs, fig.height=figwidth}
kseq <- c(1,36)
D$Residuals <- residuals(fit2)[ ,pst("h",kseq)]
D$hour <- aslt(D$t)$hour
D$hour <- anlt(D$t)$hour
pairs(D, subset=D$scoreperiod, pattern="Residuals|Ta|I|hour|^t$", kseq=kseq)
```
So inspecting the two upper rows, there are no clear patterns to be seen for the
......
......@@ -180,9 +180,9 @@ A helper function is provided with the `asp` function which can be called using
```{r}
## Convert from a time stamp (tz="GMT" per default)
asct("2019-01-01 11:00")
act("2019-01-01 11:00")
## Convert from unix time
asct(3840928387)
act(3840928387)
```
Note that for all functions where a time value as a character is given, the time
zone is always "GMT" (or "UTC", but this can result in warnings, but they can be
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment