Interactive Least-Squares Fitting of Isotropic EPR Spectra by Simulation Iteratioins/Evaulations
Source:R/eval_sim_EPR_isoFitb.R
eval_sim_EPR_isoFitb.RdOrdinary least-squares fitting of the isotropic EPR spectra by simulation iterations/evaluations.
In principle, this function is based on the eval_sim_EPR_isoFit however,
it represents a more interactive version of the eval_sim_EPR_isoFit.
Namely, it provides {ggplot2} objects (graphs, see the Value and the plot.fit description)
in order to simultaneously check/explore the optimization/fitting process at each of the evaluations
(refer to the Nevals argument). The actual function was built because during the parallel processing
(see the eval_sim_EPR_isoFit_space) it is not possible to display the actual
EPR spectra during the optimization/fitting procedure. In the upcoming package updates, it will be also implemented
into the plot_eval_ExpSim_app.
Usage
eval_sim_EPR_isoFitb(
data.spectr.expr,
Intensity.expr = "dIepr_over_dB",
Intensity.sim = "dIeprSim_over_dB",
nu.GHz,
B.unit = "G",
Blim = NULL,
nuclear.system.noA = NULL,
baseline.correct = "constant",
lineG.content = 0.5,
lineSpecs.form = "derivative",
optim.method = c("neldermead", "cobyla", "lbfgs"),
optim.params.init,
optim.params.fix.id = NULL,
Niters.per.eval = 128,
Nevals = 16
)Arguments
- data.spectr.expr
Data frame object/table, containing the experimental spectral data with the magnetic flux density (
"B_mT"or"B_G") and the intensity (see theIntensity.exprargument) columns.- Intensity.expr
Character string, pointing to column name of the experimental EPR intensity within the original
data.spectr.expr. Default:dIepr_over_dB.- Intensity.sim
Character string, pointing to column name of the simulated EPR intensity within the related output data frame. Default:
Intensity.sim = "dIeprSim_over_dB".- nu.GHz
Numeric value, microwave frequency in
GHz.- B.unit
Character string, denoting the magnetic flux density unit e.g.
B.unit = "G"(gauss, default) orB.unit = "mT"/"T"(millitesla/tesla).- Blim
Numeric vector, magnetic flux density in
mT/Gcorresponding to lower and upper visual limit of the selected \(B\)-region, such asBlim = c(3495.4,3595.4). Default:Blim = NULL(corresponding to the entire \(B\)-range of EPR spectrum). This does not correspond to simulation fit data region !. If narrower \(B\)-region (in comparison to the original one) is required to fit the EPR spectrum, the filtering has to be done prior to own fitting procedure. For example, if the original data frame (df.spectr.orginwithin 200 G), of the experimental EPR spectrum, should be fitted within the region ofB = c(3450,3550)(100 G), following operation must be performed:df.spectr.actuall <- df.spectr.origin |> dplyr::filter(dplyr::between(B_G,3450,3550)), where thedf.spectr.actuallserves as an input (represented by thedata.spectr.exprargument) for the function.- nuclear.system.noA
List or nested list without estimated hyperfine coupling constant values, such as
list("14N",1)orlist(list("14N", 2),list("1H", 4),list("1H", 12)). The \(A\)-values are already defined as elements of theoptim.params.initargument/vector. If the EPR spectrum does not display any hyperfine splitting, the argument definition readsnuclear.system.noA = NULL(default).- baseline.correct
Character string, referring to baseline correction of the simulated/fitted spectrum. Corrections like
"constant"(default),"linear"or"quadratic"can be applied.- lineG.content
Numeric value between
0and1, referring to content of the Gaussian line form. IflineG.content = 1(default) it corresponds to "pure" Gaussian line form and iflineG.content = 0it corresponds to Lorentzian one. The value from (0,1) (e.g.lineG.content = 0.5) represents the linear combination (for the example above, with the coefficients 0.5 and 0.5) of both line forms => so called pseudo-Voigt.- lineSpecs.form
Character string, describing either
"derivative"(default) or"integrated"(i.e."absorption"which can be used as well) line form of the analyzed EPR spectrum/data.- optim.method
Character string, setting the optimization method/algorithm. Even though, by default, the argument is defined as a vector,
optim.method = c("neldermead","cobyla","lbfgs"), only one method from those three can be selected. For exampleoptim.method = "neldermead"(default). For additional information to all three available methods, please refer to theoptim_for_EPR_fitness.- optim.params.init
Numeric vector with the initial parameter guess (elements) where the first five elements are immutable
g-value (g-factor)
Gaussian linewidth
Lorentzian linewidth
baseline constant (intercept or offset)
intensity multiplication constant
baseline slope (only if
baseline.correct = "linear"orbaseline.correct = "quadratic"), ifbaseline.correct = "constant"it corresponds to the first HFCC (\(A_1\))baseline quadratic coefficient (only if
baseline.correct = "quadratic"), ifbaseline.correct = "constant"it corresponds to the second HFCC (\(A_2\)), ifbaseline.correct = "linear"it corresponds to the first HFCC (\(A_1\))additional HFCC (\(A_3\)) if
baseline.correct = "constant"or ifbaseline.correct = "linear"(\(A_2\)), ifbaseline.correct = "quadratic"it corresponds to the first HFCC (\(A_1\))...additional HFCCs (\(A_k...\), each vector element is reserved only for one \(A\))
DO NOT PUT ANY OF THESE PARAMETERS to
NULL. If the lineshape is expected to be pure Lorentzian or pure Gaussian then put the corresponding vector element to0.- optim.params.fix.id
Numeric value/vector of index/indices of the
optim.params.init, corresponding to optimization/simulation parameter(s) to be fixed. For example, if the g-Value and the (intensity) multiplication constant should not be optimized (their values should be fixed during the procedure), the argument must be defined as follows:optim.params.fix.id = c(1,5)(see also theoptim.params.initdescription for the simulation/optimized parameters order). Default:optim.params.fix.id = NULL, indicating that none of theoptim.params.initis fixed, i.e. all parameters are optimized within their default/defined boundaries (see theoptim.params.lowerand theoptim.params.upper). Alternatively, the parameter value(s) can be also adjusted by assigning theoptim.params.init+optim.params.lower+optim.params.upperelements to the same value, as already demonstrated in theExamples.- Niters.per.eval
Numeric value, equal to the number of iterations per one the
Nevals(see the relatedNevalsargument description). Default:Niters.per.eval = 128. This argument, among other things, depends on the complexity of thenuclear.system(nuclear.system.noA). For example, if an aminoxyl radical with 1 x 14N nucleus interaction is considered, theNiters.per.evalmight be lower than the default one (e.g. 64). However, the more complex system like N,N,N′,N′-Tetramethyl-p-phenylenediamine radical cation, with 2 x 14N, 4 x 1H and 12 x 1H nuclei interaction may require higherNiters.per.eval(e.g. 128). The higher theNiters.per.eval, the longer the computational fitting/optimization time.- Nevals
Numeric value, corresponding to total number of evaluations/cycles (i.e. how many loops/runs will be considered for the entire fitting/optimization procedure). This argument is related to the
Niters.per.eval, where the total number of iterations readsNevalsxNiters.per.eval. For example, forNevals = 16(default) andNiters.per.eval = 128, the number of iterations = 16 x 128 = 2048. HigherNevalsrequires longer computational time. For interactive visualization of EPR spectra (during the optimization/fitting procedure) no parallel computing is supported (contrary to theeval_sim_EPR_isoFit_space).
Value
List with the following elements:
- plot.fit
A graph panel (2 rows x 2 columns) with 4 components. Simulated + experimental EPR spectra, residuals vs \(B\) magnetic flux density, sum of the residual squares (RSS) vs iteration as well as AIC+BIC information criteria vs iteration. Even though it represents the final status of those four dependencies, the actual version, appears at each of the
Nevals(see theArguments) in order to follow the progress of the fitting procedure interactively.- best.params.optim
Named vector of the optimized (best fitting) simulation parameters, corresponding to minimum
RSS. The actual values also appear at each of theNevals(see theArguments), interactively during the fitting procedure.- df.params.optim
A data frame object of all simulation parameters +
RSS(residual sum of squares) +AIC+BIC(Akaike and Bayessian information criteria, respectively, see also theeval_ABIC_forFit), evaluated during the fitting process at corresponding actual iteration. Visual progress of all those variables may be nicely followed by theesquisseror other R plotting/visualization function.- nuclear.system
List consisting of all considered nuclei, and their optimized (best fitted) coupling constants \(A\) in MHz, which may be used in any additional EPR simulation (see the
eval_sim_EPR_iso).- ra
Final (related to the minimum of
RSS) residual analysis list (refer to theplot_eval_RA_forFitand/or toeval_sim_EPR_isoFit) extended by themessagewhich distribution (Normal/Gaussian, Student's t-distribution or Cauchy) fits the residuals at best and was actually applied to evaluate AIC and BIC (consult theeval_ABIC_forFitfor a detailed description).- cor.df
Function to evaluate correlation
matrixof a data frame, consisting of EPR experimental, simulated (best fit) and residual intensities as columns/variables. Such matrix can be additionally nicely visualized by a correlationplotcreated by thecorrplotfunction. Threemethodsare available:"pearson"(default),"spearman"(captures monotonic relationships) and"kendall"(for small data ensembles), see alsocor.- plot.best.sim.expr
Visualization function to plot either static
ggplot2(interactive = FALSE, default) or interactiveplotly(interactive = TRUE) comparison between the experimental and the simulated EPR spectrum. In addition, both spectra, within the staticggplot, can be presented either in theoverlay = TRUE(default) or in the offset (overlay = FALSE) mode.- df.best.sim.expr
A data frame object, containing experimental and simulated spectra in long/tidy form, actually corresponding to
plot.best.sim.exprwhileinteractive = TRUE.
Examples
if (FALSE) { # \dontrun{
test.list <- eval_sim_EPR_isoFitb(
data.spectr.expr = data.tmpd.spec,
nu.GHz = data.tmpd.params.values[1,2],
nuclear.system.noA = list(
list("14N", 2),
list("1H", 4),
list("1H", 12)
),
optim.method = "cobyla",
optim.params.init = c(
2.00305, ## g_iso
0.521, ## Gaussian linewidth
0.52, ## Lorentz linewidth
0, ## offset/baseline constant
3.2e5, ## intensity multiplication coeff.
19.5, 5.5, 19.5 ## required As in MHz
),
Niters.per.eval = 128, ## number of iterations per evaluation
Nevals = 17 ## total number of evaluations
## total number of iterations = Niters.per.eval * Nevals
)
} # }