Skip to content

Commit

Permalink
add upper and lower bounds for optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Oct 8, 2024
1 parent a84c309 commit 7a04009
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: dsem
Type: Package
Title: Fit Dynamic Structural Equation Models
Version: 1.3.0
Date: 2024-07-18
Version: 1.4.0
Date: 2024-10-07
Authors@R:
c(person(given = "James",
family = "Thorson",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dsem 1.4.0

* Adding option for lower and upper bounds

# dsem 1.3.0

* Adding option to specify constant marginal variance, as alternative to existing
Expand Down
19 changes: 17 additions & 2 deletions R/dsem.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ function( sem,
if( ncol(tsdata) != length(unique(colnames(tsdata))) ){
stop("Please check `colnames(tsdata)` to confirm that all variables (columns) have a unique name")
}
if( !all(control$lower == -Inf) | !all(control$upper == Inf) ){
if( control$newton_loops > 0 ){
stop("If specifying `lower` or `upper`, please set `dsem_control('newton_loops'=0)`")
}
}

#
options = c(
Expand Down Expand Up @@ -264,6 +269,8 @@ function( sem,
out$opt = nlminb( start = out$opt$par,
objective = obj$fn,
gradient = obj$gr,
upper = control$upper,
lower = control$lower,
control = list( eval.max = control$eval.max,
iter.max = control$iter.max,
trace = control$trace ) )
Expand Down Expand Up @@ -369,6 +376,10 @@ function( sem,
#' to \code{\link[TMB]{sdreport}}.
#' @param extra_convergence_checks Boolean indicating whether to run extra checks on model
#' convergence.
#' @param lower vectors of lower bounds, replicated to be as long as start and passed to \code{\link[stats]{nlminb}}.
#' If unspecified, all parameters are assumed to be unconstrained.
#' @param upper vectors of upper bounds, replicated to be as long as start and passed to \code{\link[stats]{nlminb}}.
#' If unspecified, all parameters are assumed to be unconstrained.
#'
#' @return
#' An S3 object of class "dsem_control" that specifies detailed model settings,
Expand All @@ -391,7 +402,9 @@ function( nlminb_loops = 1,
parameters = NULL,
map = NULL,
getJointPrecision = FALSE,
extra_convergence_checks = TRUE ){
extra_convergence_checks = TRUE,
lower = -Inf,
upper = Inf ){

gmrf_parameterization = match.arg(gmrf_parameterization)
constant_variance = match.arg(constant_variance)
Expand All @@ -413,7 +426,9 @@ function( nlminb_loops = 1,
parameters = parameters,
map = map,
getJointPrecision = getJointPrecision,
extra_convergence_checks = extra_convergence_checks
extra_convergence_checks = extra_convergence_checks,
lower = lower,
upper = upper
), class = "dsem_control" )
}

Expand Down
10 changes: 9 additions & 1 deletion man/dsem_control.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a04009

Please sign in to comment.