Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad committed Nov 26, 2020
1 parent dd215b5 commit a6bdd49
Show file tree
Hide file tree
Showing 20 changed files with 751 additions and 83 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ BugReports: https://github.com/melmasri/traveltimeHMM/issues
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
RoxygenNote: 7.1.1
Suggests:
testthat,
knitr,
Expand All @@ -41,4 +41,3 @@ Suggests:
kableExtra
VignetteBuilder: knitr
Depends: R (>= 2.10)

6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(predict,traveltime)
S3method(predict,traveltime.HMM)
S3method(predict,traveltime.no_dependence)
S3method(predict,traveltimeHMM)
S3method(predict,traveltimeHMM.HMM)
S3method(predict,traveltimeHMM.no_dependence)
export(forwardback)
export(gaussian_param_by_factor)
export(getValidE)
Expand Down
32 changes: 16 additions & 16 deletions R/predict.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' Predict the travel time for a trip using a \code{traveltimeHMM} model object
#'
#' \code{predict.traveltime} performs a point prediction by simulation using parameter estimates provided by a \code{traveltimeHMM} model object.
#' \code{predict.traveltimeHMM} performs a point prediction by simulation using parameter estimates provided by a \code{traveltimeHMM} model object.
#' Prediction can be performed for a single trip only.
#'
#' The function begins by validating and, if required, replacing the value of the parameter \code{logE}
#' (see explanation alongside \code{logE} in the \emph{Arguments} section). It then transfers execution
#' to the appropriate function according to the selected model: \code{predict.traveltime.HMM} for
#' models of the \code{HMM} family, or \code{predict.traveltime.no_dependence} otherwise.
#' to the appropriate function according to the selected model: \code{predict.traveltimeHMM} for
#' models of the \code{HMM} family, or \code{predict.traveltimeHMM.no_dependence} otherwise.
#'
#' @param object A model object (a list) provided through the execution of function \code{timetravelHMM}.
#' The list includes information on model as well as estimates for its parameters.
Expand All @@ -27,7 +27,7 @@
#' @param time_bins.fun A functional to map real time to specified time bins, see `?rules2timebins`.
#' @param ... not used.
#'
#' @return \code{predict.traveltime} returns a numerical vector of size \code{n} representing the point prediction of total travel time, in seconds, for each run.
#' @return \code{predict.traveltimeHMM} returns a numerical vector of size \code{n} representing the point prediction of total travel time, in seconds, for each run.
#'
#' @examples
#' \dontrun{
Expand All @@ -41,19 +41,19 @@
#' single_trip <- subset(tripset, tripID==2700)
#'
#' # We need to supply the time stamp of the very first link traversal (third parameter)
#' pred <- predict.traveltime(fit, single_trip,single_trip$time[1])
#' pred <- predict(fit, single_trip,single_trip$time[1])
#'
#' hist(pred) # histogram of prediction samples
#' mean(pred) # travel time point estimate
#' sum(single_trip$traveltime) # observed travel time
#'
#' ?traveltimeHMM # for help on traveltimeHMM, the estimation function
#' ?predict.traveltime # for help on predict.traveltime, the prediction function
#' ?predict.traveltimeHMM # for help on predict.traveltimeHMM, the prediction function
#' }
#' @references
#' {Woodard, D., Nogin, G., Koch, P., Racz, D., Goldszmidt, M., Horvitz, E., 2017. Predicting travel time reliability using mobile phone GPS data. Transportation Research Part C, 75, 30-44.}
#' @export
predict.traveltime<-function(object, tripdata, starttime = Sys.time(), n = 1000, logE = NULL,time_bins.fun = time_bins, ... ){
predict.traveltimeHMM<-function(object, tripdata, starttime = Sys.time(), n = 1000, logE = NULL,time_bins.fun = time_bins, ... ){

# We first perform basic checks. 'tripdata' must be a list, data frame or data table
# that minimally includes objects 'linkID' and 'length', the latter having
Expand All @@ -66,18 +66,18 @@ predict.traveltime<-function(object, tripdata, starttime = Sys.time(), n = 1000
stop('length of objects do not match!')

# Models of the HMM family ('HMM', 'trip-HMM') are handled by function 'predict.traveltime.HMM'
# whilst others are handled by function 'predict.traveltime.no_dependence'
# whilst others are handled by function 'predict.traveltimeHMM.no_dependence'
# (both functions are below).
if(grepl('HMM', object$model))
predict.traveltime.HMM(object, tripdata, starttime, n, logE, time_bins = time_bins.fun, ...)
predict.traveltimeHMM.HMM(object, tripdata, starttime, n, logE, time_bins = time_bins.fun, ...)
else
predict.traveltime.no_dependence(object, tripdata , starttime, n, logE, time_bins = time_bins.fun, ...)
predict.traveltimeHMM.no_dependence(object, tripdata , starttime, n, logE, time_bins = time_bins.fun, ...)
}

#' Predict the travel time for a trip using a \code{traveltimeHMM} model object that is not of the HMM family
#' @keywords internal
#'
#' \code{predict.traveltime.no_dependence} performs a point prediction by simulation using parameter estimates provided by a \code{traveltimeHMM} model object that is not of the \code{HMM} family (see man page for \code{predict.traveltime}).
#' \code{predict.traveltimeHMM.no_dependence} performs a point prediction by simulation using parameter estimates provided by a \code{traveltimeHMM} model object that is not of the \code{HMM} family (see man page for \code{predict.traveltimeHMM}).
#'
#' The function implements Algorithm 2 from Woodard et al., 2017. However, the state transition matrix
#' and initial state probability vector are not handled as they were not generated at the estimation stage.
Expand All @@ -95,13 +95,13 @@ predict.traveltime<-function(object, tripdata, starttime = Sys.time(), n = 1000
#' @param time_bins a functional map between real time and time bins, see `?rules2timebins`.
#' @param ... not used.
#'
#' @return \code{predict.traveltime.no_dependence} returns a vector of size \code{n} of representing the point prediction of total travel time, in seconds, for each run.
#' @return \code{predict.traveltimeHMM.no_dependence} returns a vector of size \code{n} of representing the point prediction of total travel time, in seconds, for each run.
#'
#' @importFrom stats rnorm runif
#' @references
#' {Woodard, D., Nogin, G., Koch, P., Racz, D., Goldszmidt, M., Horvitz, E., 2017. Predicting travel time reliability using mobile phone GPS data. Transportation Research Part C, 75, 30-44.}
#' @export
predict.traveltime.no_dependence <- function(object, tripdata, starttime, n = 1000, logE = NULL, time_bins = time_bins, ...) {
predict.traveltimeHMM.no_dependence <- function(object, tripdata, starttime, n = 1000, logE = NULL, time_bins = time_bins, ...) {
linkIds = tripdata$linkID # Contains IDs of all links for a given trip
len = tripdata$length # Contains the length (in km) of each link in 'linkIds'
logE <- getValidE(object, logE, n) # Get a valid vector for 'logE'; see comments in function for details.
Expand Down Expand Up @@ -143,7 +143,7 @@ predict.traveltime.no_dependence <- function(object, tripdata, starttime, n = 10
#' Predict the travel time for a trip using a \code{traveltimeHMM} model object of the HMM family
#' @keywords internal
#'
#' \code{predict.traveltime.HMM} performs a point prediction by simulation using parameter estimates provided by a \code{traveltimeHMM} model object of the \code{HMM} family (see man page for \code{predict.traveltime}).
#' \code{predict.traveltime.HMM} performs a point prediction by simulation using parameter estimates provided by a \code{traveltimeHMM} model object of the \code{HMM} family (see man page for \code{predict.traveltimeHMM}).
#'
#' The function implements Algorithm 2 from Woodard et al., 2017, including its handling
#' of the state transition matrix and initial state probability vector.
Expand All @@ -161,13 +161,13 @@ predict.traveltime.no_dependence <- function(object, tripdata, starttime, n = 10
#' @param time_bins a functional map between real time and time bins, see `?rules2timebins`.
#' @param ... not used.
#'
#' @return \code{predict.traveltime.HMM} returns a vector of size \code{n} of representing the point prediction of total travel time, in seconds, for each run.
#' @return \code{predict.traveltimeHMM.HMM} returns a vector of size \code{n} of representing the point prediction of total travel time, in seconds, for each run.
#'
#' @importFrom stats rnorm runif
#' @references
#' {Woodard, D., Nogin, G., Koch, P., Racz, D., Goldszmidt, M., Horvitz, E., 2017. Predicting travel time reliability using mobile phone GPS data. Transportation Research Part C, 75, 30-44.}
#' @export
predict.traveltime.HMM <- function(object, tripdata, starttime, n, logE, time_bins = time_bins, ...) {
predict.traveltimeHMM.HMM <- function(object, tripdata, starttime, n, logE, time_bins = time_bins, ...) {
linkIds = tripdata$linkID # Contains IDs of all links for a given trip
len = tripdata$length # Contains the length (in km) of each link in 'linkIds'
logE <- getValidE(object, logE, n) # Get a valid vector for 'logE'; see comments in function for details.
Expand Down
2 changes: 1 addition & 1 deletion R/traveltimeHMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ traveltimeHMM <- function(logspeeds = NULL, trips = NULL, timeBins = NULL, linkI
nB = nB,
nObs = nObs,
model = model)
class(obj) <- append(class(obj),"traveltime", after=0)
class(obj) <- append(class(obj),"traveltimeHMM", after=0)
invisible(obj)
}

Empty file added R/zzz.R
Empty file.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ library(traveltimeHMM)
data(tripset)
head(tripset)
#> tripID linkID timeBin logspeed traveltime length time
#> 1 2700 10469 Weekday 1.692292 13.000000 70.61488 2014-04-28 03:07:27
#> 2 2700 10444 Weekday 2.221321 18.927792 174.50487 2014-04-28 03:07:41
#> 3 2700 10460 Weekday 2.203074 8.589937 77.76295 2014-04-28 03:07:58
#> 4 2700 10462 Weekday 1.924290 14.619859 100.15015 2014-04-28 03:08:07
#> 5 2700 10512 Weekday 1.804293 5.071986 30.81574 2014-04-28 03:08:21
#> 6 2700 5890 Weekday 2.376925 31.585355 340.22893 2014-04-28 03:08:26
#> 1 2700 10469 Weekday 1.692292 13.000000 70.61488 2014-04-28 06:07:27
#> 2 2700 10444 Weekday 2.221321 18.927792 174.50487 2014-04-28 06:07:41
#> 3 2700 10460 Weekday 2.203074 8.589937 77.76295 2014-04-28 06:07:58
#> 4 2700 10462 Weekday 1.924290 14.619859 100.15015 2014-04-28 06:08:07
#> 5 2700 10512 Weekday 1.804293 5.071986 30.81574 2014-04-28 06:08:21
#> 6 2700 5890 Weekday 2.376925 31.585355 340.22893 2014-04-28 06:08:26
```

To fit a simple `HMM` model use the following code
Expand Down
14 changes: 7 additions & 7 deletions docs/articles/traveltimeHMM.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a6bdd49

Please sign in to comment.