nlm [unused argument(s) (iter = 1)]

Ashley put the following comment on Chapter 5 of Introducing Monte Carlo Methods with R”:

I am reading chapter 5. I try to reproduced the result on page 128. The R codes don’t work on my laptop. When I try to run the following codes on page 128

> for (i in 1:(nlm(like,sta)$it)){
+ mmu=rbind(mmu,nlm(like,sta,iter=i)$est)}

I always get the error message

Error in f(x, …) : unused argument(s) (iter = 1)

It seems that the nlm function doesn’t accept the argument iter. I don’t know how to deal with it. I am in US. I guess the nlm version available to US R users is different from the version in EU. Please help.

And indeed with the most recent versions of R, like 2.12.1 on my own machine, calling nlm

> args(nlm)
function (f, p, ..., hessian = FALSE, typsize = rep(1, length(p)),
 fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-06,
 stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000), steptol = 1e-06,
 iterlim = 100, check.analyticals = TRUE)

with the abbreviated argument iter instead of iterlim produces the above error message. This means the full syntax iterlim=i should now be used. In addition, the function nlm produces the minimum of the first argument f and like should thus be defined as

> like=function(mu){
+   -sum(log((.25*dnorm(da-mu[1])+.75*dnorm(da-mu[2]))))}

to end up with local maxima as on Figure 5.2. (Note: I do not think there are US versus EU versions of R…)

Ashley also pointed out another mistake on that page, namely that we used

> da=rbind(rnorm(10^2),2.5+rnorm(3*10^2))

instead of

> da=c(rnorm(10^2),2.5+rnorm(3*10^2))

to create a sample. Since the two normal samples have different sizes, rbind induces a duplication of the smaller sample, not what we intended!

3 Responses to “nlm [unused argument(s) (iter = 1)]”

  1. I cannot plot like the Fig 5.2 by just typing lines(mmu,pch=19,lwd=2).
    Could you tell me how to do it? Thank you

    • Indeed, to cut on code length, we purposely did not include [but should have included!] the lines of code that are necessary to plot the surface of the log-posterior density. They can be found in the file Chapter.5.R [located in demo] as

      #log-likelihood surface
      mu1=mu2=seq(-2,5,le=250)
      lli=matrix(0,nco=250,nro=250)
      for (i in 1:250)
      for (j in 1:250)
        lli[i,j]=like(c(mu1[i],mu2[j]))
      par(mar=c(4,4,1,1))
      image(mu1,mu2,-lli,xlab=expression(mu[1]),
                  ylab=expression(mu[2]))
      contour(mu1,mu2,-lli,nle=100,add=T)
      

      or (since Chapter.5.R needs fixing because of the update in optimise) the very same code can be extracted from the function mhmix;

      mu1 = mu2 = seq(-2, 5, le = 250)
      lli = matrix(0, nco = 250, nro = 250)
      for (i in 1:250) for (j in 1:250) 
            lli[i, j] = like(c(mu1[i],mu2[j]))
      par(mar = c(4, 4, 1, 1))
      image(mu1, mu2, lli, xlab = expression(mu[1]), ylab = expression(mu[2]))
      contour(mu1, mu2, lli, nle = 100, add = T)
      
  2. Radford Neal Says:

    The difference seems to be that the … in the argument list for nlm is now right after the two required arguments, whereas it used to be at the end of the whole argument list (at least in 2.0.0). After …, only exact argument matches are allowed. The error message about iter being unused comes from iter not being an argument of the function being minimized, not from it not being an argument of nlm, since the iter argument is passed on as being part of the … argument of nlm.

    I’ve no idea why the order of arguments was changed.

    I think allowing partial argument matches in R is a mistake. It just promotes this sort of error, or worse, related errors in which there isn’t actually any error message. It may not be fixable now, though, for backwards compatibility reasons.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.