fit_p | R Documentation |
This function is designed to fit the optimal parameters of black-box
functions (models) to real-world data. Provided that the black-box
function adheres to the specified interface
(demo:
TD
,
RSTD
,
Utility
)
, this function can employ the various optimization algorithms detailed
below to find the best- fitting parameters for your model.
The function provides several optimization algorithms:
1. L-BFGS-B (from stats::optim
)
2. Simulated Annealing (GenSA::GenSA
)
3. Genetic Algorithm (GA::ga
)
4. Differential Evolution (DEoptim::DEoptim
)
5. Particle Swarm Optimization (pso::psoptim
)
6. Bayesian Optimization (mlrMBO::mbo
)
7. Covariance Matrix Adapting Evolutionary Strategy (cmaes::cma_es
)
8. Nonlinear Optimization (nloptr::nloptr
)
For more information, please refer to the homepage of this package: https://yuki-961004.github.io/binaryRL/
fit_p(
policy = "off",
estimate = "MLE",
data,
id = NULL,
n_trials = NULL,
funcs = NULL,
model_name = c("TD", "RSTD", "Utility"),
fit_model = list(binaryRL::TD, binaryRL::RSTD, binaryRL::Utility),
lower = list(c(0, 0), c(0, 0, 0), c(0, 0, 0)),
upper = list(c(1, 1), c(1, 1, 1), c(1, 1, 1)),
priors = NULL,
tolerance = 0.001,
iteration_i = 10,
iteration_g = 0,
initial_params = NA,
initial_size = 50,
seed = 123,
nc = 1,
algorithm
)
policy |
[string] Specifies the learning policy to be used.
This determines how the model updates action values based on observed or
simulated choices. It can be either
default: |
estimate |
[string] Estimation method. Can be either
default: |
data |
[data.frame] This data should include the following mandatory columns:
|
id |
[CharacterVector] A vector specifying the subject ID(s) for which parameters should be fitted. The function will process only the subjects provided in this vector. To fit all subjects, you can either explicitly set the argument as
It is strongly recommended to avoid using simple numeric sequences like
default: |
n_trials |
[integer] Represents the total number of trials a single subject experienced
in the experiment. If this parameter is kept at its default value
of default: |
funcs |
[CharacterVector] A character vector containing the names of all user-defined functions
required for the computation. When parallel computation is enabled
(i.e., Therefore, if you have created your own reinforcement learning model
that modifies the package's default six default functions
(default functions:
|
model_name |
[List] The name of fit modals e.g. |
fit_model |
[List] A collection of functions applied to fit models to the data. e.g. |
lower |
[List] The lower bounds for model fit models e.g. |
upper |
[List] The upper bounds for model fit models e.g. |
priors |
[List] A list specifying the prior distributions for the model parameters.
This argument is mandatory when using 1. Static MAP Estimation (Non-Hierarchical) This approach is used when you have a strong, pre-defined belief about the parameter priors and do not want the model to update them iteratively.
2. Hierarchical Bayesian Estimation via EM This approach is used to let the model learn the group-level (hierarchical) prior distributions directly from the data.
default: |
tolerance |
[double] Convergence threshold for MAP estimation. If the change in log posterior probability between iterations is smaller than this value, the algorithm is considered to have converged and the program will stop. default: |
iteration_i |
[integer] The number of iterations the optimization algorithm will perform when searching for the best-fitting parameters during the fitting phase. A higher number of iterations may increase the likelihood of finding a global optimum but also increases computation time. default: |
iteration_g |
[integer] The maximum number of iterations for the Expectation-Maximization (EM)
based MAP estimation. The algorithm will stop once this iteration
count is reached, even if the change in the log-posterior value has
not yet fallen below the default: |
initial_params |
[NumericVector] Initial values for the free parameters that the optimization algorithm will
search from. These are primarily relevant when using algorithms that require
an explicit starting point, such as default: |
initial_size |
[integer] This parameter corresponds to the population size in genetic
algorithms ( default: |
seed |
[integer] Random seed. This ensures that the results are reproducible and remain the same each time the function is run. default: |
nc |
[integer] Number of cores to use for parallel processing. Since fitting optimal parameters for each subject is an independent task, parallel computation can significantly speed up the fitting process:
|
algorithm |
[string] Choose an algorithm package from
In addition, any algorithm from the |
The optimal parameters found by the algorithm for each subject,
along with the model fit calculated using these parameters.
This is returned as an object of class binaryRL
containing results
for all subjects with all models.
While both fit_p
and rcv_d
utilize the same underlying
optimize_para
function to find optimal parameters, they play
distinct and sequential roles in the modeling pipeline.
The key differences are as follows:
Purpose and Data Source: rcv_d
should always be
performed before fit_p
. Its primary role is to validate a
model's stability by fitting it to synthetic data generated by the
model itself. This process, known as parameter recovery, ensures the
model is well-behaved. In contrast, fit_p
is used in the
subsequent stage to fit the validated model to real experimental
data.
Estimation Method: rcv_d
does not include an
estimate
argument. This is because the synthetic data is
generated from known "true" parameters, which are drawn from
pre-defined distributions (typically uniform for most parameters and
exponential for the inverse temperature). Since the ground truth is
known, a hierarchical estimation (MAP) is not applicable. The
fit_p
function, however, requires this argument to handle
real data where the true parameters are unknown.
Policy Setting: In fit_p
, the policy
setting has different effects: "on-policy" is better for learning
choice patterns, while "off-policy" yields more accurate parameter
estimates. For rcv_d
, the process defaults to an "off-policy"
approach because its main objectives are to verify if the true
parameters can be accurately recovered and to assess whether competing
models are distinguishable, tasks for which off-policy estimation is
more suitable.
## Not run:
comparison <- binaryRL::fit_p(
data = binaryRL::Mason_2024_G2,
id = unique(binaryRL::Mason_2024_G2$Subject),
#+-----------------------------------------------------------------------------+#
#|----------------------------- black-box function ----------------------------|#
#funcs = c("your_funcs"),
policy = c("off", "on"),
fit_model = list(binaryRL::TD, binaryRL::RSTD, binaryRL::Utility),
model_name = c("TD", "RSTD", "Utility"),
#|--------------------------------- estimate ----------------------------------|#
estimate = c("MLE", "MAP"),
#|------------------------------------ MLE ------------------------------------|#
lower = list(c(0, 0), c(0, 0, 0), c(0, 0, 0)),
upper = list(c(1, 10), c(1, 1, 10), c(1, 1, 10)),
#|------------------------------------ MAP ------------------------------------|#
priors = list(
list(
eta = function(x) {stats::dunif(x, min = 0, max = 1, log = TRUE)},
tau = function(x) {stats::dexp(x, rate = 1, log = TRUE)}
),
list(
eta = function(x) {stats::dunif(x, min = 0, max = 1, log = TRUE)},
eta = function(x) {stats::dunif(x, min = 0, max = 1, log = TRUE)},
tau = function(x) {stats::dexp(x, rate = 1, log = TRUE)}
),
list(
eta = function(x) {stats::dunif(x, min = 0, max = 1, log = TRUE)},
gamma = function(x) {stats::dunif(x, min = 0, max = 1, log = TRUE)},
tau = function(x) {stats::dexp(x, rate = 1, log = TRUE)}
)
),
#|----------------------------- iteration number ------------------------------|#
iteration_i = 10,
iteration_g = 10,
#|-------------------------------- algorithms ---------------------------------|#
nc = 1, # <nc > 1>: parallel computation across subjects
# Base R Optimization
algorithm = "L-BFGS-B" # Gradient-Based (stats)
#|-----------------------------------------------------------------------------|#
# Specialized External Optimization
#algorithm = "GenSA" # Simulated Annealing (GenSA)
#algorithm = "GA" # Genetic Algorithm (GA)
#algorithm = "DEoptim" # Differential Evolution (DEoptim)
#algorithm = "PSO" # Particle Swarm Optimization (pso)
#algorithm = "Bayesian" # Bayesian Optimization (mlrMBO)
#algorithm = "CMA-ES" # Covariance Matrix Adapting (cmaes)
#|-----------------------------------------------------------------------------|#
# Optimization Library (nloptr)
#algorithm = c("NLOPT_GN_MLSL", "NLOPT_LN_BOBYQA")
#|-------------------------------- algorithms ---------------------------------|#
#################################################################################
)
result <- dplyr::bind_rows(comparison)
# Ensure the output directory exists before writing
if (!dir.exists("../OUTPUT")) {
dir.create("../OUTPUT", recursive = TRUE)
}
write.csv(result, "../OUTPUT/result_comparison.csv", row.names = FALSE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.