eval_model <- FALSE if(Sys.getenv("GLOBAL_EVAL") != "") eval_model <- Sys.getenv("GLOBAL_EVAL")
library(tidymodels) library(yaml) library(modeldb) library(dbplot) library(pins) library(dbplyr) library(RPostgres) library(connections) library(config)
Use PostgreSQL TABLESAMPLE clause
Use connection_open()
to open a Database connection
r
con <- connection_open(
RPostgres::Postgres(),
host = "localhost",
user = get("user"),
password = get("pwd"),
port = 5432,
dbname = "postgres",
bigint = "integer"
)
Set the orders
variable to point to the orders table
r
orders <- tbl(con, in_schema("retail", "orders"))
Set the orders_view
variable to point to the v_orders table
r
orders_view <- tbl(con, in_schema("retail", "v_orders"))
Pipe orders
into the function show_query()
r
orders %>%
show_query()
Pipe the previous command into the class()
function to see the kind of output show_query()
returns
r
orders %>%
show_query() %>%
class()
Replace show_query()
with remote_query()
to compare the output types
r
orders %>%
remote_query() %>%
class()
Replace class()
with build_sql()
. Use con
as the value for the con
argument
r
orders %>%
remote_query() %>%
build_sql(con = con)
Add " TABLESAMPLE BERNOULLI (0.1)" to build_sql()
as another ...
argument
r
orders %>%
remote_query() %>%
build_sql(con = con, " TABLESAMPLE BERNOULLI (0.1)")
Pipe the code into tbl()
. Use con
for the con
argument, and .
for the rest
r
orders %>%
remote_query() %>%
build_sql(con = con, " TABLESAMPLE BERNOULLI (0.1)") %>%
tbl(con, .)
Use inner_join()
to add the information from the orders_view
pointer, use order_id
as the matching field
r
orders %>%
remote_query() %>%
build_sql(con = con, " TABLESAMPLE BERNOULLI (0.1)") %>%
tbl(con, .) %>%
inner_join(orders_view, by = "order_id")
Assign the resulting code to a variable orders_sample_db
r
orders_sample_db <- orders %>%
remote_query() %>%
build_sql(con = con, " TABLESAMPLE BERNOULLI (0.1)") %>%
tbl(con, .) %>%
inner_join(orders_view, by = "order_id")
Use collect()
to load the results of orders_sample_db
to a new variable called orders_sample
r
orders_sample <- collect(orders_sample_db)
Load the dbplot
library
r
library(dbplot)
Use dbplot_histogram()
to visualize the distribution of order_total
from orders_sample
r
orders_sample %>%
dbplot_histogram(order_total, binwidth = 5)
Use dbplot_histogram()
to visualize the distribution of order_total
from orders_view
r
orders_view %>%
dbplot_histogram(order_total, binwidth = 5)
tidymodels
for modelingFit and measure the model's performance using functions from parsnip
and yardstick
Load the tidymodels
library
r
library(tidymodels)
Start with the linear_reg()
command, pipe into set_engine()
, and use "lm" as its sole argument
r
linear_reg() %>%
set_engine("lm")
Pipe into the fit()
command. Use the formula: order_total ~ order_qty
, and orders_sample
as the data
argument
r
linear_reg() %>%
set_engine("lm") %>%
fit(order_total ~ order_qty, data = orders_sample)
Assign the previous code to a variable called parsnip_model
r
parsnip_model <- linear_reg() %>%
set_engine("lm") %>%
fit(order_total ~ order_qty, data = orders_sample)
Use bind_cols()
to add the predictions to order_sample
. Calculate the prediction with predict()
r
orders_sample %>%
bind_cols(predict(parsnip_model, orders_sample))
Pipe the code into the metrics()
function. Use order_total
as the truth
argument, and .pred
as the estimate
argument
r
orders_sample %>%
bind_cols(predict(parsnip_model, orders_sample)) %>%
metrics(truth = order_total, estimate = .pred)
tidypredict
Load the tidypredict
library
r
library(tidypredict)
Use the parse_model()
function to parse parsnip_model
, and assign it to a variable called parsed_parsnip
r
parsed_parsnip <- parse_model(parsnip_model)
Use str()
to see the parsed_parsnip
object's structure
r
str(parsed_parsnip)
Use tidypredict_fit()
to view the dplyr
formula that calculates the prediction
r
tidypredict_fit(parsed_parsnip)
Use head()
to get the first 10 records from orders_view
r
orders_view %>%
head(10)
Pipe the code into mutate()
. Assign to a new my_pred
variable the results of tidypredict_fit()
. Make sure to prefix tidypredict_fit()
with the bang-bang operator so that the formula is evaluated.
r
orders_view %>%
head(10) %>%
mutate(my_pred = !! tidypredict_fit(parsed_parsnip))
Replace the mutate()
command with tidypredict_to_column()
r
orders_view %>%
head(10) %>%
tidypredict_to_column(parsnip_model)
Load the yaml
library
r
library(yaml)
Use write_yaml()
to save the contents of parsed_parsnip
into a file called model.yaml
r
write_yaml(parsed_parsnip, "model.yaml")
Using read_yaml()
, read the contents of the model.yaml file into the a new variable called loaded_model
r
loaded_model <- read_yaml("model.yaml")
Use as_parsed_model()
to convert the loaded_model
variable into a tidypredict
parsed model object, assign the results to loaded_model_2
r
loaded_model_2 <- as_parsed_model(loaded_model)
Load the modeldb
library
r
library(modeldb)
Use select()
to pick the order_total
and order_qty
fields from the orders_sample_db
table pointer
r
orders_sample_db %>%
select(order_total, order_qty)
Pipe the code into the linear_regression_db()
function, pass order_total
as the only argument
r
orders_sample_db %>%
select(order_total, order_qty) %>%
linear_regression_db(order_total)
Assign the model results to a new variable called db_model
r
db_model <- orders_sample_db %>%
select(order_total, order_qty) %>%
linear_regression_db(order_total)
Use as_parsed_model()
to convert db_model
to a parsed model object. Assign to new a variable called pm
r
pm <- as_parsed_model(db_model)
Use head()
to get the top 10 records, and then pipe into tidypredict_to_column()
to add the results from pm
r
orders_view %>%
head(10) %>%
tidypredict_to_column(pm)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.