traveltimeHMM: Estimate trip- and link- specific speed parameters from...

View source: R/traveltimeHMM.R

traveltimeHMMR Documentation

Estimate trip- and link- specific speed parameters from observed average speeds

Description

traveltimeHMM estimates trip- and link- specific speed parameters from observed average speeds per unique trip and link.

Usage

traveltimeHMM(
  logspeeds = NULL,
  trips = NULL,
  timeBins = NULL,
  linkIds = NULL,
  data = NULL,
  nQ = 1L,
  model = c("HMM", "trip-HMM", "trip", "no-dependence"),
  tol.err = 10,
  L = 10L,
  max.it = 20L,
  verbose = FALSE,
  max.speed = NULL,
  seed = NULL,
  tmat.p = NULL,
  init.p = NULL
)

Arguments

logspeeds

A numeric vector of speed observations (in km/h) on the (natural) log-scale. Needs to be provided if data is NULL, otherwise it will be overwritten by logspeeds column in data. Default is NULL.

trips

An integer or character vector of trip ids for each observation of speed. Needs to be provided if data is NULL, otherwise it will be overwritten by trips column in data. Default is NULL.

timeBins

A character vector of time bins for each observation of speed. Needs to be provided if data is NULL, otherwise it will be overwritten by timeBins column in data. Default is NULL.

linkIds

A vector of link ids (route or way) for each observation of speed. Needs to be provided if data is NULL, otherwise it will be overwritten by linkIds column in data. Default is NULL.

data

A data frame or equivalent object that contains one column for each of logspeeds, trips, timeBins and linkIds. Default is NULL. This argument is mutually exclusive with the full joint set of logspeeds, trips, timeBins and linkIds. One can either pass the latter vectors explicitly or as a dataframe data.

nQ

An integer corresponding to the number of different congestion states that the traversal of a given link can take corresponding to |{1...., Q}|. Hidden Markov models (see table 2) require nQ >= 2 whilst other models require exactly nQ = 1. As an example, Woodard uses nQ = 1. Default is 1.

model

Type of model as string, trip-HMM to use a hidden Markov model (HMM) with trip effect, HMM (default) is an HMM without trip effect, trip is trip effect model without HMM, and no-dependence is model with link specific parameter only without an HMM nor a trip effect.

tol.err

A numeric variable representing the threshold under which the estimation algorithm will consider it has reached acceptable estimate values. Default is 10.

L

An integer minimum number of observations per factor (linkIds x timeBins) to estimate the parameter for. Default is 10. Factors that have fewer total observations or initial state observations than L have their estimates imputed using time bin data, without regard to road link data.

max.it

An integer for the upper limit of the iterations to be performed by the estimation algorithm. Default is 20.

verbose

A boolean that triggers verbose output. Default is FALSE.

max.speed

An optional float for the maximum speed in km/h, on the linear scale (not the log-scale, unlike for logspeeds). Default is NULL which results in a maximum speed of 130 km/h. If there is any occurrence of speed in excel of max.speed, then the system issues to the user a warning that includes the percentage of such occurrences.

seed

An optional float for the seed used for the random generation of the first Markov transition matrix and initial state vector. Default is NULL. If not provided, then those objects are generated deterministically. The effect of seed is cancelled by tmat.p or init.p when provided.

tmat.p

An optional starting value for the Markov transition matrix Γ of size nQ x nQ with rows summing to 1. Default is NULL

init.p

An optional starting value for the Markov initial state vector γ of size nQ with elements summing to 1. Default is NULL.

Value

traveltimeHMM returns a list of the following parameters.

factors

a factor of interactions (linkId x timeBin) of length nObs. Factors are in the format linkId.timeBin.

trip

a factor of trip IDs.

tmat

a transition matrix with rows corresponding to levels(factors), and with columns being the row-wise transition matrix of that factor. For example, matrix(tmat[1,], ncol = nQ, nrow = nQ, byrow = TRUE) is the transition matrix of levels(factors)[1]. NULL if hidden Markov modelling is not handled by the selected model type.

init

a initial state probability matrix with rows corresponding to levels(factors), and columns to the nQ states. NULL if hidden Markov modelling is not handled by the selected model type.

sd

a matrix of standard deviations estimates for the natural logarithm of the speed (in km/h), with rows corresponding to levels(factors), and columns to standard deviation estimates for the nQ states.

mean

a matrix of mean estimates for the natural logarithm of the speed (in km/h), with rows corresponding to levels(factors), and columns to mean estimates for the nQ states.

tau

a numeric variable for the standard deviation estimate for the trip effect parameter logE. Equals 1 if trip effect is not handled by the selected model type.

logE

a numeric vector of trip effect estimates corresponding to levels(trip). Values are set to 0 if trip effect is not handled by the selected model type. Units are the same as for logspeeds.

nQ

an integer corresponding to the number of different congestion states, equal to the parameter nQ that was passed in the function call.

nB

an integer number of unique time bins.

nObs

an integer number of observations.

model

a character string corresponding to the type of model used.

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.

Examples

## Not run: 
data(tripset)

# Fit an HMM model with 2 hidden congestion states and 20 algorithm iterations
fit <- traveltimeHMM(tripset$logspeed, tripset$tripID, tripset$timeBin, 
                      tripset$linkID, nQ = 2, max.it = 20)

# Perform prediction - use ?predict.traveltime for details
single_trip <- subset(tripset, tripID==2700)
pred <- predict.traveltime(fit, single_trip,single_trip$time[1])
hist(pred)
mean(pred)
sum(single_trip$traveltime)

?traveltimeHMM      # for help on traveltimeHMM, the estimation function
?predict.traveltime # for help on predict.traveltime, the prediction function

## End(Not run)


melmasri/traveltimeHMM documentation built on Jan. 6, 2023, 10:30 p.m.