From mboxrd@z Thu Jan 1 00:00:00 1970 From: Emmanuel Charpentier Subject: Possible bug (?): :noweb doesn't respect indentations (at least i R/BUGS/JAGS). Date: Tue, 09 Apr 2019 01:01:42 +0200 Message-ID: <92dddcaf59f004e86fee9b2b56ec51f4a686c656.camel@free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([209.51.188.92]:56598) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDdH3-0000oi-Db for emacs-orgmode@gnu.org; Mon, 08 Apr 2019 19:01:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hDdH1-00071Z-3j for emacs-orgmode@gnu.org; Mon, 08 Apr 2019 19:01:57 -0400 Received: from smtp1-g21.free.fr ([2a01:e0c:1:1599::10]:35243) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hDdGx-0006r7-41 for emacs-orgmode@gnu.org; Mon, 08 Apr 2019 19:01:53 -0400 Received: from zen-book-flip (unknown [82.228.67.28]) (Authenticated sender: emm.charpentier) by smtp1-g21.free.fr (Postfix) with ESMTPSA id 0B9AAB00535 for ; Tue, 9 Apr 2019 01:01:42 +0200 (CEST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode Compare this org source: =================================================================== # A small :noweb mystery: indentation #+property: header-args:R :session #+options: toc:nil #+author: #+date: The structure of a probablity proble can be represented by a ~JAGS~ code snippet: #+name: Struct #+begin_src R :eval never :exports code for (i in 1:nObs) { for (j in 1:nZ) { Z[i,j] ~ dbern(P[i,j]) logit(P[i,j]) <- alpha + beta[j]*X[i] } } #+end_src The same code snippet can be used for simulation: #+begin_src R :exports code :results none :noweb yes library(rjags) library(coda) ## Reproducibility ? set.seed(813) Params <- local({ nObs <- 500 nZ <- 5 X <- rnorm(nObs) alpha <- rnorm(1,0,3) beta <- rnorm(nZ,-1,2) list( nObs=nObs, nZ=nZ, X=X, alpha=alpha, beta=beta) }) ## Wrap model code M <- "model { <> }" ## Compilation JM <- jags.model(textConnection(M), data=Params, n.adapt=1, n.chains=1) ## Forward sampling JS <- coda.samples(JM, "Z", n.iter=1) #+end_src and for inference, after adding priors of parameters: #+name: Priors #+begin_src R :eval never :exports code ## Priors alpha ~ dt(0, 1e-2, 3) for (j in 1:nZ) { beta[j] ~ dt(0,1e-2, 3) } #+end_src #+name: Inference #+begin_src R :noweb yes :exports code Data <- list( nObs=Params$nObs, nZ=Params$nZ, X=Params$X, Z=matrix(as.matrix(JS), ncol=Params$nZ, byrow=FALSE)) ## Wrap inference model MI <- "model { <> <> }" ## Compilation JMI <- jags.model(textConnection(MI), Data, n.chains=4) ## Inference sampling JMS <- coda.samples(JMI, c("alpha", "beta"), n.iter=1000) #+end_src #+RESULTS: Inference =================================================================== With the result of its export to Ascii: =================================================================== The structure of a probablity proble can be represented by a `JAGS' code snippet: ,---- | for (i in 1:nObs) { | for (j in 1:nZ) { | Z[i,j] ~ dbern(P[i,j]) | logit(P[i,j]) <- alpha + beta[j]*X[i] | } | } `---- The same code snippet can be used for simulation: ,---- | library(rjags) | library(coda) | ## Reproducibility ? | set.seed(813) | Params <- local({ | nObs <- 500 | nZ <- 5 | X <- rnorm(nObs) | alpha <- rnorm(1,0,3) | beta <- rnorm(nZ,-1,2) | list( | nObs=nObs, | nZ=nZ, | X=X, | alpha=alpha, | beta=beta) | }) | ## Wrap model code | M <- | "model { | for (i in 1:nObs) { | for (j in 1:nZ) { | Z[i,j] ~ dbern(P[i,j]) | logit(P[i,j]) <- alpha + beta[j]*X[i] | } | } | }" | ## Compilation | JM <- jags.model(textConnection(M), data=Params, n.adapt=1, n.chains=1) | ## Forward sampling | JS <- coda.samples(JM, "Z", n.iter=1) `---- and for inference, after adding priors of parameters: ,---- | ## Priors | alpha ~ dt(0, 1e-2, 3) | for (j in 1:nZ) { | beta[j] ~ dt(0,1e-2, 3) | } `---- ,---- | Data <- list( | nObs=Params$nObs, | nZ=Params$nZ, | X=Params$X, | Z=matrix(as.matrix(JS), ncol=Params$nZ, byrow=FALSE)) | ## Wrap inference model | MI <- | "model { | for (i in 1:nObs) { | for (j in 1:nZ) { | Z[i,j] ~ dbern(P[i,j]) | logit(P[i,j]) <- alpha + beta[j]*X[i] | } | } | ## Priors | alpha ~ dt(0, 1e-2, 3) | for (j in 1:nZ) { | beta[j] ~ dt(0,1e-2, 3) | } | }" | ## Compilation | JMI <- jags.model(textConnection(MI), Data, n.chains=4) | ## Inference sampling | JMS <- coda.samples(JMI, c("alpha", "beta"), n.iter=1000) `---- =================================================================== The indentation is not respected in the included JAGS snippets. Further attempts with exporting to PDF (both the built-in exporter and ox- pandoc) and DOCX (ox-pandoc) show that the problem remains the same, but, IIRC, with slight variations in whitespace. That's not serious (just ugly) for R/JAGS ; but it might be serious for Python/Sage, where whitespace is syntaxic. Thoughts ? -- Emmanuel Charpentier