Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sparkfish/castpack
Magical packager of R linear models for your MS SQL Server database ✨
https://github.com/sparkfish/castpack
deploy forecast-models glm linear-models ms-sql packager r sql-server
Last synced: about 1 month ago
JSON representation
Magical packager of R linear models for your MS SQL Server database ✨
- Host: GitHub
- URL: https://github.com/sparkfish/castpack
- Owner: sparkfish
- License: other
- Created: 2020-04-14T02:52:56.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-18T22:40:37.000Z (over 4 years ago)
- Last Synced: 2024-05-01T09:37:50.139Z (9 months ago)
- Topics: deploy, forecast-models, glm, linear-models, ms-sql, packager, r, sql-server
- Language: R
- Homepage:
- Size: 55.7 KB
- Stars: 5
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
Castpack is a magical R library that lets you effortlessly package linear forecast models and deploys them for use directly in your Microsoft SQL Server database.
Leveraging the powerful open-source [modelc](https://github.com/team-sparkfish/modelc) library, Castpack will transpile models consisting of hundreds of parameters to performant ANSI SQL in mere seconds, and load them into your database in the blink of an eye. Just bring your models as `.rds` files, tell Castpack about your database with a simple configuration file, and let her rip!
Unlike other libraries and tools, Castpack was purpose-built for predictive linear and generalized linear models. This focus on linear models keeps Castpack lightweight, and allows it to support linear models and GLMs that other libraries choke on.
It was inspired by and builds upon the venerable [tidypredict](https://tidymodels.github.io/tidypredict/) library.
## Installation
Using `devtools`:
```{R}
install.packages("devtools")
install.packages("remotes")
remotes::install_github("team-sparkfish/Castpack", dependencies=T)
```Prepare a workspace directory:
```{shell}
$ mkdir workspace
```Copy the `example.models.yml` and `example.db.yml` configuration files to `workspace/models.yml` and `workspace/db.yml` respectively and fill in the details for your database and model.
Set your R working directory to your workspace, and run
```{R}
Castpack::prepare_registry()
```This will create the necessary objects for models to be loaded and run inside your database.
## How it works
Castpack is simple to use because it is opinionated (in a "convention over configuration" sense) about how models are represented in your database.
When you run `Castpack::prepare_registry()`, Castpack creates two objects: a `${schema}.Models` table (where `${schema}` is the schema you specified in your configuration file), along with `${schema}.Predict`, a stored procedure for running predictions inside the database.
The `Predict` procedure takes as arguments a model name and a datasource name. The latter must correspond to an existing view or table.
The models specified in `models.yml` are then transpiled from `.rds` format files into ANSI SQL queries, which are upserted into the `Models` table. From there, you can run the `Predict` procedure against the model and a table or view in your database.
Because the models are nothing more than formulas represented as select statements, they are blazing fast.
## Making Predictions
To make predictions, used the `Predict` function that is created when `Castpack::prepare_registry()` is run.
It takes two arguments:
``` sql
@modelName NVARCHAR(128),
@dataSourceViewName NVARCHAR(258)
````@dataSourceViewName` should be the name of an existing table or view.
## Model Configuration
Use `models.yml` to configure your models. There should be a toplevel key for each model to be imported consisting of the following attributes
- `name` The model name is used by the `Predict` procedure to apply the model against the specified dataset
- `path` The path to the model file. The model should live on disk as a `.Rds` formatted file
- `datasource` The data source should be an existing table or view the model should be applied against
- `auxiliary_columns` These are additional columns to be returned in the output of `Predict`
- `response_column` This specifies the alias of the response column in the output of `Predict`
- `raw` _(optional)_ Any additional SQL (e.g., a `WHERE` or `ORDER BY` clause) can be added hereSee `example.models.yml` for an example.
## API
- `Castpack::prepare_registry()` creates the `${schema}.Models` table and `${schema}.Predict` procedure
- `Castpack::deploy_models()` upserts the models specified in `config.r` to the `Models` table. This function depends on a `models` variable defined in `config.r` that tells Castpack about the models you'd like to load into your database. See `example.config.r` for an example configuration.