Skip to content
Snippets Groups Projects
Commit 6030a692 authored by pbac's avatar pbac
Browse files

asct() to act()

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