Archive for Introducing Monte Carlo Methods with R

CHANCE: special issue on George Casella’s books

Posted in Books, R, Statistics, University life with tags , , , , , , , on February 10, 2013 by xi'an

The special issue of CHANCE on George Casella’s books has now appeared and it contains both my earlier post on George passing away and  reviews of several of his books, as follows:

Although all of those books have appeared between twenty and five years ago, the reviews are definitely worth reading! (Disclaimer: I am the editor of the Books Review section who contacted friends of George to write the reviews, as well as the co-author of two of those books!) They bring in my (therefore biased) opinion a worthy evaluation of the depths and impacts of those major books, and they also reveal why George was a great teacher, bringing much into the classroom and to his students… (Unless I am confused the whole series of reviews is available to all, and not only to CHANCE subscribers. Thanks, Sam!)

what’s wrong with package comment?!

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

I spent most of the Sunday afternoon trying to understand why defining

[sourcecode language=”text” gutter=”false”]
\newcommand{\era}{\end{comment}}
[/sourcecode]

did not have the same effect as writing the line

[sourcecode language=”text” gutter=”false”]
\end{comment}
[/sourcecode]

until I found there is a clash due to the comment package… The assuredly simple code

[sourcecode language=”text” gutter=”false”]
\documentclass{book}
\usepackage{comment}
\begin{document}

\begin{comment}
prompt, you could embark on an on-line visit of the main features of {\tt R} by typing \verb+demo()+ after the prompt (make sure to test \verb+demo(image)+ and \verb+demo(graphics)+ to get an idea of the
\era

Self-explanatory.
\end{document}
[/sourcecode]

produces an error message:

[sourcecode language=”text” gutter=”false”]
Runaway argument?
! File ended while scanning use of \next.
<inserted text>
\par
<*> moretest.tex
[/sourcecode]

This is quite an inconvenience as I need to compile my solution manual for “Introducing Monte Carlo Methods with R” with the even-numbered exercises commented out or not depending on the version… (Leaving this package out and using the comment command within the verbatim package does not work either because era does not seem to be recognised as the end of a commented part…)

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

[sourcecode language=”r” gutter=”false”]
#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
}
[/sourcecode]

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…)

Typos in Introduction to Monte Carlo Methods with R

Posted in Books, R, Statistics, University life with tags , , , , on October 13, 2011 by xi'an

The two translators of our book in Japanese, Kazue & Motohiro Ishida, contacted me about some R code mistakes in the book. The translation is nearly done and they checked every piece of code in the book, an endeavour for which I am very grateful! Here are the two issues they have noticed (after incorporating the typos signaled in the overall up-to-date summary):

First, in Example 4.4, I omitted some checkings and forgot about a minus sign, meaning Figure 4.4 (right) is wrong. (The more frustrating since this example covers perplexity!) The zeros must be controlled via code lines like

[sourcecode language=”r” gutter=”false”]
> wachd[wachd<10^(-10)]=10^(-10)
[/sourcecode]

instead of the meaningless

[sourcecode language=”r” gutter=”false”]
wachd[apply(wachd,2,cumsum)<10^(-10)]=10^(-10)
[/sourcecode]

and the addition of

[sourcecode language=”r” gutter=”false”]
> plex[plex>0]=0
> plech[plech>0]=0
[/sourcecode]

after the definition of those two variables.  (Because entropies are necessarily positive.) The most glaring omission is however the minus in

[sourcecode language=”r” gutter=”false”]
> plob=apply(exp(-plex),1,quantile,c(.025,.975))
> ploch=apply(exp(-plech),1,quantile,c(.025,.975))
[/sourcecode]

which modifies Figure 4.4 in the following

The second case is Example 7.3 where I forgot to account for the log-transform of the data, which should read (p.204):

[sourcecode language=”r” gutter=”false”]
> x=c(91,504,557,609,693,727,764,803,857,929,970,1043,
+     1089,1195,1384,1713)
> x=log(x)
[/sourcecode]

and compounded my mistake by including log-transforms of the parameters that should not be there (pp.204-205)! So (for my simulations) the posterior means of θ and σ² are 6.62 and 0.661, respectively, leading to an estimate of σ of 0.802. There should be no log transform in Exercise 7.3 either.

The same corrections apply to the French translation, most obviously…

Bayesian Core and loose logs

Posted in Books, R, Statistics, University life with tags , , , , on July 26, 2011 by xi'an

Jean-Michel (aka Jean-Claude!) Marin came for a few days so that we could make late progress on the revision of our book Bayesian Core towards an Use R! version. In one of the R programs in the mixture chapter, we were getting improbable answers, until we found an R mistake in the shape of

[sourcecode language=”r” gutter=”false”]
> sum(c(1,2,3,log=TRUE))
[1] 7
> sum(c(1,2,3),log=TRUE)
[1] 7
[/sourcecode]

which was not detected by the compiler… There are surely plenty of good reasons for this to happen and it did not take long to fix the bug, still… annoying!