<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/"></div></div><div><a data-url="http://www.r-bloggers.com/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/" data-counturl="http://www.r-bloggers.com/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/" data-count="horizontal" data-text="All your models belong to us: how to combine package archivist and function trace()" 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://smarterpoland.pl/index.php/2016/05/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/"> SmarterPoland.pl » English</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Let’s see how to collect all linear regression models that you will ever create in R.</p>
<p>It’s easy with the <em>trace()</em> function. A really powerful, yet not that popular function, that allows you to inject any R code in any point of a body of any function.<br>
Useful in debugging and have other interesting applications.<br>
Below I will show how to use this function to store a copy of every linear model that is created with lm(). In the same way you may store copies of plots/other models/data frames/anything.</p>
<p>To store a persistent copy of an object one can simply use the <em>save()</em> function. But we are going to use the archivist package instead. It stores objects in a repository and give you some nice features, like searching within repository, sharing the repository with other users, checking session info for a particular object or restoring packages to versions consistent with a selected object.</p>
<p>To use archivist with the <em>trace()</em> function you just need to call two lines. First one will create an empty repo, and the second will execute ‘saveToLocalRepo()’ at the end of each call to the <em>lm()</em> function.</p>

<pre>library(archivist)
# create an empty repo
createLocalRepo (&quot;allModels&quot;, default = TRUE)
# add tracing code
trace(lm, exit = quote(saveToRepo(z)))</pre>

<p>Now, at the end of every <em>lm()</em> function the fitted model will be stored in the repository.<br>
Let’s see this in action.</p>

<pre>> lm(Sepal.Length~., data=iris) -> m1
Tracing lm(Sepal.Length ~ ., data = iris) on exit 

> lm(Sepal.Length~ Petal.Length, data=iris) -> m1
Tracing lm(Sepal.Length ~ Petal.Length, data = iris) on exit 

> lm(Sepal.Length~-Species, data=iris) -> m1
Tracing lm(Sepal.Length ~ -Species, data = iris) on exit</pre>

<p>All models are stored as rda files in a disk based repository.<br>
You can load them to R with the <em>asearch()</em> function.<br>
Let’s get all lm objects, apply the AIC function to each of them and sort along AIC. </p>

<pre>> asearch(&quot;class:lm&quot;) %>% 
    sapply(., AIC) %>% 
    sort
4c3ae060f3aaa2509b2faf63d857358e 5c5751e36b31b2251d2767d96993320a 
                        79.11602                        160.04042 
ed2f4d257fd568c5c6f231fadc7aa645 
                       372.07953</pre>

<p>The <em>aread()</em> function will download the selected model.</p>

<pre>> aread(&quot;4c3ae060f3aaa2509b2faf63d857358e&quot;)

Call:
lm(formula = Sepal.Length ~ ., data = iris)

Coefficients:
      (Intercept)        Sepal.Width       Petal.Length        Petal.Width  
           2.1713             0.4959             0.8292            -0.3152  
Speciesversicolor   Speciesvirginica  
          -0.7236            -1.0235</pre>

<p>Now you can just create model after model and if needed they all can be restored.</p>
<p>Read more about the archivist here: <a target="_blank" href="http://pbiecek.github.io/archivist/">http://pbiecek.github.io/archivist/</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="http://www.r-bloggers.com/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/"></div></div><div><a data-url="http://www.r-bloggers.com/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/" data-counturl="http://www.r-bloggers.com/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/" data-count="vertical" data-text="All your models belong to us: how to combine package archivist and function trace()" 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://smarterpoland.pl/index.php/2016/05/all-your-models-belong-to-us-how-to-combine-package-archivist-and-function-trace/"> SmarterPoland.pl » English</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/v24q_RD_lvE"></a></div>