{"id":13665823,"url":"https://github.com/tidymodels/tidypredict","last_synced_at":"2025-05-15T03:07:08.847Z","repository":{"id":28037377,"uuid":"114579049","full_name":"tidymodels/tidypredict","owner":"tidymodels","description":"Run predictions inside the database","archived":false,"fork":false,"pushed_at":"2024-12-19T19:37:06.000Z","size":10091,"stargazers_count":262,"open_issues_count":18,"forks_count":31,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-04-15T01:58:13.478Z","etag":null,"topics":["dbplyr","dplyr","purrr","r","rlang"],"latest_commit_sha":null,"homepage":"https://tidypredict.tidymodels.org","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tidymodels.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-18T00:26:43.000Z","updated_at":"2025-04-07T21:02:41.000Z","dependencies_parsed_at":"2024-01-05T00:56:55.865Z","dependency_job_id":"1ab6258d-a9d6-4f88-bf9d-a0f56ad87cce","html_url":"https://github.com/tidymodels/tidypredict","commit_stats":{"total_commits":311,"total_committers":17,"mean_commits":"18.294117647058822","dds":0.607717041800643,"last_synced_commit":"4e33df67c2e3c828d2ce759c896a4f7ae677bf38"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Ftidypredict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Ftidypredict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Ftidypredict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidymodels%2Ftidypredict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tidymodels","download_url":"https://codeload.github.com/tidymodels/tidypredict/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254264766,"owners_count":22041793,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbplyr","dplyr","purrr","r","rlang"],"created_at":"2024-08-02T06:00:51.374Z","updated_at":"2025-05-15T03:07:03.837Z","avatar_url":"https://github.com/tidymodels.png","language":"R","funding_links":[],"categories":["R","Keras Cheatsheet"],"sub_categories":["Model Explaination"],"readme":"---\noutput: github_document\n---\n\n# tidypredict \u003cimg src=\"man/figures/logo.png\" align=\"right\" width = \"120px\"/\u003e\n\n[![R-CMD-check](https://github.com/tidymodels/tidypredict/workflows/R-CMD-check/badge.svg)](https://github.com/tidymodels/tidypredict/actions)\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/tidypredict)](https://CRAN.r-project.org/package=tidypredict)\n[![Codecov test coverage](https://codecov.io/gh/tidymodels/tidypredict/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/tidypredict?branch=main)\n\n```{r pre, include = FALSE}\nif (!rlang::is_installed(\"randomForest\")) {\n  knitr::opts_chunk$set(\n    eval = FALSE\n  )\n}\n```\n\n```{r setup, include=FALSE}\nlibrary(dplyr)\nlibrary(tidypredict)\nlibrary(randomForest)\n```\n\n\nThe main goal of `tidypredict` 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:\n\n```{r}\nmodel \u003c- lm(mpg ~ wt + cyl, data = mtcars)\n```\n\n`tidypredict` can return a SQL statement that is ready to run inside the database.  Because it uses `dplyr`'s database interface, it works with several databases back-ends, such as MS SQL:\n\n```{r}\ntidypredict_sql(model, dbplyr::simulate_mssql())\n```\n\n\n## Installation\n\nInstall `tidypredict` from CRAN using:\n```{r, eval = FALSE}\ninstall.packages(\"tidypredict\")\n```\n\nOr install the **development version** using `devtools` as follows:\n```{r, eval = FALSE}\ninstall.packages(\"remotes\")\nremotes::install_github(\"tidymodels/tidypredict\")\n```\n\n## Functions\n\n`tidypredict` 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. \n\n| Function                    | Description \n|-----------------------------|--------------------------------------------------------------------------------|\n|`tidypredict_fit()`          | Returns an R formula that calculates the prediction                            |\n|`tidypredict_sql()`          | Returns a SQL query based on the formula from `tidypredict_fit()`              |\n|`tidypredict_to_column()`    | Adds a new column using the formula from `tidypredict_fit()`                   |\n|`tidypredict_test()`         | Tests `tidyverse` predictions against the model's native `predict()` function  |\n|`tidypredict_interval()`     | Same as `tidypredict_fit()` but for intervals (only works with `lm` and `glm`) |\n|`tidypredict_sql_interval()` | Same as `tidypredict_sql()` but for intervals (only works with `lm` and `glm`) |\n|`parse_model()`              | Creates a list spec based on the R model                                       |\n|`as_parsed_model()`          | Prepares an object to be recognized as a parsed model                          |\n\n## How it works\n\n\u003cimg src=\"man/figures/howitworks.png\"\u003e\n\nInstead of translating directly to a SQL statement, `tidypredict` creates an R formula. That formula can then be used inside `dplyr`.  The overall workflow would be as illustrated in the image above, and described here:\n\n1. Fit the model using a base R model, or one from the packages listed in [Supported Models](#supported-models)\n1. `tidypredict` reads model, and creates a list object with the necessary components to run predictions\n1. `tidypredict` builds an R formula based on the list object\n1. `dplyr` evaluates the formula created by `tidypredict`\n1. `dplyr` translates the formula into a SQL statement, or any other interfaces.\n1. The database executes the SQL statement(s) created by `dplyr`\n\n### Parsed model spec\n\n`tidypredict` 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:\n\n1. No more saving models as `.rds` - Specifically for cases when the model needs to be used for predictions in a Shiny app.\n1. Beyond R models - Technically, anything that can write a proper spec, can be read into `tidypredict`. It also means, that the parsed model spec can become a good alternative to using *PMML.*\n\n## Supported models\n\nThe following models are supported by `tidypredict`:\n\n- Linear Regression - `lm()`\n- Generalized Linear model - `glm()`\n- Random Forest models - `randomForest::randomForest()`\n- Random Forest models, via `ranger` - `ranger::ranger()`\n- MARS models - `earth::earth()`\n- XGBoost models - `xgboost::xgb.Booster.complete()` \n- Cubist models - `Cubist::cubist()` \n- Tree models, via `partykit` - `partykit::ctree()` \n\n### `parsnip` \n\n`tidypredict` supports models fitted via the `parsnip` interface. The ones confirmed currently work in `tidypredict` are:\n\n- `lm()` - `parsnip`: `linear_reg()` with *\"lm\"* as the engine.\n- `randomForest::randomForest()` - `parsnip`: `rand_forest()` with *\"randomForest\"* as the engine.\n- `ranger::ranger()` - `parsnip`: `rand_forest()` with *\"ranger\"* as the engine.\n- `earth::earth()`  - `parsnip`: `mars()` with *\"earth\"* as the engine.\n\n### `broom`\n\nThe `tidy()` function from broom works with linear models parsed via `tidypredict`\n\n```{r}\npm \u003c- parse_model(lm(wt ~ ., mtcars))\ntidy(pm)\n```\n\n## Contributing\n\nThis project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.\n\n- For questions and discussions about tidymodels packages, modeling, and machine learning, please [post on Posit Community](https://forum.posit.co/new-topic?category_id=15\u0026tags=tidymodels,question).\n\n- If you think you have encountered a bug, please [submit an issue](https://github.com/tidymodels/tidypredict/issues).\n\n- Either way, learn how to create and share a [reprex](https://reprex.tidyverse.org/articles/articles/learn-reprex.html) (a minimal, reproducible example), to clearly communicate about your code.\n\n- Check out further details on [contributing guidelines for tidymodels packages](https://www.tidymodels.org/contribute/) and [how to get help](https://www.tidymodels.org/help/).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidymodels%2Ftidypredict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftidymodels%2Ftidypredict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidymodels%2Ftidypredict/lists"}