Org-mode and sampling information from stan()

65 views
Skip to first unread message

Bill Harris

unread,
May 15, 2015, 6:20:35 PM5/15/15
to stan-...@googlegroups.com
I use R inside Emacs Org mode for its literate programming and reproducibility capabilities.  That means I use stan() in Org mode, too.

Unfortunately, I can't figure out how to get the output I want.  I create an org code block something like this:

#+NAME: blockname
#+HEADER: :session R:sessionname
#+BEGIN_SRC R :exports both :results value :exports both
<R code to define data>
<R code to call stan()>
print(stanfitobjectname)
#+END_SRC

According to http://orgmode.org/manual/Results-of-evaluation.html, ":results value" in a ":session" should give the last evaluation performed by R (technically the value of .Last.value), which I thought would be the result of the print().  Unfortunately, I get nothing except the "+RESULTS: blockname header.

Changing to ":results output" gives me all the sampling information from stan() plus the output of print().

I am not sure what stan's verbose does, but verbose = FALSE doesn't get rid of the sampling information.

Is there anyone here who might use Org mode and knows how to eliminate the sampling log from the output here while keeping the result of the print()? 

If not, does anyone know why the results of print(stanfitobjectname) doesn't seem to end up in .Last.value and what, if anything, I might do about it?

I'd ask on an Org mode channel, but I'm not yet sure how to explain it to them, and I figure the probability is higher here that someone uses Org mode than the probability that someone there uses Stan.  I may be wrong.

Bill

Bill Harris

unread,
May 16, 2015, 10:41:42 AM5/16/15
to stan-...@googlegroups.com
Here's a reproducible bit of code and the results I get.

If you have a reasonably recent org mode, load the org file into emacs, export it to anything you want (I'm doing LaTeX), and look at the result.  Then change :results output to :results value. 

I'm trying to export only the results of the print(), not the sampling log.

I'm using rstan 2.6.0, GNU Emacs 23.4.1 on Debian Wheezy, and org-mode 8.2.1.

Ideas?

Thanks,

Bill
orgtest.org
orgtest.pdf

Bob Carpenter

unread,
May 18, 2015, 2:41:22 PM5/18/15
to stan-...@googlegroups.com
Sorry you haven't gotten a response, but I haven't even
heard of org-mode, so I have no idea how to help.

Is there some control you want on the output of CmdStan
or Rstan or what? And when you say print(), you mean the print()
in Stan's language or from R or where?

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <orgtest.org><orgtest.pdf>

Bill Harris

unread,
May 18, 2015, 4:34:49 PM5/18/15
to stan-...@googlegroups.com
On Monday, May 18, 2015 at 11:41:22 AM UTC-7, Bob Carpenter wrote:
Sorry you haven't gotten a response, but I haven't even
heard of org-mode, so I have no idea how to help.

:-)  Not to worry.  In case others haven't, it's located at http://orgmode.org/.  It grew out of Emacs' very flexible outline mode.  It offers the ability to include and execute source from multiple languages, sharing output from one as input to another, and it offers flexible publishing capabilities.  In a way, it's an Emacs version of RStudio, except that it came first.  After trying RStudio for a bit and despite liking it, I came back to org mode.

Here is their section on active code in org-mode: http://orgmode.org/worg/org-contrib/babel/index.html. 


Is there some control you want on the output of CmdStan
or Rstan or what?  And when you say print(), you mean the print()
in Stan's language or from R or where?


If you open the .org file I attached, there are #+ header lines at the start that get inserted as standard templates, if you will. 

The line starting with a single '*' is a top-level header.

Then the block starting #+NAME: and ending #+END_SRC is a code block with associated headers. BEGIN_SRC R says it's an R code block, and the three lines inside are thus pure R.  The print() is R's command. 

Hmm.  One could define Stan as another supported language and then write Stan code directly into an Org code block.  It wouldn't be the first compiled language; you can already see C, C++, Fortran, .....  I don't know if it's worth the effort, though.

I think this is the problem.  http://orgmode.org/manual/Results-of-evaluation.html suggests that .Last.value may not be being set as org-mode expects; http://stat.ethz.ch/R-manual/R-devel/library/base/html/Last.value.html tells what that R variable is. 

If, using RStudio, I print something in R and then print a stanfit object in R, entering .Last.value after each, I get different results from the two print() commands:

> print('abc')
[1] "abc"
> .Last.value
[1] "abc"
> print(stanobject)
Inference for Stan model: Model01.
4 chains, each with iter=250; warmup=125; thin=1; 
post-warmup draws per chain=125, total post-warmup draws=500.

        mean se_mean   sd   2.5%    25%    50%    75%  97.5% n_eff Rhat
alpha  -1.08    0.02 0.12  -1.34  -1.14  -1.08  -1.01  -0.88    26 1.14
beta    3.59    0.03 0.16   3.31   3.49   3.58   3.67   3.93    26 1.14
sigma   0.24    0.00 0.01   0.22   0.23   0.24   0.25   0.26   233 1.00
lp__  229.47    0.16 1.34 226.10 228.98 229.82 230.46 230.91    72 1.05

Samples were drawn using NUTS(diag_e) at Fri May 15 15:22:15 2015.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at 
convergence, Rhat=1).
> .Last.value
NULL

Is it possible that print.stanfit simply forgot to set .Last.value?  Is there a reason it can't be set?

Thanks,

Bill

PS: My current workaround is to split the print() statement into a second code block. I export nothing from the stan() command block, and I export everything from the print() block.  If print.stanfit() can be made to set .Last.value, that would be better.


.




 
Reply all
Reply to author
Forward
0 new messages