Permalink
Browse files

add 'margex' example data

  • Loading branch information...
leeper committed Aug 5, 2018
1 parent f8f348a commit 52c21fea1680344c7995b9a99b27e9829b42c5cf
Showing with 198 additions and 4 deletions.
  1. +2 −1 .Rbuildignore
  2. +4 −3 DESCRIPTION
  3. +76 −0 R/margex.R
  4. +27 −0 data-raw/margex.R
  5. BIN data/margex.rda
  6. +89 −0 man/margex.Rd
@@ -15,6 +15,8 @@
^cache/.+$
^docs$
^docs/.+$
^data-raw$
^data-raw/.+$
^revdep.?
^ignore$
^inst/doc/.+\.log$
@@ -30,4 +32,3 @@
^vignettes/.+\.pdf$
^vignettes/.+\.sty$
^vignettes/.+\.tex$
^data-raw$
@@ -1,10 +1,10 @@
Package: prediction
Type: Package
Title: Tidy, Type-Safe 'prediction()' Methods
Description: A one-function package containing 'prediction()', a type-safe alternative to 'predict()' that always returns a data frame. The package currently supports common model types (e.g., "lm", "glm") from the 'stats' package, as well as numerous other model classes from other add-on packages. See the README or main package documentation page for a complete listing.
Description: A one-function package containing 'prediction()', a type-safe alternative to 'predict()' that always returns a data frame. The 'summary()' method provides a data frame with average predictions, possibly over counterfactual versions of the data (a la the 'margins' command in Stata). Marginal effect estimation is provided by the related package, 'margins' <https://cran.r-project.org/package=margins>. The package currently supports common model types (e.g., "lm", "glm") from the 'stats' package, as well as numerous other model classes from other add-on packages. See the README or main package documentation page for a complete listing.
License: MIT + file LICENSE
Version: 0.3.7
Date: 2018-08-03
Version: 0.3.8
Date: 2018-08-05
Authors@R: c(person("Thomas J.", "Leeper",
role = c("aut", "cre"),
email = "thosjleeper@gmail.com",
@@ -14,6 +14,7 @@ Authors@R: c(person("Thomas J.", "Leeper",
)
URL: https://github.com/leeper/prediction
BugReports: https://github.com/leeper/prediction/issues
Depends: R (>= 3.5.0)
Imports:
utils,
stats,
@@ -0,0 +1,76 @@
#' @rdname margex
#' @docType data
#' @title Artificial data for margins, copied from Stata
#' @description The dataset is identical to the one provided by Stata and available from \code{webuse::webuse("margex")} with categorical variables explicitly encoded as factors.
#' @format A data frame with 3000 observations on the following 11 variables.
#' \describe{
#' \item{\samp{y}}{A numeric vector}
#' \item{\samp{outcome}}{A binary numeric vector with values (0,1)}
#' \item{\samp{sex}}{A factor with two levels}
#' \item{\samp{group}}{A factor with three levels}
#' \item{\samp{age}}{A numeric vector}
#' \item{\samp{distance}}{A numeric vector}
#' \item{\samp{ycn}}{A numeric vector}
#' \item{\samp{yc}}{A numeric vector}
#' \item{\samp{treatment}}{A factor with two levels}
#' \item{\samp{agegroup}}{A factor with three levels}
#' \item{\samp{arm}}{A factor with three levels}
#' }
#' @source \url{http://www.stata-press.com/data/r14/margex.dta}
#' @examples
#' \donttest{
#'
#' # Examples from Stata's help files
#' # Also available from: webuse::webuse("margex")
#' data("margex")
#'
#' # A simple case after regress
#' # . regress y i.sex i.group
#' # . margins sex
#' m1 <- lm(y ~ factor(sex) + factor(group), data = margex)
#' prediction(m1, at = list(sex = 0:1))
#'
#' # A simple case after logistic
#' # . logistic outcome i.sex i.group
#' # . margins sex
#' m2 <- glm(outcome ~ sex + group, binomial(), data = margex)
#' prediction(m2, at = list(sex = 0:1))
#'
#' # Average response versus response at average
#' # . margins sex
#' prediction(m2, at = list(sex = c("male", "female")))
#' # . margins sex, atmeans
#' ## TODO
#'
#' # Multiple margins from one margins command
#' # . margins sex group
#' prediction(m2, at = list(sex = c("male", "female")))
#' prediction(m2, at = list(group = c("1", "2", "3")))
#'
#' # Margins with interaction terms
#' # . logistic outcome i.sex i.group sex#group
#' # . margins sex group
#' m3 <- glm(outcome ~ sex * group, binomial(), data = margex)
#' prediction(m3, at = list(sex = c("male", "female")))
#' prediction(m3, at = list(group = c("1", "2", "3")))
#'
#' # Margins with continuous variables
#' # . logistic outcome i.sex i.group sex#group age
#' # . margins sex group
#' m4 <- glm(outcome ~ sex * group + age, binomial(), data = margex)
#' prediction(m4, at = list(sex = c("male", "female")))
#' prediction(m4, at = list(group = c("1", "2", "3")))
#'
#' # Margins of continuous variables
#' # . margins, at(age=40)
#' prediction(m4, at = list(age = 40))
#' # . margins, at(age=(30 35 40 45 50))
#' prediction(m4, at = list(age = c(30, 35, 40, 45, 50)))
#'
#' # Margins of interactions
#' # . margins sex#group
#' prediction(m4, at = list(sex = c("male", "female"), group = c("1", "2", "3")))
#'
#' }
#' @seealso \code{\link{prediction}}
"margex"
@@ -0,0 +1,27 @@
# build script for 'margex' dataset

# load packages
requireNamespace("margex")
requireNamespace("rio")

# load data
webuse::webuse("margex")

# factorize
margex$sex <- rio::factorize(margex$sex)
margex$group <- factor(margex$group)
margex$agegroup <- rio::factorize(margex$agegroup)
margex$treatment <- factor(margex$treatment)
margex$arm <- factor(margex$arm)

# drop attributes
attr(margex$y, "format.stata") <- NULL
attr(margex$outcome, "format.stata") <- NULL
attr(margex$age, "format.stata") <- NULL
attr(margex$distance, "format.stata") <- NULL
attr(margex$ycn, "format.stata") <- NULL
attr(margex$yc, "format.stata") <- NULL
attr(margex$arm, "format.stata") <- NULL

# overwrite
devtools::use_data(margex, overwrite = TRUE)
BIN +18.2 KB data/margex.rda
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 52c21fe

Please sign in to comment.