Title: | A Flexible Algorithm for Model Selection |
---|---|
Description: | Given a set of parameters describing model dynamics and a corresponding cost function, FAMoS performs a dynamic forward-backward model selection on a specified selection criterion. It also applies a non-local swap search method. Works on any cost function. For detailed information see Gabel et al. (2019) <doi:10.1371/journal.pcbi.1007230>. |
Authors: | Michael Gabel, Tobias Hohl |
Maintainer: | Michael Gabel <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0 |
Built: | 2025-03-05 03:46:55 UTC |
Source: | https://github.com/gabelhub/famos |
Calculates the evidence ratios for each parameter based on Akaike weights and plots them.
aicc.weights(input = getwd(), mrun = NULL, reorder = TRUE, save.output = NULL)
aicc.weights(input = getwd(), mrun = NULL, reorder = TRUE, save.output = NULL)
input |
Either a string containing the directory which holds the "FAMoS-Results" folder or a matrix containing the tested models along with the respective information criteria. Default to |
mrun |
A string giving the number of the corresponding FAMoS run, e.g "004". If NULL (default), all FAMoS runs in the "FAMoS-Results/TestedModels/" folder will be used for evaluation. |
reorder |
If TRUE, results will be ordered by evidence ratios (descending). If FALSE, the order of parameters will be the same as the order specified in |
save.output |
A string containing the location and name under which the figure should be saved (format is pdf). Default to NULL. |
The plot shows the relative support or evidence ratio for each parameter. Parameters included in the best model are printed bold and the corresponding lines are coloured in red.
A plot showing the evidence ratios for all model parameters. Additionally, the evidence ratios are returned.
#plot evidence ratios aicc.weights(input = famos.run) aicc.weights(input = famos.run, reorder = FALSE)
#plot evidence ratios aicc.weights(input = famos.run) aicc.weights(input = famos.run, reorder = FALSE)
Given a specific model, base.optim
fits the corresponding parameters and saves the output.
base.optim( binary, parms, fit.fn, homedir, use.optim = TRUE, optim.runs = 1, default.val = NULL, random.borders = 1, con.tol = 0.01, control.optim = list(maxit = 1000), parscale.pars = FALSE, scaling = NULL, verbose = FALSE, ... )
base.optim( binary, parms, fit.fn, homedir, use.optim = TRUE, optim.runs = 1, default.val = NULL, random.borders = 1, con.tol = 0.01, control.optim = list(maxit = 1000), parscale.pars = FALSE, scaling = NULL, verbose = FALSE, ... )
binary |
A vector containing zeroes and ones. Zero indicates that the corresponding parameter is not fitted. |
parms |
A named vector containing the initial values for |
fit.fn |
A cost function. Has to take the complete parameter vector as an input argument (needs to be named |
homedir |
A string giving the directory in which the result folders generated by |
use.optim |
Logical. If true, the cost function |
optim.runs |
The number of times that each model will be optimised. Default to 1. Numbers larger than 1 use random initial conditions (see |
default.val |
A named list containing the values that the non-fitted parameters should take. If NULL, all non-fitted parameters will be set to zero. Default values can be either given by a numeric value or by the name of the corresponding parameter the value should be inherited from (NOTE: In this case the corresponding parameter entry has to contain a numeric value). Default to NULL. |
random.borders |
The ranges from which the random initial parameter conditions for all optim.runs > 1 are sampled. Can be either given as a vector containing the relative deviations for all parameters or as a matrix containing in its first column the lower and in its second column the upper border values. Parameters are uniformly sampled based on |
con.tol |
The relative convergence tolerance. |
control.optim |
Control parameters passed along to |
parscale.pars |
Logical. If TRUE (default), the |
scaling |
Numeric vector determining how newly added model parameters are scaled. Only needed if |
verbose |
Logical. If TRUE, FAMoS will output all details about the current fitting procedure. |
... |
Additional parameters. |
The fitting routine of base.optim
is based on the function optim
. The number of fitting runs can be specified by the optim.runs
parameter in the famos
function. Here, the first fitting run takes the parameters supplied in parms
as a starting condition, while all following fitting runs sample new initial sets according to a uniform distribution based on the intervals [parms
- abs
(parms
), parms
+ abs
(parms
)].
Additionally, each fitting run is based on a while
-loop that compares the outcome of the previous and the current fit. Each fitting run is terminated when the specified convergence tolerance con.tol
is reached.
Saves the results obtained from fitting the corresponding model parameters in the respective files, from which they can be accessed by the main function famos
.
#setting data true.p2 <- 3 true.p5 <- 2 sim.data <- cbind.data.frame(range = 1:10, y = true.p2^2 * (1:10)^2 - exp(true.p5 * (1:10))) #define initial parameter values and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, binary, data){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*data$range^2 + p3*sin(data$range) + p4*data$range - exp(p5*data$range) diff <- sum((res - data$y)^2) #calculate AICC nr.par <- length(which(binary == 1)) nr.data <- nrow(data) AICC <- diff + 2*nr.par + 2*nr.par*(nr.par + 1)/(nr.data - nr.par -1) return(AICC) }) } #create directories if needed make.directories(tempdir()) #optimise the model parameters base.optim(binary = c(0,1,1,0,1), parms = inits, fit.fn = cost_function, homedir = tempdir(), data = sim.data) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
#setting data true.p2 <- 3 true.p5 <- 2 sim.data <- cbind.data.frame(range = 1:10, y = true.p2^2 * (1:10)^2 - exp(true.p5 * (1:10))) #define initial parameter values and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, binary, data){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*data$range^2 + p3*sin(data$range) + p4*data$range - exp(p5*data$range) diff <- sum((res - data$y)^2) #calculate AICC nr.par <- length(which(binary == 1)) nr.data <- nrow(data) AICC <- diff + 2*nr.par + 2*nr.par*(nr.par + 1)/(nr.data - nr.par -1) return(AICC) }) } #create directories if needed make.directories(tempdir()) #optimise the model parameters base.optim(binary = c(0,1,1,0,1), parms = inits, fit.fn = cost_function, homedir = tempdir(), data = sim.data) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
Combines fitted and non-fitted parameters and calls the fitting function. Serves as a wrapping function for the user-specified fitting function fit.fn
(see famos
).
combine.and.fit(par, par.names, fit.fn, binary = NULL, default.val = NULL, ...)
combine.and.fit(par, par.names, fit.fn, binary = NULL, default.val = NULL, ...)
par |
A named vector containing all parameters that are supposed to be fitted. |
par.names |
The names of all parameters |
fit.fn |
The cost or optimisation function (see |
binary |
A vector containing zeroes and ones. Zero indicates that the corresponding parameter is not fitted. |
default.val |
A named list containing the values that the non-fitted parameters should take. If NULL, all non-fitted parameters will be set to zero. Default values can be either given by a numeric value or by the name of the corresponding parameter the value should be inherited from (NOTE: In this case the corresponding parameter entry has to contain a numeric value). Default to NULL. |
... |
Other arguments. |
Returns the value calculated by the user-supplied cost or optimisation function
#set parameters and cost function fit.par <- c(p1 = 2, p2 = 4) name.par <- c("p1", "p2", "p3") defaults <- list(p1 = 0, p2 = 2, p3 = 4) cost.function <- function(parms){ parms[1] + parms[2] + parms[3] } #call combine.and.fit combine.and.fit(par = fit.par, par.names = name.par, fit.fn = cost.function) combine.and.fit(par = fit.par, par.names = name.par, fit.fn = cost.function, default.val = defaults)
#set parameters and cost function fit.par <- c(p1 = 2, p2 = 4) name.par <- c("p1", "p2", "p3") defaults <- list(p1 = 0, p2 = 2, p3 = 4) cost.function <- function(parms){ parms[1] + parms[2] + parms[3] } #call combine.and.fit combine.and.fit(par = fit.par, par.names = name.par, fit.fn = cost.function) combine.and.fit(par = fit.par, par.names = name.par, fit.fn = cost.function, default.val = defaults)
Combines fitted and non-fitted parameters into a single vector, taking into account the specified default values.
combine.par(fit.par, all.names, default.val = NULL)
combine.par(fit.par, all.names, default.val = NULL)
fit.par |
A named vector containing all parameters that are supposed to be fitted. |
all.names |
A vector containing the names of all parameters (fitted and non-fitted). |
default.val |
A named list containing the values that the non-fitted parameters should take. If NULL, all non-fitted parameters will be set to zero. Default values can be either given by a numeric value or by the name of the corresponding parameter the value should be inherited from (NOTE: If a string is supplied, the corresponding parameter entry has to contain a numeric value). Default to NULL. |
A named vector containing the elements of fit.par
and the non-fitted parameters, in the order given by all.names
. The non-fitted parameters are determined by the remaining names in all.names
and their values are set according to default.val
.
#set parameters, names and default values fits <- c(p1 = 3, p4 = -2) par.names <- c("p1", "p2", "p3", "p4", "p5") defaults <- list(p1 = 4, p2 = 10, p3 = "p1", p4 = 0, p5 = "p4") #combine the parameters in different ways combine.par(fit.par = fits, all.names = par.names) combine.par(fit.par = fits, all.names = par.names, default.val = defaults)
#set parameters, names and default values fits <- c(p1 = 3, p4 = -2) par.names <- c("p1", "p2", "p3", "p4", "p5") defaults <- list(p1 = 4, p2 = 10, p3 = "p1", p4 = 0, p5 = "p4") #combine the parameters in different ways combine.par(fit.par = fits, all.names = par.names) combine.par(fit.par = fits, all.names = par.names, default.val = defaults)
Given a vector containing all parameters of interest and a cost function, the FAMoS
looks for the most appropriate subset model to describe the given data.
famos( init.par, fit.fn, homedir = getwd(), do.not.fit = NULL, method = "forward", init.model.type = "random", refit = FALSE, use.optim = TRUE, optim.runs = 1, default.val = NULL, swap.parameters = NULL, critical.parameters = NULL, random.borders = 1, control.optim = list(maxit = 1000), parscale.pars = FALSE, con.tol = 0.1, save.performance = TRUE, use.futures = FALSE, reattempt = FALSE, log.interval = 600, interactive.session = TRUE, verbose = FALSE, ... )
famos( init.par, fit.fn, homedir = getwd(), do.not.fit = NULL, method = "forward", init.model.type = "random", refit = FALSE, use.optim = TRUE, optim.runs = 1, default.val = NULL, swap.parameters = NULL, critical.parameters = NULL, random.borders = 1, control.optim = list(maxit = 1000), parscale.pars = FALSE, con.tol = 0.1, save.performance = TRUE, use.futures = FALSE, reattempt = FALSE, log.interval = 600, interactive.session = TRUE, verbose = FALSE, ... )
init.par |
A named vector containing the initial parameter values. |
fit.fn |
A cost function. Has to take the complete parameter vector as an input (needs to be names |
homedir |
The directory to which the results should be saved to. |
do.not.fit |
The names of the parameters that are not supposed to be fitted. Default is NULL. |
method |
The starting method of FAMoS. Options are "forward" (forward search), "backward" (backward elimination) and "swap" (only if |
init.model.type |
The starting model. Options are "global" (starts with the complete model), "random" (creates a randomly sampled starting model) or "most.distant" (uses the model most dissimilar from all other previously tested models). Alternatively, a specific model can be used by giving the corresponding names of the parameters one wants to start with. Default to "random". |
refit |
If TRUE, previously tested models will be tested again. Default to FALSE. |
use.optim |
Logical. If true, the cost function |
optim.runs |
The number of times that each model will be optimised. Default to 1. Numbers larger than 1 use random initial conditions (see |
default.val |
A named list containing the values that the non-fitted parameters should take. If NULL, all non-fitted parameters will be set to zero. Default values can be either given by a numeric value or by the name of the corresponding parameter the value should be inherited from (NOTE: In this case the corresponding parameter entry has to contain a numeric value). Default to NULL. |
swap.parameters |
A list specifying which parameters are interchangeable. Each swap set is given as a vector containing the names of the respective parameters. Default to NULL. |
critical.parameters |
A list specifying sets of critical parameters. Critical sets are parameters sets, of which at least one parameter per set has to be present in each tested model. Default to NULL. |
random.borders |
The ranges from which the random initial parameter conditions for all |
control.optim |
Control parameters passed along to |
parscale.pars |
Logical. If TRUE, the |
con.tol |
The absolute convergence tolerance of each fitting run (see Details). Default is set to 0.1. |
save.performance |
Logical. If TRUE, the performance of |
use.futures |
Logical. If TRUE, FAMoS submits model evaluations via |
reattempt |
Logical. If TRUE, FAMoS will jump to a distant model, once the search methods are exhausted and continue from there. The algorithm terminates if the best model is encountered again or if all neighbouring models have been tested. If FALSE (default), FAMOS will terminate once the search methods are exhausted. |
log.interval |
The interval (in seconds) at which FAMoS informs about the current status, i.e. which models are still running and how much time has passed. Default to 600 (= 10 minutes). |
interactive.session |
Logical. If TRUE (default), FAMoS assumes it is running in an interactive session and users can supply input. If FALSE, no input is expected from the user, which can be helpful when running the script non-locally. |
verbose |
Logical. If TRUE, FAMoS will output all details about the current fitting procedure. |
... |
Other arguments that will be passed along to |
In each iteration, FAMoS finds all neighbouring models based on the current model and method, and subsequently tests them. If one of the tested models performs better than the current model, the model, but not the method, will be updated. Otherwise, the method, but not the model, will be adaptively changed, depending on the previously used methods.
The cost function fit.fn
can take the following inputs:
A named vector containing all parameter values. This input is mandatory. If use.optim = TRUE
, FAMoS will automatically subset the complete parameter set into fitted and non-fitted parameters.
Optional input. The binary vector contains the information which parameters are currently fitted. Fitted parameters are set to 1, non-fitted to 0. This input can be used to split the complete parameter set into fitted and non-fitted parameters if a customised optimisation function is used (see use.optim
).
Other parameters that should be passed to fit.fn
If use.optim = TRUE
, the cost function needs to return a single numeric value, which corresponds to the selection criterion value. However, if use.optim = FALSE
, the cost function needs to return a list containing in its first entry the selection criterion value and in its second entry the named vector of the fitted parameter values (non-fitted parameters are internally assessed).
A list containing the following elements:
The value of the selection criterion of the best model.
The values of the fitted parameter vector corresponding to the best model.
The binary information of the best model.
Vector indicating which parameters were fitted in the best model.
The total number of different models that were analysed. May include repeats.
The number of the current FAMoS run.
The first model evaluated by the FAMoS run.
#setting data true.p2 <- 3 true.p5 <- 2 sim.data <- cbind.data.frame(range = 1:10, y = true.p2^2 * (1:10)^2 - exp(true.p5 * (1:10))) #define initial parameter values and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, binary, data){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*data$range^2 + p3*sin(data$range) + p4*data$range - exp(p5*data$range) diff <- sum((res - data$y)^2) #calculate AICC nr.par <- length(which(binary == 1)) nr.data <- nrow(data) AICC <- diff + 2*nr.par + 2*nr.par*(nr.par + 1)/(nr.data - nr.par -1) return(AICC) }) } #set swap set swaps <- list(c("p1", "p5")) #perform model selection famos(init.par = inits, fit.fn = cost_function, homedir = tempdir(), method = "swap", swap.parameters = swaps, init.model.type = c("p1", "p3"), optim.runs = 1, data = sim.data) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
#setting data true.p2 <- 3 true.p5 <- 2 sim.data <- cbind.data.frame(range = 1:10, y = true.p2^2 * (1:10)^2 - exp(true.p5 * (1:10))) #define initial parameter values and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, binary, data){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*data$range^2 + p3*sin(data$range) + p4*data$range - exp(p5*data$range) diff <- sum((res - data$y)^2) #calculate AICC nr.par <- length(which(binary == 1)) nr.data <- nrow(data) AICC <- diff + 2*nr.par + 2*nr.par*(nr.par + 1)/(nr.data - nr.par -1) return(AICC) }) } #set swap set swaps <- list(c("p1", "p5")) #perform model selection famos(init.par = inits, fit.fn = cost_function, homedir = tempdir(), method = "swap", swap.parameters = swaps, init.model.type = c("p1", "p3"), optim.runs = 1, data = sim.data) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
For each FAMoS run famos.performance
plots the corresponding best model and selection criterion value.
famos.performance( input, path = getwd(), reattempts = NULL, save.output = NULL, ... )
famos.performance( input, path = getwd(), reattempts = NULL, save.output = NULL, ... )
input |
Either a string giving the three-digit number of the corresponding FAMoS run, e.g "004", or a matrix containing the tested models along with the respective information criteria. |
path |
If |
reattempts |
A numeric vector containing other SCVs that should be plotted. Used mainly for reattempts in the main famos routine. |
save.output |
A string containing the location and name under which the figure should be saved (format is .pdf). Default to NULL. |
... |
Other graphical parameters. |
The upper plot shows the improvement of the selection criterion over each FAMoS iteration. The best value is shown on the right axis. The lower plot depicts the corresponding best model of each iteration. Here, green colour shows added, red colour removed and blue colour swapped parameters. The parameters of the final model are printed bold.
A plot showing the value of the selection criterion and best model of each FAMoS iteration.
#plot the performance of an FAMoS run famos.performance(input = famos.run)
#plot the performance of an FAMoS run famos.performance(input = famos.run)
The data shows the performance of the FAMoS over the course of 8 iterations.
famos.run
famos.run
A matrix with 7 rows and 17 columns
Created by famos
.
This function can be used to find a model that is most distinct from all previously tested models.
get.most.distant(input = getwd(), mrun = NULL, max.number = 100)
get.most.distant(input = getwd(), mrun = NULL, max.number = 100)
input |
Either a string containing the directory which holds the "FAMoS-Results" folder or a matrix containing the tested models along with the respective information criteria. Default to |
mrun |
A string giving the number of the corresponding FAMoS run, e.g "004". If NULL (default), all FAMoS runs in the "FAMoS-Results/TestedModels/" folder will be used for evaluation. |
max.number |
The maximum number of times that the |
Taking the order from the 'TestedModels' files found in 'FAMoS-Results/TestedModels/', this function successively tries to obtain a previously untested model that is most distant from all previously tested ones (here, distance means the number of difference in fitted parameters). To this end, the function collects all previously tested models and sorts them according to their information criterion value (duplicates get removed in the process). Starting with the best model, the corresponding complement model is generated (i.e. the model containing all parameters that the best model didn't use) and the distance to all other models is calculated. The total distance of this model is then taken to be the minimal distance of all calculated distances. This procedure is repeated for the second best model and so on until all models have been assessed or the max.number
of models is reached.
A list containing in its first entry the maximal distance found, the second entry the parameter names and in its third entry the corresponding binary vector. Note that the model may not fulfill previously specified critical conditions.
get.most.distant(input = famos.run)
get.most.distant(input = famos.run)
Creates the directories, in which the results are going to be saved.
make.directories(homedir)
make.directories(homedir)
homedir |
The directory in which the result folders will be created. |
All files will be stored in the main folder "FAMoS-Results". It contains the following subdirectories:
Contains the information criteria and the corresponding parameter estimates of the best fitting model.
Contains figures showing the performance of the FAMoS.
Contains the fitted parameter values of each of the tested models.
Contains the binary information of all of the tested models.
Creates directories.
Tests if a model is violating specified critical conditions.
model.appr(current.parms, critical.parms, do.not.fit = NULL)
model.appr(current.parms, critical.parms, do.not.fit = NULL)
current.parms |
A vector containing the indices of the current model. |
critical.parms |
A list containing vectors which specify the critical parameter sets. Needs to be given by index and not by name (for conversion see |
do.not.fit |
A vector containing the indices of the parameters that are not to be fitted. |
TRUE, if the model is appropriate, FALSE, if it violates the specified critical conditions.
#define the models to be checked model1 <- c(1,2,5) model2 <- c(1,4,5) #define the critical conditions crits <- list(c(2,3)) #test the models model.appr(current.parms = model1, critical.parms = crits) model.appr(current.parms = model2, critical.parms = crits)
#define the models to be checked model1 <- c(1,2,5) model2 <- c(1,4,5) #define the critical conditions crits <- list(c(2,3)) #test the models model.appr(current.parms = model1, critical.parms = crits) model.appr(current.parms = model2, critical.parms = crits)
This function is designed to use the built-in option parscale
in optim
with absolute scaling values.
parscale.famos(par, scale, fix = 1, correction = NULL)
parscale.famos(par, scale, fix = 1, correction = NULL)
par |
A vector containing the values of the original parameters. |
scale |
A vector containing the corresponding absolute scaling values that will be used during the first steps in |
fix |
Integer containing the index of the parameter that will be scaled by 10% of its original value, meaning the corresponding entry in scale will be overwritten ( |
correction |
Used for scaling newly added parameter values by their original scale as specified in |
A vector that can be used to scale parscale
in optim
accordingly.
test.func<-function(x){ print(x) 3*x[1]+4*x[2] } pars <- c(1, 1000, 10) scaling <- c(0.1, 3000, 10) p.scale <- parscale.famos(par = pars, scale = scaling) optim(par = pars, fn = test.func, control = list(maxit = 10, parscale = p.scale, trace = TRUE))
test.func<-function(x){ print(x) 3*x[1]+4*x[2] } pars <- c(1, 1000, 10) scaling <- c(0.1, 3000, 10) p.scale <- parscale.famos(par = pars, scale = scaling) optim(par = pars, fn = test.func, control = list(maxit = 10, parscale = p.scale, trace = TRUE))
Generates a random starting model (if specified by the user in famos
), taking into account the critical conditions and the parameters which should not be fitted.
random.init.model(number.par, crit.parms = NULL, no.fit = NULL)
random.init.model(number.par, crit.parms = NULL, no.fit = NULL)
number.par |
The number of total parameters available. |
crit.parms |
A list containing vectors which specify the critical parameter sets. Needs to be given by index and not by name (for conversion see |
no.fit |
A vector containing the indices of the parameters which are not to be fitted. |
A vector containing the parameter indices of the random model.
#set critical conditions crits <- list(c(1,2,3), c(4,5)) #generate random model random.init.model(number.par = 20) random.init.model(number.par = 20, crit.parms = crits)
#set critical conditions crits <- list(c(1,2,3), c(4,5)) #generate random model random.init.model(number.par = 20) random.init.model(number.par = 20, crit.parms = crits)
Returns the results of a specified model that was tested by famos
.
retrieve.results(model, homedir = getwd(), all.names = NULL)
retrieve.results(model, homedir = getwd(), all.names = NULL)
model |
Either the binary number of the model as a string (e.g. "011001"), a named vector containing the names the names of the fitted parameters or a vector containing ones and zeroes to indicate which model parameter were fitted. If the names of the fitted parameters are supplied, |
homedir |
A string giving the directory in which the result folders are found. This is the same directory in which |
all.names |
A vector containing the names of all parameters. |
A vector containing the calculated selection criteria and estimated parameters of the specified model.
#setting data x.values <- 1:7 y.values <- 3^2 * x.values^2 - exp(2 * x.values) #define initial conditions and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, x.vals, y.vals){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*x.vals^2 + p3*sin(x.vals) + p4*x.vals - exp(p5*x.vals) diff <- sum((res - y.vals)^2) }) } #perform model selection res <- famos(init.par = inits, fit.fn = cost_function, nr.of.data = length(y.values), homedir = tempdir(), init.model.type = c("p2", "p3"), optim.runs = 1, x.vals = x.values, y.vals = y.values) #get results retrieve.results(model = "01110", homedir = tempdir()) retrieve.results(model = c("p2", "p3", "p4"), homedir = tempdir(), all.names = c("p1","p2", "p3", "p4", "p5")) retrieve.results(model = c(0,1,1,1,0), homedir = tempdir()) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
#setting data x.values <- 1:7 y.values <- 3^2 * x.values^2 - exp(2 * x.values) #define initial conditions and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, x.vals, y.vals){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*x.vals^2 + p3*sin(x.vals) + p4*x.vals - exp(p5*x.vals) diff <- sum((res - y.vals)^2) }) } #perform model selection res <- famos(init.par = inits, fit.fn = cost_function, nr.of.data = length(y.values), homedir = tempdir(), init.model.type = c("p2", "p3"), optim.runs = 1, x.vals = x.values, y.vals = y.values) #get results retrieve.results(model = "01110", homedir = tempdir()) retrieve.results(model = c("p2", "p3", "p4"), homedir = tempdir(), all.names = c("p1","p2", "p3", "p4", "p5")) retrieve.results(model = c(0,1,1,1,0), homedir = tempdir()) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
Returns the results of one FAMoS run. Includes the best parameter sets and corresponding selection criterion.
return.results(homedir, mrun)
return.results(homedir, mrun)
homedir |
A string giving the directory in which the result folders are found. This is the same directory in which |
mrun |
The number of the FAMoS run that is to be evaluated. Must be a three digit string in the form of '001'. Alternatively, supplying 'best' will return the best result that is found over several FAMoS runs. |
A list containing the following elements:
The value of the selection criterion of the best model.
The values of the fitted parameter vector corresponding to the best model.
The binary information of the best model.
Vector indicating which parameters were fitted in the best model.
The total number of different models that were analysed. May include repeats.
The number of the current FAMoS run.
The first model evaluated by the FAMoS run.
#setting data x.values <- 1:7 y.values <- 3^2 * x.values^2 - exp(2 * x.values) #define initial conditions and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, x.vals, y.vals){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*x.vals^2 + p3*sin(x.vals) + p4*x.vals - exp(p5*x.vals) diff <- sum((res - y.vals)^2) }) } #perform model selection res <- famos(init.par = inits, fit.fn = cost_function, nr.of.data = length(y.values), homedir = tempdir(), init.model.type = c("p2", "p3"), optim.runs = 1, x.vals = x.values, y.vals = y.values) #get results return.results(homedir = tempdir(), mrun = res$mrun) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
#setting data x.values <- 1:7 y.values <- 3^2 * x.values^2 - exp(2 * x.values) #define initial conditions and corresponding test function inits <- c(p1 = 3, p2 = 4, p3 = -2, p4 = 2, p5 = 0) cost_function <- function(parms, x.vals, y.vals){ if(max(abs(parms)) > 5){ return(NA) } with(as.list(c(parms)), { res <- p1*4 + p2^2*x.vals^2 + p3*sin(x.vals) + p4*x.vals - exp(p5*x.vals) diff <- sum((res - y.vals)^2) }) } #perform model selection res <- famos(init.par = inits, fit.fn = cost_function, nr.of.data = length(y.values), homedir = tempdir(), init.model.type = c("p2", "p3"), optim.runs = 1, x.vals = x.values, y.vals = y.values) #get results return.results(homedir = tempdir(), mrun = res$mrun) #delete tempdir unlink(paste0(tempdir(),"/FAMoS-Results"), recursive = TRUE)
Plots the selection criteria of the tested models in ascending order.
sc.order( input = getwd(), mrun = NULL, number = NULL, colour.par = NULL, save.output = NULL, ... )
sc.order( input = getwd(), mrun = NULL, number = NULL, colour.par = NULL, save.output = NULL, ... )
input |
Either a string containing the directory which holds the "FAMoS-Results" folder or a matrix containing the tested models along with the respective selection criteria. Default to |
mrun |
A string giving the number of the corresponding FAMoS run, e.g "004". If NULL (default), all FAMoS runs in the folder will be used for evaluation. |
number |
Specifies the number of models that will be plotted. If NULL (default), all tested models will be used for plotting. |
colour.par |
The name of a model parameter. All models containing this parameter will be coloured red. Default to NULL. |
save.output |
A string containing the location and name under which the figure should be saved (format is .pdf). Default to NULL. |
... |
Additional parameters that will be passed on to |
Barplot showing the ordered selection criteria of the tested models. Also returns a data frame containing each unique tested model with its best selection criteria.
#plot the selection criteria sc.order(input = famos.run) sc.order(input = famos.run, colour.par = "p1")
#plot the selection criteria sc.order(input = famos.run) sc.order(input = famos.run, colour.par = "p1")
Converts the user-specified list containing the names of the critical or swap parameters into a format that is more convenient for calculations.
set.crit.parms(critical.parameters, all.names)
set.crit.parms(critical.parameters, all.names)
critical.parameters |
A list containing vectors which specify either critical parameter sets or swap parameter sets (see Details). |
all.names |
A vector containing the names of all parameters. |
Critical sets are parameters sets, of which at least one per set has to be present in each tested model. If a model violates on of the critical conditions, it will not be fitted (see also model.appr
). On the other hand, swap parameter sets define which parameters are interchangeable. For more information see famos
.
A list containing the indices of the respective critical or swap sets.
#set critical set and names crits <- list(c("p1", "p2"), c("p5")) par.names <- c("p1", "p2", "p3", "p4", "p5") #convert the critical conditions set.crit.parms(critical.parameters = crits, all.names = par.names)
#set critical set and names crits <- list(c("p1", "p2"), c("p5")) par.names <- c("p1", "p2", "p3", "p4", "p5") #convert the critical conditions set.crit.parms(critical.parameters = crits, all.names = par.names)