Sweave, mle and curve

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sweave, mle and curve

Göran Broström
I am trying to write a lesson on maximum likelihood with Sweave. I get
a surprising result with the following code, lec4.Snw:

\documentclass[a4paper,12pt]{article}
\usepackage[latin1]{inputenc}

\title{Maximum likelihood}

\author{Göran Broström}

\begin{document}

\maketitle

<<fig=TRUE>>=
## Simulate Y:
n <- 25
Y <- sum(rpois(n, lambda = 1))
Y
 ## Define minusloglik:
minusloglik <- function(theta) n * theta - Y  * log(theta)
curve(minusloglik, 0.2, 2, xlab = "theta")

library(stats4)
cat("Y is now ", Y, "\n")
fit <- mle(minusloglik, start = list(theta = Y/n))
summary(fit)
@

\end{document}

In R, I get:

> Sweave("lec4.Snw")
Writing to file lec4.tex
Processing code chunks ...
 1 : echo term verbatim eps pdf
Y is now  27
Y is now  24

You can now run LaTeX on 'lec4.tex'
>

and the latex document will have two different mle's,  one in the
figure, and another one from 'mle'.  One uses Y = 27 and the other Y =
24!

I can save the situation by moving "Y <- ..." to a separaye "code chunk".
Does 'fig=TRUE' imply that the code chunk is run twice?

--
Göran Broström

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Sweave, mle and curve

Friedrich.Leisch
>>>>> On Mon, 13 Feb 2006 11:16:14 +0100,
>>>>> Göran Broström (GB) wrote:

  > I am trying to write a lesson on maximum likelihood with Sweave. I get
  > a surprising result with the following code, lec4.Snw:

  > \documentclass[a4paper,12pt]{article}
  > \usepackage[latin1]{inputenc}

  > \title{Maximum likelihood}

  > \author{Göran Broström}

  > \begin{document}

  > \maketitle

  > <<fig=TRUE>>=
  > ## Simulate Y:
  > n <- 25
  > Y <- sum(rpois(n, lambda = 1))
  > Y
  >  ## Define minusloglik:
  > minusloglik <- function(theta) n * theta - Y  * log(theta)
  > curve(minusloglik, 0.2, 2, xlab = "theta")

  > library(stats4)
  > cat("Y is now ", Y, "\n")
  > fit <- mle(minusloglik, start = list(theta = Y/n))
  > summary(fit)
  > @

  > \end{document}

  > In R, I get:

  >> Sweave("lec4.Snw")
  > Writing to file lec4.tex
  > Processing code chunks ...
  >  1 : echo term verbatim eps pdf
  > Y is now  27
  > Y is now  24

  > You can now run LaTeX on 'lec4.tex'
  >>

  > and the latex document will have two different mle's,  one in the
  > figure, and another one from 'mle'.  One uses Y = 27 and the other Y =
  > 24!

  > I can save the situation by moving "Y <- ..." to a separaye "code chunk".
  > Does 'fig=TRUE' imply that the code chunk is run twice?

Yes, once to get all textual output, and then once for each graphics
format requested.

Best,

--
-------------------------------------------------------------------
                        Friedrich Leisch
Institut für Statistik                     Tel: (+43 1) 58801 10715
Technische Universität Wien                Fax: (+43 1) 58801 10798
Wiedner Hauptstraße 8-10/1071
A-1040 Wien, Austria             http://www.ci.tuwien.ac.at/~leisch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Sweave, mle and curve

Jean-Christophe BOUËTTÉ
In reply to this post by Göran Broström
> From: Göran Broström <[hidden email]>
> To: [hidden email]
> Date: Mon, 13 Feb 2006 11:16:14 +0100
> Subject: [R] Sweave, mle and curve
> I am trying to write a lesson on maximum likelihood with Sweave. I get
> a surprising result with the following code, lec4.Snw:
>
> \documentclass[a4paper,12pt]{article}
> \usepackage[latin1]{inputenc}
>
> \title{Maximum likelihood}
>
> \author{Göran Broström}
>
> \begin{document}
>
> \maketitle
>
> <<fig=TRUE>>=
> ## Simulate Y:
> n <- 25
> Y <- sum(rpois(n, lambda = 1))
> Y
>  ## Define minusloglik:
> minusloglik <- function(theta) n * theta - Y  * log(theta)
> curve(minusloglik, 0.2, 2, xlab = "theta")
>
> library(stats4)
> cat("Y is now ", Y, "\n")
> fit <- mle(minusloglik, start = list(theta = Y/n))
> summary(fit)
> @
>
> \end{document}
>
> In R, I get:
>
> > Sweave("lec4.Snw")
> Writing to file lec4.tex
> Processing code chunks ...
>  1 : echo term verbatim eps pdf
> Y is now  27
> Y is now  24
>
> You can now run LaTeX on 'lec4.tex'
> >
>
> and the latex document will have two different mle's,  one in the
> figure, and another one from 'mle'.  One uses Y = 27 and the other Y =
> 24!
>
> I can save the situation by moving "Y <- ..." to a separaye "code chunk".
> Does 'fig=TRUE' imply that the code chunk is run twice?
>
> --
> Göran Broström

Which figure did you use ? The .eps or the .pdf ? I guess one of them has Y=24.
The problem is that Sweave runs the code twice, to get the .eps plot
and the .pdf plot.
The solution is to isolate the "random" code in a separate chunk. I
think this issue was adressed some time ago in this list.

HTH,
Jean-Christophe Bouëtté.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html