| Version: | 0.2.0 |
| Title: | Bayesian Kernelized Tensor Regression |
| Description: | Facilitates scalable spatiotemporally varying coefficient modelling with Bayesian kernelized tensor regression. The important features of this package are: (a) Enabling local temporal and spatial modeling of the relationship between the response variable and covariates. (b) Implementing the model described by Lei et al. (2023) <doi:10.48550/arXiv.2109.00046>. (c) Using a Bayesian Markov Chain Monte Carlo (MCMC) algorithm to sample from the posterior distribution of the model parameters. (d) Employing a tensor decomposition to reduce the number of estimated parameters. (e) Accelerating tensor operations and enabling graphics processing unit (GPU) acceleration with the 'torch' package. |
| Depends: | R (≥ 4.0.0) |
| Encoding: | UTF-8 |
| Imports: | torch (≥ 0.13.0), R6, R6P, ggplot2, ggmap, data.table |
| License: | MIT + file LICENSE |
| RoxygenNote: | 7.2.3 |
| Collate: | 'samplers.R' 'tensor_ops.R' 'result_logger.R''likelihood_evaluator.R' 'distances.R' 'kernels.R' 'bktr.R''examples.R' 'plots.R' 'utils.R' |
| Suggests: | knitr, rmarkdown, R.rsp |
| LazyData: | true |
| VignetteBuilder: | knitr, rmarkdown, R.rsp |
| BugReports: | https://github.com/julien-hec/BKTR/issues |
| NeedsCompilation: | no |
| Packaged: | 2024-08-18 14:27:01 UTC; juju |
| Author: | Julien Lanthier |
| Maintainer: | Julien Lanthier <julien.lanthier@hec.ca> |
| Repository: | CRAN |
| Date/Publication: | 2024-08-18 14:40:02 UTC |
Operator overloading for kernel multiplication
Description
Operator overloading for kernel multiplication
Usage
## S3 method for class 'Kernel'k1 * k2Arguments
k1 | Kernel: The left kernel to use for composition |
k2 | Kernel: The right kernel to use for composition |
Value
A newKernelMulComposed object.
Examples
# Create a new locally periodic kernelk_loc_per <- KernelSE$new() * KernelPeriodic$new()# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_loc_per$set_positions(positions_df)# Generate the kernel's covariance matrixk_loc_per$kernel_gen()Operator overloading for kernel addition
Description
Operator overloading for kernel addition
Usage
## S3 method for class 'Kernel'k1 + k2Arguments
k1 | Kernel: The left kernel to use for composition |
k2 | Kernel: The right kernel to use for composition |
Value
A newKernelAddComposed object.
Examples
# Create a new additive kernelk_rq_plus_per <- KernelRQ$new() + KernelPeriodic$new()# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_rq_plus_per$set_positions(positions_df)# Generate the kernel's covariance matrixk_rq_plus_per$kernel_gen()R6 class encapsulating the BKTR regression elements
Description
A BKTRRegressor holds all the key elements to accomplish the MCMC samplingalgorithm (Algorithm 1 of the paper).
Public fields
data_dfThe dataframe containing all the covariates through time and space (includingthe response variable)
yThe response variable tensor
omegaThe tensor indicating which response values are not missing
covariatesThe tensor containing all the covariates
covariates_dimThe dimensions of the covariates tensor
logged_params_tensorThe tensor containing all the sampled hyperparameters
tauThe precision hyperparameter
spatial_decompThe spatial covariate decomposition
temporal_decompThe temporal covariate decomposition
covs_decompThe feature covariate decomposition
result_loggerThe result logger instance used to store the results of the MCMC sampling
has_completed_samplingBoolean showing wheter the MCMC sampling has been completed
spatial_kernelThe spatial kernel used
temporal_kernelThe temporal kernel used
spatial_positions_dfThe dataframe containing the spatial positions
temporal_positions_dfThe dataframe containing the temporal positions
spatial_params_samplerThe spatial kernel hyperparameter sampler
temporal_params_samplerThe temporal kernel hyperparameter sampler
tau_samplerThe tau hyperparameter sampler
precision_matrix_samplerThe precision matrix sampler
spatial_ll_evaluatorThe spatial likelihood evaluator
temporal_ll_evaluatorThe temporal likelihood evaluator
rank_decompThe rank of the CP decomposition
burn_in_iterThe number of burn in iterations
sampling_iterThe number of sampling iterations
max_iterThe total number of iterations
a_0The initial value for the shape in the gamma function generating tau
b_0The initial value for the rate in the gamma function generating tau
formulaThe formula used to specify the relation between the response variable and the covariates
spatial_labelsThe spatial labels
temporal_labelsThe temporal labels
feature_labelsThe feature labels
geo_coords_projectorThe geographic coordinates projector
Active bindings
summaryA summary of the BKTRRegressor instance
beta_covariates_summaryA dataframe containing the summary of the beta covariates
y_estimatesA dataframe containing the y estimates
imputed_y_estimatesA dataframe containing the imputed y estimates
beta_estimatesA dataframe containing the beta estimates
hyperparameters_per_iter_dfA dataframe containing the beta estimates per iteration
decomposition_tensorsList of all used decomposition tensors
Methods
Public methods
Methodnew()
Create a newBKTRRegressor object.
Usage
BKTRRegressor$new( data_df, spatial_positions_df, temporal_positions_df, rank_decomp = 10, burn_in_iter = 500, sampling_iter = 500, formula = NULL, spatial_kernel = KernelMatern$new(smoothness_factor = 3), temporal_kernel = KernelSE$new(), sigma_r = 0.01, a_0 = 1e-06, b_0 = 1e-06, has_geo_coords = TRUE, geo_coords_scale = 10)
Arguments
data_dfdata.table: A dataframe containing all the covariatesthrough time and space. It is important that the dataframe has a twoindexes named 'location' and 'time' respectively. The dataframe shouldalso contain every possible combinations of 'location' and 'time'(i.e. even missing rows should be filled present but filled with NaN).So if the dataframe has 10 locations and 5 time points, it should have50 rows (10 x 5). If formula is None, the dataframe should containthe response variable 'Y' as the first column. Note that the covariatecolumns cannot contain NaN values, but the response variable can.
spatial_positions_dfdata.table: Spatial kernel input tensor usedto calculate covariates' distance. Vector of length equal to the number of location points.
temporal_positions_dfdata.table: Temporal kernel input tensor used tocalculate covariate distance. Vector of length equal to the number of time points.
rank_decompInteger: Rank of the CP decomposition (Paper –
R). Defaults to 10.burn_in_iterInteger: Number of iteration before sampling (Paper –
K_1). Defaults to 500.sampling_iterInteger: Number of sampling iterations (Paper –
K_2). Defaults to 500.formulaA Wilkinson R formula to specify the relationbetween the response variable 'Y' and the covariates. If Null, the firstcolumn of the data frame will be used as the response variable and all theother columns will be used as the covariates. Defaults to Null.
spatial_kernelKernel: Spatial kernel Used. Defaults toa KernelMatern(smoothness_factor=3).
temporal_kernelKernel: Temporal kernel used. Defaults to KernelSE().
sigma_rNumeric: Variance of the white noise process (
\tau^{-1})defaults to 1E-2.a_0Numeric: Initial value for the shape (
\alpha) in the gamma functiongenerating tau defaults to 1E-6.b_0Numeric: Initial value for the rate (
\beta) in the gamma functiongenerating tau defaults to 1E-6.has_geo_coordsBoolean: Whether the spatial positions df use geographic coordinates(latitude, longitude). Defaults to TRUE.
geo_coords_scaleNumeric: Scale factor to convert geographic coordinates to euclidean2D space via Mercator projection using x & y domains of [-scale/2, +scale/2]. Only used ifhas_geo_coords is TRUE. Defaults to 10.
Returns
A newBKTRRegressor object.
Methodmcmc_sampling()
Launch the MCMC sampling process.
For a predefined number of iterations:
Sample spatial kernel hyperparameters
Sample temporal kernel hyperparameters
Sample the precision matrix from a wishart distribution
Sample a new spatial covariate decomposition
Sample a new feature covariate decomposition
Sample a new temporal covariate decomposition
Calculate respective errors for the iterations
Sample a new tau value
Collect all the important data for the iteration
Usage
BKTRRegressor$mcmc_sampling()
Returns
NULL Results are stored and can be accessed via summary()
Methodpredict()
Use interpolation to predict betas and response values for new data.
Usage
BKTRRegressor$predict( new_data_df, new_spatial_positions_df = NULL, new_temporal_positions_df = NULL, jitter = 1e-05)
Arguments
new_data_dfdata.table: New covariates. Must have the same columns asthe covariates used to fit the model. The index should contain the combinationof all old spatial coordinates with all new temporal coordinates, the combinationof all new spatial coordinates with all old temporal coordinates, and thecombination of all new spatial coordinates with all new temporal coordinates.
new_spatial_positions_dfdata.table or NULL: A data frame containing the newspatial positions. Defaults to NULL.
new_temporal_positions_dfdata.table or NULL: A data frame containing the newtemporal positions. Defaults to NULL.
jitterNumeric or NULL: A small value to add to the diagonal of the precision matrix.Defaults to NULL.
Returns
List: A list of two dataframes. The first represents the betaforecasted for all new spatial locations or temporal points.The second represents the forecasted response for all new spatiallocations or temporal points.
Methodget_iterations_betas()
Return all sampled betas through sampling iterations for a givenset of spatial, temporal and feature labels. Useful for plotting thedistribution of sampled beta values.
Usage
BKTRRegressor$get_iterations_betas( spatial_label, temporal_label, feature_label)
Arguments
spatial_labelString: The spatial label for which we want to get the betas
temporal_labelString: The temporal label for which we want to get the betas
feature_labelString: The feature label for which we want to get the betas
Returns
A list containing the sampled betas through iteration for the given labels
Methodget_beta_summary_df()
Get a summary of estimated beta values. If no labels are given,then the summary is for all the betas. If labels are given, then the summaryis for the given labels.
Usage
BKTRRegressor$get_beta_summary_df( spatial_labels = NULL, temporal_labels = NULL, feature_labels = NULL)
Arguments
spatial_labelsvector: The spatial labels used in summary. If NULL,then all spatial labels are used. Defaults to NULL.
temporal_labelsvector: The temporal labels used in summary. If NULL,then all temporal labels are used. Defaults to NULL.
feature_labelsvector: The feature labels used in summary. If NULL,then all feature labels are used. Defaults to NULL.
Returns
A new data.table with the beta summary for the given labels.
Methodclone()
The objects of this class are cloneable with this method.
Usage
BKTRRegressor$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a BIXI data collection instance containing multiple dataframesbixi_data <- BixiData$new(is_light = TRUE) # Use light version for example# Create a BKTRRegressor instancebktr_regressor <- BKTRRegressor$new( formula = nb_departure ~ 1 + mean_temp_c + area_park, data_df <- bixi_data$data_df, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)# Launch the MCMC samplingbktr_regressor$mcmc_sampling()# Get the summary of the bktr regressorsummary(bktr_regressor)# Get estimated response variables for missing valuesbktr_regressor$imputed_y_estimates# Get the list of sampled betas for given spatial, temporal and feature labelsbktr_regressor$get_iterations_betas( spatial_label = bixi_data$spatial_positions_df$location[1], temporal_label = bixi_data$temporal_positions_df$time[1], feature_label = 'mean_temp_c')# Get the summary of all betas for the 'mean_temp_c' featurebktr_regressor$get_beta_summary_df(feature_labels = 'mean_temp_c')## PREDICTION EXAMPLE ### Create a light version of the BIXI data collection instancebixi_data <- BixiData$new(is_light = TRUE)# Simplify variable namesdata_df <- bixi_data$data_dfspa_pos_df <- bixi_data$spatial_positions_dftemp_pos_df <- bixi_data$temporal_positions_df# Keep some data aside for predictionnew_spa_pos_df <- spa_pos_df[1:2, ]new_temp_pos_df <- temp_pos_df[1:5, ]reg_spa_pos_df <- spa_pos_df[-(1:2), ]reg_temp_pos_df <- temp_pos_df[-(1:5), ]reg_data_df_mask <- data_df$location %in% reg_spa_pos_df$location & data_df$time %in% reg_temp_pos_df$timereg_data_df <- data_df[reg_data_df_mask, ]new_data_df <- data_df[!reg_data_df_mask, ]# Launch mcmc sampling on regression databktr_regressor <- BKTRRegressor$new( formula = nb_departure ~ 1 + mean_temp_c + area_park, data_df = reg_data_df, spatial_positions_df = reg_spa_pos_df, temporal_positions_df = reg_temp_pos_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Predict response values for new databktr_regressor$predict( new_data_df = new_data_df, new_spatial_positions_df = new_spa_pos_df, new_temporal_positions_df = new_temp_pos_df)BIXI Data Class
Description
R6 class encapsulating all BIXI dataframes. It is alsopossible to use a light version of the dataset by using theis_lightparameter. In this case, the dataset is reduced to its first 25 stationsand first 50 days. The light version is only used for testing and short examples.
Public fields
departure_dfThe departure dataframe
spatial_features_dfThe spatial features dataframe
temporal_features_dfThe temporal features dataframe
spatial_positions_dfThe spatial positions dataframe
temporal_positions_dfThe temporal positions dataframe
data_dfThe data dataframe
is_lightWhether the light version of the dataset is used
Methods
Public methods
Methodnew()
Initialize the BIXI data class
Usage
BixiData$new(is_light = FALSE)
Arguments
is_lightWhether the light version of the dataset is used,defaults to FALSE.
Returns
A new BIXI data instance
Methodclone()
The objects of this class are cloneable with this method.
Usage
BixiData$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a light BIXI data collection instance containing multiple dataframes# This only uses the first 25 stations and 50 days of the full datasetbixi_data <- BixiData$new(is_light = TRUE)# Dataframe containing the position (latitude and longitude) of M stationsbixi_data$spatial_positions_df# Dataframe containing the time position of N days (O to N-1)bixi_data$temporal_positions_df# Dataframe with spatial and temporal features for each day and station (M x N rows)bixi_data$data_dfKernel Composition Operations
Description
Kernel Composition Operations Enum. Possibilities of operation betweentwo kernels to generate a new composed kernel. The values are:MUL andADD.
Usage
CompositionOpsFormat
An object of classlist of length 2.
Base R6 class for Kernels
Description
Abstract base class for kernels (Should not be instantiated)
Public fields
kernel_varianceThe variance of the kernel
jitter_valueThe jitter value to add to the kernel matrix
distance_matrixThe distance matrix between points in a tensor format
nameThe kernel's name
parametersThe parameters of the kernel (list of
KernelParameter)covariance_matrixThe covariance matrix of the kernel in a tensor format
positions_dfThe positions of the points in a dataframe format
has_dist_matrixIdentify if the kernel has a distance matrix or not
Methods
Public methods
Methodnew()
Kernel abstract base constructor
Usage
Kernel$new(kernel_variance, jitter_value)
Arguments
kernel_varianceNumeric: The variance of the kernel
jitter_valueNumeric: The jitter value to add to the kernel matrix
Returns
A newKernel object.
Methodcore_kernel_fn()
Abstract method to compute the core kernel's covariance matrix
Usage
Kernel$core_kernel_fn()
Methodadd_jitter_to_kernel()
Method to add jitter to the kernel's covariance matrix
Usage
Kernel$add_jitter_to_kernel()
Methodkernel_gen()
Method to compute the kernel's covariance matrix
Usage
Kernel$kernel_gen()
Methodset_positions()
Method to set the kernel's positions and compute the distance matrix
Usage
Kernel$set_positions(positions_df)
Arguments
positions_dfDataframe: The positions of the points in a dataframe format
Methodplot()
Method to plot the kernel's covariance matrix
Usage
Kernel$plot(show_figure = TRUE)
Arguments
show_figureBoolean: If TRUE, the figure is shown, otherwise it is returned
Returns
Ifshow_figure is TRUE, the figure is shown, otherwise it is returned
Methodclone()
The objects of this class are cloneable with this method.
Usage
Kernel$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
R6 class for Kernels Composed via Addition
Description
R6 class automatically generated whenadding two kernels together.
Super classes
BKTR::Kernel ->BKTR::KernelComposed ->KernelAddComposed
Methods
Public methods
Inherited methods
Methodnew()
Create a newKernelAddComposed object.
Usage
KernelAddComposed$new(left_kernel, right_kernel, new_name)
Arguments
left_kernelKernel: The left kernel to use for composition
right_kernelKernel: The right kernel to use for composition
new_nameString: The name of the composed kernel
Returns
A newKernelAddComposed object.
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelAddComposed$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new additive kernelk_rq_plus_per <- KernelAddComposed$new( left_kernel = KernelRQ$new(), right_kernel = KernelPeriodic$new(), new_name = 'SE + Periodic Kernel')# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_rq_plus_per$set_positions(positions_df)# Generate the kernel's covariance matrixk_rq_plus_per$kernel_gen()R6 class for Composed Kernels
Description
R6 class for Composed Kernels
Super class
BKTR::Kernel ->KernelComposed
Public fields
nameThe kernel's name
parametersThe parameters of the kernel (list of
KernelParameter)left_kernelThe left kernel to use for composition
right_kernelThe right kernel to use for composition
composition_operationThe operation to use for composition
has_dist_matrixIdentify if the kernel has a distance matrix or not
Methods
Public methods
Methodnew()
Create a newKernelComposed object.
Usage
KernelComposed$new(left_kernel, right_kernel, new_name, composition_operation)
Arguments
left_kernelKernel: The left kernel to use for composition
right_kernelKernel: The right kernel to use for composition
new_nameString: The name of the composed kernel
composition_operationCompositionOps: The operation to use for composition
Methodcore_kernel_fn()
Method to compute the core kernel's covariance matrix
Usage
KernelComposed$core_kernel_fn()
Returns
The core kernel's covariance matrix
Methodset_positions()
Method to set the kernel's positions and compute the distance matrix
Usage
KernelComposed$set_positions(positions_df)
Arguments
positions_dfDataframe: The positions of the points in a dataframe format
Returns
NULL, set the kernel's positions and compute the distance matrix
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelComposed$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new locally periodic kernelk_loc_per <- KernelComposed$new( left_kernel = KernelSE$new(), right_kernel = KernelPeriodic$new(), new_name = 'Locally Periodic Kernel', composition_operation = CompositionOps$MUL)# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_loc_per$set_positions(positions_df)# Generate the kernel's covariance matrixk_loc_per$kernel_gen()R6 class for Matern Kernels
Description
R6 class for Matern Kernels
Super class
BKTR::Kernel ->KernelMatern
Public fields
lengthscaleThe lengthscale parameter instance of the kernel
smoothness_factorThe smoothness factor of the kernel
has_dist_matrixIdentify if the kernel has a distance matrix or not
Methods
Public methods
Inherited methods
Methodnew()
Create a newKernelMatern object.
Usage
KernelMatern$new( smoothness_factor = 5, lengthscale = KernelParameter$new(2), kernel_variance = 1, jitter_value = NULL)
Arguments
smoothness_factorNumeric: The smoothness factor of the kernel (1, 3 or 5)
lengthscaleKernelParameter: The lengthscale parameter instance of the kernel
kernel_varianceNumeric: The variance of the kernel
jitter_valueNumeric: The jitter value to add to the kernel matrix
Methodget_smoothness_kernel_fn()
Method to the get the smoothness kernel function for a given integer smoothness factor
Usage
KernelMatern$get_smoothness_kernel_fn()
Returns
The smoothness kernel function
Methodcore_kernel_fn()
Method to compute the core kernel's covariance matrix
Usage
KernelMatern$core_kernel_fn()
Returns
The core kernel's covariance matrix
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelMatern$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new Matern 3/2 kernelk_matern <- KernelMatern$new(smoothness_factor = 3)# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_matern$set_positions(positions_df)# Generate the kernel's covariance matrixk_matern$kernel_gen()R6 class for Kernels Composed via Multiplication
Description
R6 class automatically generated whenmultiplying two kernels together.
Super classes
BKTR::Kernel ->BKTR::KernelComposed ->KernelMulComposed
Methods
Public methods
Inherited methods
Methodnew()
Create a newKernelMulComposed object.
Usage
KernelMulComposed$new(left_kernel, right_kernel, new_name)
Arguments
left_kernelKernel: The left kernel to use for composition
right_kernelKernel: The right kernel to use for composition
new_nameString: The name of the composed kernel
Returns
A newKernelMulComposed object.
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelMulComposed$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new locally periodic kernelk_loc_per <- KernelMulComposed$new( left_kernel = KernelSE$new(), right_kernel = KernelPeriodic$new(), new_name = 'Locally Periodic Kernel')# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_loc_per$set_positions(positions_df)# Generate the kernel's covariance matrixk_loc_per$kernel_gen()R6 class for kernel's hyperparameter
Description
KernelParameter contains all information and behaviour related to a kernel parameters.
Public fields
valueThe hyperparameter mean's prior value or its constant value
is_fixedSays if the kernel parameter is fixed or not (if fixed, there is no sampling)
lower_boundThe hyperparameter's minimal value during sampling
upper_boundThe hyperparameter's maximal value during sampling
slice_sampling_scaleThe sampling range's amplitude
hparam_precisionPrecision of the hyperparameter
kernelThe kernel associated with the parameter (it is set at kernel instanciation)
nameThe kernel parameter's name
Active bindings
full_nameThe kernel parameter's full name
Methods
Public methods
Methodnew()
Create a newKernelParameter object.
Usage
KernelParameter$new( value, is_fixed = FALSE, lower_bound = DEFAULT_LBOUND, upper_bound = DEFAULT_UBOUND, slice_sampling_scale = log(10), hparam_precision = 1)
Arguments
valueNumeric: The hyperparameter mean's prior value (Paper -
\phi) or its constant valueis_fixedBoolean: Says if the kernel parameter is fixed or not (if fixed, there is no sampling)
lower_boundNumeric: Hyperparameter's minimal value during sampling (Paper -
\phi_{min})upper_boundNumeric: Hyperparameter's maximal value during sampling (Paper -
\phi_{max})slice_sampling_scaleNumeric: The sampling range's amplitude (Paper -
\rho)hparam_precisionNumeric: The hyperparameter's precision
Returns
A newKernelParameter object.
Methodset_kernel()
SetKernel for a givenKernelParameter object.
Usage
KernelParameter$set_kernel(kernel, param_name)
Arguments
kernelKernel: The kernel to associate with the parameter
param_nameString: The parameter's name
Returns
NULL, set a new kernel for the parameter
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelParameter$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# A kernel parameter can be a constant valueconst_param <- KernelParameter$new(7, is_fixed = TRUE)# It can otherwise be sampled and have its value updated through samplingsamp_param <- KernelParameter$new(1, lower_bound = 0.1, upper_bound = 10, slice_sampling_scale = 4)# A kernel parameter can be associated with any type of kernelKernelPeriodic$new(period_length = const_param, lengthscale = samp_param)R6 class for Periodic Kernels
Description
R6 class for Periodic Kernels
Super class
BKTR::Kernel ->KernelPeriodic
Public fields
lengthscaleThe lengthscale parameter instance of the kernel
period_lengthThe period length parameter instance of the kernel
has_dist_matrixIdentify if the kernel has a distance matrix or not
nameThe kernel's name
Methods
Public methods
Inherited methods
Methodnew()
Create a newKernelPeriodic object.
Usage
KernelPeriodic$new( lengthscale = KernelParameter$new(2), period_length = KernelParameter$new(2), kernel_variance = 1, jitter_value = NULL)
Arguments
lengthscaleKernelParameter: The lengthscale parameter instance of the kernel
period_lengthKernelParameter: The period length parameter instance of the kernel
kernel_varianceNumeric: The variance of the kernel
jitter_valueNumeric: The jitter value to add to the kernel matrix
Returns
A newKernelPeriodic object.
Methodcore_kernel_fn()
Method to compute the core kernel's covariance matrix
Usage
KernelPeriodic$core_kernel_fn()
Returns
The core kernel's covariance matrix
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelPeriodic$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new Periodic kernelk_periodic <- KernelPeriodic$new()# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_periodic$set_positions(positions_df)# Generate the kernel's covariance matrixk_periodic$kernel_gen()R6 class for Rational Quadratic Kernels
Description
R6 class for Rational Quadratic Kernels
Super class
BKTR::Kernel ->KernelRQ
Public fields
lengthscaleThe lengthscale parameter instance of the kernel
alphaThe alpha parameter instance of the kernel
has_dist_matrixThe distance matrix between points in a tensor format
nameThe kernel's name
Methods
Public methods
Inherited methods
Methodnew()
Create a newKernelRQ object.
Usage
KernelRQ$new( lengthscale = KernelParameter$new(2), alpha = KernelParameter$new(2), kernel_variance = 1, jitter_value = NULL)
Arguments
lengthscaleKernelParameter: The lengthscale parameter instance of the kernel
alphaKernelParameter: The alpha parameter instance of the kernel
kernel_varianceNumeric: The variance of the kernel
jitter_valueNumeric: The jitter value to add to the kernel matrix
Returns
A newKernelRQ object.
Methodcore_kernel_fn()
Method to compute the core kernel's covariance matrix
Usage
KernelRQ$core_kernel_fn()
Returns
The core kernel's covariance matrix
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelRQ$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new RQ kernelk_rq <- KernelRQ$new()# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_rq$set_positions(positions_df)# Generate the kernel's covariance matrixk_rq$kernel_gen()R6 class for Square Exponential Kernels
Description
R6 class for Square Exponential Kernels
Super class
BKTR::Kernel ->KernelSE
Public fields
lengthscaleThe lengthscale parameter instance of the kernel
has_dist_matrixIdentify if the kernel has a distance matrix or not
nameThe kernel's name
Methods
Public methods
Inherited methods
Methodnew()
Create a newKernelSE object.
Usage
KernelSE$new( lengthscale = KernelParameter$new(2), kernel_variance = 1, jitter_value = NULL)
Arguments
lengthscaleKernelParameter: The lengthscale parameter instance of the kernel
kernel_varianceNumeric: The variance of the kernel
jitter_valueNumeric: The jitter value to add to the kernel matrix
Returns
A newKernelSE object.
Methodcore_kernel_fn()
Method to compute the core kernel's covariance matrix
Usage
KernelSE$core_kernel_fn()
Returns
The core kernel's covariance matrix
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelSE$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new SE kernelk_se <- KernelSE$new()# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_se$set_positions(positions_df)# Generate the kernel's covariance matrixk_se$kernel_gen()R6 class for White Noise Kernels
Description
R6 class for White Noise Kernels
Super class
BKTR::Kernel ->KernelWhiteNoise
Public fields
has_dist_matrixIdentify if the kernel has a distance matrix or not
nameThe kernel's name
Methods
Public methods
Inherited methods
Methodnew()
Usage
KernelWhiteNoise$new(kernel_variance = 1, jitter_value = NULL)
Arguments
kernel_varianceNumeric: The variance of the kernel
jitter_valueNumeric: The jitter value to add to the kernel matrix
Returns
A newKernelWhiteNoise object.
Methodcore_kernel_fn()
Method to compute the core kernel's covariance matrix
Usage
KernelWhiteNoise$core_kernel_fn()
Returns
The core kernel's covariance matrix
Methodclone()
The objects of this class are cloneable with this method.
Usage
KernelWhiteNoise$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Create a new white noise kernelk_white_noise <- KernelWhiteNoise$new()# Set the kernel's positionspositions_df <- data.frame(x=c(-4, 0, 3), y=c(-2, 0, 2))k_white_noise$set_positions(positions_df)# Generate the kernel's covariance matrixk_white_noise$kernel_gen()Tensor Operator Singleton
Description
Singleton instance of theTensorOperator class that containsall informations related the tensor API; tensor methods, used data type and used device.
Usage
TSRFormat
An object of classTensorOperator (inherits fromSingleton,R6) of length 19.
R6 singleton that contains the configuration for the tensor backend
Description
Tensor backend configuration and methods for all the tensor operationsin BKTR
Super class
R6P::Singleton ->TensorOperator
Public fields
fp_typeThe floating point type to use for the tensor operations
fp_deviceThe device to use for the tensor operations
Methods
Public methods
Methodnew()
Initialize the tensor operator with the given floating point typeand device
Usage
TensorOperator$new(fp_type = "float64", fp_device = "cpu")
Arguments
fp_typeThe floating point type to use for the tensor operations (either"float64" or "float32")
fp_deviceThe device to use for the tensor operations (either "cpu" or"cuda")
Returns
A new tensor operator instance
Methodset_params()
Set the tensor operator parameters
Usage
TensorOperator$set_params(fp_type = NULL, fp_device = NULL, seed = NULL)
Arguments
fp_typeThe floating point type to use for the tensor operations (either"float64" or "float32")
fp_deviceThe device to use for the tensor operations (either "cpu" or"cuda")
seedThe seed to use for the random number generator
Methodget_default_jitter()
Get the default jitter value for the floating point type used by the tensor operator
Usage
TensorOperator$get_default_jitter()
Returns
The default jitter value for the floating point type used by the tensor operator
Methodtensor()
Create a tensor from a vector or matrix of data with the tensor operator dtype and device
Usage
TensorOperator$tensor(tensor_data)
Arguments
tensor_dataThe vector or matrix of data to create the tensor from
Returns
A new tensor with the tensor operator dtype and device
Methodis_tensor()
Check if a provided object is a tensor
Usage
TensorOperator$is_tensor(tensor)
Arguments
tensorThe object to check
Returns
A boolean indicating if the object is a tensor
Methodeye()
Create a tensor with a diagonal of ones and zeros with the tensor operator dtype and devicefor a given dimension
Usage
TensorOperator$eye(eye_dim)
Arguments
eye_dimThe dimension of the tensor to create
Returns
A new tensor with a diagonal of ones and zeros with the tensor operator dtype and device
Methodones()
Create a tensor of ones with the tensor operator dtype and device for a given dimension
Usage
TensorOperator$ones(tsr_dim)
Arguments
tsr_dimThe dimension of the tensor to create
Returns
A new tensor of ones with the tensor operator dtype and device
Methodzeros()
Create a tensor of zeros with the tensor operator dtype and device for a given dimension
Usage
TensorOperator$zeros(tsr_dim)
Arguments
tsr_dimThe dimension of the tensor to create
Returns
A new tensor of zeros with the tensor operator dtype and device
Methodrand()
Create a tensor of random uniform values with the tensor operator dtype anddevice for a given dimension
Usage
TensorOperator$rand(tsr_dim)
Arguments
tsr_dimThe dimension of the tensor to create
Returns
A new tensor of random values with the tensor operator dtype and device
Methodrandn()
Create a tensor of random normal values with the tensor operator dtype and devicefor a given dimension
Usage
TensorOperator$randn(tsr_dim)
Arguments
tsr_dimThe dimension of the tensor to create
Returns
A new tensor of random normal values with the tensor operator dtype and device
Methodrandn_like()
Create a tensor of random uniform values with the same shape as a given tensorwith the tensor operator dtype and device
Usage
TensorOperator$randn_like(input_tensor)
Arguments
input_tensorThe tensor to use as a shape reference
Returns
A new tensor of random uniform values with the same shape as a given tensor
Methodarange()
Create a tensor of a range of values with the tensor operator dtype and devicefor a given start and end
Usage
TensorOperator$arange(start, end)
Arguments
startThe start of the range
endThe end of the range
Returns
A new tensor of a range of values with the tensor operator dtype and device
Methodrand_choice()
Choose random values from a tensor for a given number of samples
Usage
TensorOperator$rand_choice( choices_tsr, nb_sample, use_replace = FALSE, weights_tsr = NULL)
Arguments
choices_tsrThe tensor to choose values from
nb_sampleThe number of samples to choose
use_replaceA boolean indicating if the sampling should be done with replacement.Defaults to FALSE
weights_tsrThe weights to use for the sampling. If NULL, the sampling is uniform.Defaults to NULL
Returns
A new tensor of randomly chosen values from a tensor
Methodkronecker_prod()
Efficiently compute the kronecker product of two matrices in tensor format
Usage
TensorOperator$kronecker_prod(a, b)
Arguments
aThe first tensor
bThe second tensor
Returns
The kronecker product of the two matrices
Methodkhatri_rao_prod()
Efficiently compute the khatri rao product of two matrices in tensor formathaving the same number of columns
Usage
TensorOperator$khatri_rao_prod(a, b)
Arguments
aThe first tensor
bThe second tensor
Returns
The khatri rao product of the two matrices
Methodclone()
The objects of this class are cloneable with this method.
Usage
TensorOperator$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# Set the seed, setup the tensor floating point type and deviceTSR$set_params(fp_type='float64', fp_device='cpu', seed=42)# Create a tensor from a vectorTSR$tensor(c(1, 2, 3))# Create a tensor from a matrixTSR$tensor(matrix(c(1, 2, 3, 4), nrow=2))# Create a 3x3 tensor with a diagonal of ones and zeros elsewhereTSR$eye(3)# Create a tensor of ones (with 6 elements, 2 rows and 3 columns)TSR$ones(c(2, 3))# Create a tensor of zeros (with 12 elements, 3 rows and 4 columns)TSR$zeros(c(3, 4))# Create a tensor of random uniform values (with 6 elements)TSR$rand(c(2, 3))# Create a tensor of random normal values (with 6 elements)TSR$randn(c(2, 3))# Create a tensor of random normal values with the same shape as a given tensortsr_a <- TSR$randn(c(2, 3))TSR$randn_like(tsr_a)# Create a tensor of a range of values (1, 2, 3, 4)TSR$arange(1, 4)# Choose two random values from a given tensor without replacementtsr_b <- TSR$rand(6)TSR$rand_choice(tsr_b, 2)# Use the tensor operator to compute the kronecker product of two 2x2 matricestsr_c <- TSR$tensor(matrix(c(1, 2, 3, 4), nrow=2))tsr_d <- TSR$tensor(matrix(c(5, 6, 7, 8), nrow=2))TSR$kronecker_prod(tsr_c, tsr_d) # Returns a 4x4 tensor# Use the tensor operator to compute the khatri rao product of two 2x2 matricesTSR$khatri_rao_prod(tsr_c, tsr_d) # Returns a 4x2 tensor# Check if a given object is a tensorTSR$is_tensor(tsr_d) # Returns TRUETSR$is_tensor(TSR$eye(2)) # Returns TRUETSR$is_tensor(1) # Returns FALSESpatial Features of Montreal BIXI Stations in 2019
Description
These data represent 14 spatial features (columns) for 587 bike sharingstations (rows) located at different geographical coordinates (longitude,latitude) in Montreal. The Montreal based bike sharing company is named BIXI.The first column contains the descriptive label affected to each station and theother columns contain information about the infrastructure, points of interests,walkscore and population surrounding each station for 2019.
Usage
data("bixi_spatial_features")Format
A data frame with 587 observations on the following 14 variables.
locationa character vector
area_parka numeric vector
len_cycle_patha numeric vector
len_major_roada numeric vector
len_minor_roada numeric vector
num_metro_stationsa numeric vector
num_other_commerciala numeric vector
num_restaurantsa numeric vector
num_universitya numeric vector
num_popa numeric vector
num_bus_stationsa numeric vector
num_bus_routesa numeric vector
walkscorea numeric vector
capacitya numeric vector
Source
Wang, X., Cheng, Z., Trépanier, M., & Sun, L. (2021).Modeling bike-sharing demand using a regression model with spatiallyvarying coefficients. Journal of Transport Geography, 93, 103059.
References
Reference for the BIXI station informations:BIXI Montréal (2023). “Open Data.” Accessed: 2023-07-11, URLhttps://bixi.com/en/ open-data.
Reference for point of interests and infrastructure informations:DMTI Spatial Inc (2019). “Enhanced Point of Interest (DMTI).”URLhttps://www.dmtispatial.com.
Reference for Walkscore:Walk Score (2023). “Walk Score Methodology.” Accessed: 2023-07-11,URLhttps://www.walkscore.com/methodology.shtml.
The population information comes from the 2016 Canada census data at adissemination block level.
Spatial Locations of Montreal BIXI Stations in 2019
Description
Data points representing the spatial locations of 587 bike sharing stations forthe Montreal based bike sharing company named BIXI. The dataframe containsa label to identify each station and its associated longitude and latitudecoordinates.
Usage
data("bixi_spatial_locations")Format
A data frame with 587 observations on the following 3 variables.
locationa character vector
latitudea numeric vector
longitudea numeric vector
Source
Wang, X., Cheng, Z., Trépanier, M., & Sun, L. (2021).Modeling bike-sharing demand using a regression model with spatiallyvarying coefficients. Journal of Transport Geography, 93, 103059.
References
BIXI Montréal (2023). “Open Data.” Accessed: 2023-07-11, URLhttps://bixi.com/en/ open-data.
Daily Departure from BIXI Stations in 2019
Description
These data capture the number of daily departure for 587 bike sharing stations(rows) through 197 days (columns). The data is limited to the 2019 season ofa Montreal based bike sharing company named BIXI.
Usage
data("bixi_station_departures")Format
A data frame with 587 rows and 197 columns.
Source
Wang, X., Cheng, Z., Trépanier, M., & Sun, L. (2021).Modeling bike-sharing demand using a regression model with spatiallyvarying coefficients. Journal of Transport Geography, 93, 103059.
References
BIXI Montréal (2023). “Open Data.” Accessed: 2023-07-11, URLhttps://bixi.com/en/ open-data.
Temporal Features in Montreal applicable to BIXI for 2019
Description
These data represent the temporal features in Montreal applicable to a Montrealbased bike sharing company named BIXI. The data include six features (columns)for 196 days (rows). The time column represent the label associated to eachcaptured time for the 2019 season of BIXI. The other columns contain informationabout Montral weather and applicable holidays for each day.
Usage
data("bixi_temporal_features")Format
A data frame with 196 observations on the following 6 variables.
timea IDate
humiditya numeric vector
max_temp_fa numeric vector
mean_temp_ca numeric vector
total_precip_mma numeric vector
holidaya numeric vector
Source
Lei, M., Labbe, A., & Sun, L. (2021). Scalable Spatiotemporally VaryingCoefficient Modelling with Bayesian Kernelized Tensor Regression.arXiv preprint arXiv:2109.00046.
References
The weather data is sourced from the Environment and Climate ChangeCanada Historical Climate Data website.
The holiday column is specifying if a date is a holiday or not,according to the Quebec government.
Temporal indices for the 2019 BIXI season
Description
These data represent 196 temporal indices (rows) related to each dayof the 2019 season of Montreal based bike sharing company named BIXI.The time column represent the label associated to each day and the time_indexcolumn represent the location in time space of each day when compared to eachother. Since no days are missing and they are all spaced by exactly one day,the time_index is simply a range from 0 to 195.
Usage
data("bixi_temporal_locations")Format
A data frame with 196 observations on the following 2 variables.
timea IDate
time_indexa numeric vector
Source
Lei, M., Labbe, A., & Sun, L. (2021). Scalable Spatiotemporally VaryingCoefficient Modelling with Bayesian Kernelized Tensor Regression.arXiv preprint arXiv:2109.00046.
Plot Beta Coefficients Distribution
Description
Plot the distribution of beta values for a given list of labels.
Usage
plot_beta_dists( bktr_reg, labels_list, show_figure = TRUE, fig_width = 9, fig_height = 6, fig_resolution = 200)Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
labels_list | List: List of labels tuple (spatial, temporal, feature) forwhich to plot the beta distribution through iterations |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
fig_width | Integer: Figure width. Defaults to 9. |
fig_height | Integer: Figure height. Defaults to 6. |
fig_resolution | Numeric: Figure resolution PPI. Defaults to 200. |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)bktr_regressor <- BKTRRegressor$new( data_df <- bixi_data$data_df, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot temporal beta coefficients for the first station and the first featurespa_lab <- bixi_data$spatial_positions_df$location[3]plot_beta_dists( bktr_regressor, labels_list = list( c(spa_lab, '2019-04-15', 'area_park'), c(spa_lab, '2019-04-16', 'area_park'), c(spa_lab, '2019-04-16', 'mean_temp_c') ),)Plot Beta Coefficients Distribution Regrouped by Covariates
Description
Plot the distribution of beta estimates regrouped by covariates.
Usage
plot_covariates_beta_dists( bktr_reg, feature_labels = NULL, show_figure = TRUE, fig_width = 9, fig_height = 6, fig_resolution = 200)Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
feature_labels | Array or NULL: Array of feature labels forwhich to plot the beta estimates distribution. If NULL plot for all features. |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
fig_width | Integer: Figure width. Defaults to 9. |
fig_height | Integer: Figure height. Defaults to 6. |
fig_resolution | Numeric: Figure resolution PPI. Defaults to 200. |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)bktr_regressor <- BKTRRegressor$new( formula = 'nb_departure ~ 1 + area_park + mean_temp_c + total_precip_mm', data_df <- bixi_data$data_df, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot beta estimates distribution for all featuresplot_covariates_beta_dists(bktr_regressor)# Or plot for a subset of featuresplot_covariates_beta_dists(bktr_regressor, c('area_park', 'mean_temp_c'))Plot Hyperparameters Distributions
Description
Plot the distribution of hyperparameters through iterations
Usage
plot_hyperparams_dists( bktr_reg, hyperparameters = NULL, show_figure = TRUE, fig_width = 9, fig_height = 6, fig_resolution = 200)Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
hyperparameters | Array or NULL: Array of hyperparameters to plot.If NULL, plot all hyperparameters. Defaults to NULL. |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
fig_width | Integer: Figure width. Defaults to 9. |
fig_height | Integer: Figure height. Defaults to 6. |
fig_resolution | Numeric: Figure resolution PPI. Defaults to 200. |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)k_matern <- KernelMatern$new()k_periodic <- KernelPeriodic$new()bktr_regressor <- BKTRRegressor$new( data_df <- bixi_data$data_df, spatial_kernel = k_matern, temporal_kernel = k_periodic, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot the distribution of all hyperparametersplot_hyperparams_dists(bktr_regressor)# Plot the distribution of the spatial kernel hyperparametersspa_par_name <- paste0('Spatial - ', k_matern$parameters[[1]]$full_name)plot_hyperparams_dists(bktr_regressor, spa_par_name)# Plot the distribution of the temporal kernel hyperparameterstemp_par_names <- sapply(k_periodic$parameters, function(x) x$full_name)temp_par_names <- paste0('Temporal - ', temp_par_names)plot_hyperparams_dists(bktr_regressor, temp_par_names)Plot Hyperparameters Traceplot
Description
Plot the evolution of hyperparameters through iterations. (Traceplot)
Usage
plot_hyperparams_traceplot( bktr_reg, hyperparameters = NULL, show_figure = TRUE, fig_width = 9, fig_height = 5.5, fig_resolution = 200)Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
hyperparameters | Array or NULL: Array of hyperparameters to plot.If NULL, plot all hyperparameters. Defaults to NULL. |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
fig_width | Integer: Figure width. Defaults to 9. |
fig_height | Integer: Figure height. Defaults to 5.5. |
fig_resolution | Numeric: Figure resolution PPI. Defaults to 200. |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)k_matern <- KernelMatern$new()k_periodic <- KernelPeriodic$new()bktr_regressor <- BKTRRegressor$new( data_df <- bixi_data$data_df, spatial_kernel = k_matern, temporal_kernel = k_periodic, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot the traceplot of all hyperparametersplot_hyperparams_traceplot(bktr_regressor)# Plot the traceplot of the spatial kernel hyperparametersspa_par_name <- paste0('Spatial - ', k_matern$parameters[[1]]$full_name)plot_hyperparams_traceplot(bktr_regressor, spa_par_name)# Plot the traceplot of the temporal kernel hyperparameterstemp_par_names <- sapply(k_periodic$parameters, function(x) x$full_name)temp_par_names <- paste0('Temporal - ', temp_par_names)plot_hyperparams_traceplot(bktr_regressor, temp_par_names)Plot Spatial Beta Coefficients
Description
Create a plot of beta values through space for a giventemporal point and a set of feature labels. We use ggmap under the hood,so you need to provide a Google or Stadia API token to plot on a map.See: https://cran.r-project.org/web/packages/ggmap/readme/README.html for more detailson how to get an API token.
Usage
plot_spatial_betas( bktr_reg, plot_feature_labels, temporal_point_label, nb_cols = 1, use_dark_mode = TRUE, show_figure = TRUE, zoom = 11, google_token = NULL, stadia_token = NULL, fig_width = 8.5, fig_height = 5.5, fig_resolution = 200)Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
plot_feature_labels | Array: Array of feature labels to plot. |
temporal_point_label | String: Temporal point label to plot. |
nb_cols | Integer: The number of columns to use in the facet grid. |
use_dark_mode | Boolean: Whether to use a dark mode for the geographic map or not. Defaults to TRUE. |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
zoom | Integer: Zoom level for the geographic map. Defaults to 11. |
google_token | String or NULL: Google API token to use for the geographic map. Defaults to NULL. |
stadia_token | String or NULL: Stadia API token to use for the geographic map. Defaults to NULL. |
fig_width | Numeric: Figure width when figure is shown. Defaults to 8.5. |
fig_height | Numeric: Figure height when figure is shown. Defaults to 5.5. |
fig_resolution | Numeric: Figure resolution PPI. Defaults to 200. |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)bktr_regressor <- BKTRRegressor$new( data_df <- bixi_data$data_df, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot spatial beta coefficients for the first time point and the two features# Use your own Google API token instead of 'GOOGLE_API_TOKEN'plot_spatial_betas( bktr_regressor, plot_feature_labels = c('mean_temp_c', 'area_park'), temporal_point_label = bixi_data$temporal_positions_df$time[1], google_token = 'GOOGLE_API_TOKEN')# We can also use light mode and plot the maps side by side# Use your own Stadia API token instead of 'STADIA_API_TOKEN'plot_spatial_betas( bktr_regressor, plot_feature_labels = c('mean_temp_c', 'area_park', 'total_precip_mm'), temporal_point_label = bixi_data$temporal_positions_df$time[10], use_dark_mode = FALSE, nb_cols = 3, stadia_token = 'STADIA_API_TOKEN')Plot Temporal Beta Coefficients
Description
Create a plot of the beta values through time for a givenspatial point and a set of feature labels.
Usage
plot_temporal_betas( bktr_reg, plot_feature_labels, spatial_point_label, date_format = "%Y-%m-%d", show_figure = TRUE, fig_width = 8.5, fig_height = 5.5, fig_resolution = 200)Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
plot_feature_labels | Array: Array of feature labels to plot. |
spatial_point_label | String: Spatial point label to plot. |
date_format | String: Format of the date to use in bktr dataframes for the time.Defaults to '%Y-%m-%d'. |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
fig_width | Numeric: Figure width when figure is shown. Defaults to 8.5. |
fig_height | Numeric: Figure height when figure is shown. Defaults to 5.5. |
fig_resolution | Numeric: Figure resolution PPI. Defaults to 200. |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)bktr_regressor <- BKTRRegressor$new( data_df <- bixi_data$data_df, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot temporal beta coefficients for the first station and the two featuresplot_temporal_betas( bktr_regressor, plot_feature_labels = c('mean_temp_c', 'area_park'), spatial_point_label = bixi_data$spatial_positions_df$location[1])Plot Y Estimates
Description
Plot y estimates vs observed y values.
Usage
plot_y_estimates( bktr_reg, show_figure = TRUE, fig_width = 5, fig_height = 5, fig_resolution = 200, fig_title = "y estimates vs observed y values")Arguments
bktr_reg | BKTRRegressor: BKTRRegressor object. |
show_figure | Boolean: Whether to show the figure. Defaults to True. |
fig_width | Numeric: Figure width when figure is shown. Defaults to 5. |
fig_height | Numeric: Figure height when figure is shown. Defaults to 5. |
fig_resolution | Numeric: Figure resolution PPI when figure is shown. Defaults to 200. |
fig_title | String or NULL: Figure title if provided. Defaults to 'y estimates vs observed y values' |
Value
ggplot or NULL: ggplot object or NULL if show_figure is set to FALSE.
Examples
# Launch MCMC sampling on a light version of the BIXI datasetbixi_data <- BixiData$new(is_light = TRUE)bktr_regressor <- BKTRRegressor$new( data_df <- bixi_data$data_df, spatial_positions_df = bixi_data$spatial_positions_df, temporal_positions_df = bixi_data$temporal_positions_df, burn_in_iter = 5, sampling_iter = 10) # For example only (too few iterations)bktr_regressor$mcmc_sampling()# Plot Y estimates vs observed y valuesplot_y_estimates(bktr_regressor)Print the summary of a BKTRRegressor instance
Description
Print the summary of a BKTRRegressor instance
Usage
## S3 method for class 'BKTRRegressor'print(x, ...)Arguments
x | A BKTRRegressor instance |
... | Additional arguments to comply with generic function |
Function used to transform covariates coming from two dataframes one for spatial andone for temporal into a single dataframe with the right shape for the BKTR Regressor.This is useful when the temporal covariates do not vary trough space and the spatialcovariates do not vary trough time (Like in the BIXI example). The function also addsa column for the target variable at the beginning of the dataframe.
Description
Function used to transform covariates coming from two dataframes one for spatial andone for temporal into a single dataframe with the right shape for the BKTR Regressor.This is useful when the temporal covariates do not vary trough space and the spatialcovariates do not vary trough time (Like in the BIXI example). The function also addsa column for the target variable at the beginning of the dataframe.
Usage
reshape_covariate_dfs(spatial_df, temporal_df, y_df, y_column_name = "y")Arguments
spatial_df | data.table: Spatial covariates dataframe with an index namedlocation and a shape of (n_locations, n_spatial_covariates) |
temporal_df | data.table: Temporal covariates dataframe with an index namedtime and a shape of (n_times, n_temporal_covariates) |
y_df | data.table: The dataframe containing the target variable. It should havea shape of (n_locations, n_times). The columns and index names of this dataframeshould be correspond to the one of the spatial_df and temporal_df. |
y_column_name | string: The name of the target variable column in y_df. Defaultto 'y'. |
Value
data.table: The reshaped covariates dataframe with a shape of(n_locations * n_times, 1 + n_spatial_covariates + n_temporal_covariates).The first two columns are the indexes (location, time), the following columnis the target variable and the other columns are the covariates.
Examples
# Let's reshape the BIXI dataframes without normalizationnew_data_df <- reshape_covariate_dfs( spatial_df = BKTR::bixi_spatial_features, temporal_df = BKTR::bixi_temporal_features, y_df = BKTR::bixi_station_departures, y_column_name = 'whole_nb_departure')# The resulting dataframe has the right shape to be a BKTRRegressor data_dfhead(new_data_df)Simulate Spatiotemporal Data Using Kernel Covariances.
Description
Simulate Spatiotemporal Data Using Kernel Covariances.
Usage
simulate_spatiotemporal_data( nb_locations, nb_time_points, nb_spatial_dimensions, spatial_scale, time_scale, spatial_covariates_means, temporal_covariates_means, spatial_kernel, temporal_kernel, noise_variance_scale)Arguments
nb_locations | Integer: Number of spatial locations |
nb_time_points | Integer: Number of time points |
nb_spatial_dimensions | Integer: Number of spatial dimensions |
spatial_scale | Numeric: Spatial scale |
time_scale | Numeric: Time scale |
spatial_covariates_means | Vector: Spatial covariates means |
temporal_covariates_means | Vector: Temporal covariates means |
spatial_kernel | Kernel: Spatial kernel |
temporal_kernel | Kernel: Temporal kernel |
noise_variance_scale | Numeric: Noise variance scale |
Value
A list containing 4 dataframes:- 'data_df' contains the response variable and the covariates- 'spatial_positions_df' contains the spatial locations and their coordinates- 'temporal_positions_df' contains the time points and their coordinates- 'beta_df' contains the true beta coefficients
Examples
# Simulate data with 20 locations, 30 time points, in 2D spatial dimensions# with 3 spatial covariates and 2 temporal covariatessimu_data <- simulate_spatiotemporal_data( nb_locations=20, nb_time_points=30, nb_spatial_dimensions=2, spatial_scale=10, time_scale=10, spatial_covariates_means=c(0, 2, 4), temporal_covariates_means=c(1, 3), spatial_kernel=KernelMatern$new(), temporal_kernel=KernelSE$new(), noise_variance_scale=1)# The dataframes are similar to bixi_data, we have:# - data_dfhead(simu_data$data_df)# - spatial_positions_dfhead(simu_data$spatial_positions_df)# - temporal_positions_dfhead(simu_data$temporal_positions_df)# We also obtain the true beta coefficients used to simulate the datahead(simu_data$beta_df)Summarize a BKTRRegressor instance
Description
Summarize a BKTRRegressor instance
Usage
## S3 method for class 'BKTRRegressor'summary(object, ...)Arguments
object | A BKTRRegressor instance |
... | Additional arguments to comply with generic function |