## another R new trick [new for me!]

Posted in Books, Kids, R, Statistics, University life with tags , , , , , , , on July 16, 2014 by xi'an

While working with Andrew and a student from Dauphine on importance sampling, we wanted to assess the distribution of the resulting sample via the Kolmogorov-Smirnov measure

$\max_x |\hat{F_n}(x)-F(x)|$

where F is the target.  This distance (times √n) has an asymptotic distribution that does not depend on n, called the Kolmogorov distribution. After searching for a little while, we could not figure where this distribution was available in R. It had to, since ks.test was returning a p-value. Hopefully correct! So I looked into the ks.test function, which happens not to be entirely programmed in C, and found the line

PVAL <- 1 - if (alternative == "two.sided")
.Call(C_pKolmogorov2x, STATISTIC, n)


which means that the Kolmogorov distribution is coded as a C function C_pKolmogorov2x in R. However, I could not call the function myself.

> .Call(C_pKolmogorov2x,.3,4)


Hence, as I did not want to recode this distribution cdf, I posted the question on stackoverflow (long time no see!) and got a reply almost immediately as to use the package kolmim. Followed by the extra comment from the same person that calling the C code only required to add the path to its name, as in

> .Call(stats:::C_pKolmogorov2x,STAT=.3,n=4)
[1] 0.2292


## Ｒによるモンテカルロ法入門

Posted in Books, R, Statistics with tags , , on May 14, 2013 by xi'an

Here is the cover of the Japanese translation of our Introducing Monte Carlo methods with R book.  A few year after the French translation. It actually appeared last year in August but I was not informed of this till a few weeks ago. The publisher is Maruzen, with an associated webpage if you want to order… Unless I am confused the translators are Hiro Ishida and Kazue Ishida; they deserve a major ありがとう ! And too bad George is no longer with us: this must have been the first translation of one of his books in Japanese..

## 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

\newcommand{\era}{\end{comment}}


did not have the same effect as writing the line

\end{comment}


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

\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}


produces an error message:

Runaway argument?
! File ended while scanning use of \next.
<inserted text>
\par
<*> moretest.tex


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

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

## 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

> wachd[wachd<10^(-10)]=10^(-10)


wachd[apply(wachd,2,cumsum)<10^(-10)]=10^(-10)


> plex[plex>0]=0
> plech[plech>0]=0


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

> plob=apply(exp(-plex),1,quantile,c(.025,.975))
> ploch=apply(exp(-plech),1,quantile,c(.025,.975))


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

> x=c(91,504,557,609,693,727,764,803,857,929,970,1043,
+     1089,1195,1384,1713)
> x=log(x)


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

 > sum(c(1,2,3,log=TRUE))
[1] 7
> sum(c(1,2,3),log=TRUE)
[1] 7


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!