Skip to contents

This function allows the selection of a custom S3 optimizer and additional arguments to be passed to it.

Usage

set_optimizer(
  optimizer_name,
  objective_name,
  start_name,
  lower_name,
  upper_name,
  optim_vals_name,
  objective_vals_name,
  ...
)

Arguments

optimizer_name

a one-length character specifying the name of the optimizer to be used.

objective_name

a one-length character with the name of the optimizer.

start_name

a one-length character with the name of the start/initial values input argument.

lower_name

a one-length character with the name of the lower bounds input argument.

upper_name

a one-length character with the name of the upper bounds input argument.

optim_vals_name

a one-length character with the name of the optimum values output argument.

objective_vals_name

a one-length character with the name of the objective function name output argument.

...

Further arguments to be passed to the selected optimizer. Do not include lower bounds, upper bounds and start/initial values. It must be included in the maxlogLreg function definition

Value

a list containing the selected optimizer name and the additional, the names of the start values argument, upper and lower bounds, optimum output values, objective function output value and the arguments passed to it.

Examples

library(EstimationTools)

#--------------------------------------------------------------------------------
# Example 1: Estimation in simulated normal distribution (alternative
# implementation using `nlminb` as a custom optimizer)
n <- 1000
x <- runif(n = n, -5, 6)
y <- rnorm(n = n, mean = -2 + 3 * x, sd = exp(1 + 0.3* x))
norm_data <- data.frame(y = y, x = x)

formulas <- list(sd.fo = ~ x, mean.fo = ~ x)
support <- list(interval = c(-Inf, Inf), type = 'continuous')

# Default optimizer
norm_mod1 <- maxlogLreg(formulas, y_dist = y ~ dnorm, support = support,
                        data = norm_data,
                        link = list(over = "sd", fun = "log_link"))
summary(norm_mod1)
#> _______________________________________________________________
#> Optimization routine: nlminb 
#> Standard Error calculation: Hessian from optim 
#> _______________________________________________________________
#>        AIC      BIC
#>   5148.477 5168.108
#> _______________________________________________________________
#> Fixed effects for mean
#> ---------------------------------------------------------------
#>              Estimate Std. Error Z value  Pr(>|z|)    
#> (Intercept) -1.781010   0.113784 -15.653 < 2.2e-16 ***
#> x            3.054806   0.030641  99.695 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> _______________________________________________________________
#> Fixed effects for log(sd) 
#> ---------------------------------------------------------------
#>              Estimate Std. Error Z value  Pr(>|z|)    
#> (Intercept) 1.0120201  0.0226350  44.710 < 2.2e-16 ***
#> x           0.2896703  0.0073068  39.644 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> _______________________________________________________________
#> Note: p-values valid under asymptotic normality of estimators 
#> ---

# Use the default optimizer as a custom one
optimizer <- set_optimizer(
  optimizer_name = "nlminb",
  objective_name = "objective",
  start_name = "start",
  lower_name = "lower",
  upper_name = "upper",
  optim_vals_name = "par",
  objective_vals_name = "objective"
)

norm_mod2 <- maxlogLreg(formulas, y_dist = y ~ dnorm, support = support,
                        data = norm_data,
                        link = list(over = "sd", fun = "log_link"),
                        optimizer = optimizer, start= NULL, lower = NULL,
                        upper = NULL)
summary(norm_mod2)
#> _______________________________________________________________
#> Optimization routine: nlminb Optimization routine: objective Optimization routine: list(lower_name = "lower", upper_name = "upper", start_name = "start") Optimization routine: list(optim_vals = "par", objective_vals = "objective") Optimization routine: NULL 
#> Standard Error calculation: Hessian from optim 
#> _______________________________________________________________
#>         AIC       BIC
#>   -5132.477 -5112.846
#> _______________________________________________________________
#> Fixed effects for mean
#> ---------------------------------------------------------------
#>              Estimate Std. Error Z value  Pr(>|z|)    
#> (Intercept) -1.781010   0.113784 -15.653 < 2.2e-16 ***
#> x            3.054806   0.030641  99.695 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> _______________________________________________________________
#> Fixed effects for log(sd) 
#> ---------------------------------------------------------------
#>              Estimate Std. Error Z value  Pr(>|z|)    
#> (Intercept) 1.0120201  0.0226350  44.710 < 2.2e-16 ***
#> x           0.2896703  0.0073068  39.644 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> _______________________________________________________________
#> Note: p-values valid under asymptotic normality of estimators 
#> ---