## Example 7.17 in Introduction to Monte Carlo methods with R

Posted in Books, R, Statistics, University life with tags , on January 4, 2012 by xi'an

I received the following email about Introducing Monte Carlo Methods with R a few days ago:

Hallo Dr. Robert,

I  am studying your fine book for myself. There´s a little problem in examples 7.17 and 8.1: in the R code a function “gu” is used and a reference given to ex. 5.17, but I cann´t find there a definition of “gu“. (gu = log formula (5.15) ?) Could you give me a hint?

from Elmar Kisslinger. Indeed, the gu function used in this analysis of the logit model is not available in the book, it is provided by

```#function for MCMC
gu=function(mu,i,beta,sigma){

sum((y[i,]*(beta*x[i,]+mu))-log(1+exp(beta*x[i,]+mu)))-0.5*mu^2/sigma^2
}
```

and is only available in the associated mcsm R package as part of the randogit.R code. (Incidentally, this is my 1500th post on the ‘Og! And this coincides with the 3000th comment…)

## pimax(mcsm)

Posted in Books, R, Statistics with tags , , , , on May 13, 2010 by xi'an

The function pimax from our package mcsm is used in to reproduce Figure 5.11 of our book Introducing Monte Carlo Methods with R. (The name comes from using the Pima Indian R benchmark as the reference dataset.) I got this email from Josué

I ran the ‘pimax’ example from the mcsm manual, and it gave me the following message:

`> pimax(Nsim = 10^3)`
`Error in raaj[t, ] = apply(as.matrix(aas), 1, margap) :`
`number of items to replace is not a multiple of replacement length`
`> pimax()`
`Error in raaj[t, ] = apply(as.matrix(aas), 1, margap) :`
`number of items to replace is not a multiple of replacement length`

but when running pimax(10^2) on my machine I did get the following picture and no error message. So I wonder if this is a matter of version of R or something else…

## Difficulty with mcsm?

Posted in Books, R, Statistics with tags , , , on May 5, 2010 by xi'an

An email from Keith I got this morning:

Professor Robert,
I have loaded the mcsm package to windows.
The following messages appear in the R console:

```trying URL 'http://cran.stat.ucla.edu/bin/windows/contrib/2.9/mcsm_1.0.zip'
Content type 'application/zip' length 193590 bytes (189 Kb)
opened URL
package 'mcsm' successfully unpacked and MD5 sums checked```

But when I use the demo command as on page 37 from Introducing Monte Carlo Methods with R, I get:

```demo (Chapter.1)
Error in demo (Chapter.1): No demo found for topic 'Chapter.1'```

How do I fix this?

I think the fix is in loading the package by library(mcsm) [each time one needs it] after installing it [only once]…

## How to use mcsm

Posted in Books, R, Statistics with tags , , , , , on February 28, 2010 by xi'an

Within the past two days, I received this email

Dear Prof.Robert
I have just bought your recent book on Introducing Monte Carlo Methods with R.  Although I have checked your web page for the R programs (bits of the code in the book, codes for generating the figures and tec – not the package available on cran)  used in the book, I have not found them.
I wonder whether you could make them available.
Thank you very much for your time and patience.
Yours Sincerely

and that one

Dear Prof. Robert,
I bought “Introducing Monte Carlo Methods with R” from Amazon booksore. I am a teacher at […] University, and I choose this book as a textbook in my class.
I can not find the R package “mcsm” according to your book (page 5). Where can I download the R package “mcsm”?
Best regards,

so I fear that readers may miss the piece of information provided in the book. As indicated on pages 36-37 of Introducing Monte Carlo Methods with R, mcsm is a registred R package, readers can therefore download it manually from CRAN,  but they should first try using install.packages in R as this is both easier and safer. (They should check on the main R project webpage for more help in installing packages.)

Another useful information for readers is that the code used on the examples of Introducing Monte Carlo Methods with R is available from mcsm through the demo command/code. Typing demo(Chapter.3) starts the production of the examples of Chapter 3:

> demo(Chapter.3)

demo(Chapter.3)
————————
Type  <Return>   to start :
> # Section 3.1, Introduction
>
> ch=function(la){ integrate(function(x){x^(la-1)*exp(-x)},0,Inf)\$val}
> plot(lgamma(seq(.01,10,le=100)),log(apply(as.matrix(
+  seq(.01,10,le=100)),1,ch)),xlab=”log(integrate(f))”,
+  ylab=expression(log(Gamma(lambda))),pch=19,cex=.6)
> S=readline(prompt=”Type  <Return>   to continue : “)
Type  <Return>   to continue :

and obviously the same for all other chapters. This also means the code is available in the corresponding file, something like

/usr/lib/R/site-library/mcsm/demo/Chapter.3.R

## Andrew’s criticisms

Posted in Books, Statistics with tags , , , , , , , , on January 23, 2010 by xi'an

Andrew Gelman has just written a most entertaining review of “Introducing Monte Carlo Methods with R” on his blog. The first sentence is ominous as the book seemingly reminded him of communists and fascists…! The explanation for this frightening debut is that the connection between the components of statistics

… ↔ Probability theory ↔ Theoretical statistics↔Statistical methodology ↔ Applications ↔ Computation ↔ Probability theory ↔ …

may be seen as a torus just as the range of political ideologies, the argument being that both George and I switched from proving mathematical minimaxity theorems about James-Stein estimators to proving convergence theorems. about Metropolis-Hastings algorithms. After pondering Andrew’s lines for a while, I am far from sure this is a positive assessment of Introducing Monte Carlo Methods with R! Indeed, at the first glance, it may give the blog reader the feeling that this is yet another theoretical book about Monte Carlo methods, written by theorists and mainly for theorists (Andrew wrote “applied researchers such as myself will get much more use out of theory as applied to computation“)… While we strive to distance ourselves from making a baby version of Monte Carlo Statistical Method, choosing the format of a Use R! book to clarify even further the purpose of the book: to lead (our students and) our readers to understand Monte Carlo methods through worked-out examples to the point of developping their own methods, while keeping the theory at bay.

A second read shows that Andrew’s point is much more subtle, namely that as (formerly?) mathematical statisticians, we have adopted a terse style that (maybe unconsciously) shy way from giving too much detail and explanations: once a definition is provided, it should suffice to itself! This leads to what Andrew calls little puzzles, where the reader needs to stop and reason out why things are as they are. (“I noticed a bunch of other examples of this sort, where the narrative just flows by and, as a reader, you have to stop and grab it. Lots of fun.”)  I noticed the same reactions from my students, so I quite agree with this point. When learning with a book, you need to sit with a piece of paper on one side (if the margins are too narrow), your computer on the other side and test everything for yourself. This is actually an intended feature, if not spelled out more clearly, and I thus appreciate very much Andrew’s conclusion that “it would also be an excellent book for a course on statistical computing“!

There is also Andrew’s comment that the book is ugly, which stings, but again can be seen in a different light.I obviously do not find Introducing Monte Carlo Methods with R ugly but the printing could have been indeed nicer and the fact that the printers used the jpeg versions of the figures instead of the postscript or pdf versions did not help. The raw R output presented verbatim in most pages is not particularly beautiful either, but this is truly intended, for readers who cannot test the code immediately (as when reading in the metro or listening to the course at the same time). The R programs are far from perfect R programs, but examples of what a “standard” beginner would do. I also agree with the suggestion of an epilogue: we wrote several times during the course of the book that we were not providing the big picture and that many aspects of the Monte Carlo methodology were not covered, but this would be worth repeating at the end, along with the few general recommendations we can make about better R programming. Another thing to add in the next edition!

A final interesting remark is that the very first comment on Andrew’s post was about solutions! This is a strong request from readers. nowadays, and thus seems like a compulsory element of publishing books with exercises. (As we discovered a wee too late for Bayesian Core!)