-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.html
316 lines (296 loc) · 21.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Run Predictions Inside the Database • tidypredict</title>
<script src="deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet">
<script src="deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="deps/Source_Sans_Pro-0.4.9/font.css" rel="stylesheet">
<link href="deps/Source_Code_Pro-0.4.9/font.css" rel="stylesheet">
<link href="deps/font-awesome-6.5.2/css/all.min.css" rel="stylesheet">
<link href="deps/font-awesome-6.5.2/css/v4-shims.min.css" rel="stylesheet">
<script src="deps/headroom-0.11.0/headroom.min.js"></script><script src="deps/headroom-0.11.0/jQuery.headroom.min.js"></script><script src="deps/bootstrap-toc-1.0.1/bootstrap-toc.min.js"></script><script src="deps/clipboard.js-2.0.11/clipboard.min.js"></script><script src="deps/search-1.0.0/autocomplete.jquery.min.js"></script><script src="deps/search-1.0.0/fuse.min.js"></script><script src="deps/search-1.0.0/mark.min.js"></script><!-- pkgdown --><script src="pkgdown.js"></script><meta property="og:title" content="Run Predictions Inside the Database">
<meta name="description" content="It parses a fitted R model object, and returns a formula in Tidy Eval code that calculates the predictions. It works with several databases back-ends because it leverages dplyr and dbplyr for the final SQL translation of the algorithm. It currently supports lm(), glm(), randomForest(), ranger(), earth(), xgb.Booster.complete(), cubist(), and ctree() models.">
<meta property="og:description" content="It parses a fitted R model object, and returns a formula in Tidy Eval code that calculates the predictions. It works with several databases back-ends because it leverages dplyr and dbplyr for the final SQL translation of the algorithm. It currently supports lm(), glm(), randomForest(), ranger(), earth(), xgb.Booster.complete(), cubist(), and ctree() models.">
<meta property="og:image" content="https://tidypredict.tidymodels.org/logo.png">
<script defer data-domain="tidypredict.tidymodels.org,all.tidymodels.org" src="https://plausible.io/js/plausible.js"></script>
</head>
<body>
<a href="#container" class="visually-hidden-focusable">Skip to content</a>
<nav class="navbar navbar-expand-lg fixed-top bg-none" data-bs-theme="light" aria-label="Site navigation"><div class="container">
<a class="navbar-brand me-2" href="index.html">tidypredict</a>
<small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">0.5.1.9000</small>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse ms-3">
<ul class="navbar-nav me-auto">
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" type="button" id="dropdown-model-list" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Model list</button>
<ul class="dropdown-menu" aria-labelledby="dropdown-model-list">
<li><a class="dropdown-item" href="articles/lm.html">Linear Regression - lm()</a></li>
<li><a class="dropdown-item" href="articles/glm.html">Generalized Regression - glm()</a></li>
<li><a class="dropdown-item" href="articles/ranger.html">Random Forest - Ranger - ranger()</a></li>
<li><a class="dropdown-item" href="articles/rf.html">Random Forest - randomForest()</a></li>
<li><a class="dropdown-item" href="articles/mars.html">MARS - earth()</a></li>
<li><a class="dropdown-item" href="articles/cubist.html">Cubist - cubist()</a></li>
<li><a class="dropdown-item" href="articles/xgboost.html">XGboost - xgb.Booster.complete()</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" type="button" id="dropdown-articles" data-bs-toggle="dropdown" aria-expanded="false" aria-haspopup="true">Articles</button>
<ul class="dropdown-menu" aria-labelledby="dropdown-articles">
<li><a class="dropdown-item" href="articles/save.html">Save/Reload</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="articles/non-r.html">Non-R models</a></li>
<li><a class="dropdown-item" href="articles/regression.html">Regression spec</a></li>
<li><a class="dropdown-item" href="articles/tree.html">Tree model spec</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="articles/sql.html">DB Writeback</a></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="news/index.html">News</a></li>
<li class="nav-item"><a class="nav-link" href="reference/index.html">Reference</a></li>
</ul>
<ul class="navbar-nav">
<li class="nav-item"><form class="form-inline" role="search">
<input class="form-control" type="search" name="search-input" id="search-input" autocomplete="off" aria-label="Search site" placeholder="Search for" data-search-index="search.json">
</form></li>
<li class="nav-item"><a class="external-link nav-link" href="https://github.com/tidymodels/tidypredict/" aria-label="GitHub"><span class="fa fab fa-github fa-lg"></span></a></li>
</ul>
</div>
</div>
</nav><div class="container template-home" id="container">
<div class="row">
<main id="main" class="col-md-9"><div class="section level1">
<div class="page-header">
<img src="logo.png" class="logo" alt=""><h1 id="tidypredict-">tidypredict <a class="anchor" aria-label="anchor" href="#tidypredict-"></a>
</h1>
</div>
<p><a href="https://github.com/tidymodels/tidypredict/actions" class="external-link"><img src="https://github.com/tidymodels/tidypredict/workflows/R-CMD-check/badge.svg" alt="R-CMD-check"></a> <a href="https://CRAN.r-project.org/package=tidypredict" class="external-link"><img src="http://www.r-pkg.org/badges/version/tidypredict" alt="CRAN_Status_Badge"></a> <a href="https://app.codecov.io/gh/tidymodels/tidypredict?branch=main" class="external-link"><img src="https://codecov.io/gh/tidymodels/tidypredict/branch/main/graph/badge.svg" alt="Codecov test coverage"></a></p>
<p>The main goal of <code>tidypredict</code> is to enable running predictions inside databases. It reads the model, extracts the components needed to calculate the prediction, and then creates an R formula that can be translated into SQL. In other words, it is able to parse a model such as this one:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">model</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/stats/lm.html" class="external-link">lm</a></span><span class="op">(</span><span class="va">mpg</span> <span class="op">~</span> <span class="va">wt</span> <span class="op">+</span> <span class="va">cyl</span>, data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span></span></code></pre></div>
<p><code>tidypredict</code> can return a SQL statement that is ready to run inside the database. Because it uses <code>dplyr</code>’s database interface, it works with several databases back-ends, such as MS SQL:</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="reference/tidypredict_sql.html">tidypredict_sql</a></span><span class="op">(</span><span class="va">model</span>, <span class="fu">dbplyr</span><span class="fu">::</span><span class="fu"><a href="https://dbplyr.tidyverse.org/reference/backend-mssql.html" class="external-link">simulate_mssql</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<div class="sourceCode" id="cb3"><pre class="sourceCode R"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="do">## <SQL> (39.686261480253 + (`wt` * -3.19097213898374)) + (`cyl` * -1.5077949682598)</span></span></code></pre></div>
<div class="section level2">
<h2 id="installation">Installation<a class="anchor" aria-label="anchor" href="#installation"></a>
</h2>
<p>Install <code>tidypredict</code> from CRAN using:</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html" class="external-link">install.packages</a></span><span class="op">(</span><span class="st">"tidypredict"</span><span class="op">)</span></span></code></pre></div>
<p>Or install the <strong>development version</strong> using <code>devtools</code> as follows:</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html" class="external-link">install.packages</a></span><span class="op">(</span><span class="st">"remotes"</span><span class="op">)</span></span>
<span><span class="fu">remotes</span><span class="fu">::</span><span class="fu">install_github</span><span class="op">(</span><span class="st">"tidymodels/tidypredict"</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="functions">Functions<a class="anchor" aria-label="anchor" href="#functions"></a>
</h2>
<p><code>tidypredict</code> has only a few functions, and it is not expected that number to grow much. The main focus at this time is to add more models to support.</p>
<table class="table">
<colgroup>
<col width="50%">
<col width="50%">
</colgroup>
<thead><tr class="header">
<th>Function</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr class="odd">
<td><code><a href="reference/tidypredict_fit.html">tidypredict_fit()</a></code></td>
<td>Returns an R formula that calculates the prediction</td>
</tr>
<tr class="even">
<td><code><a href="reference/tidypredict_sql.html">tidypredict_sql()</a></code></td>
<td>Returns a SQL query based on the formula from <code><a href="reference/tidypredict_fit.html">tidypredict_fit()</a></code>
</td>
</tr>
<tr class="odd">
<td><code><a href="reference/tidypredict_to_column.html">tidypredict_to_column()</a></code></td>
<td>Adds a new column using the formula from <code><a href="reference/tidypredict_fit.html">tidypredict_fit()</a></code>
</td>
</tr>
<tr class="even">
<td><code><a href="reference/tidypredict_test.html">tidypredict_test()</a></code></td>
<td>Tests <code>tidyverse</code> predictions against the model’s native <code><a href="https://rdrr.io/r/stats/predict.html" class="external-link">predict()</a></code> function</td>
</tr>
<tr class="odd">
<td><code><a href="reference/tidypredict_interval.html">tidypredict_interval()</a></code></td>
<td>Same as <code><a href="reference/tidypredict_fit.html">tidypredict_fit()</a></code> but for intervals (only works with <code>lm</code> and <code>glm</code>)</td>
</tr>
<tr class="even">
<td><code><a href="reference/tidypredict_sql_interval.html">tidypredict_sql_interval()</a></code></td>
<td>Same as <code><a href="reference/tidypredict_sql.html">tidypredict_sql()</a></code> but for intervals (only works with <code>lm</code> and <code>glm</code>)</td>
</tr>
<tr class="odd">
<td><code><a href="reference/parse_model.html">parse_model()</a></code></td>
<td>Creates a list spec based on the R model</td>
</tr>
<tr class="even">
<td><code><a href="reference/as_parsed_model.html">as_parsed_model()</a></code></td>
<td>Prepares an object to be recognized as a parsed model</td>
</tr>
</tbody>
</table>
</div>
<div class="section level2">
<h2 id="how-it-works">How it works<a class="anchor" aria-label="anchor" href="#how-it-works"></a>
</h2>
<p><img src="reference/figures/howitworks.png"></p>
<p>Instead of translating directly to a SQL statement, <code>tidypredict</code> creates an R formula. That formula can then be used inside <code>dplyr</code>. The overall workflow would be as illustrated in the image above, and described here:</p>
<ol style="list-style-type: decimal">
<li>Fit the model using a base R model, or one from the packages listed in <a href="#supported-models">Supported Models</a>
</li>
<li>
<code>tidypredict</code> reads model, and creates a list object with the necessary components to run predictions</li>
<li>
<code>tidypredict</code> builds an R formula based on the list object</li>
<li>
<code>dplyr</code> evaluates the formula created by <code>tidypredict</code>
</li>
<li>
<code>dplyr</code> translates the formula into a SQL statement, or any other interfaces.</li>
<li>The database executes the SQL statement(s) created by <code>dplyr</code>
</li>
</ol>
<div class="section level3">
<h3 id="parsed-model-spec">Parsed model spec<a class="anchor" aria-label="anchor" href="#parsed-model-spec"></a>
</h3>
<p><code>tidypredict</code> writes and reads a spec based on a model. Instead of simply writing the R formula directly, splitting the spec from the formula adds the following capabilities:</p>
<ol style="list-style-type: decimal">
<li>No more saving models as <code>.rds</code> - Specifically for cases when the model needs to be used for predictions in a Shiny app.</li>
<li>Beyond R models - Technically, anything that can write a proper spec, can be read into <code>tidypredict</code>. It also means, that the parsed model spec can become a good alternative to using <em>PMML.</em>
</li>
</ol>
</div>
</div>
<div class="section level2">
<h2 id="supported-models">Supported models<a class="anchor" aria-label="anchor" href="#supported-models"></a>
</h2>
<p>The following models are supported by <code>tidypredict</code>:</p>
<ul>
<li>Linear Regression - <code><a href="https://rdrr.io/r/stats/lm.html" class="external-link">lm()</a></code>
</li>
<li>Generalized Linear model - <code><a href="https://rdrr.io/r/stats/glm.html" class="external-link">glm()</a></code>
</li>
<li>Random Forest models - <code><a href="https://rdrr.io/pkg/randomForest/man/randomForest.html" class="external-link">randomForest::randomForest()</a></code>
</li>
<li>Random Forest models, via <code>ranger</code> - <code><a href="http://imbs-hl.github.io/ranger/reference/ranger.html" class="external-link">ranger::ranger()</a></code>
</li>
<li>MARS models - <code><a href="https://rdrr.io/pkg/earth/man/earth.html" class="external-link">earth::earth()</a></code>
</li>
<li>XGBoost models - <code><a href="https://rdrr.io/pkg/xgboost/man/xgb.Booster.complete.html" class="external-link">xgboost::xgb.Booster.complete()</a></code>
</li>
<li>Cubist models - <code><a href="http://topepo.github.io/Cubist/reference/cubist.default.html" class="external-link">Cubist::cubist()</a></code>
</li>
<li>Tree models, via <code>partykit</code> - <code><a href="https://rdrr.io/pkg/partykit/man/ctree.html" class="external-link">partykit::ctree()</a></code>
</li>
</ul>
<div class="section level3">
<h3 id="parsnip">
<code>parsnip</code><a class="anchor" aria-label="anchor" href="#parsnip"></a>
</h3>
<p><code>tidypredict</code> supports models fitted via the <code>parsnip</code> interface. The ones confirmed currently work in <code>tidypredict</code> are:</p>
<ul>
<li>
<code><a href="https://rdrr.io/r/stats/lm.html" class="external-link">lm()</a></code> - <code>parsnip</code>: <code>linear_reg()</code> with <em>“lm”</em> as the engine.</li>
<li>
<code><a href="https://rdrr.io/pkg/randomForest/man/randomForest.html" class="external-link">randomForest::randomForest()</a></code> - <code>parsnip</code>: <code>rand_forest()</code> with <em>“randomForest”</em> as the engine.</li>
<li>
<code><a href="http://imbs-hl.github.io/ranger/reference/ranger.html" class="external-link">ranger::ranger()</a></code> - <code>parsnip</code>: <code>rand_forest()</code> with <em>“ranger”</em> as the engine.</li>
<li>
<code><a href="https://rdrr.io/pkg/earth/man/earth.html" class="external-link">earth::earth()</a></code> - <code>parsnip</code>: <code>mars()</code> with <em>“earth”</em> as the engine.</li>
</ul>
</div>
<div class="section level3">
<h3 id="broom">
<code>broom</code><a class="anchor" aria-label="anchor" href="#broom"></a>
</h3>
<p>The <code><a href="https://generics.r-lib.org/reference/tidy.html" class="external-link">tidy()</a></code> function from broom works with linear models parsed via <code>tidypredict</code></p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">pm</span> <span class="op"><-</span> <span class="fu"><a href="reference/parse_model.html">parse_model</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/stats/lm.html" class="external-link">lm</a></span><span class="op">(</span><span class="va">wt</span> <span class="op">~</span> <span class="va">.</span>, <span class="va">mtcars</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://generics.r-lib.org/reference/tidy.html" class="external-link">tidy</a></span><span class="op">(</span><span class="va">pm</span><span class="op">)</span></span></code></pre></div>
<div class="sourceCode" id="cb7"><pre class="sourceCode R"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="do">## # A tibble: 11 × 2</span></span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="do">## term estimate</span></span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="do">## <chr> <dbl></span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="do">## 1 (Intercept) -0.231 </span></span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="do">## 2 mpg -0.0417 </span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="do">## 3 cyl -0.0573 </span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="do">## 4 disp 0.00669</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="do">## 5 hp -0.00323</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="do">## 6 drat -0.0901 </span></span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="do">## 7 qsec 0.200 </span></span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="do">## 8 vs -0.0664 </span></span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="do">## 9 am 0.0184 </span></span>
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="do">## 10 gear -0.0935 </span></span>
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a><span class="do">## 11 carb 0.249</span></span></code></pre></div>
</div>
</div>
<div class="section level2">
<h2 id="contributing">Contributing<a class="anchor" aria-label="anchor" href="#contributing"></a>
</h2>
<p>This project is released with a <a href="https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html" class="external-link">Contributor Code of Conduct</a>. By contributing to this project, you agree to abide by its terms.</p>
<ul>
<li><p>For questions and discussions about tidymodels packages, modeling, and machine learning, please <a href="https://forum.posit.co/new-topic?category_id=15&tags=tidymodels,question" class="external-link">post on Posit Community</a>.</p></li>
<li><p>If you think you have encountered a bug, please <a href="https://github.com/tidymodels/tidypredict/issues" class="external-link">submit an issue</a>.</p></li>
<li><p>Either way, learn how to create and share a <a href="https://reprex.tidyverse.org/articles/articles/learn-reprex.html" class="external-link">reprex</a> (a minimal, reproducible example), to clearly communicate about your code.</p></li>
<li><p>Check out further details on <a href="https://www.tidymodels.org/contribute/" class="external-link">contributing guidelines for tidymodels packages</a> and <a href="https://www.tidymodels.org/help/" class="external-link">how to get help</a>.</p></li>
</ul>
</div>
</div>
</main><aside class="col-md-3"><div class="links">
<h2 data-toc-skip>Links</h2>
<ul class="list-unstyled">
<li><a href="https://cloud.r-project.org/package=tidypredict" class="external-link">View on CRAN</a></li>
<li><a href="https://github.com/tidymodels/tidypredict/" class="external-link">Browse source code</a></li>
<li><a href="https://github.com/tidymodels/tidypredict/issues" class="external-link">Report a bug</a></li>
</ul>
</div>
<div class="license">
<h2 data-toc-skip>License</h2>
<ul class="list-unstyled">
<li><a href="LICENSE.html">Full license</a></li>
<li><small><a href="https://opensource.org/licenses/mit-license.php" class="external-link">MIT</a> + file <a href="LICENSE-text.html">LICENSE</a></small></li>
</ul>
</div>
<div class="community">
<h2 data-toc-skip>Community</h2>
<ul class="list-unstyled">
<li><a href="CODE_OF_CONDUCT.html">Code of conduct</a></li>
</ul>
</div>
<div class="citation">
<h2 data-toc-skip>Citation</h2>
<ul class="list-unstyled">
<li><a href="authors.html#citation">Citing tidypredict</a></li>
</ul>
</div>
<div class="developers">
<h2 data-toc-skip>Developers</h2>
<ul class="list-unstyled">
<li>Emil Hvitfeldt <br><small class="roles"> Author, maintainer </small> </li>
<li>Edgar Ruiz <br><small class="roles"> Author </small> </li>
<li>
<a href="https://github.com/topepo" class="external-link">Max Kuhn</a> <br><small class="roles"> Author </small> </li>
</ul>
</div>
</aside>
</div>
</div>
<footer><div class="container">
<div class="pkgdown-footer-left">
<p>Developed by Emil Hvitfeldt, Edgar Ruiz, <a href="https://github.com/topepo" class="external-link">Max Kuhn</a>.</p>
</div>
<div class="pkgdown-footer-right">
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.1.</p>
</div>
</div></footer>
</body>
</html>