<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/yaml-define-an-r-function/"></div></div><div><a data-url="https://www.r-bloggers.com/yaml-define-an-r-function/" data-counturl="https://www.r-bloggers.com/yaml-define-an-r-function/" data-count="horizontal" data-text="YAML: Define an R function" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ipub.com/yaml/"> R – ipub</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>YAML to define hierarchic models</h2>
<p>Glenn is using the data.tree package to model commercial mortgage-backed securities. If you don’t know what that is, you can read it all up on <a target="_blank" href="https://en.wikipedia.org/wiki/Commercial_mortgage-backed_security">Wikipedia</a>. But in a nutshell, CMBS (or REMIC) are financial instruments that bundle multiple commercial real estate mortgages, and securitize (or bundle) them into tradeable entities. A CMBS has multiple tranches, and multiple classes of tranches. Each tranche starts paying interest depending on certain conditions.</p>
<p>Glenn knows much more about this than me, of course. He wrote a <a target="_blank" href="https://www.amazon.de/Investing-Mortgage-Backed-Asset-Backed-Securities-Financial-ebook/dp/B01AXTF8TA/ref=sr_1_2?ie=UTF8&qid=1471533048&sr=8-2&keywords=glenn+schultz">great book</a> about the topic, and also published an <a target="_blank" href="http://www.bondlab.co/">R package</a> that accompanies the book. More recently, he started to realize that MBS structures are in essence tree structures. And if you start modelling MBS as trees, many of the complexities start disappearing. So he started to rewrite the bond lab package, using data.tree. I hope he’ll find time to publish a guest post here as soon as that is published!</p>
<h2>YAML and data.tree</h2>
<p>Anyway, one thing he’d like to do is to define the MBS structure in a YAML files. This is straight forward. I’ve shown how to do this, for example, in the <a target="_blank" href="http://ipub.com/ahp/">AHP</a> package, or in the <a target="_blank" href="https://cran.r-project.org/web/packages/data.tree/vignettes/applications.html#jenny-lind-decision-tree-plotting">decision tree example</a> in the data.tree applications vignette. In a nutshell, you define your tree structure in a file, and load it using the R yaml package. The resulting list-of-list you then convert to a data.tree structure:</p>
<pre>library(data.tree)
library(yaml)


yaml &lt;- &quot;
node1:
  value: 2
node2:
  value: 6
&quot;


lol &lt;- yaml.load(yaml)
tree &lt;- as.Node(lol)</pre>

<h2>YAML and functions</h2>
<p>Now, Glenn asked me whether it’s possible to define R functions in the YAML file.</p>
<p>Answer: Yes!</p>
<p>All you need is an additional step to parse and evaluate the functions, so as to convert them from strings to actual R functions.</p>
<pre>yaml &lt;- &quot;
node1:
 value: 2
 fnct: >
 function(x) {
 2 * x
 }
node2:
 value: 6
 # can be named if you prefer, though that will have no impact
 fnct: >
 DoCalculate &lt;- function(node) {
 node$value * 2
 }
&quot;

lol &lt;- yaml.load(yaml)
tree &lt;- as.Node(lol)

evaluator &lt;- function(node) node$fnct &lt;- eval(parse(text = node$fnct))

tree$Do(fun = evaluator, 
 filterFun = function(node) !is.null(node$fnct))

tree$node1$fnct(5)
tree$node2$fnct(tree$node2)</pre>
<p>This will call the function, as expected. For example, this technique has been used for the ahp package, where we can define preference functions in our ahp yaml model.</p>
<h2>Functions that reference a node</h2>
<p>Obviously, it doesn’t end here. What we would like to do, very often, is to reference the enclosing data.tree from a given function. This is also possible. All we need to do is to wrap the function, like so:</p>
<pre># if you want to reference an enclosing node from within your function, you can define the functions
# having a node argument, and do a slightly more complex evaluator function. e.g.

yaml &lt;- &quot;
node1:
  value: 2
  fnct: >
    function(node) {
      2 * node$value
    }
node2:
  value: 5
  fnct: >
    function(node) {
      node$value ^ 2 
    }

&quot;


lol &lt;- yaml.load(yaml)
tree &lt;- as.Node(lol)

evaluator &lt;- function(node) {
  f &lt;- eval(parse(text = node$fnct))
  # warp the function and call using node
  f2 &lt;- function() {
    f(node)
  }
  node$fnct &lt;- f2
}


tree$Do(fun = evaluator, 
        filterFun = function(node) !is.null(node$fnct))



tree$node1$fnct()
tree$node2$fnct()


Aggregate(tree, &quot;fnct&quot;, sum)</pre>
<p>I hope you find this useful!</p>
<p>You can download the entire code from <a target="_blank" href="https://gist.github.com/gluc/1176a4240554e4db5204dd229c8505ce">this gist</a>.</p>
<p> </p>
<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="http://ipub.com/yaml/">YAML: Define an R function</a> appeared first on <a target="_blank" rel="nofollow" href="http://ipub.com/">ipub</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/yaml-define-an-r-function/"></div></div><div><a data-url="https://www.r-bloggers.com/yaml-define-an-r-function/" data-counturl="https://www.r-bloggers.com/yaml-define-an-r-function/" data-count="vertical" data-text="YAML: Define an R function" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ipub.com/yaml/"> R – ipub</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Pf0KREMdIo4"></a></div>