Predict"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The explore package offers a simplified way to use machine learning and make a prediction.

We use synthetic data in this example

library(dplyr)
library(explore)

train <- create_data_buy(obs = 1000, seed = 1)
glimpse(train)

Train model

First we create a decision tree model, using buy as target (buy contains only 0 and 1 values)

train %>% explain_tree(target = buy)

We see some clear patterns. Now we create a random forest model (as it is more accurate). To get the model itself, use parameter out = "model"

model <- train %>% explain_forest(target = buy, out = "model")

Predict

Now we create test data and use the model for a prediction. We use a different seed so we get different data.

test <- create_data_buy(obs = 1000, seed = 2)
glimpse(test)
test <- test %>% predict_target(model = model)
glimpse(test)

Now we got 2 new variables prediction_0 (the probability of buy == 0) and prediction_1 (the probability of buy == 1). We can check the predictions by comparing prediction_1 with real values of buy.

test %>% explore(prediction_1, target = buy)

There is a clear difference between buy == 0 and buy == 1. So the prediction works.



Try the explore package in your browser

Any scripts or data that you put into this service are public.

explore documentation built on Oct. 11, 2023, 9:07 a.m.