Skip to content

Commit

Permalink
added population model
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad committed Nov 26, 2020
1 parent 4f59a48 commit b40403e
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 70 deletions.
47 changes: 47 additions & 0 deletions R/population_estimates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Estimates the population mean and variance
#'
#' \code{population_estimates} estimates the population mean, variance of travel time
#'
#' @param data A data frame of trips and their road level travel information, formatted as \code{trips}, see \code{trips} or \code{data(trips); View(trips)}.
#' @param nsamples The number of trips to sample for parameter estimation. Default is 500.
#' @param level Significance level. Default is 0.95.
#'
#' @details
#'
#' @return
#'
#' @examples
#' \dontrun{
#'
#'
#' }
#' @import data.table
#' @export
population_estimates <- function(data, nsamples=500L, level = 0.95){

if(!'data.table' %in% class(data)) data = data.table(data)
if(!is.null(nsamples)){
samp = sample_trips(data, nsamples)
dt = data[tripID %in% samp][order(tripID, entry_time)]
}else dt = data

tr = dt[,.(dur = sum(duration_secs)/.N),tripID]

hatmu = mean(tr$dur)
hatsigmu = var(tr$dur)
m = length(unique(dt$tripID))
alpha = level + (1-level)/2
q = qt(alpha, df = m-1)
lwr = hatmu - q * sqrt(hatsigmu/m)
upr = hatmu + q * sqrt(hatsigmu/m)
En = dt[, .N, tripID][, mean(1/N)]

list(mu = hatmu,
mu.CI = c(lwr, upr) ,
nsamples = nsamples,
sigma2 = hatsigmu,
En = En,
sigma.prof = sqrt(hatsigmu/En))
}


32 changes: 32 additions & 0 deletions R/predict.traveltimeCLT.population.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Predict trip-specific travel time
#'
#' \code{predict.traveltimeCLT.trip_specific} returns the predicted mean and variance of travel time for a specific route and start time.
#'
#' @param N Number of links in a route.
#' @param population_parameters An output of \code{population_estimates}, see \code{?population_estimates}.
#' @param level Significance level.
#'
#' @details NULL.
#'
#' @return Returns a list of predictions.
#'
#' @examples
#' \dontrun{
#'
#' }
#' @import data.table
#' @export
predict.traveltimeCLT.population <- function(N, population_parameters, level = 0.95){

mu = population_parameters$mu
m = population_parameters$nsamples
sig = population_parameters$sigma.prof
ETA = N * mu
q = abs(qnorm((1 - level)/2))

lwr = ETA - q * sig * sqrt(N * (1 + 1/m))
upr = ETA + q * sig * sqrt(N * (1 + 1/m))

list(ETA = N * mu, lwr = lwr, upr =upr)
}

4 changes: 2 additions & 2 deletions man/from_to_format.Rd

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

15 changes: 9 additions & 6 deletions man/link_mean_variance.Rd

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

30 changes: 30 additions & 0 deletions man/population_estimates.Rd

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

26 changes: 12 additions & 14 deletions man/predict.traveltimeCLT.Rd

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

29 changes: 29 additions & 0 deletions man/predict.traveltimeCLT.population.Rd

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

23 changes: 14 additions & 9 deletions man/predict.traveltimeCLT.trip_specific.Rd

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

26 changes: 12 additions & 14 deletions man/residual_autocorrelation.Rd

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

28 changes: 14 additions & 14 deletions man/residual_variance.Rd

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

Loading

0 comments on commit b40403e

Please sign in to comment.