From 05004363d84e5a157fbfb74f1d2d4b0badf09fa1 Mon Sep 17 00:00:00 2001 From: JorisChau Date: Tue, 30 Apr 2024 10:50:55 +0200 Subject: [PATCH] revert to static Makevars.win + add hatvalues() --- .github/workflows/R-CMD-check.yaml | 11 ------- DESCRIPTION | 4 +-- NAMESPACE | 2 ++ NEWS.md | 5 +++ R/nls.R | 3 +- R/nls_methods.R | 26 ++++++++++++++++ cleanup.win | 4 --- configure | 18 +++++------ configure.ac | 2 +- configure.win | 47 ----------------------------- cran-comments.md | 5 +-- inst/unit_tests/unit_tests_gslnls.R | 6 ++-- man/hatvalues.gsl_nls.Rd | 36 ++++++++++++++++++++++ src/Makevars.win | 6 ++++ src/Makevars.win.in | 9 ------ 15 files changed, 96 insertions(+), 88 deletions(-) delete mode 100755 cleanup.win delete mode 100755 configure.win create mode 100644 man/hatvalues.gsl_nls.Rd create mode 100644 src/Makevars.win delete mode 100644 src/Makevars.win.in diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index b6d93c4..049d60f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -39,7 +39,6 @@ jobs: - {os: windows-latest, r: 'devel', rtools: '44'} - {os: windows-latest, r: 'next', rtools: '44'} - {os: windows-latest, r: 'release', rtools: '43'} - #- {os: windows-latest, r: 'oldrel', rtools: '42'} - {os: ubuntu-latest, r: 'devel'} - {os: ubuntu-latest, r: 'next'} - {os: ubuntu-latest, r: 'release'} @@ -48,9 +47,6 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes - RTOOLS42_HOME: C:\rtools42 - RTOOLS43_HOME: C:\rtools43 - RTOOLS44_HOME: C:\rtools44 steps: - name: Configure git @@ -72,13 +68,6 @@ jobs: run: | brew install gsl - - name: Install gsl (windows) - if: runner.os == 'windows' - run: | - pacman -Sy --noconfirm - pacman -S --noconfirm mingw-w64-{i686,x86_64}-gsl - shell: bash - - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: rcmdcheck diff --git a/DESCRIPTION b/DESCRIPTION index 4f01ffd..0b96019 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: gslnls Type: Package Title: GSL Multi-Start Nonlinear Least-Squares Fitting -Version: 1.3.1 -Date: 2024-04-25 +Version: 1.3.2 +Date: 2024-04-30 Authors@R: person("Joris", "Chau", email = "joris.chau@openanalytics.eu", role = c("aut", "cre")) Description: An R interface to nonlinear least-squares optimization with the GNU Scientific Library (GSL), see M. Galassi et al. (2009, ISBN:0954612078). The available trust region methods include the Levenberg-Marquardt algorithm with and without geodesic acceleration, the Steihaug-Toint conjugate gradient algorithm for large systems and several variants of Powell's dogleg algorithm. Multi-start optimization based on quasi-random samples is implemented using a modified version of the algorithm in Hickernell and Yuan (1997, OR Transactions). Bindings are provided to tune a number of parameters affecting the low-level aspects of the trust region algorithms. The interface mimics R's nls() function and returns model objects inheriting from the same class. BugReports: https://github.com/JorisChau/gslnls/issues diff --git a/NAMESPACE b/NAMESPACE index d6e880a..36358ad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ S3method(gsl_nls,"function") S3method(gsl_nls,formula) S3method(gsl_nls_large,"function") S3method(gsl_nls_large,formula) +S3method(hatvalues,gsl_nls) S3method(logLik,gsl_nls) S3method(nobs,gsl_nls) S3method(predict,gsl_nls) @@ -35,6 +36,7 @@ importFrom(stats,df.residual) importFrom(stats,fitted) importFrom(stats,formula) importFrom(stats,getInitial) +importFrom(stats,hatvalues) importFrom(stats,model.weights) importFrom(stats,nls) importFrom(stats,nls.control) diff --git a/NEWS.md b/NEWS.md index a8faba9..5eb307a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# gslnls 1.3.2 + +* Reverted to static Makevars.win (supplied by T. Kalibera) +* Added new method `hatvalues()` + # gslnls 1.3.1 * Minor edits configure.ac to fix cran check results diff --git a/R/nls.R b/R/nls.R index 1ca1ffc..63040b1 100644 --- a/R/nls.R +++ b/R/nls.R @@ -108,7 +108,8 @@ #' See the individual method descriptions for the structures of the returned lists and the generic functions #' applicable to objects of both classes. #' @useDynLib gslnls, .registration = TRUE -#' @importFrom stats nls numericDeriv deriv as.formula coef deviance df.residual fitted vcov formula getInitial model.weights pf pt qt setNames sigma nobs +#' @importFrom stats nls numericDeriv deriv as.formula coef deviance df.residual fitted vcov formula getInitial model.weights +#' @importFrom stats pf pt qt setNames sigma nobs hatvalues #' @seealso \code{\link[stats]{nls}} #' @seealso \url{https://www.gnu.org/software/gsl/doc/html/nls.html} #' @references M. Galassi et al., \emph{GNU Scientific Library Reference Manual (3rd Ed.)}, ISBN 0954612078. diff --git a/R/nls_methods.R b/R/nls_methods.R index b59d75e..b27a4a7 100644 --- a/R/nls_methods.R +++ b/R/nls_methods.R @@ -425,6 +425,32 @@ vcov.gsl_nls <- function(object, ...) { sm$cov.unscaled * sm$sigma^2 } +#' Calculate leverage values +#' @description Returns leverage values (hat values) from a fitted \code{"gsl_nls"} object based on the estimated +#' variance-covariance matrix of the model parameters. +#' @inheritParams coef.gsl_nls +#' @param model An object inheriting from class \code{"gsl_nls"}. +#' @return Numeric vector of leverage values similar to \code{\link[stats]{hatvalues}}. +#' @seealso \code{\link[stats]{hatvalues}} +#' @examples +#' ## data +#' set.seed(1) +#' n <- 25 +#' xy <- data.frame( +#' x = (1:n) / n, +#' y = 2.5 * exp(-1.5 * (1:n) / n) + rnorm(n, sd = 0.1) +#' ) +#' ## model +#' obj <- gsl_nls(fn = y ~ A * exp(-lam * x), data = xy, start = c(A = 1, lam = 1)) +#' +#' hatvalues(obj) +#' @export +hatvalues.gsl_nls <- function(model, ...) { + J <- model$m$gradient() + JtJinv <- chol2inv(model$m$Rmat()) + diag((J %*% JtJinv) %*% t(J)) +} + #' Anova tables #' @description Returns the analysis of variance (or deviance) tables for two or #' more fitted \code{"gsl_nls"} objects. diff --git a/cleanup.win b/cleanup.win deleted file mode 100755 index 9453232..0000000 --- a/cleanup.win +++ /dev/null @@ -1,4 +0,0 @@ -#! /bin/sh -rm -f src/*.o src/*.so src/Makevars.win src/symbols.rds \ - *~ */*~ -exit 0 diff --git a/configure b/configure index d1a8c4f..25ed1fa 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.71 for gslnls 1.3.1. +# Generated by GNU Autoconf 2.71 for gslnls 1.3.2. # # Report bugs to . # @@ -610,8 +610,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='gslnls' PACKAGE_TARNAME='gslnls' -PACKAGE_VERSION='1.3.1' -PACKAGE_STRING='gslnls 1.3.1' +PACKAGE_VERSION='1.3.2' +PACKAGE_STRING='gslnls 1.3.2' PACKAGE_BUGREPORT='https://github.com/JorisChau/gslnls/issues/' PACKAGE_URL='' @@ -1231,7 +1231,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures gslnls 1.3.1 to adapt to many kinds of systems. +\`configure' configures gslnls 1.3.2 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1293,7 +1293,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of gslnls 1.3.1:";; + short | recursive ) echo "Configuration of gslnls 1.3.2:";; esac cat <<\_ACEOF @@ -1375,7 +1375,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -gslnls configure 1.3.1 +gslnls configure 1.3.2 generated by GNU Autoconf 2.71 Copyright (C) 2021 Free Software Foundation, Inc. @@ -1533,7 +1533,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by gslnls $as_me 1.3.1, which was +It was created by gslnls $as_me 1.3.2, which was generated by GNU Autoconf 2.71. Invocation command line was $ $0$ac_configure_args_raw @@ -4066,7 +4066,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by gslnls $as_me 1.3.1, which was +This file was extended by gslnls $as_me 1.3.2, which was generated by GNU Autoconf 2.71. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -4121,7 +4121,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\ cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config='$ac_cs_config_escaped' ac_cs_version="\\ -gslnls config.status 1.3.1 +gslnls config.status 1.3.2 configured by $0, generated by GNU Autoconf 2.71, with options \\"\$ac_cs_config\\" diff --git a/configure.ac b/configure.ac index 89d4884..107e363 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ -AC_INIT([gslnls],[1.3.1],[https://github.com/JorisChau/gslnls/issues/]) +AC_INIT([gslnls],[1.3.2],[https://github.com/JorisChau/gslnls/issues/]) : ${R_HOME=`R RHOME`} if test -z "${R_HOME}"; then diff --git a/configure.win b/configure.win deleted file mode 100755 index 7dfe3ba..0000000 --- a/configure.win +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env sh - -: ${R_HOME=`R RHOME`} -if test -z "${R_HOME}"; then - echo "could not determine R_HOME" - exit 1 -fi -CC=`"${R_HOME}/bin/R" CMD config CC` -CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS` -LDFLAGS=`"${R_HOME}/bin/R" CMD config LDFLAGS` -R_VER=`"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" --vanilla -e "cat(paste0(version[['major']], as.integer(version[['minor']])))"` - -# Set LIB_GSL for gh-actions and r-winbuilder -if [ -z "${LIB_GSL}" ]; then - if [ "${R_VER}" = '42' ] && [ -n "${RTOOLS42_HOME}" ] && [ -f "${RTOOLS42_HOME}/x86_64-w64-mingw32.static.posix/lib/libgsl.a" ]; then - echo "** LIB_GSL is missing, detected GSL installation in ${RTOOLS42_HOME}" - LIB_GSL="${RTOOLS42_HOME}/x86_64-w64-mingw32.static.posix" - elif [ "${R_VER}" = '43' ] && [ -n "${RTOOLS43_HOME}" ] && [ -f "${RTOOLS43_HOME}/x86_64-w64-mingw32.static.posix/lib/libgsl.a" ]; then - echo "** LIB_GSL is missing, detected GSL installation in ${RTOOLS43_HOME}" - LIB_GSL="${RTOOLS43_HOME}/x86_64-w64-mingw32.static.posix" - elif [ "${R_VER}" = '44' -o "${R_VER}" = '45' ] && [ -n "${RTOOLS44_HOME}" ] && [ -f "${RTOOLS44_HOME}/x86_64-w64-mingw32.static.posix/lib/libgsl.a" ]; then - echo "** LIB_GSL is missing, detected GSL installation in ${RTOOLS44_HOME}" - LIB_GSL="${RTOOLS44_HOME}/x86_64-w64-mingw32.static.posix" - else - echo "------------------------- LIBGSL ERROR ---------------------------------" - echo "Configuration failed because environment variable LIB_GSL was not found." - echo "Set variable LIB_GSL to the parent of the directory containing libgsl.a," - echo "for instance \${RTOOLS43_HOME}/x86_64-w64-mingw32.static.posix. Note the" - echo "use of forward instead of backward slashes in the directory path. If GSL" - echo "(>= 2.2) is not yet installed, preferably install GSL using the Rtools" - echo "package manager, e.g. pacman -S mingw-w64-{i686,x86_64}-gsl. (See the" - echo "Rtools documentation for additional details)." - echo "------------------------------------------------------------------------" - exit 1; - fi -fi - -# Use LIB_GSL for header and linker arguments -GSL_CFLAGS='-I"${LIB_GSL}/include"' -GSL_LIBS='-L${LIB_GSL}/lib -lgsl -lgslcblas' - -sed \ - -e "s|@GSL_CFLAGS@|${GSL_CFLAGS}|" \ - -e "s|@GSL_LIBS@|${GSL_LIBS}|" \ - src/Makevars.win.in > src/Makevars.win - -exit 0 diff --git a/cran-comments.md b/cran-comments.md index d70202a..ae4f7c9 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,10 +1,11 @@ -## CRAN package version 1.3.1 +## CRAN package version 1.3.2 * System requirements: GSL (>= 2.2) ## Comments -* Edited configure.ac + configure to fix cran check errors (fedora builds) +* Removed configure.win + Makevars.win.in +* Added static Makevars.win as recommended by T. Kalibera ## Test environments diff --git a/inst/unit_tests/unit_tests_gslnls.R b/inst/unit_tests/unit_tests_gslnls.R index 4c26b3c..f81c24a 100644 --- a/inst/unit_tests/unit_tests_gslnls.R +++ b/inst/unit_tests/unit_tests_gslnls.R @@ -237,13 +237,13 @@ penalty_fit_dgT <- gsl_nls_large( fn = function(theta) c(sqrt(1e-5) * (theta - 1), sum(theta^2) - 0.25), y = rep(0, 11L), start = rep(0.15, 10L), - jac = function(theta) as(rbind(diag(rep(sqrt(1e-5), length(theta))), 2 * t(theta)), Class = "dgTMatrix") + jac = function(theta) as(rbind(diag(rep(sqrt(1e-5), length(theta))), 2 * t(theta)), Class = "TsparseMatrix") ) penalty_fit_dge <- gsl_nls_large( fn = function(theta) c(sqrt(1e-5) * (theta - 1), sum(theta^2) - 0.25), y = rep(0, 11L), start = rep(0.15, 10L), - jac = function(theta) as(rbind(diag(rep(sqrt(1e-5), length(theta))), 2 * t(theta)), Class = "dgeMatrix") + jac = function(theta) as(rbind(diag(rep(sqrt(1e-5), length(theta))), 2 * t(theta)), Class = "unpackedMatrix") ) dotest_tol("1.8.6", inherits(penalty_fit_dgC, "gsl_nls"), TRUE) @@ -272,6 +272,7 @@ if(requireNamespace("MASS")) { } dotest("1.9.14", dim(confintd(misra1a_fit1, expr = "b1 + b2")), c(1L, 3L)) dotest("1.9.15", capture.output(misra1a_fit1)[c(1, 2)], c("Nonlinear regression model", " model: y ~ b1 * (1 - exp(-b2 * x))")) +dotest("1.9.16", length(hatvalues(misra1a_fit1)), 14L) dotest("1.10.1", length(fitted(madsen_fit1)), 3L) dotest("1.10.2", nobs(madsen_fit1), 3L) @@ -290,5 +291,6 @@ dotest("1.10.12", names(anova(madsen_fit1, madsen_fit2)), c("Res.Df", "Res.Sum S dotest("1.10.13", dim(confint(madsen_fit1, parm = c(1L, 2L))), c(2L, 2L)) dotest("1.10.14", dim(confintd(madsen_fit1, expr = quote(x1 - x2), dtype = "numeric")), c(1L, 3L)) dotest("1.10.15", capture.output(madsen_fit1)[c(1, 2)], c("Nonlinear regression model", " model: y ~ fn(x)")) +dotest("1.10.16", length(hatvalues(madsen_fit1)), 3L) cat("Completed gslnls unit tests\n") diff --git a/man/hatvalues.gsl_nls.Rd b/man/hatvalues.gsl_nls.Rd new file mode 100644 index 0000000..4e09a16 --- /dev/null +++ b/man/hatvalues.gsl_nls.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/nls_methods.R +\name{hatvalues.gsl_nls} +\alias{hatvalues.gsl_nls} +\title{Calculate leverage values} +\usage{ +\method{hatvalues}{gsl_nls}(model, ...) +} +\arguments{ +\item{model}{An object inheriting from class \code{"gsl_nls"}.} + +\item{...}{At present no optional arguments are used.} +} +\value{ +Numeric vector of leverage values similar to \code{\link[stats]{hatvalues}}. +} +\description{ +Returns leverage values (hat values) from a fitted \code{"gsl_nls"} object based on the estimated +variance-covariance matrix of the model parameters. +} +\examples{ +## data +set.seed(1) +n <- 25 +xy <- data.frame( + x = (1:n) / n, + y = 2.5 * exp(-1.5 * (1:n) / n) + rnorm(n, sd = 0.1) +) +## model +obj <- gsl_nls(fn = y ~ A * exp(-lam * x), data = xy, start = c(A = 1, lam = 1)) + +hatvalues(obj) +} +\seealso{ +\code{\link[stats]{hatvalues}} +} diff --git a/src/Makevars.win b/src/Makevars.win new file mode 100644 index 0000000..d7599e0 --- /dev/null +++ b/src/Makevars.win @@ -0,0 +1,6 @@ +# use pkg-config conditionally to support older versions of Rtools +ifeq (,$(shell pkg-config --version 2>/dev/null)) + PKG_LIBS = -lgsl -lgslcblas -lm +else + PKG_LIBS = $(shell pkg-config --libs gsl) +endif diff --git a/src/Makevars.win.in b/src/Makevars.win.in deleted file mode 100644 index e84b92a..0000000 --- a/src/Makevars.win.in +++ /dev/null @@ -1,9 +0,0 @@ - -# set by configure.win -GSL_CFLAGS = @GSL_CFLAGS@ -GSL_LIBS = @GSL_LIBS@ - -# combine with standard arguments for R -PKG_CPPFLAGS = $(GSL_CFLAGS) -PKG_LIBS = $(GSL_LIBS) -