Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
onlineforecast
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
packages
onlineforecast
Commits
08ae69e7
Commit
08ae69e7
authored
3 years ago
by
pbac
Browse files
Options
Downloads
Patches
Plain Diff
made periodic function
parent
378fe5c1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
R/make_periodic.R
+46
-0
46 additions, 0 deletions
R/make_periodic.R
R/make_tday.R
+17
-13
17 additions, 13 deletions
R/make_tday.R
with
63 additions
and
13 deletions
R/make_periodic.R
0 → 100644
+
46
−
0
View file @
08ae69e7
## Do this in a separate tmp.R file to check the documentation
#library(devtools)
#document()
#load_all(as.package("../../onlineforecast"))
#?make_periodic
#' Make an forecast matrix with a periodic time signal.
#'
#' This function creates a data.frame with k-steps-ahead values of a periodic time signal,
#' such that it can be added to a data.list and used inputs to a forecast model.
#'
#' @param time vector of times of class "POSIXct" "POSIXt".
#' @param kseq vector of integers, representing the desired "k-steps ahead".
#' @param period a numeric setting the length of the period in seconds.
#' @param offset a numeric setting an offset in the period start in seconds.
#' @param tstep step time of k in seconds.
#' @return Returns a forecast matrix (data.frame) with rownames = times, colnames = k1, k2, k3, ...
#' The content of the data frame is the hour of day.
#' @keywords periodic lags data.frame
#' @seealso make_tday
#' @examples
#' # Create a time sequence of 30 min sample period
#' tseq <- seq(ct("2019-01-01"), ct("2019-02-01 12:00"), by=1800)
#'
#' # Make the three hourly periodic sequence
#' make_periodic(tseq, 1:10, 3*3600)
#'
#' # With an offset of one hour
#' make_periodic(tseq, 1:10, 3*3600, 3600)
#'
#' @export
make_periodic
<-
function
(
time
,
kseq
,
period
,
offset
=
0
,
tstep
=
NA
){
# If tstep not given, then take it from time assuming same k-step is the sampling period
if
(
is.na
(
tstep
)){
tstep
<-
as.numeric
(
time
[
2
]
-
time
[
1
],
units
=
"secs"
)
}
# The time of day (in the specified units)
tday
<-
sapply
(
kseq
,
function
(
k
){
tk
<-
time
+
k
*
tstep
-
offset
as.numeric
(
tk
,
units
=
"secs"
)
%%
period
})
# set row and column names
nams
(
tday
)
<-
paste0
(
'k'
,
kseq
)
return
(
as.data.frame
(
tday
)
)
}
This diff is collapsed.
Click to expand it.
R/make_tday.R
+
17
−
13
View file @
08ae69e7
...
@@ -4,38 +4,42 @@
...
@@ -4,38 +4,42 @@
#load_all(as.package("../../onlineforecast"))
#load_all(as.package("../../onlineforecast"))
#?make_tday
#?make_tday
#' Make an hour-of-day
data.frame with k-step ahead columns.
#' Make an hour-of-day
forecast matrix
#'
#'
#' This function creates a data.frame with k-steps-ahead values of hour of day,
#' This function creates a data.frame with k-steps-ahead values of hour of day,
#' such that it can be added to a data.list and used inputs to a forecast model.
#' such that it can be added to a data.list and used inputs to a forecast model.
#'
#'
#' @param time vector of times of class "POSIXct" "POSIXt".
#' @param time vector of times of class "POSIXct" "POSIXt".
#' @param kseq vector of integers, re
s
presenting the desired "k-steps ahead".
#' @param kseq vector of integers, representing the desired "k-steps ahead".
#' @param tstep step time of k in seconds.
#' @param tstep step time of k in seconds.
#' @param units to return in, e.g. "hours" or "mins"
#' @return Returns a forecast matrix (data.frame) with rownames = times, colnames = k1, k2, k3, ...
#' @return Returns a data.frame with rownames = times, colnames = k1, k2, k5, ...
#' The content of the data frame is the hour of day.
#' The content of the data frame is the hour of day, following the setup in "onlineforecast" setup.
#' @keywords hourofday lags data.frame
#' @keywords hourofday lags data.frame
#' @seealso make_periodic
#' @examples
#' @examples
#' # Create a time sequence
#' # Create a time sequence
of 30 min sample period
#' tseq <- seq(ct("2019-01-01"), ct("2019-02-01 12:00"), by=1800)
#' tseq <- seq(ct("2019-01-01"), ct("2019-02-01 12:00"), by=1800)
#'
#'
#' # Make the time of day sequence
#' # Make the time of day sequence
(assuming time between k steps is same as for tseq)
#' make_tday(tseq, 1:10)
#' make_tday(tseq, 1:10)
#'
#'
#' # With 0.5 hour steps
and in minute
s
#' # With 0.5 hour steps
, kstep in hour
s
#' make_tday(tseq, 1:10, tstep=
1800, units="mins"
)
#' make_tday(tseq, 1:10, tstep=
3600
)
#'
#'
#'
#'
#' @export
#' @export
make_tday
<-
function
(
time
,
kseq
,
tstep
=
3600
,
units
=
"hours"
){
make_tday
<-
function
(
time
,
kseq
,
tstep
=
NA
){
## The time of day (in the specified units)
# If tstep not given, then take it from time assuming same k-step is the sampling period
if
(
is.na
(
tstep
)){
tstep
<-
as.numeric
(
time
[
2
]
-
time
[
1
],
units
=
"secs"
)
}
# The time of day (in the specified units)
tday
<-
sapply
(
kseq
,
function
(
k
){
tday
<-
sapply
(
kseq
,
function
(
k
){
tk
<-
time
+
k
*
tstep
tk
<-
time
+
k
*
tstep
as.numeric
(
tk
-
trunc
(
tk
,
units
=
"days"
),
units
=
units
)
as.numeric
(
tk
-
trunc
(
tk
,
units
=
"days"
),
units
=
"hours"
)
})
})
#
#
set row and column names
# set row and column names
nams
(
tday
)
<-
paste0
(
'k'
,
kseq
)
nams
(
tday
)
<-
paste0
(
'k'
,
kseq
)
return
(
as.data.frame
(
tday
)
)
return
(
as.data.frame
(
tday
)
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment