Permalink
Browse files

initial commit

  • Loading branch information...
leeper committed Oct 30, 2016
0 parents commit 892d7bb01d83ccff9d342dfb92d1a7a04ebe657b
Showing with 777 additions and 0 deletions.
  1. +19 −0 .Rbuildignore
  2. +11 −0 .gitignore
  3. +7 −0 .travis.yml
  4. +29 −0 CONTRIBUTING.md
  5. +20 −0 DESCRIPTION
  6. +2 −0 LICENSE
  7. +21 −0 NAMESPACE
  8. +3 −0 NEWS.md
  9. +20 −0 R/find_data.R
  10. +9 −0 R/margins-package.R
  11. +64 −0 R/prediction.R
  12. +239 −0 R/prediction_methods.R
  13. +52 −0 README.Rmd
  14. +89 −0 README.md
  15. +34 −0 appveyor.yml
  16. +15 −0 inst/CITATION
  17. +29 −0 man/find_data.Rd
  18. +18 −0 man/prediction-package.Rd
  19. +81 −0 man/prediction.Rd
  20. +3 −0 tests/testthat-prediction.R
  21. +12 −0 tests/testthat/tests-core.R
@@ -0,0 +1,19 @@
.travis.yml
appveyor.yml
travis-tool.sh
README.Rmd
README.html
CONTRIBUTING.md
/inst/standarderrors.pdf
/figure/*
/cache/*
/inst/doc/*.log
/inst/doc/*.Rmd
/vignettes/mtcars.csv
/vignettes/Cigar.csv
/vignettes/*.log
/vignettes/*.aux
/vignettes/*.pdf
/vignettes/*.sty
/vignettes/*.tex
^data-raw$
@@ -0,0 +1,11 @@
README.html
/inst/doc/*.log
/vignettes/*.log
/vignettes/*.aux
/vignettes/*.pdf
/vignettes/*.sty
/vignettes/*.tex
/vignettes/*.out
/vignettes/*.blg
/vignettes/figure/
/vignettes/*.bib.bak
@@ -0,0 +1,7 @@
language: r
sudo: false
cache: packages
r_packages:
- covr
after_success:
- Rscript -e 'library("covr");codecov()'
@@ -0,0 +1,29 @@
Contributions to **prediction** are welcome from anyone and are best sent as pull requests on [the GitHub repository](https://github.com/leeper/prediction/). This page provides some instructions to potential contributors about how to add to the package.

1. Contributions can be submitted as [a pull request](https://help.github.com/articles/creating-a-pull-request/) on GitHub by forking or cloning the [repo](https://github.com/leeper/prediction/), making changes and submitting the pull request.

2. This package follows [the cloudyr project style guide](http://cloudyr.github.io/styleguide/index.html). Please refer to this when editing package code.

3. Pull requests should involve only one commit per substantive change. This means if you change multiple files (e.g., code and documentation), these changes should be committed together. If you don't know how to do this (e.g., you are making changes in the GitHub web interface) just submit anyway and the maintainer will clean things up.

4. All contributions must be submitted consistent with the package license ([MIT](https://opensource.org/licenses/MIT)).

5. Non-trivial contributions need to be noted in the `Authors@R` field in the [DESCRIPTION](https://github.com/leeper/prediction/blob/master/DESCRIPTION). Just follow the format of the existing entries to add your name (and, optionally, email address). Substantial contributions should also be noted in [`inst/CITATION`](https://github.com/leeper/prediction/blob/master/inst/CITATION).

6. The package uses royxgen code and documentation markup, so changes should be made to roxygen comments in the source code `.R` files. If changes are made, roxygen needs to be run. The easiest way to do this is a command line call to: `Rscript -e devtools::document()`. Please resolve any roxygen errors before submitting a pull request. The [README.md](https://github.com/leeper/prediction/blob/master/README.md) file is built from [README.Rmd](https://github.com/leeper/prediction/blob/master/README.Rmd); changes should be made in both places or to [README.Rmd](https://github.com/leeper/prediction/blob/master/README.Rmd) and then knitted using using `knitr::knit("README.Rmd")`.

7. Please run `R CMD BUILD prediction` and `R CMD CHECK prediction_VERSION.tar.gz` before submitting the pull request to check for any errors.

Some specific types of changes that you might make are:

1. Bug fixes. Great!

2. Documentation-only changes (e.g., to Rd files, README, vignettes). This is great! All contributions are welcome.

3. New functionality. This is fine, but should be discussed on [the GitHub issues page](https://github.com/leeper/prediction/issues) before submitting a pull request.

3. Changes requiring a new package dependency should also be discussed on [the GitHub issues page](https://github.com/leeper/prediction/issues) before submitting a pull request.

4. Message translations. These are very appreciated! The format is a pain, but if you're doing this I'm assuming you're already familiar with it.

Any questions you have can be opened as GitHub issues or directed to thosjleeper (at) gmail.com.
@@ -0,0 +1,20 @@
Package: prediction
Type: Package
Title: Tidy, Type-Safe 'prediction()' Methods
Description: A one-function package containing 'prediction()', a type-safe alernative to 'predict()' that always returns a data frame.
License: MIT + file LICENSE
Version: 0.1.0
Date: 2016-10-30
Authors@R: c(person("Thomas J.", "Leeper",
role = c("aut", "cre"),
email = "thosjleeper@gmail.com"))
Imports:
utils,
stats
Suggests:
testthat
Enhances:
survival,
nlme,
AER
RoxygenNote: 5.0.1
@@ -0,0 +1,2 @@
YEAR: 2016
COPYRIGHT HOLDER: Thomas J. Leeper
@@ -0,0 +1,21 @@
# Generated by roxygen2: do not edit by hand

S3method(head,prediction)
S3method(prediction,coxph)
S3method(prediction,default)
S3method(prediction,glm)
S3method(prediction,gls)
S3method(prediction,ivreg)
S3method(prediction,lm)
S3method(prediction,loess)
S3method(prediction,nls)
S3method(prediction,polr)
S3method(prediction,survreg)
S3method(print,prediction)
S3method(tail,prediction)
export(find_data)
export(prediction)
importFrom(stats,get_all_vars)
importFrom(stats,predict)
importFrom(utils,head)
importFrom(utils,tail)
@@ -0,0 +1,3 @@
# CHANGES TO prediction 0.1.0

* Initial package released.
@@ -0,0 +1,20 @@
#' @title Extract data from a model object
#' @description Find a model frame in a model object or try to reconstruct one
#' @param model The model object.
#' @param env An environment in which to look for the \code{data} argument to the modelling call.
#' @return A data.frame, typically with one column unless the variable is a factor with more than two levels.
#' @examples
#' require("datasets")
#' x <- lm(mpg ~ cyl * hp + wt, data = head(mtcars))
#' find_data(x)
#'
#' @seealso \code{\link{prediction}}
#' @export
find_data <- function(model, env = parent.frame()) {
if (!is.null(model[["call"]][["data"]])) {
data <- eval(model[["call"]][["data"]], env)
} else {
data <- get_all_vars(model[["terms"]], data = model[["model"]])
}
data
}
@@ -0,0 +1,9 @@
#' @name prediction-package
#' @title Tidy, Type-safe \code{prediction()} Methods
#' @aliases margins-package
#' @docType package
#' @description A one-function package containing \code{\link{prediction}}, a type-safe alernative to \code{\link[stats]{predict}} that always returns a data frame.
#' @author Thomas J. Leeper
#' @keywords package
#' @seealso \code{\link{prediction}}
NULL
@@ -0,0 +1,64 @@
#' @rdname prediction
#' @title Extract Predictions from a Model Object
#' @description Extract predicted values via \code{\link[stats]{predict}} from a model object, conditional on data
#' @param model A model object, perhaps returned by \code{\link[stats]{lm}} or \code{\link[stats]{glm}}.
#' @param data A data.frame over which to calculate marginal effects. If missing, \code{\link{find_data}} is used to specify the data frame.
#' @param type A character string indicating the type of marginal effects to estimate. Mostly relevant for non-linear models, where the reasonable options are \dQuote{response} (the default) or \dQuote{link} (i.e., on the scale of the linear predictor in a GLM). For models of class \dQuote{polr} (from \code{\link[MASS]{polr}}), possible values are \dQuote{class} or \dQuote{probs}; both are returned.
#' @param \dots Additional arguments passed to \code{\link[stats]{predict}} methods.
#' @details This function is simply a wrapper around \code{\link[stats]{predict}} that returns a data frame containing predicted values with respect to all variables specified in \code{data}.
#'
#' Methods are currently implemented for the following object classes:
#' \itemize{
#' \item \dQuote{lm}, see \code{\link[stats]{lm}}
#' \item \dQuote{glm}, see \code{\link[stats]{glm}}, \code{\link[MASS]{glm.nb}}
#' \item \dQuote{loess}, see \code{\link[stats]{loess}}
#' \item \dQuote{polr}, see \code{\link[MASS]{polr}}
#' \item \dQuote{gls}, see \code{\link[nlme]{gls}}
#' \item \dQuote{ivreg}, see \code{\link[AER]{ivreg}}
#' \item \dQuote{nls}, see \code{\link[stats]{nls}}
#' \item \dQuote{coxph}, see \code{\link[survival]{coxph}}
#' \item \dQuote{survreg}, see \code{\link[survival]{survreg}}
#' }
#'
#' @return A data.frame with class \dQuote{prediction} that has a number of rows equal to number of rows in \code{data}, where each row is an observation and the first two columns represent fitted/predicted values (\code{fitted}) and the standard errors thereof (\code{se.fitted}). Additional columns may be reported depending on the object class.
#' require("datasets")
#' x <- lm(mpg ~ cyl * hp + wt, data = mtcars)
#' prediction(x)
#'
#' @keywords models
#' @importFrom stats predict get_all_vars
#' @export
prediction <- function(model, data, ...) {
UseMethod("prediction")
}

#' @importFrom utils head
#' @export
print.prediction <- function(x, digits = 4, ...) {
f <- x[["fitted"]]
if (is.numeric(f)) {
m <- mean(x[["fitted"]], na.rm = TRUE)
m <- sprintf(paste0("%0.", digits, "f"), m)
message(paste0("Average prediction: ", m, ", for ", length(f), " ", ngettext(length(f), "observation", "observations")))
} else if (is.factor(f)) {
m <- sort(table(x[["fitted"]]), decreasing = TRUE)[1]
message(paste0("Modal prediction: ", shQuote(names(m)), " for ", m, " of ", length(f), " ",
ngettext(length(f), "observation", "observations"),
" with total ", nlevels(f), " ", ngettext(nlevels(f), "level", "levels") ))
} else {
print(head(x), ...)
}
invisible(x)
}

#' @importFrom utils head
#' @export
head.prediction <- function(x, ...) {
head(`class<-`(x, "data.frame"), ...)
}

#' @importFrom utils tail
#' @export
tail.prediction <- function(x, ...) {
tail(`class<-`(x, "data.frame"), ...)
}
Oops, something went wrong.

0 comments on commit 892d7bb

Please sign in to comment.