## 14.6 Predict Class

20200607 R provides stats::predict() to obtain predictions from the model. We can obtain the predictions, for example, on the test dataset and thereby determine the apparent accuracy of the model.

For different types of models stats::predict() will behave in a similar way. There are however variations that we need to be aware of for each. For an \Sexpr{mtype} model to predict the class (i.e., Yes or No) use type="class":

ds[te, vars] %>% predict(model, newdata=., type="class") -> predict_te

head(predict_te)
##  1  2  3  4  5  6
## No No No No No No
## Levels: No Yes

We can then compare this to the actual class for these observations as is recorded in the original te dataset. The actual classes have already been stored as the variable target_te:

head(actual_te)
## [1] No No No No No No
## Levels: No Yes

We can observe from the above that the model correctly predicts of the first 6 observations from the test dataset, suggesting a % accuracy. Over the full observations contained in the test dataset are correctly predicted, which is % accurate.

For different evaluations of the model we will collect the class predictions from the training and tuning datasets as well:

ds[tr, vars] %>% predict(model, newdata=., type="class") -> predict_tr
ds[tu, vars] %>% predict(model, newdata=., type="class") -> predict_tu

We can also calculate the accuracy for each of these datasets:

glue("{round(100*sum(predict_tr==actual_tr)/length(predict_tr))}%")
## 83%
glue("{round(100*sum(predict_tu==actual_tu)/length(predict_tu))}%")
## 83%

Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.2511897683143616, "perplexity": 2494.7012370284415}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-39/segments/1631780056392.79/warc/CC-MAIN-20210918093220-20210918123220-00238.warc.gz"}