Description Details Author(s) References See Also Examples
The package provides some tools related to using the Nash variant of Marquardt's algorithm for nonlinear least squares. This package has been replaced with package nlsr that has similar and improved capabilities, but with some differences in structure and syntax.
Package: | nlmrt |
Type: | Package |
Version: | 1.0 |
Date: | 2012-03-05 |
License: | GPL-2 |
This package includes methods for solving nonlinear least squares problems specified by a modeling expression and given a starting vector of named paramters. Note: You must provide an expression of the form lhs ~ rhsexpression so that the residual expression rhsexpression - lhs can be computed. The expression can be enclosed in quotes, and this seems to give fewer difficulties with R. Data variables must already be defined, either within the parent environment or else in the dot-arguments. Other symbolic elements in the modeling expression must be standard functions or else parameters that are named in the start vector.
The main functions in nlmrt
are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | nlfb - Nash variant of the Marquardt procedure for nonlinear least squares,
with bounds constraints, using a residual and optionally Jacobian
described as \code{R} functions.
20120803: Todo: Make masks more consistent between nlfb and nlxb.
nlxb - Nash variant of the Marquardt procedure for nonlinear least squares,
with bounds constraints, using an expression to describe the residual via
an \code{R} modeling expression. The Jacobian is computed via symbolic
differentiation.
wrapnls - Uses nlxb to solve nonlinear least squares then calls nls() to
create an object of type nls.
model2grfun.R - Generate a gradient vector function from a nonlinear
model expression and a vector of named parameters.
model2jacfun.R - Generate a Jacobian matrix function from a nonlinear
model expression and a vector of named parameters.
model2resfun.R - Generate a residual vector function from a nonlinear
model expression and a vector of named parameters.
model2ssfun.R - Generate a sum of squares objective function from a
nonlinear model expression and a vector of named parameters.
modgr.R - compute gradient of the sum of squares function using the
Jacobian and residuals for a nonlinear least squares problem
modss.R - computer the sum of squares function from the residuals of
a nonlinear least squares problem
myfn.R, mygr.R, myjac.R, myres.R, myss.R - dummy functions that seem to
be needed so there is an available handle for output of functions that
generate various functions from expressions.
|
For testing purposes, there are also some experimental codes using different internal computations for the linear algebraic sub-problems in the inst/dev-codes/ sub-folder.
John C Nash
Maintainer: <nashjc@uottawa.ca>
Nash, J. C. (1979, 1990) _Compact Numerical Methods for Computers. Linear Algebra and Function Minimisation._ Adam Hilger./Institute of Physics Publications
others!!??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | rm(list=ls())
# library(nlmrt)
# traceval set TRUE to debug or give full history
traceval <- FALSE
## Problem in 1 parameter to ensure methods work in trivial case
cat("Problem in 1 parameter to ensure methods work in trivial case\n")
nobs<-8
tt <- seq(1,nobs)
dd <- 1.23*tt + 4*runif(nobs)
df <- data.frame(tt, dd)
a1par<-nlxb(dd ~ a*tt, start=c(a=1), data=df)
a1par
# Data for Hobbs problem
cat("Hobbs weed problem -- unscaled\n")
ydat <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
38.558, 50.156, 62.948, 75.995, 91.972) # for testing
y <- ydat # for testing
tdat <- seq_along(ydat) # for testing
eunsc <- y ~ b1/(1+b2*exp(-b3*tt))
cat("Hobbs unscaled with data in data frames\n")
weeddata1 <- data.frame(y=ydat, tt=tdat)
# scale the data
weeddata2 <- data.frame(y=1.5*ydat, tt=tdat)
start1 <- c(b1=1, b2=1, b3=1)
anlxb1 <- try(nlxb(eunsc, start=start1, trace=traceval, data=weeddata1))
print(anlxb1)
anlxb2 <- try(nlxb(eunsc, start=start1, trace=traceval, data=weeddata2))
print(anlxb2)
# Problem 2 - Gabor Grothendieck 2016-3-2
cat("Gabor G problem with zero residuals\n")
DF <- data.frame(x = c(5, 4, 3, 2, 1), y = c(1, 2, 3, 4, 5))
library(nlmrt)
nlxb1 <- nlxb(y ~ A * x + B, data = DF, start = c(A = 1, B = 6), trace=TRUE)
print(nlxb1)
# tmp <- readline("continue with start at the minimum -- failed on 2014 version. ")
nlxb0 <- nlxb(y ~ A * x + B, data = DF, start = c(A = -1, B = 6), trace=TRUE)
print(nlxb0)
## Not run:
# WARNING -- using T can get confusion with TRUE
tt <- tdat
# A simple starting vector -- must have named parameters for nlxb, nls, wrapnls.
cat("GLOBAL DATA\n")
anls1g <- try(nls(eunsc, start=start1, trace=traceval))
print(anls1g)
cat("GLOBAL DATA AND EXPRESSION -- SHOULD FAIL\n")
anlxb1g <- try(nlxb(eunsc, start=start1, trace=traceval))
print(anlxb1g)
## End(Not run) # end dontrun
rm(y)
rm(tt)
startf1 <- c(b1=1, b2=1, b3=.1)
## Not run:
## With BOUNDS
anlxb1 <- try(nlxb(eunsc, start=startf1, lower=c(b1=0, b2=0, b3=0),
upper=c(b1=500, b2=100, b3=5), trace=traceval, data=weeddata1))
print(anlxb1)
## End(Not run) # end dontrun
# Check nls too
## Not run:
cat("check nls result\n")
anlsb1 <- try(nls(eunsc, start=start1, lower=c(b1=0, b2=0, b3=0),
upper=c(b1=500, b2=100, b3=5), trace=traceval, data=weeddata1,
algorithm='port'))
print(anlsb1)
# tmp <- readline("next")
## End(Not run) # end dontrun
## Not run:
anlxb2 <- try(nlxb(eunsc, start=start1, lower=c(b1=0, b2=0, b3=0),
upper=c(b1=500, b2=100, b3=.25), trace=traceval, data=weeddata1))
print(anlxb2)
anlsb2 <- try(nls(eunsc, start=start1, lower=c(b1=0, b2=0, b3=0),
upper=c(b1=500, b2=100, b3=.25), trace=traceval,
data=weeddata1, algorithm='port'))
print(anlsb2)
# tmp <- readline("next")
## End(Not run) # end dontrun
## Not run:
cat("UNCONSTRAINED\n")
an1q <- try(nlxb(eunsc, start=start1, trace=traceval, data=weeddata1))
print(an1q)
# tmp <- readline("next")
## End(Not run) # end dontrun
## Not run:
cat("TEST MASKS\n")
anlsmnqm <- try(nlxb(eunsc, start=start1, lower=c(b1=0, b2=0, b3=0),
upper=c(b1=500, b2=100, b3=5), masked=c("b2"), trace=traceval, data=weeddata1))
print(anlsmnqm)
## End(Not run) # end dontrun
## Not run:
cat("MASKED\n")
an1qm3 <- try(nlxb(eunsc, start=start1, trace=traceval, data=weeddata1,
masked=c("b3")))
print(an1qm3)
# tmp <- readline("next")
# Note that the parameters are put in out of order to test code.
an1qm123 <- try(nlxb(eunsc, start=start1, trace=traceval, data=weeddata1,
masked=c("b2","b1","b3")))
print(an1qm123)
# tmp <- readline("next")
## End(Not run) # end dontrun
cat("BOUNDS test problem for Hobbs")
start2 <- c(b1=100, b2=10, b3=0.1)
an1qb1 <- try(nlxb(eunsc, start=start2, trace=traceval, data=weeddata1,
lower=c(0,0,0), upper=c(200, 60, .3)))
print(an1qb1)
## tmp <- readline("next")
cat("BOUNDS and MASK")
## Not run:
an1qbm2 <- try(nlxb(eunsc, start=start2, trace=traceval, data=weeddata1,
lower=c(0,0,0), upper=c(200, 60, .3), masked=c("b2")))
print(an1qbm2)
# tmp <- readline("next")
## End(Not run) # end dontrun
escal <- y ~ 100*b1/(1+10*b2*exp(-0.1*b3*tt))
suneasy <- c(b1=200, b2=50, b3=0.3)
ssceasy <- c(b1=2, b2=5, b3=3)
st1scal <- c(b1=100, b2=10, b3=0.1)
cat("EASY start -- unscaled")
anls01 <- try(nls(eunsc, start=suneasy, trace=traceval, data=weeddata1))
print(anls01)
anlmrt01 <- try(nlxb(eunsc, start=ssceasy, trace=traceval, data=weeddata1))
print(anlmrt01)
## Not run:
cat("All 1s start -- unscaled")
anls02 <- try(nls(eunsc, start=start1, trace=traceval, data=weeddata1))
if (class(anls02) == "try-error") {
cat("FAILED:")
print(anls02)
} else {
print(anls02)
}
anlmrt02 <- nlxb(eunsc, start=start1, trace=traceval, data=weeddata1)
print(anlmrt02)
cat("ones start -- scaled")
anls03 <- try(nls(escal, start=start1, trace=traceval, data=weeddata1))
print(anls03)
anlmrt03 <- nlxb(escal, start=start1, trace=traceval, data=weeddata1)
print(anlmrt03)
cat("HARD start -- scaled")
anls04 <- try(nls(escal, start=st1scal, trace=traceval, data=weeddata1))
print(anls04)
anlmrt04 <- nlxb(escal, start=st1scal, trace=traceval, data=weeddata1)
print(anlmrt04)
cat("EASY start -- scaled")
anls05 <- try(nls(escal, start=ssceasy, trace=traceval, data=weeddata1))
print(anls05)
anlmrt05 <- nlxb(escal, start=ssceasy, trace=traceval, data=weeddata1)
print(anlmrt03)
## End(Not run) # end dontrun
## Not run:
shobbs.res <- function(x){ # scaled Hobbs weeds problem -- residual
# This variant uses looping
if(length(x) != 3) stop("hobbs.res -- parameter vector n!=3")
y <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
38.558, 50.156, 62.948, 75.995, 91.972)
tt <- 1:12
res <- 100.0*x[1]/(1+x[2]*10.*exp(-0.1*x[3]*tt)) - y
}
shobbs.jac <- function(x) { # scaled Hobbs weeds problem -- Jacobian
jj <- matrix(0.0, 12, 3)
tt <- 1:12
yy <- exp(-0.1*x[3]*tt)
zz <- 100.0/(1+10.*x[2]*yy)
jj[tt,1] <- zz
jj[tt,2] <- -0.1*x[1]*zz*zz*yy
jj[tt,3] <- 0.01*x[1]*zz*zz*yy*x[2]*tt
return(jj)
}
cat("try nlfb\n")
st <- c(b1=1, b2=1, b3=1)
low <- -Inf
up <- Inf
ans1 <- nlfb(st, shobbs.res, shobbs.jac, trace=traceval)
ans1
cat("No jacobian function -- use internal approximation\n")
ans1n <- nlfb(st, shobbs.res, trace=TRUE, control=list(watch=TRUE)) # NO jacfn
ans1n
# tmp <- readline("Try with bounds at 2")
time2 <- system.time(ans2 <- nlfb(st, shobbs.res, shobbs.jac, upper=c(2,2,2),
trace=traceval))
ans2
time2
## End(Not run) # end dontrun
## Not run:
cat("BOUNDS")
st2s <- c(b1=1, b2=1, b3=1)
an1qb1 <- try(nlxb(escal, start=st2s, trace=traceval, data=weeddata1,
lower=c(0,0,0), upper=c(2, 6, 3), control=list(watch=FALSE)))
print(an1qb1)
# tmp <- readline("next")
ans2 <- nlfb(st2s,shobbs.res, shobbs.jac, lower=c(0,0,0), upper=c(2, 6, 3),
trace=traceval, control=list(watch=FALSE))
print(ans2)
cat("BUT ... nls() seems to do better from the TRACE information\n")
anlsb <- nls(escal, start=st2s, trace=traceval, data=weeddata1, lower=c(0,0,0),
upper=c(2,6,3), algorithm='port')
cat("However, let us check the answer\n")
print(anlsb)
cat("BUT...crossprod(resid(anlsb))=",crossprod(resid(anlsb)),"\n")
## End(Not run) # end dontrun
# tmp <- readline("next")
cat("Try wrapnls\n")
traceval <- FALSE
# Data for Hobbs problem
ydat <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
38.558, 50.156, 62.948, 75.995, 91.972) # for testing
tdat <- seq_along(ydat) # for testing
start1 <- c(b1=1, b2=1, b3=1)
escal <- y ~ 100*b1/(1+10*b2*exp(-0.1*b3*tt))
up1 <- c(2,6,3)
up2 <- c(1, 5, 9)
weeddata1 <- data.frame(y=ydat, tt=tdat)
an1w <- try(wrapnls(escal, start=start1, trace=traceval, data=weeddata1))
print(an1w)
## Not run:
cat("BOUNDED wrapnls\n")
an1wb <- try(wrapnls(escal, start=start1, trace=traceval, data=weeddata1, upper=up1))
print(an1wb)
cat("BOUNDED wrapnls\n")
an2wb <- try(wrapnls(escal, start=start1, trace=traceval, data=weeddata1, upper=up2))
print(an2wb)
cat("TRY MASKS ONLY\n")
an1xm3 <- try(nlxb(escal, start1, trace=traceval, data=weeddata1,
masked=c("b3")))
printsum(an1xm3)
an1fm3 <- try(nlfb(start1, shobbs.res, shobbs.jac, trace=traceval,
data=weeddata1, maskidx=c(3)))
printsum(an1fm3)
an1xm1 <- try(nlxb(escal, start1, trace=traceval, data=weeddata1,
masked=c("b1")))
printsum(an1xm1)
an1fm1 <- try(nlfb(start1, shobbs.res, shobbs.jac, trace=traceval,
data=weeddata1, maskidx=c(1)))
printsum(an1fm1)
## End(Not run) # end dontrun
# Need to check when all parameters masked.??
## Not run:
cat("\n\n Now check conversion of expression to function\n\n")
cat("K Vandepoel function\n")
x <- c(1,3,5,7) # data
y <- c(37.98,11.68,3.65,3.93)
penetrationks28 <- data.frame(x=x,y=y)
cat("Try nls() -- note the try() function!\n")
fit0 <- try(nls(y ~ (a+b*exp(1)^(-c * x)), data = penetrationks28,
start = c(a=0,b = 1,c=1), trace = TRUE))
print(fit0)
cat("\n\n")
fit1 <- nlxb(y ~ (a+b*exp(-c*x)), data = penetrationks28,
start = c(a=0,b=1,c=1), trace = TRUE)
printsum(fit1)
mexprn <- "y ~ (a+b*exp(-c*x))"
pvec <- c(a=0,b=1,c=1)
bnew <- c(a=10,b=3,c=4)
k.r <- model2resfun(mexprn , pvec)
k.j <- model2jacfun(mexprn , pvec)
k.f <- model2ssfun(mexprn , pvec)
k.g <- model2grfun(mexprn , pvec)
cat("At pvec:")
print(pvec)
rp <- k.r(pvec, x=x, y=y)
cat(" rp=")
print(rp)
rf <- k.f(pvec, x=x, y=y)
cat(" rf=")
print(rf)
rj <- k.j(pvec, x=x, y=y)
cat(" rj=")
print(rj)
rg <- k.g(pvec, x=x, y=y)
cat(" rg=")
print(rg)
cat("modss at pvec gives ")
print(modss(pvec, k.r, x=x, y=y))
cat("modgr at pvec gives ")
print(modgr(pvec, k.r, k.j, x=x, y=y))
cat("\n\n")
cat("At bnew:")
print(bnew)
rb <- k.r(bnew, x=x, y=y)
cat(" rb=")
print(rb)
rf <- k.f(bnew, x=x, y=y)
cat(" rf=")
print(rf)
rj <- k.j(bnew, x=x, y=y)
cat(" rj=")
print(rj)
rg <- k.g(bnew, x=x, y=y)
cat(" rg=")
print(rg)
cat("modss at bnew gives ")
print(modss(bnew, k.r, x=x, y=y))
cat("modgr at bnew gives ")
print(modgr(bnew, k.r, k.j, x=x, y=y))
cat("\n\n")
## End(Not run) ## end of dontrun
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.