This article describes how to convert a predictive model simulator created in Displayr into a tool that can allocate a predicted category for a range of records. The predictive model simulator is designed to only predict one record at a time using the control selections. However, we can update this setup to apply the predictions to all records in a new data set.
Requirements
- An existing regression or machine learning predictive model. This uses the Gradient Boost example from How to Create a Predictive Model Simulator in Displayr.
- New data to apply the predictive model to.
Method
Set up your new data
Either:
- Create a pasted table:
- In another data source, such as Excel, copy your table, including any headers.
- Navigate to the location you want to paste in the table (either on a Page or an output off a Page directly in the Report tree) and use right click > Paste to create the output. If pasting a table on a Page, you can also use Ctrl+V (Cmd+V on a Mac).
- A table output will be created where you selected, as well as a summary.table in the Data Sources tree.
- [Optional] If needed, you can modify what is treated as row and column headers in your table, sorting, and more, see Method - Adjusting the look of your pasted summary table.
- Create a raw data table:
- Add your new data set to your document via Data Sources > Plus (+).
- Select the variables that match the ones used as predictors in your predictive model.
- In the toolbar, select Table
> Raw Data > Variable(s) and tick Variable Names.
Importantly, the response labels need to match exactly those used in your predictive model; otherwise, the prediction will be NA for that record.
The column names also need to match the variable names used in your predictive model. The variable name is found under General > General > Name when you select a variable in the Data Sources tree:
Set up your prediction calculation
In this example, we now have a table called raw.data. To predict the outcome from the model on this new data, we can simplify the code as follows:
1. Select Calculation > Custom Code in the toolbar and click on the page.
2. Paste the below into the code window:
DF = raw.data
arguments <- list(model, newdata = DF)
as.vector(do.call(predict, arguments))Update the names of your new data (DF) table and model above, if necessary.
This will now run the prediction algorithm on all our new data:
3. OPTIONAL: Export the predicted category output from the toolbar via Share > Export Report > Excel.
Next
How to Create a Predictive Model Simulator in Displayr
UPCOMING WEBINAR: 10 Market Research Predictions Over the Next 4 Years