Skip to content

Commit

Permalink
Allow 'constrOptim' optimizer in location-scale models fitted with rm…
Browse files Browse the repository at this point in the history
…a() when using an identity link and 'optbeta=TRUE'.
  • Loading branch information
wviechtb committed Apr 10, 2024
1 parent 0aecc95 commit 30a8013
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: metafor
Version: 4.7-1
Date: 2024-04-09
Version: 4.7-2
Date: 2024-04-10
Title: Meta-Analysis Package for R
Authors@R: person(given = "Wolfgang", family = "Viechtbauer", role = c("aut","cre"), email = "wvb@metafor-project.org", comment = c(ORCID = "0000-0003-3463-4063"))
Depends: R (>= 4.0.0), methods, Matrix, metadat, numDeriv
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# metafor 4.7-1 (2024-04-09)
# metafor 4.7-2 (2024-04-10)

- argument `shade` in `funnel()` now automatically uses a color gradient for the regions when multiple `level` values are specified

Expand Down
32 changes: 15 additions & 17 deletions R/rma.uni.r
Original file line number Diff line number Diff line change
Expand Up @@ -1746,26 +1746,15 @@ test="z", level=95, btt, att, tau2, verbose=FALSE, digits, control, ...) {
if (optimizer == "alabama")
optimizer <- "constrOptim.nl"

### when using an identity link, automatically set 'constrOptim' as the default optimizer (but 'solnp' by default when optbeta=TRUE)
### when using an identity link, automatically set 'constrOptim' as the default optimizer

if (link == "identity") {
if (optbeta) {
if (optimizer == "nlminb") {
optimizer <- "solnp"
} else {
if (!is.element(optimizer, c("solnp","nloptr","constrOptim.nl"))) {
optimizer <- "solnp"
warning(mstyle$warning(paste0("Can only use optimizers 'solnp', 'nloptr', or 'constrOptim.nl' when link='identity' and optbeta=TRUE (resetting to '", optimizer, "').")), call.=FALSE)
}
}
if (optimizer == "nlminb") {
optimizer <- "constrOptim"
} else {
if (optimizer == "nlminb") {
if (!is.element(optimizer, c("constrOptim","solnp","nloptr","constrOptim.nl"))) {
optimizer <- "constrOptim"
} else {
if (!is.element(optimizer, c("constrOptim","solnp","nloptr","constrOptim.nl"))) {
optimizer <- "constrOptim"
warning(mstyle$warning(paste0("Can only use optimizers 'constrOptim', 'solnp', 'nloptr', or 'constrOptim.nl' when link='identity' (resetting to '", optimizer, "').")), call.=FALSE)
}
warning(mstyle$warning(paste0("Can only use optimizers 'constrOptim', 'solnp', 'nloptr', or 'constrOptim.nl' when link='identity' (resetting to '", optimizer, "').")), call.=FALSE)
}
}
}
Expand Down Expand Up @@ -1842,6 +1831,15 @@ test="z", level=95, btt, att, tau2, verbose=FALSE, digits, control, ...) {
stop(mstyle$stop(paste0("Length of 'beta' argument (", length(beta), ") does not match actual number of parameters (", p, ").")))
}

### needed for constrOptim() when optbeta=TRUE

X0 <- X
X0[] <- 0

} else {

X0 <- NULL

}

### rescale Z matrix (only for models with moderators, models including a non-fixed intercept term, when not placing constraints on alpha, and when not optimizing over beta)
Expand Down Expand Up @@ -2042,7 +2040,7 @@ test="z", level=95, btt, att, tau2, verbose=FALSE, digits, control, ...) {
if (link == "identity") {

if (optimizer == "constrOptim")
optcall <- paste0("constrOptim(theta=c(beta.init, alpha.init), f=.ll.rma.ls, grad=NULL, ui=Z, ci=rep(0,k),
optcall <- paste0("constrOptim(theta=c(beta.init, alpha.init), f=.ll.rma.ls, grad=NULL, ui=cbind(X0,Z), ci=rep(0,k),
yi=yi, vi=vi, X=X, Z=Z, reml=reml, k=k, pX=p, alpha.arg=alpha, beta.arg=beta, verbose=verbose, digits=digits,
REMLf=con$REMLf, link=link, mZ=mZ, alpha.min=alpha.min, alpha.max=alpha.max, alpha.transf=TRUE,
tau2.min=con$tau2.min, tau2.max=con$tau2.max, optbeta=optbeta", ctrl.arg, ")\n")
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.onAttach <- function(libname, pkgname) {

ver <- "4.7-1"
ver <- "4.7-2"

loadmsg <- paste0("\nLoading the 'metafor' package (version ", ver, "). For an\nintroduction to the package please type: help(metafor)\n")

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metafor: A Meta-Analysis Package for R
[![R build status](https://github.com/wviechtb/metafor/workflows/R-CMD-check/badge.svg)](https://github.com/wviechtb/metafor/actions)
[![Code Coverage](https://codecov.io/gh/wviechtb/metafor/branch/master/graph/badge.svg)](https://app.codecov.io/gh/wviechtb/metafor)
[![CRAN Version](https://www.r-pkg.org/badges/version/metafor)](https://cran.r-project.org/package=metafor)
[![devel Version](https://img.shields.io/badge/devel-4.7--1-brightgreen.svg)](https://www.metafor-project.org/doku.php/installation#development_version)
[![devel Version](https://img.shields.io/badge/devel-4.7--2-brightgreen.svg)](https://www.metafor-project.org/doku.php/installation#development_version)
[![Monthly Downloads](https://cranlogs.r-pkg.org/badges/metafor)](https://cranlogs.r-pkg.org/badges/metafor)
[![Total Downloads](https://cranlogs.r-pkg.org/badges/grand-total/metafor)](https://cranlogs.r-pkg.org/badges/grand-total/metafor)

Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test_misc_rma_mv.r
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,13 @@ test_that("rma.mv() correctly handles 'beta' argument", {
dat <- escalc(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg)
res <- rma(yi, vi, scale = ~ 1, data=dat, optbeta=TRUE, beta=0)
ll1 <- logLik(res)
res <- rma.mv(yi, vi, random = ~ 1 | trial, data=dat, beta=0)
res <- rma(yi, vi, scale = ~ 1, data=dat, optbeta=TRUE, beta=0, link="identity")
ll2 <- logLik(res)
res <- rma.mv(yi, vi, random = ~ 1 | trial, data=dat, beta=0)
ll3 <- logLik(res)

expect_equivalent(ll1, ll2, tolerance=.tol[["fit"]])
expect_equivalent(ll1, ll3, tolerance=.tol[["fit"]])

})

Expand Down

0 comments on commit 30a8013

Please sign in to comment.