{"id":13401445,"url":"https://github.com/leeper/prediction","last_synced_at":"2025-08-14T18:32:30.387Z","repository":{"id":47031949,"uuid":"72349017","full_name":"leeper/prediction","owner":"leeper","description":"Tidy, Type-Safe 'prediction()' Methods","archived":false,"fork":false,"pushed_at":"2024-06-11T14:01:24.000Z","size":503,"stargazers_count":89,"open_issues_count":23,"forks_count":14,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-10-13T19:09:26.423Z","etag":null,"topics":["model","predict","prediction","r","regression","tidy-data"],"latest_commit_sha":null,"homepage":"https://cran.r-project.org/package=prediction","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leeper.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-30T12:46:53.000Z","updated_at":"2024-06-06T17:02:11.000Z","dependencies_parsed_at":"2024-10-25T18:34:24.584Z","dependency_job_id":"0f726d24-01a7-48d9-a123-782f857ef45a","html_url":"https://github.com/leeper/prediction","commit_stats":{"total_commits":194,"total_committers":3,"mean_commits":64.66666666666667,"dds":"0.020618556701030966","last_synced_commit":"c239565c6b029e339969d1d13a480b66ffd438e9"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leeper%2Fprediction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leeper%2Fprediction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leeper%2Fprediction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leeper%2Fprediction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leeper","download_url":"https://codeload.github.com/leeper/prediction/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229856090,"owners_count":18134840,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["model","predict","prediction","r","regression","tidy-data"],"created_at":"2024-07-30T19:01:02.717Z","updated_at":"2024-12-15T18:10:06.066Z","avatar_url":"https://github.com/leeper.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\ntitle: \"Tidy, Type-Safe 'prediction()' Methods\"\noutput: github_document\n---\n\n\u003cimg src=\"man/figures/logo.png\" align=\"right\" /\u003e\n\nThe **prediction** and **margins** packages are a combined effort to port the functionality of Stata's (closed source) [`margins`](http://www.stata.com/help.cgi?margins) command to (open source) R. **prediction** is focused on one function - `prediction()` - that provides type-safe methods for generating predictions from fitted regression models. `prediction()` is an S3 generic, which always return a `\"data.frame\"` class object rather than the mix of vectors, lists, etc. that are returned by the `predict()` methods for various model types. It provides a key piece of underlying infrastructure for the **margins** package. Users interested in generating marginal (partial) effects, like those generated by Stata's `margins, dydx(*)` command, should consider using `margins()` from the sibling project, [**margins**](https://cran.r-project.org/package=margins).\n\nIn addition to `prediction()`, this package provides a number of utility functions for generating useful predictions:\n\n - `find_data()`, an S3 generic with methods that find the data frame used to estimate a regression model. This is a wrapper around `get_all_vars()` that attempts to locate data as well as modify it according to `subset` and `na.action` arguments used in the original modelling call.\n - `mean_or_mode()` and `median_or_mode()`, which provide a convenient way to compute the data needed for predicted values *at means* (or *at medians*), respecting the differences between factor and numeric variables.\n - `seq_range()`, which generates a vector of *n* values based upon the range of values in a variable\n - `build_datalist()`, which generates a list of data frames from an input data frame and a specified set of replacement `at` values (mimicking the `atlist` option of Stata's `margins` command)\n\n## Simple code examples\n\n```{r opts, echo = FALSE}\nlibrary(\"knitr\")\noptions(width = 100)\nopts_knit$set(upload.fun = imgur_upload, base.url = NULL)\nopts_chunk$set(fig.width=7, fig.height=4)\n```\n\nA major downside of the `predict()` methods for common modelling classes is that the result is not type-safe. Consider the following simple example:\n\n```{r predict}\nlibrary(\"stats\")\nlibrary(\"datasets\")\nx \u003c- lm(mpg ~ cyl * hp + wt, data = mtcars)\nclass(predict(x))\nclass(predict(x, se.fit = TRUE))\n```\n\n**prediction** solves this issue by providing a wrapper around `predict()`, called `prediction()`, that always returns a tidy data frame with a very simple `print()` method:\n\n```{r prediction}\nlibrary(\"prediction\")\n(p \u003c- prediction(x))\nclass(p)\nhead(p)\n```\n\nThe output always contains the original data (i.e., either data found using the `find_data()` function or passed to the `data` argument to `prediction()`). This makes it much simpler to pass predictions to, e.g., further summary or plotting functions.\n\nAdditionally the vast majority of methods allow the passing of an `at` argument, which can be used to obtain predicted values using modified version of `data` held to specific values:\n\n```{r at_arg}\nprediction(x, at = list(hp = seq_range(mtcars$hp, 5)))\n```\n\nThis more or less serves as a direct R port of (the subset of functionality of) Stata's `margins` command that calculates predictive marginal means, etc. For calculation of marginal or partial effects, see the [**margins**](https://cran.r-project.org/package=margins) package.\n\n## Supported model classes\n\nThe currently supported model classes are:\n\n - \"lm\" from `stats::lm()`\n - \"glm\" from `stats::glm()`, `MASS::glm.nb()`, `glmx::glmx()`, `glmx::hetglm()`, `brglm::brglm()`\n - \"ar\" from `stats::ar()`\n - \"Arima\" from `stats::arima()`\n - \"arima0\" from `stats::arima0()`\n - \"biglm\" from `biglm::biglm()` (including `\"ffdf\"` backed models)\n - \"betareg\" from `betareg::betareg()`\n - \"bruto\" from `mda::bruto()`\n - \"clm\" from `ordinal::clm()`\n - \"coxph\" from `survival::coxph()`\n - \"crch\" from `crch::crch()`\n - \"earth\" from `earth::earth()`\n - \"fda\" from `mda::fda()`\n - \"Gam\" from `gam::gam()`\n - \"gausspr\" from `kernlab::gausspr()`\n - \"gee\" from `gee::gee()`\n - \"glimML\" from `aod::betabin()`, `aod::negbin()`\n - \"glimQL\" from `aod::quasibin()`, `aod::quasipois()`\n - \"glmnet\" from `glmnet::glmnet()`\n - \"gls\" from `nlme::gls()`\n - \"hurdle\" from `pscl::hurdle()`\n - \"hxlr\" from `crch::hxlr()`\n - \"ivreg\" from `AER::ivreg()`\n - \"knnreg\" from `caret::knnreg()`\n - \"kqr\" from `kernlab::kqr()`\n - \"ksvm\" from `kernlab::ksvm()`\n - \"lda\" from `MASS:lda()`\n - \"lme\" from `nlme::lme()`\n - \"loess\" from `stats::loess()`\n - \"lqs\" from `MASS::lqs()`\n - \"mars\" from `mda::mars()`\n - \"mca\" from `MASS::mca()`\n - \"mclogit\" from `mclogit::mclogit()`\n - \"mda\" from `mda::mda()`\n - \"merMod\" from `lme4::lmer()` and `lme4::glmer()`\n - \"mnp\" from `MNP::mnp()`\n - \"naiveBayes\" from `e1071::naiveBayes()`\n - \"nlme\" from `nlme::nlme()`\n - \"nls\" from `stats::nls()`\n - \"nnet\" from `nnet::nnet()`, `nnet::multinom()`\n - \"plm\" from `plm::plm()`\n - \"polr\" from `MASS::polr()`\n - \"ppr\" from `stats::ppr()`\n - \"princomp\" from `stats::princomp()`\n - \"qda\" from `MASS:qda()`\n - \"rlm\" from `MASS::rlm()`\n - \"rpart\" from `rpart::rpart()`\n - \"rq\" from `quantreg::rq()`\n - \"selection\" from `sampleSelection::selection()`\n - \"speedglm\" from `speedglm::speedglm()`\n - \"speedlm\" from `speedglm::speedlm()`\n - \"survreg\" from `survival::survreg()`\n - \"svm\" from `e1071::svm()`\n - \"svyglm\" from `survey::svyglm()`\n - \"tobit\" from `AER::tobit()`\n - \"train\" from `caret::train()`\n - \"truncreg\" from `truncreg::truncreg()`\n - \"zeroinfl\" from `pscl::zeroinfl()`\n\n## Requirements and Installation\n\n[![CRAN](https://www.r-pkg.org/badges/version/prediction)](https://cran.r-project.org/package=prediction)\n![Downloads](https://cranlogs.r-pkg.org/badges/prediction)\n[![Build Status](https://travis-ci.org/leeper/prediction.svg?branch=master)](https://travis-ci.org/leeper/prediction)\n[![Build status](https://ci.appveyor.com/api/projects/status/a4tebeoa98cq07gy/branch/master?svg=true)](https://ci.appveyor.com/project/leeper/prediction/branch/master)\n[![codecov.io](https://codecov.io/github/leeper/prediction/coverage.svg?branch=master)](https://codecov.io/github/leeper/prediction?branch=master)\n[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)\n\nThe development version of this package can be installed directly from GitHub using `remotes`:\n\n``` r\nif (!require(\"remotes\")) {\n    install.packages(\"remotes\")\n}\nremotes::install_github(\"leeper/prediction\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleeper%2Fprediction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleeper%2Fprediction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleeper%2Fprediction/lists"}