Typo in Chapter 5

Gilles Guillot from Technical University of Denmark taught a course based on our R book and he pointed out to me several typos in Chapter 5 of “Introducing Monte Carlo Methods with R”:

  • p.137 second equation from bottom

    h(\theta+\beta \zeta) - h(\theta+\beta \zeta)

    should be

    h(\theta+\beta \zeta) - h(\theta-\beta \zeta)

    [right, another victim of cut-and-paste]

  • p. 138  Example 5.7 denominator in the gradient should be 2*beta [yes, the error actually occurs twice. And once again in the R code]
  • p. 138 : First paragraph Not a typo but a lack of details: are the conditions on \alpha and \beta necessary and sufficient? [indeed, they are sufficient]
  • demo(Chapter.5) triggers an error message [true, the shortcut max=TRUE instead of maximise=TRUE in optimise does not work with R version 2.11.1]

I checked the last item with the new version of R and got the following confirmation that optimise does not accept (any longer) the abbreviation of its arguments…

demo(Chapter.5)
————————

Type  <Return>   to start :

> # Section 5.1, Numerical approximation
>
> ref=rcauchy(5001)

> f=function(y){-sum(log(1+(x-y)^2))}

> mi=NULL

> for (i in 1:400){
+   x=ref[1:i]
+   aut=optimise(f,interval=c(-10,10),max=T)
+   mi=c(mi,aut$max)
+   }
Error in f(arg, …) : unused argument(s) (max = TRUE)

> optimise(f,interval=c(-10,10),maximum=T)
$maximum
[1] -2.571893

$objective
[1] -6.661338e-15

9 Responses to “Typo in Chapter 5”

  1. […] Ashley’s latest comments on Chapter 5 of Introducing Monte Carlo Methods with R, I realised Example 5.5 was totally […]

  2. Dr. Robert,

    Thanks for your reply.

    On page 134, we have the following R codes
    “> cau=rcauchy(10^2)
    > mcau=median(cau)
    > rcau=diff(quantile(cau,c(.25,.75)))
    > f=function(x){
    + z=dcauchy(outer(x,cau,FUN=”-“))
    + apply(z,1,mean)}
    > fcst=integrate(f,from=-20,to=20)
    > ft=function(x){f(x)/fcst}
    > g=function(x){dt((x-mcau)/rcau,df=49)/rcau}
    > curve(ft,from=-10,to=10)
    > curve(g,add=T)

    I think we should replace apply(z,1,mean) with apply(z,1,prod). I am 100% sure about this.

    Thanks.

    Ashley

  3. […] put the following comment on Chapter 5 of Introducing Monte Carlo Methods with R”: I am reading chapter 5. I try to […]

  4. Hello Dr. Robert,
    My question is still on example 5.2, to simulate a sample of 400 observations from the mixture, we use

    > da=rbind(rnorm(10^2),2.5+rnorm(3*10^2))
    > like=function(mu){
    + sum(log((.25*dnorm(da-mu[1])+.75*dnorm(da-mu[2]))))}

    I think we need to replace rbind with c.

    > da=sample(c(rnorm(10^2),2.5+rnorm(3*10^2)))
    > like=function(mu){
    + sum(log((.25*dnorm(da-mu[1])+.75*dnorm(da-mu[2]))))}

    Since rnorm(10^2) is shorter than norm2.5+(3*10^2), rbind will recycle the values in rnorm(10^2).

    Regards,

    Ashley

    • Indeed, this is a clear mistake! The right syntax is c(rnorm(10^2),2.5+rnorm(3*10^2)). Note that you do not need to use sample, though, because the order in which the sample is produced does not matter. And da is not a sample from a mixture, either, because the proportion from each component is fixed.

  5. Dr. Robert,

    After one night’s sleep, I realized what a silly question I asked. Now I got to know the meaning of the R codes.

    For your information, the package relaimpo, for assessing relative importance of repressors in linear regression, has different versions for users in US and users outside US.

    Thank you very much.

    Ashley

  6. Dear Dr.Robert,

    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.

    Best regards,

    Ashley

    • It is indeed no longer possible with latest versions of R to use abbreviations in functions like optimise or nlm. A shame in my opinion! Note that R is international and does not have different versions for the US and for the EU.

Leave a comment

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