<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/a-simple-example-of-using-replyrgapply/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/12/a-simple-example-of-using-replyrgapply/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It’s a common situation to have data from multiple processes in a “long” data format, for example a table with columns <code>measurement</code> and <code>process_that_produced_measurement</code>. It’s also natural to split that data apart to analyze or transform it, per-process — and then to bring the results of that data processing together, for comparison. Such a work pattern is called “<a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v040i01">Split-Apply-Combine</a>,” and we discuss several R implementations of this pattern <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/12/the-case-for-index-free-data-manipulation/">here</a>. In this article we show a simple example of one such implementation, <code>replyr::gapply</code>, from our latest package, <a target="_blank" rel="nofollow" href="https://github.com/WinVector/replyr"><code>replyr</code></a>.</p>
<p><center><br>
<img alt="4140852348 2ebe864822 z" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/12/4140852348_2ebe864822_z.jpg?w=660" border="0" data-recalc-dims="1"><br>
Illustration by Boris Artzybasheff. Image: <a target="_blank" rel="nofollow" href="https://www.flickr.com/photos/x-ray_delta_one/4140852348/in/album-72157622345988524/">James Vaughn</a>, <a target="_blank" rel="nofollow" href="https://creativecommons.org/licenses/by-nc-sa/2.0/">some rights reserved</a>.<br>
</center></p>
<p>The example task is to evaluate how several different models perform on the same classification problem, in terms of deviance, accuracy, precision and recall. We will use the <a target="_blank" rel="nofollow" href="https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients">“default of credit card clients”</a> data set from the UCI Machine Learning Repository.</p>
<p><span></span>To keep this post short, we will skip over the preliminary data processing and the modeling; if you are interested, the code for the full example is available <a target="_blank" rel="nofollow" href="https://github.com/WinVector/Examples/tree/master/gapplyExample">here</a>. We will fit a logistic regression model (GLM), a generalized additive model (GAM), and a random forest model (<code>ranger</code> implementation) to a training set, and evaluate the models’ performance on a hold-out set.</p>
<pre>
# load the file of model fitting and prediction functions
source(&quot;modelfitting.R&quot;)
algolist = list(glm=glm_predictor, 
                gam=gam_predictor, 
                rangerRF=ranger_predictor)

# define outcome column and variables
outcome = &quot;defaults&quot;
varlist =  ...

# Fit models for each algorithm and gather together the 
# predictions each model makes on a test set.
predictors = fit_models(algolist, outcome, varlist, train)
predframe = make_predictions(predictors, test, outcome)

library(replyr)
replyr_summary(predframe)[, c(&quot;column&quot;, &quot;class&quot;, &quot;nunique&quot;)]
##     column     class nunique
## 2 defaults   logical       2
## 3    model character       3
## 1     pred   numeric   17973
replyr_uniqueValues(predframe, &quot;model&quot;)
## # A tibble: 3 × 2
##      model     n
##      </pre>