├── codecov.yml ├── LICENSE ├── tests ├── test-all.R └── testthat │ ├── test-gam.R │ ├── test-lm.R │ ├── test-augment.R │ ├── test-bootstrap.R │ ├── test-ivreg.R │ ├── test-rstanarm.R │ ├── test-dplyr.R │ ├── test-rowwise.R │ ├── test-lme4.R │ └── test-nlme.R ├── .gitignore ├── inst └── extdata │ ├── rstan_example.rda │ └── 8schools.stan ├── man-roxygen ├── boilerplate.R ├── coefficients.R └── augment_NAs.R ├── .Rbuildignore ├── .travis.yml ├── man ├── unrowname.Rd ├── compact.Rd ├── glance.Rd ├── tidy.NULL.Rd ├── insert_NAs.Rd ├── tidy.ts.Rd ├── tidy.Rd ├── inflate.Rd ├── fix_data_frame.Rd ├── tidy.ftable.Rd ├── tidy.spec.Rd ├── broom.Rd ├── tidy.density.Rd ├── sparse_tidiers.Rd ├── vector_tidiers.Rd ├── tidy.power.htest.Rd ├── process_rq.Rd ├── tidy.table.Rd ├── tidy.default.Rd ├── process_geeglm.Rd ├── process_ergm.Rd ├── process_lm.Rd ├── bootstrap.Rd ├── confint.geeglm.Rd ├── mle2_tidiers.Rd ├── confint_tidy.Rd ├── augment.Rd ├── list_tidiers.Rd ├── xyz_tidiers.Rd ├── tidy.coeftest.Rd ├── tidy.map.Rd ├── rlm_tidiers.Rd ├── augment_columns.Rd ├── summary_tidiers.Rd ├── finish_glance.Rd ├── tidy.dist.Rd ├── glm_tidiers.Rd ├── kde_tidiers.Rd ├── gamlss_tidiers.Rd ├── acf_tidiers.Rd ├── matrix_tidiers.Rd ├── tidy.manova.Rd ├── kappa_tidiers.Rd ├── binWidth_tidiers.Rd ├── auc_tidiers.Rd ├── fitdistr_tidiers.Rd ├── sp_tidiers.Rd ├── aareg_tidiers.Rd ├── sexpfit_tidiers.Rd ├── optim_tidiers.Rd ├── tidy.TukeyHSD.Rd ├── tidy.pairwise.htest.Rd ├── smooth.spline_tidiers.Rd ├── anova_tidiers.Rd ├── multcomp_tidiers.Rd ├── zoo_tidiers.Rd ├── binDesign_tidiers.Rd ├── orcutt_tidiers.Rd ├── rcorr_tidiers.Rd ├── Arima_tidiers.Rd ├── htest_tidiers.Rd ├── robust_tidiers.Rd ├── boot_tidiers.Rd ├── gam_tidiers.Rd ├── loess_tidiers.Rd ├── ridgelm_tidiers.Rd ├── pyears_tidiers.Rd ├── data.frame_tidiers.Rd ├── plm_tidiers.Rd ├── svd_tidiers.Rd ├── kmeans_tidiers.Rd ├── btergm_tidiers.Rd ├── lmodel2_tidiers.Rd ├── geeglm_tidiers.Rd ├── multinom_tidiers.Rd ├── biglm_tidiers.Rd ├── betareg_tidiers.Rd ├── glmnet_tidiers.Rd ├── rowwise_df_tidiers.Rd ├── rf_tidiers.Rd ├── mcmc_tidiers.Rd ├── mclust_tidiers.Rd ├── ivreg_tidiers.Rd └── cv.glmnet_tidiers.Rd ├── broom.Rproj ├── R ├── globals.R ├── glance.R ├── broom.R ├── xyz_tidiers.R ├── augment.R ├── lmtest_tidiers.R ├── mle2_tidiers.R ├── sparse_tidiers.R ├── vector_tidiers.R ├── rlm_tidiers.R ├── map_tidiers.R ├── summary_tidiers.R ├── glm_tidiers.R ├── matrix_tidiers.R ├── loess_tidiers.R ├── auc_tidiers.R ├── fitdistr_tidiers.R ├── kde_tidiers.R ├── psych_tidiers.R ├── tidy.R ├── zoo_tidiers.R ├── extras.R ├── optim_tidiers.R ├── smooth.spline_tidiers.R ├── gamlss_tidiers.R ├── orcutt_tidiers.R ├── rcorr_tidiers.R ├── list_tidiers.R ├── arima_tidiers.R ├── robust_tidiers.R ├── multcomp_tidiers.R ├── data.frame_tidiers.R ├── plm_tidiers.R └── ridgelm_tidiers.R ├── appveyor.yml ├── cran-comments.md └── CONDUCT.md /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2015 2 | COPYRIGHT HOLDER: David Robinson 3 | -------------------------------------------------------------------------------- /tests/test-all.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | test_check("broom") 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | vignettes/*cache 5 | inst/doc 6 | revdep 7 | -------------------------------------------------------------------------------- /inst/extdata/rstan_example.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjskay/broom/master/inst/extdata/rstan_example.rda -------------------------------------------------------------------------------- /man-roxygen/boilerplate.R: -------------------------------------------------------------------------------- 1 | #' @return All tidying methods return a data.frame without rownames, whose 2 | #' structure depends on the method chosen. 3 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^NEWS\.md$ 4 | ^man-roxygen$ 5 | ^revdep$ 6 | ^cran-comments\.md$ 7 | ^README.Rmd 8 | ^\.travis\.yml$ 9 | ^appveyor\.yml$ 10 | ^CONDUCT\.md$ 11 | ^codecov\.yml$ 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Sample .travis.yml for R projects 2 | 3 | language: r 4 | sudo: false 5 | cache: packages 6 | 7 | before_install: 8 | - Rscript -e 'update.packages(ask = FALSE)' 9 | 10 | after_success: 11 | - Rscript -e 'covr::codecov()' 12 | -------------------------------------------------------------------------------- /man/unrowname.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{unrowname} 4 | \alias{unrowname} 5 | \title{strip rownames from an object} 6 | \usage{ 7 | unrowname(x) 8 | } 9 | \arguments{ 10 | \item{x}{a data frame} 11 | } 12 | \description{ 13 | strip rownames from an object 14 | } 15 | 16 | -------------------------------------------------------------------------------- /man/compact.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{compact} 4 | \alias{compact} 5 | \title{Remove NULL items in a vector or list} 6 | \usage{ 7 | compact(x) 8 | } 9 | \arguments{ 10 | \item{x}{a vector or list} 11 | } 12 | \description{ 13 | Remove NULL items in a vector or list 14 | } 15 | 16 | -------------------------------------------------------------------------------- /man-roxygen/coefficients.R: -------------------------------------------------------------------------------- 1 | #' @return \code{tidy} returns a data.frame with one row for each term 2 | #' \item{term}{name of term} 3 | #' \item{estimate}{estimate of coefficient} 4 | #' \item{stderror}{standard error} 5 | #' \item{statistic}{Z statistic} 6 | #' \item{p.value}{p-value} 7 | #' \item{conf.low}{low end of confidence interval} 8 | #' \item{conf.high}{high end of confidence interval} 9 | -------------------------------------------------------------------------------- /broom.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | 17 | BuildType: Package 18 | PackageUseDevtools: Yes 19 | PackageInstallArgs: --no-multiarch --with-keep.source 20 | PackageRoxygenize: rd,collate,namespace 21 | -------------------------------------------------------------------------------- /inst/extdata/8schools.stan: -------------------------------------------------------------------------------- 1 | data { 2 | int J; // number of schools 3 | real y[J]; // estimated treatment effects 4 | real sigma[J]; // s.e. of effect estimates 5 | } 6 | parameters { 7 | real mu; 8 | real tau; 9 | real eta[J]; 10 | } 11 | transformed parameters { 12 | real theta[J]; 13 | for (j in 1:J) 14 | theta[j] <- mu + tau * eta[j]; 15 | } 16 | model { 17 | eta ~ normal(0, 1); 18 | y ~ normal(theta, sigma); 19 | } 20 | -------------------------------------------------------------------------------- /man/glance.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/glance.R 3 | \name{glance} 4 | \alias{glance} 5 | \title{Construct a single row summary "glance" of a model, fit, or other 6 | object} 7 | \usage{ 8 | glance(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{model or other R object to convert to single-row data frame} 12 | 13 | \item{...}{other arguments passed to methods} 14 | } 15 | \description{ 16 | glance methods always return either a one-row data frame, or NULL 17 | } 18 | 19 | -------------------------------------------------------------------------------- /man/tidy.NULL.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tidy.R 3 | \name{tidy.NULL} 4 | \alias{tidy.NULL} 5 | \title{tidy on a NULL input} 6 | \usage{ 7 | \method{tidy}{NULL}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{A value NULL} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | An empty data.frame 16 | } 17 | \description{ 18 | tidy on a NULL input returns an empty data frame, which means it can be 19 | combined with other data frames (treated as "empty") 20 | } 21 | 22 | -------------------------------------------------------------------------------- /man/insert_NAs.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{insert_NAs} 4 | \alias{insert_NAs} 5 | \title{insert a row of NAs into a data frame wherever another data frame has NAs} 6 | \usage{ 7 | insert_NAs(x, original) 8 | } 9 | \arguments{ 10 | \item{x}{data frame that has one row for each non-NA row in original} 11 | 12 | \item{original}{data frame with NAs} 13 | } 14 | \description{ 15 | insert a row of NAs into a data frame wherever another data frame has NAs 16 | } 17 | 18 | -------------------------------------------------------------------------------- /man/tidy.ts.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.ts} 4 | \alias{tidy.ts} 5 | \title{tidy a ts timeseries object} 6 | \usage{ 7 | \method{tidy}{ts}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a "ts" object} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | a tidy data frame 16 | } 17 | \description{ 18 | Turn a ts object into a tidy data frame. Right now simply uses 19 | \code{as.data.frame.ts}. 20 | } 21 | \seealso{ 22 | \link{as.data.frame.ts} 23 | } 24 | 25 | -------------------------------------------------------------------------------- /R/globals.R: -------------------------------------------------------------------------------- 1 | globalVariables(c(".", "estimate", "std.error", "statistic", "term", "p.value", 2 | "effect", "se", "objs", "comp", "N", "lambda", "GCV", "obs", 3 | ".id", "level", "group1", "group2", "series", "value", 4 | "index", "df.residual", "stratum", "conf.low", "conf.high", 5 | "step", "fit", "type", "expCIWidth", "column1", "column2", 6 | "PC", "loading", "column", "comparison", "item1", "item2", 7 | "Method", "Intercept", "Slope", "method", "key", "Var1", 8 | "Var2", "variable")) 9 | -------------------------------------------------------------------------------- /R/glance.R: -------------------------------------------------------------------------------- 1 | #' Construct a single row summary "glance" of a model, fit, or other 2 | #' object 3 | #' 4 | #' glance methods always return either a one-row data frame, or NULL 5 | #' 6 | #' @param x model or other R object to convert to single-row data frame 7 | #' @param ... other arguments passed to methods 8 | #' @export 9 | glance <- function(x, ...) UseMethod("glance") 10 | 11 | #' @export 12 | glance.NULL <- function(x, ...) NULL 13 | 14 | #' @export 15 | glance.default <- function(x, ...) { 16 | stop("glance doesn't know how to deal with data of class ", class(x), call. = FALSE) 17 | } 18 | -------------------------------------------------------------------------------- /man/tidy.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tidy.R 3 | \name{tidy} 4 | \alias{tidy} 5 | \title{Tidy the result of a test into a summary data.frame} 6 | \usage{ 7 | tidy(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{An object to be converted into a tidy data.frame} 11 | 12 | \item{...}{extra arguments} 13 | } 14 | \value{ 15 | a data.frame 16 | } 17 | \description{ 18 | The output of tidy is always a data.frame with disposable row names. It is 19 | therefore suited for further manipulation by packages like dplyr, reshape2, 20 | ggplot2 and ggvis. 21 | } 22 | 23 | -------------------------------------------------------------------------------- /man-roxygen/augment_NAs.R: -------------------------------------------------------------------------------- 1 | #' @details When the modeling was performed with \code{na.action = "na.omit"} 2 | #' (as is the typical default), rows with NA in the initial data are omitted 3 | #' entirely from the augmented data frame. When the modeling was performed 4 | #' with \code{na.action = "na.exclude"}, one should provide the original data 5 | #' as a second argument, at which point the augmented data will contain those 6 | #' rows (typically with NAs in place of the new columns). If the original data 7 | #' is not provided to \code{augment} and \code{na.action = "na.exclude"}, a 8 | #' warning is raised and the incomplete rows are dropped. 9 | #' 10 | #' @seealso \link{na.action} 11 | -------------------------------------------------------------------------------- /man/inflate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{inflate} 4 | \alias{inflate} 5 | \title{Expand a dataset to include all factorial combinations of one or more 6 | variables} 7 | \usage{ 8 | inflate(.data, ..., stringsAsFactors = FALSE) 9 | } 10 | \arguments{ 11 | \item{.data}{a tbl} 12 | 13 | \item{...}{arguments} 14 | 15 | \item{stringsAsFactors}{logical specifying if character vectors are 16 | converted to factors.} 17 | } 18 | \value{ 19 | A tbl, grouped by the arguments in \code{...} 20 | } 21 | \description{ 22 | Expand a dataset to include all factorial combinations of one or more 23 | variables 24 | } 25 | 26 | -------------------------------------------------------------------------------- /man/fix_data_frame.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{fix_data_frame} 4 | \alias{fix_data_frame} 5 | \title{Ensure an object is a data frame, with rownames moved into a column} 6 | \usage{ 7 | fix_data_frame(x, newnames = NULL, newcol = "term") 8 | } 9 | \arguments{ 10 | \item{x}{a data.frame or matrix} 11 | 12 | \item{newnames}{new column names, not including the rownames} 13 | 14 | \item{newcol}{the name of the new rownames column} 15 | } 16 | \value{ 17 | a data.frame, with rownames moved into a column and new column 18 | names assigned 19 | } 20 | \description{ 21 | Ensure an object is a data frame, with rownames moved into a column 22 | } 23 | 24 | -------------------------------------------------------------------------------- /man/tidy.ftable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.ftable} 4 | \alias{tidy.ftable} 5 | \title{tidy an ftable object} 6 | \usage{ 7 | \method{tidy}{ftable}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{An object of class "ftable"} 11 | 12 | \item{...}{Extra arguments (not used)} 13 | } 14 | \description{ 15 | An ftable contains a "flat" contingency table. This melts it into a 16 | data.frame with one column for each variable, then a \code{Freq} 17 | column. It directly uses the \code{stats:::as.data.frame.ftable} function 18 | } 19 | \examples{ 20 | 21 | tidy(ftable(Titanic, row.vars = 1:3)) 22 | 23 | } 24 | \seealso{ 25 | \code{\link{ftable}} 26 | } 27 | 28 | -------------------------------------------------------------------------------- /man/tidy.spec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.spec} 4 | \alias{tidy.spec} 5 | \title{tidy a spec objet} 6 | \usage{ 7 | \method{tidy}{spec}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{an object of class "spec"} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | a data frame with "freq" and "spec" columns 16 | } 17 | \description{ 18 | Given a "spec" object, which shows a spectrum across a range of frequencies, 19 | returns a tidy data frame with two columns: "freq" and "spec" 20 | } 21 | \examples{ 22 | 23 | spc <- spectrum(lh) 24 | tidy(spc) 25 | 26 | library(ggplot2) 27 | ggplot(tidy(spc), aes(freq, spec)) + geom_line() 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /man/broom.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/broom.R 3 | \docType{package} 4 | \name{broom} 5 | \alias{broom} 6 | \alias{broom-package} 7 | \title{Convert Statistical Analysis Objects into Tidy Data Frames} 8 | \description{ 9 | Convert statistical analysis objects from R into tidy data frames, 10 | so that they can more easily be combined, reshaped and otherwise processed 11 | with tools like dplyr, tidyr and ggplot2. The package provides three S3 12 | generics: tidy, which summarizes a model's statistical findings such as 13 | coefficients of a regression; augment, which adds columns to the original 14 | data such as predictions, residuals and cluster assignments; and glance, 15 | which provides a one-row summary of model-level statistics. 16 | } 17 | 18 | -------------------------------------------------------------------------------- /man/tidy.density.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.density} 4 | \alias{tidy.density} 5 | \title{tidy a density objet} 6 | \usage{ 7 | \method{tidy}{density}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{an object of class "density"} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | a data frame with "x" and "y" columns 16 | 17 | d <- density(faithful$eruptions, bw = "sj") 18 | head(tidy(d)) 19 | 20 | library(ggplot2) 21 | ggplot(tidy(d), aes(x, y)) + geom_line() 22 | } 23 | \description{ 24 | Given a "density" object, returns a tidy data frame with two 25 | columns: points x where the density is estimated, points y 26 | for the estimate 27 | } 28 | \seealso{ 29 | \code{\link{density}} 30 | } 31 | 32 | -------------------------------------------------------------------------------- /man/sparse_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sparse_tidiers.R 3 | \name{sparse_tidiers} 4 | \alias{sparse_tidiers} 5 | \alias{tidy.dgCMatrix} 6 | \alias{tidy.dgTMatrix} 7 | \alias{tidy.sparseMatrix} 8 | \title{Tidy a sparseMatrix object from the Matrix package} 9 | \usage{ 10 | \method{tidy}{dgTMatrix}(x, ...) 11 | 12 | \method{tidy}{dgCMatrix}(x, ...) 13 | 14 | \method{tidy}{sparseMatrix}(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{A Matrix object} 18 | 19 | \item{...}{Extra arguments, not used} 20 | } 21 | \description{ 22 | Tidy a sparseMatrix object from the Matrix package into 23 | a three-column data frame, row, column, and value (with 24 | zeros missing). If there are row names or column names, 25 | use those, otherwise use indices 26 | } 27 | 28 | -------------------------------------------------------------------------------- /tests/testthat/test-gam.R: -------------------------------------------------------------------------------- 1 | if (requireNamespace("gam")) { 2 | context("gam models from package gam") 3 | data(kyphosis, package = "gam") 4 | g <- gam::gam(Kyphosis ~ gam::s(Age,4) + Number, family = binomial, data = kyphosis) 5 | test_that("tidy works on gam models", { 6 | tidy(g) 7 | }) 8 | test_that("glance works on gam models", { 9 | glance(g) 10 | }) 11 | } 12 | 13 | if (requireNamespace("mgcv")) { 14 | context("gam models from package mgcv") 15 | d <- as.data.frame(ChickWeight) 16 | g <- mgcv::gam(weight ~ s(Time) + factor(Diet), data = d) 17 | test_that("tidy works on mgcv::gam models", { 18 | tidy(g) 19 | tidy(g, parametric = TRUE) 20 | }) 21 | test_that("glance works on mgcv::gam models", { 22 | glance(g) 23 | }) 24 | } 25 | 26 | -------------------------------------------------------------------------------- /man/vector_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vector_tidiers.R 3 | \name{tidy.numeric} 4 | \alias{tidy.character} 5 | \alias{tidy.logical} 6 | \alias{tidy.numeric} 7 | \title{Tidy atomic vectors} 8 | \usage{ 9 | \method{tidy}{numeric}(x, ...) 10 | 11 | \method{tidy}{character}(x, ...) 12 | 13 | \method{tidy}{logical}(x, ...) 14 | } 15 | \arguments{ 16 | \item{x}{An object of class "numeric", "integer", "character", or "logical". 17 | Most likely a named vector} 18 | 19 | \item{...}{Extra arguments (not used)} 20 | } 21 | \description{ 22 | Turn atomic vectors into data frames, where the names of the vector (if they 23 | exist) are a column and the values of the vector are a column. 24 | } 25 | \examples{ 26 | 27 | x <- 1:5 28 | names(x) <- letters[1:5] 29 | tidy(x) 30 | } 31 | 32 | -------------------------------------------------------------------------------- /man/tidy.power.htest.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.power.htest} 4 | \alias{tidy.power.htest} 5 | \title{tidy a power.htest} 6 | \usage{ 7 | \method{tidy}{power.htest}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a power.htest object} 11 | 12 | \item{...}{extra arguments, not used} 13 | } 14 | \value{ 15 | A data frame with one row per parameter passed in, with 16 | columns \code{n}, \code{delta}, \code{sd}, \code{sig.level}, and 17 | \code{power} (from the \code{power.htest} object). 18 | } 19 | \description{ 20 | tidy a power.htest 21 | } 22 | \examples{ 23 | 24 | ptt <- power.t.test(n = 2:30, delta = 1) 25 | tidy(ptt) 26 | 27 | library(ggplot2) 28 | ggplot(tidy(ptt), aes(n, power)) + geom_line() 29 | 30 | } 31 | \seealso{ 32 | \link{power.t.test} 33 | } 34 | 35 | -------------------------------------------------------------------------------- /man/process_rq.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rq_tidiers.R 3 | \name{process_rq} 4 | \alias{process_rq} 5 | \title{Helper function for tidy.rq and tidy.rqs} 6 | \usage{ 7 | process_rq(rq_obj, se.type = "rank", conf.int = TRUE, conf.level = 0.95, 8 | ...) 9 | } 10 | \arguments{ 11 | \item{rq_obj}{an object returned by \code{summary.rq} or \code{summary.rqs}} 12 | 13 | \item{se.type}{type of standard errors used in \code{summary.rq} or \code{summary.rqs}} 14 | 15 | \item{conf.int}{whether to include a confidence interval} 16 | 17 | \item{conf.level}{confidence level for confidence interval} 18 | 19 | \item{\dots}{currently unused} 20 | } 21 | \description{ 22 | See documentation for \code{summary.rq} for complete description 23 | of the options for \code{se.type}, \code{conf.int}, etc. 24 | } 25 | 26 | -------------------------------------------------------------------------------- /man/tidy.table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.table} 4 | \alias{tidy.table} 5 | \title{tidy a table object} 6 | \usage{ 7 | \method{tidy}{table}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{An object of class "table"} 11 | 12 | \item{...}{Extra arguments (not used)} 13 | } 14 | \description{ 15 | A table, typically created by the \link{table} function, contains a 16 | contingency table of frequencies across multiple vectors. This directly 17 | calls the \code{\link{as.data.frame.table}} method, which melts it into a 18 | data frame with one column for each variable and a \code{Freq} column. 19 | } 20 | \examples{ 21 | 22 | tab <- with(airquality, table(cut(Temp, quantile(Temp)), Month)) 23 | tidy(tab) 24 | 25 | } 26 | \seealso{ 27 | \code{\link{as.data.frame.table}} 28 | } 29 | 30 | -------------------------------------------------------------------------------- /man/tidy.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tidy.R 3 | \name{tidy.default} 4 | \alias{tidy.default} 5 | \title{Default tidying method} 6 | \usage{ 7 | \method{tidy}{default}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{an object to be tidied} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | A data frame, from \code{as.data.frame} applied to the input x. 16 | } 17 | \description{ 18 | By default, tidy uses \code{as.data.frame} to convert its output. This is 19 | dangerous, as it may fail with an uninformative error message. 20 | Generally tidy is intended to be used on structured model objects 21 | such as lm or htest for which a specific S3 object exists. 22 | } 23 | \details{ 24 | If you know that you want to use \code{as.data.frame} on your untidy 25 | object, just use it directly. 26 | } 27 | 28 | -------------------------------------------------------------------------------- /man/process_geeglm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geeglm_tidiers.R 3 | \name{process_geeglm} 4 | \alias{process_geeglm} 5 | \title{helper function to process a tidied geeglm object} 6 | \usage{ 7 | process_geeglm(ret, x, conf.int = FALSE, conf.level = 0.95, 8 | exponentiate = FALSE) 9 | } 10 | \arguments{ 11 | \item{ret}{data frame with a tidied version of a coefficient matrix} 12 | 13 | \item{x}{a "geeglm" object} 14 | 15 | \item{conf.int}{whether to include a confidence interval} 16 | 17 | \item{conf.level}{confidence level of the interval, used only if 18 | \code{conf.int=TRUE}} 19 | 20 | \item{exponentiate}{whether to exponentiate the coefficient estimates 21 | and confidence intervals (typical for log distributions)} 22 | } 23 | \description{ 24 | Adds a confidence interval, and possibly exponentiates, a tidied 25 | object. 26 | } 27 | 28 | -------------------------------------------------------------------------------- /R/broom.R: -------------------------------------------------------------------------------- 1 | #' @title Convert Statistical Analysis Objects into Tidy Data Frames 2 | #' @name broom 3 | #' @description Convert statistical analysis objects from R into tidy data frames, 4 | #' so that they can more easily be combined, reshaped and otherwise processed 5 | #' with tools like dplyr, tidyr and ggplot2. The package provides three S3 6 | #' generics: tidy, which summarizes a model's statistical findings such as 7 | #' coefficients of a regression; augment, which adds columns to the original 8 | #' data such as predictions, residuals and cluster assignments; and glance, 9 | #' which provides a one-row summary of model-level statistics. 10 | #' 11 | #' @importFrom stats AIC coef confint fitted logLik model.frame na.omit 12 | #' @importFrom stats predict qnorm qt residuals setNames var 13 | #' 14 | #' @importFrom utils head 15 | #' 16 | #' @docType package 17 | #' @aliases broom broom-package 18 | NULL 19 | -------------------------------------------------------------------------------- /man/process_ergm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ergm_tidiers.R 3 | \name{process_ergm} 4 | \alias{process_ergm} 5 | \title{helper function to process a tidied ergm object} 6 | \usage{ 7 | process_ergm(ret, x, conf.int = FALSE, conf.level = 0.95, 8 | exponentiate = FALSE) 9 | } 10 | \arguments{ 11 | \item{ret}{data frame with a tidied version of a coefficient matrix} 12 | 13 | \item{x}{an "ergm" object} 14 | 15 | \item{conf.int}{whether to include a confidence interval} 16 | 17 | \item{conf.level}{confidence level of the interval, used only if 18 | \code{conf.int=TRUE}} 19 | 20 | \item{exponentiate}{whether to exponentiate the coefficient estimates 21 | and confidence intervals (typical for logistic regression)} 22 | } 23 | \description{ 24 | Optionally exponentiates the coefficients, and optionally adds 25 | a confidence interval, to a tidied ergm object. 26 | } 27 | 28 | -------------------------------------------------------------------------------- /man/process_lm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lm_tidiers.R 3 | \name{process_lm} 4 | \alias{process_lm} 5 | \title{helper function to process a tidied lm object} 6 | \usage{ 7 | process_lm(ret, x, conf.int = FALSE, conf.level = 0.95, 8 | exponentiate = FALSE) 9 | } 10 | \arguments{ 11 | \item{ret}{data frame with a tidied version of a coefficient matrix} 12 | 13 | \item{x}{an "lm", "glm", "biglm", or "bigglm" object} 14 | 15 | \item{conf.int}{whether to include a confidence interval} 16 | 17 | \item{conf.level}{confidence level of the interval, used only if 18 | \code{conf.int=TRUE}} 19 | 20 | \item{exponentiate}{whether to exponentiate the coefficient estimates 21 | and confidence intervals (typical for logistic regression)} 22 | } 23 | \description{ 24 | Adds a confidence interval, and possibly exponentiates, a tidied 25 | object. Useful for operations shared between lm and biglm. 26 | } 27 | 28 | -------------------------------------------------------------------------------- /man/bootstrap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extras.R 3 | \name{bootstrap} 4 | \alias{bootstrap} 5 | \title{Set up bootstrap replicates of a dplyr operation} 6 | \usage{ 7 | bootstrap(df, m, by_group = FALSE) 8 | } 9 | \arguments{ 10 | \item{df}{a data frame} 11 | 12 | \item{m}{number of bootstrap replicates to perform} 13 | 14 | \item{by_group}{If \code{TRUE}, then bootstrap within each group if \code{df} is 15 | a grouped tbl.} 16 | } 17 | \description{ 18 | Set up bootstrap replicates of a dplyr operation 19 | } 20 | \details{ 21 | This code originates from Hadley Wickham (with a few small 22 | corrections) here: 23 | 24 | https://github.com/hadley/dplyr/issues/269 25 | 26 | Some examples can be found at 27 | 28 | https://github.com/dgrtwo/broom/blob/master/vignettes/bootstrapping.Rmd 29 | } 30 | \examples{ 31 | 32 | library(dplyr) 33 | mtcars \%>\% bootstrap(10) \%>\% do(tidy(lm(mpg ~ wt, .))) 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /man/confint.geeglm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geeglm_tidiers.R 3 | \name{confint.geeglm} 4 | \alias{confint.geeglm} 5 | \title{Confidence interval for \code{geeglm} objects} 6 | \usage{ 7 | \method{confint}{geeglm}(object, parm, level = 0.95, ...) 8 | } 9 | \arguments{ 10 | \item{object}{The 'geeglm' object} 11 | 12 | \item{parm}{The parameter to calculate the confidence interval 13 | for. If not specified, the default is to calculate a confidence 14 | interval on all parameters (all variables in the model).} 15 | 16 | \item{level}{confidence level of the interval, used only if 17 | \code{conf.int=TRUE}} 18 | 19 | \item{...}{Additional parameters} 20 | } 21 | \value{ 22 | Returns the upper and lower confidence intervals 23 | } 24 | \description{ 25 | Generate confidence intervals for GEE analyses 26 | } 27 | \details{ 28 | This function was taken from 29 | http://stackoverflow.com/a/21221995/2632184. 30 | } 31 | 32 | -------------------------------------------------------------------------------- /man/mle2_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mle2_tidiers.R 3 | \name{mle2_tidiers} 4 | \alias{mle2_tidiers} 5 | \alias{tidy.mle2} 6 | \title{Tidy mle2 maximum likelihood objects} 7 | \usage{ 8 | \method{tidy}{mle2}(x, conf.int = FALSE, conf.level = 0.95, ...) 9 | } 10 | \arguments{ 11 | \item{x}{An "mle2" object} 12 | 13 | \item{conf.int}{Whether to add \code{conf.low} and \code{conf.high} columns} 14 | 15 | \item{conf.level}{Confidence level to use for interval} 16 | 17 | \item{...}{Extra arguments, not used} 18 | } 19 | \description{ 20 | Tidy mle2 objects from the bbmle package. 21 | } 22 | \examples{ 23 | 24 | if (require("bbmle", quietly = TRUE)) { 25 | x <- 0:10 26 | y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8) 27 | d <- data.frame(x,y) 28 | 29 | fit <- mle2(y ~ dpois(lambda = ymean), 30 | start = list(ymean = mean(y)), data = d) 31 | 32 | tidy(fit) 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /man/confint_tidy.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{confint_tidy} 4 | \alias{confint_tidy} 5 | \title{Calculate confidence interval as a tidy data frame} 6 | \usage{ 7 | confint_tidy(x, conf.level = 0.95, func = stats::confint, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a model object for which \code{\link{confint}} can be calculated} 11 | 12 | \item{conf.level}{confidence level} 13 | 14 | \item{func}{Function to use for computing confint} 15 | 16 | \item{...}{extra arguments passed on to \code{confint}} 17 | } 18 | \value{ 19 | A data frame with two columns: \code{conf.low} and \code{conf.high}. 20 | } 21 | \description{ 22 | Return a confidence interval as a tidy data frame. This directly wraps the 23 | \code{\link{confint}} function, but ensures it folllows broom conventions: 24 | column names of \code{conf.low} and \code{conf.high}, and no row names 25 | } 26 | \seealso{ 27 | \link{confint} 28 | } 29 | 30 | -------------------------------------------------------------------------------- /man/augment.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/augment.R 3 | \name{augment} 4 | \alias{augment} 5 | \title{Augment data according to a tidied model} 6 | \usage{ 7 | augment(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{model or other R object to convert to data frame} 11 | 12 | \item{...}{other arguments passed to methods} 13 | } 14 | \description{ 15 | Given an R statistical model or other non-tidy object, add columns to the 16 | original dataset such as predictions, residuals and cluster assignments. 17 | } 18 | \details{ 19 | Note that by convention the first argument is almost always \code{data}, 20 | which specifies the original data object. This is not part of the S3 21 | signature, partly because it prevents \link{rowwise_df_tidiers} from 22 | taking a column name as the first argument. 23 | 24 | This generic originated in the ggplot2 package, where it was called 25 | "fortify." 26 | } 27 | \seealso{ 28 | \code{\link{augment.lm}} 29 | } 30 | 31 | -------------------------------------------------------------------------------- /man/list_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/list_tidiers.R 3 | \name{list_tidiers} 4 | \alias{glance.list} 5 | \alias{list_tidiers} 6 | \alias{tidy.list} 7 | \title{Tidiers for return values from functions that aren't S3 objects} 8 | \usage{ 9 | \method{tidy}{list}(x, ...) 10 | 11 | \method{glance}{list}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{list object} 15 | 16 | \item{...}{extra arguments, passed to the tidying function} 17 | } 18 | \description{ 19 | This method handles the return values of functions that return lists 20 | rather than S3 objects, such as \code{optim}, \code{svd}, or 21 | \code{\link[akima]{interp}}, and therefore cannot be handled by 22 | S3 dispatch. 23 | } 24 | \details{ 25 | Those tiders themselves are implemented as functions of the 26 | form tidy_ or glance_ that are not exported. 27 | } 28 | \seealso{ 29 | \link{optim_tidiers}, \link{xyz_tidiers}, 30 | \link{svd_tidiers}, \link{orcutt_tidiers} 31 | } 32 | 33 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # DO NOT CHANGE the "init" and "install" sections below 2 | 3 | # Download script file from GitHub 4 | init: 5 | ps: | 6 | $ErrorActionPreference = "Stop" 7 | Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1" 8 | Import-Module '..\appveyor-tool.ps1' 9 | 10 | install: 11 | ps: Bootstrap 12 | 13 | # Adapt as necessary starting from here 14 | 15 | build_script: 16 | - travis-tool.sh install_deps 17 | 18 | test_script: 19 | - travis-tool.sh run_tests 20 | 21 | on_failure: 22 | - 7z a failure.zip *.Rcheck\* 23 | - appveyor PushArtifact failure.zip 24 | 25 | artifacts: 26 | - path: '*.Rcheck\**\*.log' 27 | name: Logs 28 | 29 | - path: '*.Rcheck\**\*.out' 30 | name: Logs 31 | 32 | - path: '*.Rcheck\**\*.fail' 33 | name: Logs 34 | 35 | - path: '*.Rcheck\**\*.Rout' 36 | name: Logs 37 | 38 | - path: '\*_*.tar.gz' 39 | name: Bits 40 | 41 | - path: '\*_*.zip' 42 | name: Bits 43 | -------------------------------------------------------------------------------- /R/xyz_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for x, y, z lists suitable for persp, image, etc. 2 | #' 3 | #' Tidies lists with components x, y (vector of coordinates) and z (matrix of 4 | #' values) which are typically used by functions such as 5 | #' \code{\link[graphics]{persp}} or \code{\link[graphics]{image}} and returned 6 | #' by interpolation functions such as \code{\link[akima]{interp}}. 7 | #' 8 | #' @param x list with components x, y and z 9 | #' @param ... extra arguments 10 | #' 11 | #' @template boilerplate 12 | #' 13 | #' @return \code{tidy} returns a data frame with columns x, y and z and one row 14 | #' per value in matrix z. 15 | #' 16 | #' @examples 17 | #' 18 | #' A <- list(x=1:5, y=1:3, z=matrix(runif(5*3), nrow=5)) 19 | #' image(A) 20 | #' tidy(A) 21 | #' 22 | #' @name xyz_tidiers 23 | #' @importFrom reshape2 melt 24 | tidy_xyz <- function(x, ...) { 25 | # convert to data.frame 26 | d <- melt(x$z) 27 | names(d) <- c("x", "y", "z") 28 | # get coordinates 29 | d$x <- x$x[d$x] 30 | d$y <- x$y[d$y] 31 | return(d) 32 | } 33 | -------------------------------------------------------------------------------- /man/xyz_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/xyz_tidiers.R 3 | \name{xyz_tidiers} 4 | \alias{tidy_xyz} 5 | \alias{xyz_tidiers} 6 | \title{Tidiers for x, y, z lists suitable for persp, image, etc.} 7 | \usage{ 8 | tidy_xyz(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{list with components x, y and z} 12 | 13 | \item{...}{extra arguments} 14 | } 15 | \value{ 16 | All tidying methods return a data.frame without rownames, whose 17 | structure depends on the method chosen. 18 | 19 | \code{tidy} returns a data frame with columns x, y and z and one row 20 | per value in matrix z. 21 | } 22 | \description{ 23 | Tidies lists with components x, y (vector of coordinates) and z (matrix of 24 | values) which are typically used by functions such as 25 | \code{\link[graphics]{persp}} or \code{\link[graphics]{image}} and returned 26 | by interpolation functions such as \code{\link[akima]{interp}}. 27 | } 28 | \examples{ 29 | 30 | A <- list(x=1:5, y=1:3, z=matrix(runif(5*3), nrow=5)) 31 | image(A) 32 | tidy(A) 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /R/augment.R: -------------------------------------------------------------------------------- 1 | #' Augment data according to a tidied model 2 | #' 3 | #' Given an R statistical model or other non-tidy object, add columns to the 4 | #' original dataset such as predictions, residuals and cluster assignments. 5 | #' 6 | #' Note that by convention the first argument is almost always \code{data}, 7 | #' which specifies the original data object. This is not part of the S3 8 | #' signature, partly because it prevents \link{rowwise_df_tidiers} from 9 | #' taking a column name as the first argument. 10 | #' 11 | #' @details This generic originated in the ggplot2 package, where it was called 12 | #' "fortify." 13 | #' 14 | #' @seealso \code{\link{augment.lm}} 15 | #' @param x model or other R object to convert to data frame 16 | #' @param ... other arguments passed to methods 17 | #' @export 18 | augment <- function(x, ...) UseMethod("augment") 19 | 20 | #' @export 21 | augment.NULL <- function(x, ...) NULL 22 | 23 | #' @export 24 | augment.default <- function(x, ...) { 25 | stop("augment doesn't know how to deal with data of class ", class(x), call. = FALSE) 26 | } 27 | -------------------------------------------------------------------------------- /man/tidy.coeftest.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lmtest_tidiers.R 3 | \name{tidy.coeftest} 4 | \alias{tidy.coeftest} 5 | \title{Tidying methods for coeftest objects} 6 | \usage{ 7 | \method{tidy}{coeftest}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{coeftest object} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | A \code{data.frame} with one row for each coefficient, with five columns: 16 | \item{term}{The term in the linear model being estimated and tested} 17 | \item{estimate}{The estimated coefficient} 18 | \item{std.error}{The standard error} 19 | \item{statistic}{test statistic} 20 | \item{p.value}{p-value} 21 | } 22 | \description{ 23 | This tidies the result of a coefficient test, from the \code{coeftest} 24 | function in the \code{lmtest} package. 25 | } 26 | \examples{ 27 | 28 | if (require("lmtest", quietly = TRUE)) { 29 | data(Mandible) 30 | fm <- lm(length ~ age, data=Mandible, subset=(age <= 28)) 31 | 32 | coeftest(fm) 33 | tidy(coeftest(fm)) 34 | } 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /R/lmtest_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for coeftest objects 2 | #' 3 | #' This tidies the result of a coefficient test, from the \code{coeftest} 4 | #' function in the \code{lmtest} package. 5 | #' 6 | #' @param x coeftest object 7 | #' @param ... extra arguments (not used) 8 | #' 9 | #' @return A \code{data.frame} with one row for each coefficient, with five columns: 10 | #' \item{term}{The term in the linear model being estimated and tested} 11 | #' \item{estimate}{The estimated coefficient} 12 | #' \item{std.error}{The standard error} 13 | #' \item{statistic}{test statistic} 14 | #' \item{p.value}{p-value} 15 | #' 16 | #' @examples 17 | #' 18 | #' if (require("lmtest", quietly = TRUE)) { 19 | #' data(Mandible) 20 | #' fm <- lm(length ~ age, data=Mandible, subset=(age <= 28)) 21 | #' 22 | #' coeftest(fm) 23 | #' tidy(coeftest(fm)) 24 | #' } 25 | #' 26 | #' @export 27 | tidy.coeftest <- function(x, ...) { 28 | co <- as.data.frame(unclass(x)) 29 | nn <- c("estimate", "std.error", "statistic", "p.value")[1:ncol(co)] 30 | ret <- fix_data_frame(co, nn) 31 | ret 32 | } 33 | -------------------------------------------------------------------------------- /R/mle2_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidy mle2 maximum likelihood objects 2 | #' 3 | #' Tidy mle2 objects from the bbmle package. 4 | #' 5 | #' @param x An "mle2" object 6 | #' @param conf.int Whether to add \code{conf.low} and \code{conf.high} columns 7 | #' @param conf.level Confidence level to use for interval 8 | #' @param ... Extra arguments, not used 9 | #' 10 | #' @examples 11 | #' 12 | #' if (require("bbmle", quietly = TRUE)) { 13 | #' x <- 0:10 14 | #' y <- c(26, 17, 13, 12, 20, 5, 9, 8, 5, 4, 8) 15 | #' d <- data.frame(x,y) 16 | #' 17 | #' fit <- mle2(y ~ dpois(lambda = ymean), 18 | #' start = list(ymean = mean(y)), data = d) 19 | #' 20 | #' tidy(fit) 21 | #' } 22 | #' 23 | #' @name mle2_tidiers 24 | #' 25 | #' @export 26 | tidy.mle2 <- function(x, conf.int = FALSE, conf.level = .95, ...) { 27 | co <- bbmle::coef(bbmle::summary(x)) 28 | nn <- c("estimate", "std.error", "statistic", "p.value") 29 | ret <- fix_data_frame(co, nn) 30 | 31 | if (conf.int) { 32 | CI <- confint_tidy(x, conf.level = conf.level, func = bbmle::confint) 33 | ret <- cbind(ret, CI) 34 | } 35 | ret 36 | } 37 | -------------------------------------------------------------------------------- /man/tidy.map.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/map_tidiers.R 3 | \name{tidy.map} 4 | \alias{tidy.map} 5 | \title{Tidy method for map objects.} 6 | \usage{ 7 | \method{tidy}{map}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{map object} 11 | 12 | \item{...}{not used by this method} 13 | } 14 | \description{ 15 | This function turns a map into a data frame. 16 | } 17 | \details{ 18 | This code and documentation originated in ggplot2, but was called "fortify." 19 | In broom, "fortify" became "augment", which is reserved for functions that *add* 20 | columns to existing data (based on a model fit, for example) so these functions 21 | were renamed as "tidy." 22 | } 23 | \examples{ 24 | if (require("maps") && require("ggplot2")) { 25 | ca <- map("county", "ca", plot = FALSE, fill = TRUE) 26 | head(tidy(ca)) 27 | qplot(long, lat, data = ca, geom = "polygon", group = group) 28 | 29 | tx <- map("county", "texas", plot = FALSE, fill = TRUE) 30 | head(tidy(tx)) 31 | qplot(long, lat, data = tx, geom = "polygon", group = group, 32 | colour = I("white")) 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /man/rlm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rlm_tidiers.R 3 | \name{rlm_tidiers} 4 | \alias{glance.rlm} 5 | \alias{rlm_tidiers} 6 | \title{Tidying methods for an rlm (robust linear model) object} 7 | \usage{ 8 | \method{glance}{rlm}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{rlm object} 12 | 13 | \item{...}{extra arguments (not used)} 14 | } 15 | \value{ 16 | \code{glance.rlm} returns a one-row data.frame with the columns 17 | \item{sigma}{The square root of the estimated residual variance} 18 | \item{converged}{whether the IWLS converged} 19 | \item{logLik}{the data's log-likelihood under the model} 20 | \item{AIC}{the Akaike Information Criterion} 21 | \item{BIC}{the Bayesian Information Criterion} 22 | \item{deviance}{deviance} 23 | } 24 | \description{ 25 | This method provides a glance of an "rlm" object. The \code{tidy} and 26 | \code{augment} methods are handled by \link{lm_tidiers}. 27 | } 28 | \examples{ 29 | 30 | library(MASS) 31 | 32 | r <- rlm(stack.loss ~ ., stackloss) 33 | tidy(r) 34 | augment(r) 35 | glance(r) 36 | 37 | } 38 | \seealso{ 39 | \link{lm_tidiers} 40 | } 41 | 42 | -------------------------------------------------------------------------------- /R/sparse_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidy a sparseMatrix object from the Matrix package 2 | #' 3 | #' Tidy a sparseMatrix object from the Matrix package into 4 | #' a three-column data frame, row, column, and value (with 5 | #' zeros missing). If there are row names or column names, 6 | #' use those, otherwise use indices 7 | #' 8 | #' @param x A Matrix object 9 | #' @param ... Extra arguments, not used 10 | #' 11 | #' @name sparse_tidiers 12 | #' 13 | #' @export 14 | tidy.dgTMatrix <- function(x, ...) { 15 | s <- Matrix::summary(x) 16 | 17 | row <- s$i 18 | if (!is.null(rownames(x))) { 19 | row <- rownames(x)[row] 20 | } 21 | col <- s$j 22 | if (!is.null(colnames(x))) { 23 | col <- colnames(x)[col] 24 | } 25 | 26 | ret <- data.frame(row = row, column = col, value = s$x, 27 | stringsAsFactors = FALSE) 28 | ret 29 | } 30 | 31 | 32 | #' @rdname sparse_tidiers 33 | #' @export 34 | tidy.dgCMatrix <- function(x, ...) { 35 | tidy(methods::as(x, "dgTMatrix")) 36 | } 37 | 38 | 39 | #' @rdname sparse_tidiers 40 | #' @export 41 | tidy.sparseMatrix <- function(x, ...) { 42 | tidy(methods::as(x, "dgTMatrix")) 43 | } 44 | -------------------------------------------------------------------------------- /R/vector_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidy atomic vectors 2 | #' 3 | #' Turn atomic vectors into data frames, where the names of the vector (if they 4 | #' exist) are a column and the values of the vector are a column. 5 | #' 6 | #' @param x An object of class "numeric", "integer", "character", or "logical". 7 | #' Most likely a named vector 8 | #' @param ... Extra arguments (not used) 9 | #' 10 | #' @examples 11 | #' 12 | #' x <- 1:5 13 | #' names(x) <- letters[1:5] 14 | #' tidy(x) 15 | #' @export 16 | #' @rdname vector_tidiers 17 | tidy.numeric <- function(x, ...) { 18 | if (!is.null(names(x))) 19 | dplyr::data_frame(names = names(x), x = unname(x)) 20 | else 21 | dplyr::data_frame(x = x) 22 | } 23 | 24 | #' @export 25 | #' @rdname vector_tidiers 26 | tidy.character <- function(x, ...) { 27 | if (!is.null(names(x))) 28 | dplyr::data_frame(names = names(x), x = unname(x)) 29 | else 30 | dplyr::data_frame(x = x) 31 | } 32 | 33 | #' @export 34 | #' @rdname vector_tidiers 35 | tidy.logical <- function(x, ...) { 36 | if (!is.null(names(x))) 37 | dplyr::data_frame(names = names(x), x = unname(x)) 38 | else 39 | dplyr::data_frame(x = x) 40 | } 41 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | broom 0.4.2 2 | ----------- 3 | 4 | This is a resubmission now that orcutt 2.0 is on CRAN, which returns a different type of object. 5 | 6 | Most important change is fixing behavior with the survival package update. My sincere apologies for taking so long to do so. 7 | 8 | ## Changes 9 | 10 | * Changed `tidy.glmnet` to filter out rows where estimate == 0. 11 | * Updates to `rstanarm` tidiers (thanks to #177 from Jonah Gabry) 12 | * Repo moved; pointing URLs towards tidyverse/broom rather than dgrtwo/broom 13 | 14 | ### New tidiers 15 | 16 | * Added tidiers for `lsmobj` and `ref.grid` objects from the lsmeans package 17 | * Added tidiers for `betareg` objects from the betareg package 18 | * Added tidiers for `lmRob` and `glmRob` objects from the robust package 19 | * Added tidiers for `brms` objects from the brms package (thanks to #149 from Paul Buerkner) 20 | 21 | ### Bug fixes 22 | 23 | * Fixed issue with survival package 2.40-1 (thanks to #180 from Marcus Walz) 24 | 25 | ## Test environments 26 | * local OS X install, R 3.3.0 27 | * ubuntu 12.04 (on travis-ci), R 3.3.0 28 | * win-builder (devel and release) 29 | 30 | ## R CMD check results 31 | There were no ERRORs, WARNINGs or NOTEs. 32 | -------------------------------------------------------------------------------- /tests/testthat/test-lm.R: -------------------------------------------------------------------------------- 1 | # test tidy, augment, glance from lm objects 2 | 3 | context("lm tidiers") 4 | 5 | test_that("tidy.lm works", { 6 | lmfit <- lm(mpg ~ wt, mtcars) 7 | td = tidy(lmfit) 8 | check_tidy(td, exp.row=2) 9 | expect_equal(td$term, c("(Intercept)", "wt")) 10 | 11 | lmfit2 <- lm(mpg ~ wt + disp, mtcars) 12 | td2 = tidy(lmfit2) 13 | check_tidy(td2, exp.row=3) 14 | expect_equal(td2$term, c("(Intercept)", "wt", "disp")) 15 | 16 | expect_warning(tidy(lmfit2, exponentiate = TRUE)) 17 | }) 18 | 19 | test_that("tidy.glm works", { 20 | glmfit <- glm(am ~ wt, mtcars, family="binomial") 21 | td = tidy(glmfit) 22 | check_tidy(td, exp.row=2, exp.col=5) 23 | expect_equal(td$term, c("(Intercept)", "wt")) 24 | # check exponentiation works 25 | check_tidy(tidy(glmfit, exponentiate = TRUE), exp.row=2, exp.col=5) 26 | 27 | glmfit2 <- glm(cyl ~ wt + disp, mtcars, family="poisson") 28 | td2 = tidy(glmfit2) 29 | check_tidy(td2, exp.row=3, exp.col=5) 30 | expect_equal(td2$term, c("(Intercept)", "wt", "disp")) 31 | # check exponentiation works 32 | check_tidy(tidy(glmfit2, exponentiate = TRUE), exp.row=3, exp.col=5) 33 | }) 34 | -------------------------------------------------------------------------------- /man/augment_columns.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{augment_columns} 4 | \alias{augment_columns} 5 | \title{add fitted values, residuals, and other common outputs to 6 | an augment call} 7 | \usage{ 8 | augment_columns(x, data, newdata, type, type.predict = type, 9 | type.residuals = type, se.fit = TRUE, ...) 10 | } 11 | \arguments{ 12 | \item{x}{a model} 13 | 14 | \item{data}{original data onto which columns should be added} 15 | 16 | \item{newdata}{new data to predict on, optional} 17 | 18 | \item{type}{Type of prediction and residuals to compute} 19 | 20 | \item{type.predict}{Type of prediction to compute; by default 21 | same as \code{type}} 22 | 23 | \item{type.residuals}{Type of residuals to compute; by default 24 | same as \code{type}} 25 | 26 | \item{se.fit}{Value to pass to predict's \code{se.fit}, or NULL for 27 | no value} 28 | 29 | \item{...}{extra arguments (not used)} 30 | } 31 | \description{ 32 | Add fitted values, residuals, and other common outputs to 33 | the value returned from \code{augment}. 34 | } 35 | \details{ 36 | In the case that a residuals or influence generic is not implemented for the 37 | model, fail quietly. 38 | } 39 | 40 | -------------------------------------------------------------------------------- /man/summary_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/summary_tidiers.R 3 | \name{summary_tidiers} 4 | \alias{glance.summaryDefault} 5 | \alias{summary_tidiers} 6 | \alias{tidy.summaryDefault} 7 | \title{Tidiers for summaryDefault objects} 8 | \usage{ 9 | \method{tidy}{summaryDefault}(x, ...) 10 | 11 | \method{glance}{summaryDefault}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{summaryDefault object} 15 | 16 | \item{...}{extra arguments, not used} 17 | } 18 | \value{ 19 | Both \code{tidy} and \code{glance} return the same object: 20 | a one-row data frame with columns 21 | \item{minimum}{smallest value in original vector} 22 | \item{q1}{value at the first quartile} 23 | \item{median}{median of original vector} 24 | \item{mean}{mean of original vector} 25 | \item{q3}{value at the third quartile} 26 | \item{maximum}{largest value in original vector} 27 | \item{NAs}{number of NA values (if any)} 28 | } 29 | \description{ 30 | Tidy a summary of a vector. 31 | } 32 | \examples{ 33 | 34 | v <- rnorm(1000) 35 | s <- summary(v) 36 | s 37 | 38 | tidy(s) 39 | glance(s) 40 | 41 | v2 <- c(v,NA) 42 | tidy(summary(v2)) 43 | 44 | } 45 | \seealso{ 46 | \code{\link{summary}} 47 | } 48 | 49 | -------------------------------------------------------------------------------- /R/rlm_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for an rlm (robust linear model) object 2 | #' 3 | #' This method provides a glance of an "rlm" object. The \code{tidy} and 4 | #' \code{augment} methods are handled by \link{lm_tidiers}. 5 | #' 6 | #' @param x rlm object 7 | #' @param ... extra arguments (not used) 8 | #' 9 | #' @return \code{glance.rlm} returns a one-row data.frame with the columns 10 | #' \item{sigma}{The square root of the estimated residual variance} 11 | #' \item{converged}{whether the IWLS converged} 12 | #' \item{logLik}{the data's log-likelihood under the model} 13 | #' \item{AIC}{the Akaike Information Criterion} 14 | #' \item{BIC}{the Bayesian Information Criterion} 15 | #' \item{deviance}{deviance} 16 | #' 17 | #' @name rlm_tidiers 18 | #' 19 | #' @examples 20 | #' 21 | #' library(MASS) 22 | #' 23 | #' r <- rlm(stack.loss ~ ., stackloss) 24 | #' tidy(r) 25 | #' augment(r) 26 | #' glance(r) 27 | #' 28 | #' @seealso \link{lm_tidiers} 29 | #' 30 | #' @export 31 | glance.rlm <- function(x, ...) { 32 | s <- summary(x) 33 | ret <- data.frame(sigma = s$sigma, converged = x$converged) 34 | ret <- finish_glance(ret, x) 35 | # remove df.residual, which is always set to NA in rlm objects 36 | dplyr::select(ret, -df.residual) 37 | } 38 | -------------------------------------------------------------------------------- /man/finish_glance.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utilities.R 3 | \name{finish_glance} 4 | \alias{finish_glance} 5 | \title{Add logLik, AIC, BIC, and other common measurements to a glance of 6 | a prediction} 7 | \usage{ 8 | finish_glance(ret, x) 9 | } 10 | \arguments{ 11 | \item{ret}{a one-row data frame (a partially complete glance)} 12 | 13 | \item{x}{the prediction model} 14 | } 15 | \value{ 16 | a one-row data frame with additional columns added, such as 17 | \item{logLik}{log likelihoods} 18 | \item{AIC}{Akaike Information Criterion} 19 | \item{BIC}{Bayesian Information Criterion} 20 | \item{deviance}{deviance} 21 | \item{df.residual}{residual degrees of freedom} 22 | 23 | Each of these are produced by the corresponding generics 24 | } 25 | \description{ 26 | A helper function for several functions in the glance generic. Methods 27 | such as logLik, AIC, and BIC are defined for many prediction 28 | objects, such as lm, glm, and nls. This is a helper function that adds 29 | them to a glance data.frame can be performed. If any of them cannot be 30 | computed, it fails quietly. 31 | } 32 | \details{ 33 | In one special case, deviance for objects of the 34 | \code{lmerMod} class from lme4 is computed with 35 | \code{deviance(x, REML=FALSE)}. 36 | } 37 | 38 | -------------------------------------------------------------------------------- /man/tidy.dist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.dist} 4 | \alias{tidy.dist} 5 | \title{Tidy a distance matrix} 6 | \usage{ 7 | \method{tidy}{dist}(x, diag = attr(x, "Diag"), upper = attr(x, "Upper"), 8 | ...) 9 | } 10 | \arguments{ 11 | \item{x}{A "dist" object} 12 | 13 | \item{diag}{Whether to include the diagonal of the distance 14 | matrix. Defaults to whether the distance matrix includes it} 15 | 16 | \item{upper}{Whether to include the upper right triangle of 17 | the distance matrix. Defaults to whether the distance matrix 18 | includes it} 19 | 20 | \item{...}{Extra arguments, not used} 21 | } 22 | \value{ 23 | A data frame with one row for each pair of 24 | item distances, with columns: 25 | \describe{ 26 | \item{item1}{First item} 27 | \item{item2}{Second item} 28 | \item{distance}{Distance between items} 29 | } 30 | } 31 | \description{ 32 | Tidy a distance matrix, such as that computed by the \link{dist} 33 | function, into a one-row-per-pair table. If the distance matrix 34 | does not include an upper triangle and/or diagonal, this will 35 | not either. 36 | } 37 | \examples{ 38 | 39 | iris_dist <- dist(t(iris[, 1:4])) 40 | iris_dist 41 | 42 | tidy(iris_dist) 43 | tidy(iris_dist, upper = TRUE) 44 | tidy(iris_dist, diag = TRUE) 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /man/glm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/glm_tidiers.R 3 | \name{glm_tidiers} 4 | \alias{glance.glm} 5 | \alias{glm_tidiers} 6 | \title{Tidying methods for a glm object} 7 | \usage{ 8 | \method{glance}{glm}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{glm object} 12 | 13 | \item{...}{extra arguments, not used} 14 | } 15 | \value{ 16 | \code{tidy} and \code{augment} return the same values as do 17 | \code{\link{tidy.lm}} and \code{\link{augment.lm}}. 18 | 19 | \code{glance} returns a one-row data.frame with the columns 20 | \item{null.deviance}{the deviance of the null model} 21 | \item{df.null}{the residual degrees of freedom for the null model} 22 | \item{logLik}{the data's log-likelihood under the model} 23 | \item{AIC}{the Akaike Information Criterion} 24 | \item{BIC}{the Bayesian Information Criterion} 25 | \item{deviance}{deviance} 26 | \item{df.residual}{residual degrees of freedom} 27 | } 28 | \description{ 29 | Tidy a \code{glm} object. The \code{tidy} and \code{augment} methods are handled 30 | by \link{lm_tidiers}. 31 | } 32 | \examples{ 33 | 34 | g <- glm(am ~ mpg, mtcars, family = "binomial") 35 | glance(g) 36 | 37 | } 38 | \seealso{ 39 | \code{\link{tidy.lm}} and \code{\link{augment.lm}}. Also \code{\link{glm}}, which 40 | computes the values reported by the \code{glance} method. 41 | } 42 | 43 | -------------------------------------------------------------------------------- /man/kde_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kde_tidiers.R 3 | \name{kde_tidiers} 4 | \alias{kde_tidiers} 5 | \alias{tidy.kde} 6 | \title{Tidy a kernel density estimate object from the ks package} 7 | \usage{ 8 | \method{tidy}{kde}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{A "ks" object from the kde package} 12 | 13 | \item{...}{Extra arguments, not used} 14 | } 15 | \value{ 16 | A data frame with one row for each point in the 17 | estimated grid. The result contains one column (named \code{x1}, 18 | \code{x2}, etc) for each dimension, and an \code{estimate} column 19 | containing the estimated density. 20 | } 21 | \description{ 22 | Tidy a kernel density estimate object, into a table with 23 | one row for each point in the estimated grid, and one column 24 | for each dimension (along with an \code{estimate} column with 25 | the estimated density). 26 | } 27 | \examples{ 28 | 29 | if (require("ks", quietly = TRUE)) { 30 | dat <- replicate(2, rnorm(100)) 31 | k <- kde(dat) 32 | 33 | td <- tidy(k) 34 | head(td) 35 | 36 | library(ggplot2) 37 | ggplot(td, aes(x1, x2, fill = estimate)) + 38 | geom_tile() + 39 | theme_void() 40 | 41 | # also works with 3 dimensions 42 | dat3 <- replicate(3, rnorm(100)) 43 | k3 <- kde(dat3) 44 | 45 | td3 <- tidy(k3) 46 | head(td3) 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /man/gamlss_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/gamlss_tidiers.R 3 | \name{gamlss_tidiers} 4 | \alias{gamlss_tidiers} 5 | \alias{tidy.gamlss} 6 | \title{Tidying methods for gamlss objects} 7 | \usage{ 8 | \method{tidy}{gamlss}(x, quick = FALSE, ...) 9 | } 10 | \arguments{ 11 | \item{x}{A "gamlss" object} 12 | 13 | \item{quick}{Whether to perform a fast version, and return only the coefficients} 14 | 15 | \item{...}{Extra arguments (not used)} 16 | } 17 | \value{ 18 | All tidying methods return a data.frame without rownames, whose 19 | structure depends on the method chosen. 20 | 21 | A data.frame with one row for each coefficient, containing columns 22 | \item{parameter}{Type of coefficient being estimated: \code{mu}, \code{sigma}, 23 | \code{nu}, or \code{tau}} 24 | \item{term}{The term in the model being estimated and tested} 25 | \item{estimate}{The estimated coefficient} 26 | \item{std.error}{The standard error from the linear model} 27 | \item{statistic}{t-statistic} 28 | \item{p.value}{two-sided p-value} 29 | 30 | if (requireNamespace("gamlss", quietly = TRUE)) { 31 | data(abdom) 32 | mod<-gamlss(y~pb(x),sigma.fo=~pb(x),family=BCT, data=abdom, method=mixed(1,20)) 33 | 34 | tidy(mod) 35 | } 36 | } 37 | \description{ 38 | Tidying methods for "gamlss" objects from the gamlss package. 39 | } 40 | 41 | -------------------------------------------------------------------------------- /R/map_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidy method for map objects. 2 | #' 3 | #' This function turns a map into a data frame. 4 | #' 5 | #' This code and documentation originated in ggplot2, but was called "fortify." 6 | #' In broom, "fortify" became "augment", which is reserved for functions that *add* 7 | #' columns to existing data (based on a model fit, for example) so these functions 8 | #' were renamed as "tidy." 9 | #' 10 | #' @param x map object 11 | #' @param ... not used by this method 12 | #' 13 | #' @examples 14 | #' if (require("maps") && require("ggplot2")) { 15 | #' ca <- map("county", "ca", plot = FALSE, fill = TRUE) 16 | #' head(tidy(ca)) 17 | #' qplot(long, lat, data = ca, geom = "polygon", group = group) 18 | #' 19 | #' tx <- map("county", "texas", plot = FALSE, fill = TRUE) 20 | #' head(tidy(tx)) 21 | #' qplot(long, lat, data = tx, geom = "polygon", group = group, 22 | #' colour = I("white")) 23 | #' } 24 | #' 25 | #' @export 26 | tidy.map <- function(x, ...) { 27 | df <- as.data.frame(x[c("x", "y")]) 28 | names(df) <- c("long", "lat") 29 | df$group <- cumsum(is.na(df$long) & is.na(df$lat)) + 1 30 | df$order <- 1:nrow(df) 31 | 32 | names <- do.call("rbind", lapply(strsplit(x$names, "[:,]"), "[", 1:2)) 33 | df$region <- names[df$group, 1] 34 | df$subregion <- names[df$group, 2] 35 | df[stats::complete.cases(df$lat, df$long), ] 36 | } 37 | -------------------------------------------------------------------------------- /man/acf_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{acf_tidiers} 4 | \alias{acf_tidiers} 5 | \alias{tidy.acf} 6 | \title{Tidying method for the acf function} 7 | \usage{ 8 | \method{tidy}{acf}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{acf object} 12 | 13 | \item{...}{(not used)} 14 | } 15 | \value{ 16 | \code{data.frame} with columns 17 | \item{lag}{lag values} 18 | \item{acf}{calucated correlation} 19 | } 20 | \description{ 21 | Tidy an "acf" object, which is the output of \code{acf} and the 22 | related \code{pcf} and \code{ccf} functions. 23 | } 24 | \examples{ 25 | 26 | # acf 27 | result <- acf(lh, plot=FALSE) 28 | tidy(result) 29 | 30 | # ccf 31 | result <- ccf(mdeaths, fdeaths, plot=FALSE) 32 | tidy(result) 33 | 34 | # pcf 35 | result <- pacf(lh, plot=FALSE) 36 | tidy(result) 37 | 38 | # lag plot 39 | library(ggplot2) 40 | result <- tidy(acf(lh, plot=FALSE)) 41 | p <- ggplot(result, aes(x=lag, y=acf)) + 42 | geom_bar(stat='identity', width=0.1) + 43 | theme_bw() 44 | p 45 | 46 | # with confidence intervals 47 | conf.level <- 0.95 48 | # from \\code{plot.acf} method 49 | len.data <- length(lh) # same as acf$n.used 50 | conf.int <- qnorm((1 + conf.level) / 2) / sqrt(len.data) 51 | p + geom_hline(yintercept = c(-conf.int, conf.int), 52 | color='blue', linetype='dashed') 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /R/summary_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for summaryDefault objects 2 | #' 3 | #' Tidy a summary of a vector. 4 | #' 5 | #' @param x summaryDefault object 6 | #' @param ... extra arguments, not used 7 | #' 8 | #' @return Both \code{tidy} and \code{glance} return the same object: 9 | #' a one-row data frame with columns 10 | #' \item{minimum}{smallest value in original vector} 11 | #' \item{q1}{value at the first quartile} 12 | #' \item{median}{median of original vector} 13 | #' \item{mean}{mean of original vector} 14 | #' \item{q3}{value at the third quartile} 15 | #' \item{maximum}{largest value in original vector} 16 | #' \item{NAs}{number of NA values (if any)} 17 | #' @seealso \code{\link{summary}} 18 | #' 19 | #' @examples 20 | #' 21 | #' v <- rnorm(1000) 22 | #' s <- summary(v) 23 | #' s 24 | #' 25 | #' tidy(s) 26 | #' glance(s) 27 | #' 28 | #' v2 <- c(v,NA) 29 | #' tidy(summary(v2)) 30 | #' 31 | #' @name summary_tidiers 32 | #' 33 | #' @export 34 | tidy.summaryDefault <- function(x, ...) { 35 | ret <- as.data.frame(t(as.matrix(x))) 36 | cnms <- c("minimum", "q1", "median", "mean", "q3", "maximum") 37 | if ("NA's" %in% names(x)) { 38 | cnms <- c(cnms, "na") 39 | } 40 | return(setNames(ret,cnms)) 41 | } 42 | 43 | 44 | 45 | #' @rdname summary_tidiers 46 | #' 47 | #' @export 48 | glance.summaryDefault <- function(x, ...) { 49 | tidy.summaryDefault(x, ...) 50 | } 51 | -------------------------------------------------------------------------------- /R/glm_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for a glm object 2 | #' 3 | #' Tidy a \code{glm} object. The \code{tidy} and \code{augment} methods are handled 4 | #' by \link{lm_tidiers}. 5 | #' 6 | #' @param x glm object 7 | #' @param ... extra arguments, not used 8 | #' 9 | #' @return \code{tidy} and \code{augment} return the same values as do 10 | #' \code{\link{tidy.lm}} and \code{\link{augment.lm}}. 11 | #' 12 | #' @seealso \code{\link{tidy.lm}} and \code{\link{augment.lm}}. Also \code{\link{glm}}, which 13 | #' computes the values reported by the \code{glance} method. 14 | #' 15 | #' @name glm_tidiers 16 | #' 17 | #' @examples 18 | #' 19 | #' g <- glm(am ~ mpg, mtcars, family = "binomial") 20 | #' glance(g) 21 | #' 22 | #' @export 23 | 24 | 25 | #' @rdname glm_tidiers 26 | #' 27 | #' @return \code{glance} returns a one-row data.frame with the columns 28 | #' \item{null.deviance}{the deviance of the null model} 29 | #' \item{df.null}{the residual degrees of freedom for the null model} 30 | #' \item{logLik}{the data's log-likelihood under the model} 31 | #' \item{AIC}{the Akaike Information Criterion} 32 | #' \item{BIC}{the Bayesian Information Criterion} 33 | #' \item{deviance}{deviance} 34 | #' \item{df.residual}{residual degrees of freedom} 35 | glance.glm <- function(x, ...) { 36 | s <- summary(x) 37 | ret <- unrowname(as.data.frame(s[c("null.deviance", "df.null")])) 38 | finish_glance(ret, x) 39 | } 40 | -------------------------------------------------------------------------------- /man/matrix_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/matrix_tidiers.R 3 | \name{matrix_tidiers} 4 | \alias{glance.matrix} 5 | \alias{matrix_tidiers} 6 | \alias{tidy.matrix} 7 | \title{Tidiers for matrix objects} 8 | \usage{ 9 | \method{tidy}{matrix}(x, ...) 10 | 11 | \method{glance}{matrix}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{A matrix} 15 | 16 | \item{...}{extra arguments, not used} 17 | } 18 | \value{ 19 | \code{tidy.matrix} returns the original matrix converted into 20 | a data.frame, except that it incorporates rownames (if they exist) 21 | into a column called \code{.rownames}. 22 | 23 | \code{glance} returns a one-row data.frame with 24 | \item{nrow}{number of rows} 25 | \item{ncol}{number of columns} 26 | \item{complete.obs}{number of rows that have no missing values} 27 | \item{na.fraction}{fraction of values across all rows and columns that 28 | are missing} 29 | } 30 | \description{ 31 | These perform tidying operations on matrix objects. \code{tidy} turns the 32 | matrix into a data.frame while bringing rownames, if they exist, in as 33 | a column called \code{.rownames} (since results of tidying operations never 34 | contain rownames). \code{glance} simply reports the number of rows and 35 | columns. Note that no augment method exists for matrices. 36 | } 37 | \examples{ 38 | 39 | mat <- as.matrix(mtcars) 40 | tidy(mat) 41 | glance(mat) 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /man/tidy.manova.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.manova} 4 | \alias{tidy.manova} 5 | \title{tidy a MANOVA object} 6 | \usage{ 7 | \method{tidy}{manova}(x, test = "Pillai", ...) 8 | } 9 | \arguments{ 10 | \item{x}{object of class "manova"} 11 | 12 | \item{test}{one of "Pillai" (Pillai's trace), "Wilks" (Wilk's lambda), "Hotelling-Lawley" (Hotelling-Lawley trace) or "Roy" (Roy's greatest root) indicating which test statistic should be used. Defaults to "Pillai"} 13 | 14 | \item{...}{additional arguments passed on to \code{summary.manova}} 15 | } 16 | \value{ 17 | A data.frame with the columns 18 | \item{term}{Term in design} 19 | \item{statistic}{Approximate F statistic} 20 | \item{num.df}{Degrees of freedom} 21 | \item{p.value}{P-value} 22 | 23 | Depending on which test statistic is specified, one of the following columns is also included: 24 | \item{pillai}{Pillai's trace} 25 | \item{wilks}{Wilk's lambda} 26 | \item{hl}{Hotelling-Lawley trace} 27 | \item{roy}{Roy's greatest root} 28 | } 29 | \description{ 30 | Constructs a data frame with one row for each of the terms in the model, 31 | containing the information from \link{summary.manova}. 32 | } 33 | \examples{ 34 | 35 | npk2 <- within(npk, foo <- rnorm(24)) 36 | npk2.aov <- manova(cbind(yield, foo) ~ block + N*P*K, npk2) 37 | 38 | } 39 | \seealso{ 40 | \code{\link{summary.manova}} 41 | } 42 | 43 | -------------------------------------------------------------------------------- /tests/testthat/test-augment.R: -------------------------------------------------------------------------------- 1 | # test the augment method of lm, glm, nls, lmer, coxph, and survreg 2 | # (note that test_that cases contained within the check_augment_NAs 3 | # function) 4 | 5 | context("lm augment") 6 | lm_func <- function(.data, ...) lm(mpg ~ wt, .data, ...) 7 | check_augment_NAs(lm_func, mtcars, "mpg", "wt") 8 | 9 | context("glm augment") 10 | glm_func <- function(.data, ...) glm(am ~ wt, .data, family = "poisson", ...) 11 | check_augment_NAs(glm_func, mtcars, "am", "wt") 12 | 13 | context("nls augment") 14 | nls_func <- function(.data, ...) { 15 | nls(mpg ~ k * e ^ wt, data = .data, start = list(k = 50, e = 1), ...) 16 | } 17 | check_augment_NAs(nls_func, mtcars, "mpg", "wt") 18 | 19 | if (require("lme4", quietly = TRUE)) { 20 | context("lme4 augment") 21 | lmer_func <- function(.data, ...) { 22 | lmer(Reaction ~ Days + (Days | Subject), .data, ...) 23 | } 24 | check_augment_NAs(lmer_func, sleepstudy, "Reaction", "Days") 25 | } 26 | 27 | if (require("survival", quietly = TRUE)) { 28 | context("survival augment") 29 | coxph_func <- function(.data, ...) { 30 | coxph(Surv(time, status) ~ age + sex, .data, ...) 31 | } 32 | check_augment_NAs(coxph_func, lung, "age", "sex") 33 | 34 | survreg_func <- function(.data, ...) { 35 | survreg(Surv(futime, fustat) ~ ecog.ps + rx, .data, dist = "exponential", ...) 36 | } 37 | check_augment_NAs(survreg_func, ovarian, "ecog.ps", "rx") 38 | } -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who 4 | contribute through reporting issues, posting feature requests, updating documentation, 5 | submitting pull requests or patches, and other activities. 6 | 7 | We are committed to making participation in this project a harassment-free experience for 8 | everyone, regardless of level of experience, gender, gender identity and expression, 9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 10 | 11 | Examples of unacceptable behavior by participants include the use of sexual language or 12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment, 13 | insults, or other unprofessional conduct. 14 | 15 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed 18 | from the project team. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 21 | opening an issue or contacting one or more of the project maintainers. 22 | 23 | This Code of Conduct is adapted from the Contributor Covenant 24 | (http:contributor-covenant.org), version 1.0.0, available at 25 | http://contributor-covenant.org/version/1/0/0/ 26 | -------------------------------------------------------------------------------- /R/matrix_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for matrix objects 2 | #' 3 | #' These perform tidying operations on matrix objects. \code{tidy} turns the 4 | #' matrix into a data.frame while bringing rownames, if they exist, in as 5 | #' a column called \code{.rownames} (since results of tidying operations never 6 | #' contain rownames). \code{glance} simply reports the number of rows and 7 | #' columns. Note that no augment method exists for matrices. 8 | #' 9 | #' @param x A matrix 10 | #' @param ... extra arguments, not used 11 | #' 12 | #' @examples 13 | #' 14 | #' mat <- as.matrix(mtcars) 15 | #' tidy(mat) 16 | #' glance(mat) 17 | #' 18 | #' @name matrix_tidiers 19 | 20 | 21 | #' @rdname matrix_tidiers 22 | #' 23 | #' @return \code{tidy.matrix} returns the original matrix converted into 24 | #' a data.frame, except that it incorporates rownames (if they exist) 25 | #' into a column called \code{.rownames}. 26 | #' 27 | #' @export 28 | tidy.matrix <- function(x, ...) { 29 | fix_data_frame(x, newcol = ".rownames") 30 | } 31 | 32 | 33 | #' @rdname matrix_tidiers 34 | #' 35 | #' @return \code{glance} returns a one-row data.frame with 36 | #' \item{nrow}{number of rows} 37 | #' \item{ncol}{number of columns} 38 | #' \item{complete.obs}{number of rows that have no missing values} 39 | #' \item{na.fraction}{fraction of values across all rows and columns that 40 | #' are missing} 41 | #' 42 | #' @export 43 | glance.matrix <- function(x, ...) { 44 | glance.data.frame(x) 45 | } 46 | -------------------------------------------------------------------------------- /man/kappa_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/psych_tidiers.R 3 | \name{kappa_tidiers} 4 | \alias{kappa_tidiers} 5 | \alias{tidy.kappa} 6 | \title{Tidy a kappa object from a Cohen's kappa calculation} 7 | \usage{ 8 | \method{tidy}{kappa}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{An object of class "kappa"} 12 | 13 | \item{...}{extra arguments (not used)} 14 | } 15 | \value{ 16 | A data.frame with columns 17 | \item{type}{Either "weighted" or "unweighted"} 18 | \item{estimate}{The estimated value of kappa with this method} 19 | \item{conf.low}{Lower bound of confidence interval} 20 | \item{conf.high}{Upper bound of confidence interval} 21 | } 22 | \description{ 23 | Tidy a "kappa" object, from the \code{\link{cohen.kappa}} function 24 | in the psych package. This represents the agreement of two raters 25 | when using nominal scores. 26 | } 27 | \details{ 28 | Note that the alpha of the confidence interval is determined 29 | when the \code{cohen.kappa} function is originally run. 30 | } 31 | \examples{ 32 | 33 | library(psych) 34 | 35 | rater1 = 1:9 36 | rater2 = c(1, 3, 1, 6, 1, 5, 5, 6, 7) 37 | ck <- cohen.kappa(cbind(rater1, rater2)) 38 | 39 | tidy(ck) 40 | 41 | # graph the confidence intervals 42 | library(ggplot2) 43 | ggplot(tidy(ck), aes(estimate, type)) + 44 | geom_point() + 45 | geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) 46 | 47 | } 48 | \seealso{ 49 | \code{\link{cohen.kappa}} 50 | } 51 | 52 | -------------------------------------------------------------------------------- /man/binWidth_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bingroup_tidiers.R 3 | \name{binWidth_tidiers} 4 | \alias{binWidth_tidiers} 5 | \alias{tidy.binWidth} 6 | \title{Tidy a binWidth object} 7 | \usage{ 8 | \method{tidy}{binWidth}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{A "binWidth" object} 12 | 13 | \item{...}{Extra arguments (not used)} 14 | } 15 | \value{ 16 | A one-row data.frame with columns: 17 | \item{ci.width}{Expected width of confidence interval} 18 | \item{alternative}{Alternative hypothesis} 19 | \item{p}{True proportion} 20 | \item{n}{Total sample size} 21 | } 22 | \description{ 23 | Tidy a binWidth object from the "binGroup" package, 24 | which calculates the expected width of a confidence 25 | interval from a binomial test. 26 | } 27 | \examples{ 28 | 29 | if (require("binGroup", quietly = TRUE)) { 30 | bw <- binWidth(100, .1) 31 | bw 32 | tidy(bw) 33 | 34 | library(dplyr) 35 | d <- expand.grid(n = seq(100, 800, 100), 36 | p = .5, 37 | method = c("CP", "Blaker", "Score", "Wald"), 38 | stringsAsFactors = FALSE) \%>\% 39 | group_by(n, p, method) \%>\% 40 | do(tidy(binWidth(.$n, .$p, method = .$method))) 41 | 42 | library(ggplot2) 43 | ggplot(d, aes(n, ci.width, color = method)) + 44 | geom_line() + 45 | xlab("Total Observations") + 46 | ylab("Expected CI Width") 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /man/auc_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/auc_tidiers.R 3 | \name{auc_tidiers} 4 | \alias{auc_tidiers} 5 | \alias{tidy.roc} 6 | \title{Tidiers for objects from the AUC package} 7 | \usage{ 8 | \method{tidy}{roc}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{an "roc" object} 12 | 13 | \item{...}{Additional arguments, not used} 14 | } 15 | \value{ 16 | A data frame with three columns: 17 | \item{cutoff}{The cutoff of the prediction scores used 18 | for classification} 19 | \item{tpr}{The resulting true positive rate at that cutoff} 20 | \item{fpr}{The resulting false positive rate at that cutoff} 21 | 22 | If the labels had names, those are added as an "instance" column. 23 | } 24 | \description{ 25 | Tidy "roc" objects from the "auc" package. This can be used to, 26 | for example, draw ROC curves in ggplot2. 27 | } 28 | \examples{ 29 | 30 | if (require("AUC", quietly = TRUE)) { 31 | data(churn) 32 | r <- roc(churn$predictions,churn$labels) 33 | 34 | td <- tidy(r) 35 | head(td) 36 | 37 | library(ggplot2) 38 | ggplot(td, aes(fpr, tpr)) + 39 | geom_line() 40 | 41 | # compare the ROC curves for two prediction algorithms 42 | library(dplyr) 43 | library(tidyr) 44 | 45 | rocs <- churn \%>\% 46 | tidyr::gather(algorithm, value, -labels) \%>\% 47 | group_by(algorithm) \%>\% 48 | do(tidy(roc(.$value, .$labels))) 49 | 50 | ggplot(rocs, aes(fpr, tpr, color = algorithm)) + 51 | geom_line() 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /man/fitdistr_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fitdistr_tidiers.R 3 | \name{fitdistr_tidiers} 4 | \alias{fitdistr_tidiers} 5 | \alias{glance.fitdistr} 6 | \alias{tidy.fitdistr} 7 | \title{Tidying methods for fitdistr objects from the MASS package} 8 | \usage{ 9 | \method{tidy}{fitdistr}(x, ...) 10 | 11 | \method{glance}{fitdistr}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{An object of class "fitdistr"} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy.fitdistr} returns one row for each parameter that 23 | was estimated, with columns: 24 | \item{term}{The term that was estimated} 25 | \item{estimate}{Estimated value} 26 | \item{std.error}{Standard error of estimate} 27 | 28 | \code{glance.fitdistr} returns a one-row data.frame with the columns 29 | \item{n}{Number of observations used in estimation} 30 | \item{logLik}{log-likelihood of estimated data} 31 | \item{AIC}{Akaike Information Criterion} 32 | \item{BIC}{Bayesian Information Criterion} 33 | } 34 | \description{ 35 | These methods tidies the parameter estimates resulting 36 | from an estimation of a univariate distribution's parameters. 37 | } 38 | \examples{ 39 | 40 | set.seed(2015) 41 | x <- rnorm(100, 5, 2) 42 | 43 | library(MASS) 44 | fit <- fitdistr(x, dnorm, list(mean = 3, sd = 1)) 45 | 46 | tidy(fit) 47 | glance(fit) 48 | } 49 | 50 | -------------------------------------------------------------------------------- /tests/testthat/test-bootstrap.R: -------------------------------------------------------------------------------- 1 | context("bootstrapping") 2 | 3 | test_that("bootstrap works with by_group and grouped tbl", { 4 | df <- data_frame(x = c(rep("a", 3), rep("b", 5)), 5 | y = rnorm(length(x))) 6 | df_reps <- 7 | df %>% 8 | group_by(x) %>% 9 | bootstrap(20, by_group = TRUE) %>% 10 | do(tally(group_by(., x))) 11 | expect_true(all(filter(df_reps, x == "a")$n == 3)) 12 | expect_true(all(filter(df_reps, x == "b")$n == 5)) 13 | }) 14 | 15 | test_that("bootstrap does not sample within groups if by_group = FALSE", { 16 | set.seed(12334) 17 | df <- data_frame(x = c(rep("a", 3), rep("b", 5)), 18 | y = rnorm(length(x))) 19 | df_reps <- 20 | df %>% 21 | group_by(x) %>% 22 | bootstrap(20, by_group = FALSE) %>% 23 | do(tally(group_by(., x))) 24 | expect_true(!all(filter(df_reps, x == "a")$n == 3)) 25 | expect_true(!all(filter(df_reps, x == "b")$n == 5)) 26 | }) 27 | 28 | test_that("bootstrap does not sample within groups if no groups", { 29 | set.seed(12334) 30 | df <- data_frame(x = c(rep("a", 3), rep("b", 5)), 31 | y = rnorm(length(x))) 32 | df_reps <- 33 | df %>% 34 | ungroup() %>% 35 | bootstrap(20, by_group = TRUE) %>% 36 | do(tally(group_by(., x))) 37 | expect_true(!all(filter(df_reps, x == "a")$n == 3)) 38 | expect_true(!all(filter(df_reps, x == "b")$n == 5)) 39 | }) 40 | -------------------------------------------------------------------------------- /man/sp_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sp_tidiers.R 3 | \name{sp_tidiers} 4 | \alias{sp_tidiers} 5 | \alias{tidy.Line} 6 | \alias{tidy.Lines} 7 | \alias{tidy.Polygon} 8 | \alias{tidy.Polygons} 9 | \alias{tidy.SpatialLinesDataFrame} 10 | \alias{tidy.SpatialPolygons} 11 | \alias{tidy.SpatialPolygonsDataFrame} 12 | \title{tidying methods for classes from the sp package.} 13 | \usage{ 14 | \method{tidy}{SpatialPolygonsDataFrame}(x, region = NULL, ...) 15 | 16 | \method{tidy}{SpatialPolygons}(x, ...) 17 | 18 | \method{tidy}{Polygons}(x, ...) 19 | 20 | \method{tidy}{Polygon}(x, ...) 21 | 22 | \method{tidy}{SpatialLinesDataFrame}(x, ...) 23 | 24 | \method{tidy}{Lines}(x, ...) 25 | 26 | \method{tidy}{Line}(x, ...) 27 | } 28 | \arguments{ 29 | \item{x}{\code{SpatialPolygonsDataFrame} to convert into a dataframe.} 30 | 31 | \item{region}{name of variable used to split up regions} 32 | 33 | \item{...}{not used by this method} 34 | } 35 | \description{ 36 | Tidy classes from the sp package to allow them to be plotted using ggplot2. 37 | To figure out the correct variable name for region, inspect 38 | \code{as.data.frame(x)}. 39 | } 40 | \details{ 41 | These functions originated in the ggplot2 package as "fortify" functions. 42 | } 43 | \examples{ 44 | if (require("maptools")) { 45 | sids <- system.file("shapes/sids.shp", package="maptools") 46 | nc1 <- readShapePoly(sids, 47 | proj4string = CRS("+proj=longlat +datum=NAD27")) 48 | nc1_df <- tidy(nc1) 49 | } 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /R/loess_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Augmenting methods for loess models 2 | #' 3 | #' This method augments the original data with information 4 | #' on the fitted values and residuals, and optionally the 5 | #' standard errors. 6 | #' 7 | #' @param x A "loess" object 8 | #' @param data Original data, defaults to the extracting it from the model 9 | #' @param newdata If provided, performs predictions on the new data 10 | #' @param ... extra arguments 11 | #' 12 | #' @name loess_tidiers 13 | #' 14 | #' @template augment_NAs 15 | #' 16 | #' @return When \code{newdata} is not supplied \code{augment.loess} 17 | #' returns one row for each observation with three columns added 18 | #' to the original data: 19 | #' \item{.fitted}{Fitted values of model} 20 | #' \item{.se.fit}{Standard errors of the fitted values} 21 | #' \item{.resid}{Residuals of the fitted values} 22 | #' 23 | #' When \code{newdata} is supplied \code{augment.loess} returns 24 | #' one row for each observation with one additional column: 25 | #' \item{.fitted}{Fitted values of model} 26 | #' \item{.se.fit}{Standard errors of the fitted values} 27 | #' 28 | #' @examples 29 | #' 30 | #' lo <- loess(mpg ~ wt, mtcars) 31 | #' augment(lo) 32 | #' 33 | #' # with all columns of original data 34 | #' augment(lo, mtcars) 35 | #' 36 | #' # with a new dataset 37 | #' augment(lo, newdata = head(mtcars)) 38 | #' 39 | #' @export 40 | augment.loess <- function(x, data = stats::model.frame(x), newdata, ...){ 41 | augment_columns(x, data, newdata, se.fit = FALSE, se = TRUE, ...) 42 | } 43 | -------------------------------------------------------------------------------- /man/aareg_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/survival_tidiers.R 3 | \name{aareg_tidiers} 4 | \alias{aareg_tidiers} 5 | \alias{glance.aareg} 6 | \alias{tidy.aareg} 7 | \title{Tidiers for aareg objects} 8 | \usage{ 9 | \method{tidy}{aareg}(x, ...) 10 | 11 | \method{glance}{aareg}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{an "aareg" object} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy.aareg} returns one row for each coefficient, with 23 | the columns 24 | \item{term}{name of coefficient} 25 | \item{estimate}{estimate of the slope} 26 | \item{statistic}{test statistic for coefficient} 27 | \item{std.error}{standard error of statistic} 28 | \item{robust.se}{robust version of standard error estimate} 29 | \item{z}{z score} 30 | \item{p.value}{p-value} 31 | 32 | \code{glance} returns a one-row data frame containing 33 | \item{statistic}{chi-squared statistic} 34 | \item{p.value}{p-value based on chi-squared statistic} 35 | \item{df}{degrees of freedom used by coefficients} 36 | } 37 | \description{ 38 | These tidy the coefficients of Aalen additive regression objects. 39 | } 40 | \examples{ 41 | 42 | if (require("survival", quietly = TRUE)) { 43 | afit <- aareg(Surv(time, status) ~ age + sex + ph.ecog, data=lung, 44 | dfbeta=TRUE) 45 | summary(afit) 46 | tidy(afit) 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /tests/testthat/test-ivreg.R: -------------------------------------------------------------------------------- 1 | # test tidy, augment, glance methods from lme4-tidiers.R 2 | 3 | if (require(AER, quietly = TRUE)) { 4 | context("AER::ivreg models") 5 | 6 | data("CigarettesSW", package = "AER") 7 | CigarettesSW$rprice <- with(CigarettesSW, price/cpi) 8 | CigarettesSW$rincome <- with(CigarettesSW, income/population/cpi) 9 | CigarettesSW$tdiff <- with(CigarettesSW, (taxs - tax)/cpi) 10 | ivr <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi), 11 | data = CigarettesSW, subset = year == "1995") 12 | 13 | test_that("tidy works on AER::ivreg fits", { 14 | td <- tidy(ivr) 15 | td2 <- tidy(ivr, conf.int = TRUE) 16 | expect_warning(tidy(ivr, exponentiate = TRUE)) # warning as we didn't use a link function, maybe this is bad? 17 | }) 18 | 19 | test_that("augment works on ivreg fits", { 20 | au <- augment(ivr) 21 | expect_true(all(c('.resid', '.fitted') %in% names(au))) 22 | expect_equivalent(au$.resid, residuals(ivr)) 23 | expect_equivalent(au$.fitted, fitted(ivr)) 24 | old_cigs <- CigarettesSW[CigarettesSW$year == "1985" & CigarettesSW$tax < 40, ] 25 | au2 <- augment(ivr, newdata = old_cigs) 26 | expect_true('.fitted' %in% names(au2)) 27 | expect_equivalent(au2$.fitted, predict(ivr, newdata = old_cigs)) 28 | }) 29 | 30 | test_that("glance works on ivreg fits", { 31 | g <- glance(ivr) 32 | g <- glance(ivr, diagnostics = TRUE) 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /man/sexpfit_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/survival_tidiers.R 3 | \name{sexpfit_tidiers} 4 | \alias{glance.survexp} 5 | \alias{sexpfit_tidiers} 6 | \alias{tidy.survexp} 7 | \title{Tidy an expected survival curve} 8 | \usage{ 9 | \method{tidy}{survexp}(x, ...) 10 | 11 | \method{glance}{survexp}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{"survexp" object} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy} returns a one row for each time point, with columns 23 | \item{time}{time point} 24 | \item{estimate}{estimated survival} 25 | \item{n.risk}{number of individuals at risk} 26 | 27 | \code{glance} returns a one-row data.frame with the columns: 28 | \item{n.max}{maximum number of subjects at risk} 29 | \item{n.start}{starting number of subjects at risk} 30 | \item{timepoints}{number of timepoints} 31 | } 32 | \description{ 33 | This constructs a summary across time points or overall of an expected survival 34 | curve. Note that this contains less information than most survfit objects. 35 | } 36 | \examples{ 37 | 38 | if (require("survival", quietly = TRUE)) { 39 | sexpfit <- survexp(futime ~ 1, rmap=list(sex="male", year=accept.dt, 40 | age=(accept.dt-birth.dt)), 41 | method='conditional', data=jasa) 42 | 43 | tidy(sexpfit) 44 | glance(sexpfit) 45 | } 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /man/optim_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/optim_tidiers.R 3 | \name{optim_tidiers} 4 | \alias{glance_optim} 5 | \alias{optim_tidiers} 6 | \alias{tidy_optim} 7 | \title{Tidiers for lists returned from optim} 8 | \usage{ 9 | tidy_optim(x, ...) 10 | 11 | glance_optim(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{list returned from \code{optim}} 15 | 16 | \item{...}{extra arguments} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy} returns a data frame with one row per parameter that 23 | was estimated, with columns 24 | \item{parameter}{name of the parameter, or \code{parameter1}, 25 | \code{parameter2}... if the input vector is not named} 26 | \item{value}{parameter value that minimizes or maximizes the output} 27 | 28 | \code{glance} returns a one-row data frame with the columns 29 | \item{value}{minimized or maximized output value} 30 | \item{function.count}{number of calls to \code{fn}} 31 | \item{gradient.count}{number of calls to \code{gr}} 32 | \item{convergence}{convergence code representing the error state} 33 | } 34 | \description{ 35 | Tidies objects returned by the \code{\link{optim}} function for 36 | general-purpose minimization and maximization. 37 | } 38 | \examples{ 39 | 40 | func <- function(x) { 41 | (x[1] - 2)^2 + (x[2] - 3)^2 + (x[3] - 8)^2 42 | } 43 | 44 | o <- optim(c(1, 1, 1), func) 45 | 46 | tidy(o) 47 | glance(o) 48 | 49 | } 50 | \seealso{ 51 | \code{\link{optim}} 52 | } 53 | 54 | -------------------------------------------------------------------------------- /R/auc_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for objects from the AUC package 2 | #' 3 | #' Tidy "roc" objects from the "auc" package. This can be used to, 4 | #' for example, draw ROC curves in ggplot2. 5 | #' 6 | #' @param x an "roc" object 7 | #' @param ... Additional arguments, not used 8 | #' 9 | #' @return A data frame with three columns: 10 | #' \item{cutoff}{The cutoff of the prediction scores used 11 | #' for classification} 12 | #' \item{tpr}{The resulting true positive rate at that cutoff} 13 | #' \item{fpr}{The resulting false positive rate at that cutoff} 14 | #' 15 | #' If the labels had names, those are added as an "instance" column. 16 | #' 17 | #' @examples 18 | #' 19 | #' if (require("AUC", quietly = TRUE)) { 20 | #' data(churn) 21 | #' r <- roc(churn$predictions,churn$labels) 22 | #' 23 | #' td <- tidy(r) 24 | #' head(td) 25 | #' 26 | #' library(ggplot2) 27 | #' ggplot(td, aes(fpr, tpr)) + 28 | #' geom_line() 29 | #' 30 | #' # compare the ROC curves for two prediction algorithms 31 | #' library(dplyr) 32 | #' library(tidyr) 33 | #' 34 | #' rocs <- churn %>% 35 | #' tidyr::gather(algorithm, value, -labels) %>% 36 | #' group_by(algorithm) %>% 37 | #' do(tidy(roc(.$value, .$labels))) 38 | #' 39 | #' ggplot(rocs, aes(fpr, tpr, color = algorithm)) + 40 | #' geom_line() 41 | #' } 42 | #' 43 | #' @name auc_tidiers 44 | #' 45 | #' @export 46 | tidy.roc <- function(x, ...) { 47 | fix_data_frame(as.data.frame(unclass(x)), 48 | newnames = c("cutoff", "fpr", "tpr"), 49 | newcol = "instance") 50 | } 51 | -------------------------------------------------------------------------------- /man/tidy.TukeyHSD.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.TukeyHSD} 4 | \alias{tidy.TukeyHSD} 5 | \title{tidy a TukeyHSD object} 6 | \usage{ 7 | \method{tidy}{TukeyHSD}(x, separate.levels = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{x}{object of class "TukeyHSD"} 11 | 12 | \item{separate.levels}{Whether to separate comparison into 13 | \code{level1} and \code{level2} columns} 14 | 15 | \item{...}{additional arguments (not used)} 16 | } 17 | \value{ 18 | A data.frame with one row per comparison, containing columns 19 | \item{term}{Term for which levels are being compared} 20 | \item{comparison}{Levels being compared, separated by -} 21 | \item{estimate}{Estimate of difference} 22 | \item{conf.low}{Low end of confidence interval of difference} 23 | \item{conf.high}{High end of confidence interval of difference} 24 | \item{adj.p.value}{P-value adjusted for multiple comparisons} 25 | 26 | If \code{separate.levels = TRUE}, the \code{comparison} column will be 27 | split up into \code{level1} and \code{level2}. 28 | } 29 | \description{ 30 | Returns a data.frame with one row for each pairwise comparison 31 | } 32 | \examples{ 33 | 34 | fm1 <- aov(breaks ~ wool + tension, data = warpbreaks) 35 | thsd <- TukeyHSD(fm1, "tension", ordered = TRUE) 36 | tidy(thsd) 37 | tidy(thsd, separate.levels = TRUE) 38 | 39 | # may include comparisons on multiple terms 40 | fm2 <- aov(mpg ~ as.factor(gear) * as.factor(cyl), data = mtcars) 41 | tidy(TukeyHSD(fm2)) 42 | 43 | } 44 | \seealso{ 45 | \code{\link{TukeyHSD}} 46 | } 47 | 48 | -------------------------------------------------------------------------------- /man/tidy.pairwise.htest.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats_tidiers.R 3 | \name{tidy.pairwise.htest} 4 | \alias{tidy.pairwise.htest} 5 | \title{tidy a pairwise hypothesis test} 6 | \usage{ 7 | \method{tidy}{pairwise.htest}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a "pairwise.htest" object} 11 | 12 | \item{...}{extra arguments (not used)} 13 | } 14 | \value{ 15 | A data frame with one row per group/group comparison, with columns 16 | \item{group1}{First group being compared} 17 | \item{group2}{Second group being compared} 18 | \item{p.value}{(Adjusted) p-value of comparison} 19 | } 20 | \description{ 21 | Tidy a pairwise.htest object, containing (adjusted) p-values for multiple 22 | pairwise hypothesis tests. 23 | } 24 | \details{ 25 | Note that in one-sided tests, the alternative hypothesis of each 26 | test can be stated as "group1 is greater/less than group2". 27 | 28 | Note also that the columns of group1 and group2 will always be a factor, 29 | even if the original input is (e.g.) numeric. 30 | } 31 | \examples{ 32 | 33 | attach(airquality) 34 | Month <- factor(Month, labels = month.abb[5:9]) 35 | ptt <- pairwise.t.test(Ozone, Month) 36 | tidy(ptt) 37 | 38 | attach(iris) 39 | ptt2 <- pairwise.t.test(Petal.Length, Species) 40 | tidy(ptt2) 41 | 42 | tidy(pairwise.t.test(Petal.Length, Species, alternative = "greater")) 43 | tidy(pairwise.t.test(Petal.Length, Species, alternative = "less")) 44 | 45 | tidy(pairwise.wilcox.test(Petal.Length, Species)) 46 | 47 | } 48 | \seealso{ 49 | \link{pairwise.t.test}, \link{pairwise.wilcox.test} 50 | } 51 | 52 | -------------------------------------------------------------------------------- /R/fitdistr_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for fitdistr objects from the MASS package 2 | #' 3 | #' These methods tidies the parameter estimates resulting 4 | #' from an estimation of a univariate distribution's parameters. 5 | #' 6 | #' @param x An object of class "fitdistr" 7 | #' @param ... extra arguments (not used) 8 | #' 9 | #' @template boilerplate 10 | #' 11 | #' @name fitdistr_tidiers 12 | #' 13 | #' @examples 14 | #' 15 | #' set.seed(2015) 16 | #' x <- rnorm(100, 5, 2) 17 | #' 18 | #' library(MASS) 19 | #' fit <- fitdistr(x, dnorm, list(mean = 3, sd = 1)) 20 | #' 21 | #' tidy(fit) 22 | #' glance(fit) 23 | NULL 24 | 25 | 26 | #' @rdname fitdistr_tidiers 27 | #' 28 | #' @return \code{tidy.fitdistr} returns one row for each parameter that 29 | #' was estimated, with columns: 30 | #' \item{term}{The term that was estimated} 31 | #' \item{estimate}{Estimated value} 32 | #' \item{std.error}{Standard error of estimate} 33 | #' 34 | #' @export 35 | tidy.fitdistr <- function(x, ...) { 36 | data.frame(term = names(x$estimate), 37 | estimate = unname(x$estimate), 38 | std.error = unname(x$sd)) 39 | } 40 | 41 | 42 | #' @rdname fitdistr_tidiers 43 | #' 44 | #' @return \code{glance.fitdistr} returns a one-row data.frame with the columns 45 | #' \item{n}{Number of observations used in estimation} 46 | #' \item{logLik}{log-likelihood of estimated data} 47 | #' \item{AIC}{Akaike Information Criterion} 48 | #' \item{BIC}{Bayesian Information Criterion} 49 | #' 50 | #' @export 51 | glance.fitdistr <- function(x, ...) { 52 | finish_glance(data.frame(n = x$n), x) 53 | } 54 | -------------------------------------------------------------------------------- /man/smooth.spline_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/smooth.spline_tidiers.R 3 | \name{smooth.spline_tidiers} 4 | \alias{augment.smooth.spline} 5 | \alias{glance.smooth.spline} 6 | \alias{smooth.spline_tidiers} 7 | \title{tidying methods for smooth.spline objects} 8 | \usage{ 9 | \method{augment}{smooth.spline}(x, data = x$data, ...) 10 | 11 | \method{glance}{smooth.spline}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{a smooth.spline object} 15 | 16 | \item{data}{defaults to data used to fit model} 17 | 18 | \item{...}{not used in this method} 19 | } 20 | \value{ 21 | \code{augment} returns the original data with extra columns: 22 | \item{.fitted}{Fitted values of model} 23 | \item{.resid}{Residuals} 24 | 25 | \code{glance} returns one row with columns 26 | \item{spar}{smoothing parameter} 27 | \item{lambda}{choice of lambda corresponding to \code{spar}} 28 | \item{df}{equivalent degrees of freedom} 29 | \item{crit}{minimized criterion} 30 | \item{pen.crit}{penalized criterion} 31 | \item{cv.crit}{cross-validation score} 32 | } 33 | \description{ 34 | This combines the original data given to smooth.spline with the 35 | fit and residuals 36 | } 37 | \details{ 38 | No \code{tidy} method is provided for smooth.spline objects. 39 | } 40 | \examples{ 41 | 42 | spl <- smooth.spline(mtcars$wt, mtcars$mpg, df = 4) 43 | head(augment(spl, mtcars)) 44 | head(augment(spl)) # calls original columns x and y 45 | 46 | library(ggplot2) 47 | ggplot(augment(spl, mtcars), aes(wt, mpg)) + 48 | geom_point() + geom_line(aes(y = .fitted)) 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /R/kde_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidy a kernel density estimate object from the ks package 2 | #' 3 | #' Tidy a kernel density estimate object, into a table with 4 | #' one row for each point in the estimated grid, and one column 5 | #' for each dimension (along with an \code{estimate} column with 6 | #' the estimated density). 7 | #' 8 | #' @param x A "ks" object from the kde package 9 | #' @param ... Extra arguments, not used 10 | #' 11 | #' @return A data frame with one row for each point in the 12 | #' estimated grid. The result contains one column (named \code{x1}, 13 | #' \code{x2}, etc) for each dimension, and an \code{estimate} column 14 | #' containing the estimated density. 15 | #' 16 | #' @name kde_tidiers 17 | #' 18 | #' @examples 19 | #' 20 | #' if (require("ks", quietly = TRUE)) { 21 | #' dat <- replicate(2, rnorm(100)) 22 | #' k <- kde(dat) 23 | #' 24 | #' td <- tidy(k) 25 | #' head(td) 26 | #' 27 | #' library(ggplot2) 28 | #' ggplot(td, aes(x1, x2, fill = estimate)) + 29 | #' geom_tile() + 30 | #' theme_void() 31 | #' 32 | #' # also works with 3 dimensions 33 | #' dat3 <- replicate(3, rnorm(100)) 34 | #' k3 <- kde(dat3) 35 | #' 36 | #' td3 <- tidy(k3) 37 | #' head(td3) 38 | #' } 39 | #' 40 | #' @export 41 | tidy.kde <- function(x, ...) { 42 | estimate <- reshape2::melt(x$estimate) 43 | 44 | dims <- seq_len(length(x$eval.points)) 45 | 46 | ret <- purrr::map2(x$eval.points, estimate[dims], function(e, d) e[d]) %>% 47 | as.data.frame() %>% 48 | setNames(paste0("x", dims)) %>% 49 | mutate(estimate = estimate$value) 50 | 51 | ret 52 | } 53 | -------------------------------------------------------------------------------- /man/anova_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/anova_tidiers.R 3 | \name{anova_tidiers} 4 | \alias{anova_tidiers} 5 | \alias{tidy.anova} 6 | \alias{tidy.aov} 7 | \alias{tidy.aovlist} 8 | \title{Tidying methods for anova and AOV objects} 9 | \usage{ 10 | \method{tidy}{anova}(x, ...) 11 | 12 | \method{tidy}{aov}(x, ...) 13 | 14 | \method{tidy}{aovlist}(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{An object of class "anova", "aov", or "aovlist"} 18 | 19 | \item{...}{extra arguments (not used)} 20 | } 21 | \value{ 22 | A data.frame with columns 23 | \item{term}{Term within the model, or "Residuals"} 24 | \item{df}{Degrees of freedom used by this term in the model} 25 | \item{sumsq}{Sum of squares explained by this term} 26 | \item{meansq}{Mean of sum of squares among degrees of freedom} 27 | \item{statistic}{F statistic} 28 | \item{p.value}{P-value from F test} 29 | 30 | In the case of an \code{"aovlist"} object, there is also a \code{stratum} 31 | column describing the error stratum 32 | } 33 | \description{ 34 | Tidies the result of an analysis of variance into an ANOVA table. 35 | Only a \code{tidy} method is provided, not an \code{augment} or 36 | \code{glance} method. 37 | } 38 | \details{ 39 | Note that the "term" column of an ANOVA table can come with 40 | leading or trailing whitespace, which this tidying method trims. 41 | } 42 | \examples{ 43 | 44 | a <- anova(lm(mpg ~ wt + qsec + disp, mtcars)) 45 | tidy(a) 46 | 47 | a <- aov(mpg ~ wt + qsec + disp, mtcars) 48 | tidy(a) 49 | 50 | al <- aov(mpg ~ wt + qsec + Error(disp / am), mtcars) 51 | tidy(al) 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /R/psych_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidy a kappa object from a Cohen's kappa calculation 2 | #' 3 | #' Tidy a "kappa" object, from the \code{\link{cohen.kappa}} function 4 | #' in the psych package. This represents the agreement of two raters 5 | #' when using nominal scores. 6 | #' 7 | #' @param x An object of class "kappa" 8 | #' @param ... extra arguments (not used) 9 | #' 10 | #' @return A data.frame with columns 11 | #' \item{type}{Either "weighted" or "unweighted"} 12 | #' \item{estimate}{The estimated value of kappa with this method} 13 | #' \item{conf.low}{Lower bound of confidence interval} 14 | #' \item{conf.high}{Upper bound of confidence interval} 15 | #' 16 | #' @details Note that the alpha of the confidence interval is determined 17 | #' when the \code{cohen.kappa} function is originally run. 18 | #' 19 | #' @seealso \code{\link{cohen.kappa}} 20 | #' 21 | #' @name kappa_tidiers 22 | #' 23 | #' @examples 24 | #' 25 | #' library(psych) 26 | #' 27 | #' rater1 = 1:9 28 | #' rater2 = c(1, 3, 1, 6, 1, 5, 5, 6, 7) 29 | #' ck <- cohen.kappa(cbind(rater1, rater2)) 30 | #' 31 | #' tidy(ck) 32 | #' 33 | #' # graph the confidence intervals 34 | #' library(ggplot2) 35 | #' ggplot(tidy(ck), aes(estimate, type)) + 36 | #' geom_point() + 37 | #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) 38 | #' 39 | #' @name anova_tidiers 40 | #' 41 | #' @export 42 | tidy.kappa <- function(x, ...) { 43 | nn <- c("conf.low", "estimate", "conf.high") 44 | ret <- fix_data_frame(x$confid, nn, newcol = "type") %>% 45 | select(type, estimate, conf.low, conf.high) %>% 46 | mutate(type = gsub(" kappa", "", type)) 47 | 48 | ret 49 | } 50 | -------------------------------------------------------------------------------- /man/multcomp_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/multcomp_tidiers.R 3 | \name{multcomp_tidiers} 4 | \alias{multcomp_tidiers} 5 | \alias{tidy.cld} 6 | \alias{tidy.confint.glht} 7 | \alias{tidy.glht} 8 | \alias{tidy.summary.glht} 9 | \title{tidying methods for objects produced by \pkg{multcomp}} 10 | \usage{ 11 | \method{tidy}{glht}(x, ...) 12 | 13 | \method{tidy}{confint.glht}(x, ...) 14 | 15 | \method{tidy}{summary.glht}(x, ...) 16 | 17 | \method{tidy}{cld}(x, ...) 18 | } 19 | \arguments{ 20 | \item{x}{an object of class \code{glht}, \code{confint.glht}, 21 | \code{summary.glht} or \code{\link[multcomp]{cld}}} 22 | 23 | \item{...}{extra arguments (not used)} 24 | } 25 | \description{ 26 | These methods originated in ggplot2, as "fortify." In broom, 27 | they were renamed "tidy" because they summarize terms and 28 | tests, rather than adding columns to a dataset. 29 | } 30 | \examples{ 31 | 32 | if (require("multcomp") && require("ggplot2")) { 33 | amod <- aov(breaks ~ wool + tension, data = warpbreaks) 34 | wht <- glht(amod, linfct = mcp(tension = "Tukey")) 35 | 36 | tidy(wht) 37 | ggplot(wht, aes(lhs, estimate)) + geom_point() 38 | 39 | CI <- confint(wht) 40 | tidy(CI) 41 | ggplot(CI, aes(lhs, estimate, ymin = lwr, ymax = upr)) + 42 | geom_pointrange() 43 | 44 | tidy(summary(wht)) 45 | ggplot(mapping = aes(lhs, estimate)) + 46 | geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) + 47 | geom_point(aes(size = p), data = summary(wht)) + 48 | scale_size(trans = "reverse") 49 | 50 | cld <- cld(wht) 51 | tidy(cld) 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /man/zoo_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/zoo_tidiers.R 3 | \name{zoo_tidiers} 4 | \alias{tidy.zoo} 5 | \alias{zoo_tidiers} 6 | \title{Tidying methods for a zoo object} 7 | \usage{ 8 | \method{tidy}{zoo}(x, ...) 9 | } 10 | \arguments{ 11 | \item{x}{An object of class \code{"zoo"}} 12 | 13 | \item{...}{extra arguments (not used)} 14 | } 15 | \value{ 16 | \code{tidy} returns a data frame with one row for each observation 17 | in each series, with the following columns: 18 | \item{index}{Index (usually date) for the zoo object} 19 | \item{series}{Name of the series} 20 | \item{value}{Value of the observation} 21 | } 22 | \description{ 23 | Tidies \code{zoo} (Z's ordered observations) time series objects. 24 | \code{zoo} objects are not tidy by default because they contain one row 25 | for each index and one series per column, rather than one row per 26 | observation per series. 27 | } 28 | \examples{ 29 | 30 | if (require("zoo", quietly = TRUE)) { 31 | set.seed(1071) 32 | 33 | # data generated as shown in the zoo vignette 34 | Z.index <- as.Date(sample(12450:12500, 10)) 35 | Z.data <- matrix(rnorm(30), ncol = 3) 36 | colnames(Z.data) <- c("Aa", "Bb", "Cc") 37 | Z <- zoo(Z.data, Z.index) 38 | 39 | tidy(Z) 40 | 41 | if (require("ggplot2", quietly = TRUE)) { 42 | ggplot(tidy(Z), aes(index, value, color = series)) + geom_line() 43 | ggplot(tidy(Z), aes(index, value)) + geom_line() + 44 | facet_wrap(~ series, ncol = 1) 45 | 46 | Zrolled <- rollmean(Z, 5) 47 | ggplot(tidy(Zrolled), aes(index, value, color = series)) + geom_line() 48 | } 49 | } 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /man/binDesign_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bingroup_tidiers.R 3 | \name{binDesign_tidiers} 4 | \alias{binDesign_tidiers} 5 | \alias{glance.binDesign} 6 | \alias{tidy.binDesign} 7 | \title{Tidy a binDesign object} 8 | \usage{ 9 | \method{tidy}{binDesign}(x, ...) 10 | 11 | \method{glance}{binDesign}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{A "binDesign" object} 15 | 16 | \item{...}{Extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | The \code{tidy} method returns a data.frame 23 | with one row for each iteration that was performed, 24 | with columns 25 | \item{n}{Number of trials in this iteration} 26 | \item{power}{The power achieved for this n} 27 | 28 | The \code{glance} method returns a one-row data.frame 29 | with columns 30 | \item{power}{The power achieved by the analysis} 31 | \item{n}{The sample size used to achieve this power} 32 | \item{power.reached}{Whether the desired power was reached} 33 | \item{maxit}{Number of iterations performed} 34 | } 35 | \description{ 36 | Tidy a binDesign object from the "binGroup" package, 37 | which determines the sample size needed for 38 | a particular power. 39 | } 40 | \examples{ 41 | 42 | if (require("binGroup", quietly = TRUE)) { 43 | des <- binDesign(nmax = 300, delta = 0.06, 44 | p.hyp = 0.1, power = .8) 45 | 46 | glance(des) 47 | head(tidy(des)) 48 | 49 | # the ggplot2 equivalent of plot(des) 50 | library(ggplot2) 51 | ggplot(tidy(des), aes(n, power)) + 52 | geom_line() 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /R/tidy.R: -------------------------------------------------------------------------------- 1 | #' Tidy the result of a test into a summary data.frame 2 | #' 3 | #' The output of tidy is always a data.frame with disposable row names. It is 4 | #' therefore suited for further manipulation by packages like dplyr, reshape2, 5 | #' ggplot2 and ggvis. 6 | #' 7 | #' @param x An object to be converted into a tidy data.frame 8 | #' @param ... extra arguments 9 | #' 10 | #' @return a data.frame 11 | #' 12 | #' @export 13 | tidy <- function(x, ...) UseMethod("tidy") 14 | 15 | 16 | #' tidy on a NULL input 17 | #' 18 | #' tidy on a NULL input returns an empty data frame, which means it can be 19 | #' combined with other data frames (treated as "empty") 20 | #' 21 | #' @param x A value NULL 22 | #' @param ... extra arguments (not used) 23 | #' 24 | #' @return An empty data.frame 25 | #' 26 | #' @export 27 | tidy.NULL <- function(x, ...) { 28 | data.frame() 29 | } 30 | 31 | 32 | #' Default tidying method 33 | #' 34 | #' By default, tidy uses \code{as.data.frame} to convert its output. This is 35 | #' dangerous, as it may fail with an uninformative error message. 36 | #' Generally tidy is intended to be used on structured model objects 37 | #' such as lm or htest for which a specific S3 object exists. 38 | #' 39 | #' If you know that you want to use \code{as.data.frame} on your untidy 40 | #' object, just use it directly. 41 | #' 42 | #' @param x an object to be tidied 43 | #' @param ... extra arguments (not used) 44 | #' 45 | #' @return A data frame, from \code{as.data.frame} applied to the input x. 46 | #' 47 | #' @export 48 | tidy.default <- function(x, ...) { 49 | warning(paste("No method for tidying an S3 object of class", 50 | class(x), ", using as.data.frame")) 51 | as.data.frame(x) 52 | } 53 | -------------------------------------------------------------------------------- /man/orcutt_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/orcutt_tidiers.R 3 | \name{orcutt_tidiers} 4 | \alias{glance.orcutt} 5 | \alias{orcutt_tidiers} 6 | \alias{tidy.orcutt} 7 | \title{Tidiers for Cochrane Orcutt object} 8 | \usage{ 9 | \method{tidy}{orcutt}(x, ...) 10 | 11 | \method{glance}{orcutt}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{An "orcutt" object returned by \code{cochrane.orcutt}} 15 | 16 | \item{...}{Extra arguments passed on to \code{\link{tidy.lm}}} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy} returns the same information as 23 | \code{\link{tidy.lm}}, though without confidence interval options. 24 | 25 | \code{glance}{} 26 | 27 | \code{glance} returns a one-row data frame with the following columns: 28 | \item{r.squared}{R-squared} 29 | \item{adj.r.squared}{Adjusted R-squared} 30 | \item{rho}{Spearman's rho autocorrelation} 31 | \item{number.interaction}{Number of interactions} 32 | \item{dw.original}{Durbin-Watson statistic of original fit} 33 | \item{p.value.original}{P-value of original Durbin-Watson statistic} 34 | \item{dw.transformed}{Durbin-Watson statistic of transformed fit} 35 | \item{p.value.transformed}{P-value of autocorrelation after transformation} 36 | } 37 | \description{ 38 | Tidies a Cochrane Orcutt object, which estimates autocorrelation 39 | and beta coefficients in a linear fit. 40 | } 41 | \examples{ 42 | 43 | reg <- lm(mpg ~ wt + qsec + disp, mtcars) 44 | tidy(reg) 45 | 46 | if (require("orcutt", quietly = TRUE)) { 47 | co <- cochrane.orcutt(reg) 48 | co 49 | 50 | tidy(co) 51 | glance(co) 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /R/zoo_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for a zoo object 2 | #' 3 | #' Tidies \code{zoo} (Z's ordered observations) time series objects. 4 | #' \code{zoo} objects are not tidy by default because they contain one row 5 | #' for each index and one series per column, rather than one row per 6 | #' observation per series. 7 | #' 8 | #' @param x An object of class \code{"zoo"} 9 | #' @param ... extra arguments (not used) 10 | #' 11 | #' @return \code{tidy} returns a data frame with one row for each observation 12 | #' in each series, with the following columns: 13 | #' \item{index}{Index (usually date) for the zoo object} 14 | #' \item{series}{Name of the series} 15 | #' \item{value}{Value of the observation} 16 | #' 17 | #' @name zoo_tidiers 18 | #' 19 | #' @examples 20 | #' 21 | #' if (require("zoo", quietly = TRUE)) { 22 | #' set.seed(1071) 23 | #' 24 | #' # data generated as shown in the zoo vignette 25 | #' Z.index <- as.Date(sample(12450:12500, 10)) 26 | #' Z.data <- matrix(rnorm(30), ncol = 3) 27 | #' colnames(Z.data) <- c("Aa", "Bb", "Cc") 28 | #' Z <- zoo(Z.data, Z.index) 29 | #' 30 | #' tidy(Z) 31 | #' 32 | #' if (require("ggplot2", quietly = TRUE)) { 33 | #' ggplot(tidy(Z), aes(index, value, color = series)) + geom_line() 34 | #' ggplot(tidy(Z), aes(index, value)) + geom_line() + 35 | #' facet_wrap(~ series, ncol = 1) 36 | #' 37 | #' Zrolled <- rollmean(Z, 5) 38 | #' ggplot(tidy(Zrolled), aes(index, value, color = series)) + geom_line() 39 | #' } 40 | #' } 41 | #' 42 | #' @export 43 | tidy.zoo <- function(x, ...) { 44 | ret <- data.frame(as.matrix(x), index = zoo::index(x)) 45 | ret <- tidyr::gather(ret, series, value, -index) 46 | ret 47 | } 48 | -------------------------------------------------------------------------------- /tests/testthat/test-rstanarm.R: -------------------------------------------------------------------------------- 1 | # test tidy and glance methods from rstanarm_tidiers.R 2 | 3 | context("rstanarm tidiers") 4 | suppressPackageStartupMessages(library(rstanarm)) 5 | 6 | if (require(rstanarm, quietly = TRUE)) { 7 | set.seed(2016) 8 | capture.output( 9 | fit <- stan_glmer(mpg ~ wt + (1|cyl) + (1+wt|gear), data = mtcars, 10 | iter = 200, chains = 2) 11 | ) 12 | 13 | context("rstanarm models") 14 | test_that("tidy works on rstanarm fits", { 15 | td1 <- tidy(fit) 16 | td2 <- tidy(fit, parameters = "varying") 17 | td3 <- tidy(fit, parameters = "hierarchical") 18 | td4 <- tidy(fit, parameters = "auxiliary") 19 | expect_equal(colnames(td1), c("term", "estimate", "std.error")) 20 | }) 21 | 22 | test_that("tidy with multiple 'parameters' selections works on rstanarm fits", { 23 | td1 <- tidy(fit, parameters = c("varying", "auxiliary")) 24 | expect_true(all(c("sigma", "mean_PPD") %in% td1$term)) 25 | expect_equal(colnames(td1), c("term", "estimate", "std.error", "level", "group")) 26 | }) 27 | 28 | test_that("intervals works on rstanarm fits", { 29 | td1 <- tidy(fit, intervals = TRUE, prob = 0.8) 30 | td2 <- tidy(fit, parameters = "varying", intervals = TRUE, prob = 0.5) 31 | nms <- c("level", "group", "term", "estimate", "std.error", "lower", "upper") 32 | expect_equal(colnames(td2), nms) 33 | }) 34 | 35 | test_that("glance works on rstanarm fits", { 36 | g1 <- glance(fit) 37 | g2 <- glance(fit, looic = TRUE, cores = 1) 38 | expect_equal(colnames(g1), c("algorithm", "pss", "nobs", "sigma")) 39 | expect_equal(colnames(g2), c(colnames(g1), "looic", "elpd_loo", "p_loo")) 40 | }) 41 | } 42 | -------------------------------------------------------------------------------- /R/extras.R: -------------------------------------------------------------------------------- 1 | # this contains functions that are useful to use with broom, but are not 2 | # actually involved in tidying objects. 3 | 4 | #' Set up bootstrap replicates of a dplyr operation 5 | #' 6 | #' @param df a data frame 7 | #' @param m number of bootstrap replicates to perform 8 | #' @param by_group If \code{TRUE}, then bootstrap within each group if \code{df} is 9 | #' a grouped tbl. 10 | #' 11 | #' @details This code originates from Hadley Wickham (with a few small 12 | #' corrections) here: 13 | #' 14 | #' https://github.com/hadley/dplyr/issues/269 15 | #' 16 | #' Some examples can be found at 17 | #' 18 | #' https://github.com/dgrtwo/broom/blob/master/vignettes/bootstrapping.Rmd 19 | #' 20 | #' @examples 21 | #' 22 | #' library(dplyr) 23 | #' mtcars %>% bootstrap(10) %>% do(tidy(lm(mpg ~ wt, .))) 24 | #' 25 | #' @export 26 | bootstrap <- function (df, m, by_group = FALSE) { 27 | n <- nrow(df) 28 | attr(df, "indices") <- 29 | if (by_group && !is.null(groups(df))) { 30 | replicate(m, 31 | unlist(lapply(attr(df, "indices"), 32 | function(x) { 33 | sample(x, replace = TRUE) 34 | }), 35 | recursive = FALSE, use.names = FALSE), 36 | simplify = FALSE) 37 | } else { 38 | replicate(m, sample(n, replace = TRUE) - 39 | 1, simplify = FALSE) 40 | } 41 | attr(df, "drop") <- TRUE 42 | attr(df, "group_sizes") <- rep(n, m) 43 | attr(df, "biggest_group_size") <- n 44 | attr(df, "labels") <- data.frame(replicate = 1:m) 45 | attr(df, "vars") <- list(quote(replicate)) 46 | class(df) <- c("grouped_df", "tbl_df", "tbl", "data.frame") 47 | df 48 | } 49 | -------------------------------------------------------------------------------- /R/optim_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for lists returned from optim 2 | #' 3 | #' Tidies objects returned by the \code{\link{optim}} function for 4 | #' general-purpose minimization and maximization. 5 | #' 6 | #' @param x list returned from \code{optim} 7 | #' @param ... extra arguments 8 | #' 9 | #' @template boilerplate 10 | #' 11 | #' @return \code{tidy} returns a data frame with one row per parameter that 12 | #' was estimated, with columns 13 | #' \item{parameter}{name of the parameter, or \code{parameter1}, 14 | #' \code{parameter2}... if the input vector is not named} 15 | #' \item{value}{parameter value that minimizes or maximizes the output} 16 | #' 17 | #' @examples 18 | #' 19 | #' func <- function(x) { 20 | #' (x[1] - 2)^2 + (x[2] - 3)^2 + (x[3] - 8)^2 21 | #' } 22 | #' 23 | #' o <- optim(c(1, 1, 1), func) 24 | #' 25 | #' tidy(o) 26 | #' glance(o) 27 | #' 28 | #' @name optim_tidiers 29 | tidy_optim <- function(x, ...) { 30 | if (is.null(names(x$par))) { 31 | names(x$par) <- paste0("parameter", seq_along(x$par)) 32 | } 33 | data.frame(parameter = names(x$par), value = unname(x$par)) 34 | } 35 | 36 | 37 | #' @rdname optim_tidiers 38 | #' 39 | #' @return \code{glance} returns a one-row data frame with the columns 40 | #' \item{value}{minimized or maximized output value} 41 | #' \item{function.count}{number of calls to \code{fn}} 42 | #' \item{gradient.count}{number of calls to \code{gr}} 43 | #' \item{convergence}{convergence code representing the error state} 44 | #' 45 | #' @seealso \code{\link{optim}} 46 | glance_optim <- function(x, ...) { 47 | unrowname(data.frame(value = x$value, 48 | function.count = x$counts["function"], 49 | gradient.count = x$counts["gradient"], 50 | convergence = x$convergence)) 51 | } 52 | -------------------------------------------------------------------------------- /R/smooth.spline_tidiers.R: -------------------------------------------------------------------------------- 1 | #' tidying methods for smooth.spline objects 2 | #' 3 | #' This combines the original data given to smooth.spline with the 4 | #' fit and residuals 5 | #' 6 | #' @details No \code{tidy} method is provided for smooth.spline objects. 7 | #' 8 | #' @param x a smooth.spline object 9 | #' @param data defaults to data used to fit model 10 | #' @param ... not used in this method 11 | #' 12 | #' @examples 13 | #' 14 | #' spl <- smooth.spline(mtcars$wt, mtcars$mpg, df = 4) 15 | #' head(augment(spl, mtcars)) 16 | #' head(augment(spl)) # calls original columns x and y 17 | #' 18 | #' library(ggplot2) 19 | #' ggplot(augment(spl, mtcars), aes(wt, mpg)) + 20 | #' geom_point() + geom_line(aes(y = .fitted)) 21 | #' 22 | #' @name smooth.spline_tidiers 23 | 24 | 25 | #' @rdname smooth.spline_tidiers 26 | #' 27 | #' @return \code{augment} returns the original data with extra columns: 28 | #' \item{.fitted}{Fitted values of model} 29 | #' \item{.resid}{Residuals} 30 | #' 31 | #' @export 32 | augment.smooth.spline <- function(x, data = x$data, ...) { 33 | # move rownames if necessary 34 | data <- unrowname(as.data.frame(data)) 35 | 36 | data <- as.data.frame(data) 37 | data$.fitted <- stats::fitted(x) 38 | data$.resid <- stats::resid(x) 39 | data 40 | } 41 | 42 | 43 | #' @rdname smooth.spline_tidiers 44 | #' 45 | #' @return \code{glance} returns one row with columns 46 | #' \item{spar}{smoothing parameter} 47 | #' \item{lambda}{choice of lambda corresponding to \code{spar}} 48 | #' \item{df}{equivalent degrees of freedom} 49 | #' \item{crit}{minimized criterion} 50 | #' \item{pen.crit}{penalized criterion} 51 | #' \item{cv.crit}{cross-validation score} 52 | #' 53 | #' @export 54 | glance.smooth.spline <- function(x, ...) { 55 | unrowname(as.data.frame( 56 | x[c("df", "lambda", "cv.crit", "pen.crit", "crit", "spar", "lambda")] 57 | )) 58 | } 59 | -------------------------------------------------------------------------------- /tests/testthat/test-dplyr.R: -------------------------------------------------------------------------------- 1 | context("dplyr and broom") 2 | 3 | suppressPackageStartupMessages(library(dplyr)) 4 | 5 | # set up the lahman batting table, and filter to make it faster 6 | batting <- tbl(src_df("Lahman"), "Batting") 7 | batting <- batting %>% filter(yearID > 1980) 8 | 9 | lm0 <- failwith(NULL, lm, quiet = TRUE) 10 | 11 | test_that("can perform regressions with tidying in dplyr", { 12 | regressions <- batting %>% group_by(yearID) %>% do(tidy(lm0(SB ~ CS, data=.))) 13 | 14 | expect_lt(30, nrow(regressions)) 15 | expect_true(all(c("yearID", "estimate", "statistic", "p.value") %in% 16 | colnames(regressions))) 17 | }) 18 | 19 | test_that("tidying methods work with rowwise_df", { 20 | regressions <- batting %>% group_by(yearID) %>% do(mod = lm0(SB ~ CS, data=.)) 21 | tidied <- regressions %>% tidy(mod) 22 | augmented <- regressions %>% augment(mod) 23 | glanced <- regressions %>% glance(mod) 24 | 25 | num_years <- length(unique(batting$yearID)) 26 | expect_equal(nrow(tidied), num_years * 2) 27 | expect_equal(nrow(augmented), sum(!is.na(batting$SB) & !is.na(batting$CS))) 28 | expect_equal(nrow(glanced), num_years) 29 | }) 30 | 31 | 32 | test_that("can perform correlations with tidying in dplyr", { 33 | cor.test0 <- purrr::possibly(cor.test, NULL) 34 | pcors <- batting %>% group_by(yearID) %>% do(tidy(cor.test0(.$SB, .$CS))) 35 | expect_true(all(c("yearID", "estimate", "statistic", "p.value") %in% 36 | colnames(pcors))) 37 | expect_lt(30, nrow(pcors)) 38 | 39 | scors <- suppressWarnings(batting %>% group_by(yearID) %>% 40 | do(tidy(cor.test0(.$SB, .$CS, method="spearman")))) 41 | expect_true(all(c("yearID", "estimate", "statistic", "p.value") %in% 42 | colnames(scors))) 43 | expect_lt(30, nrow(scors)) 44 | expect_false(all(pcors$estimate == scors$estimate)) 45 | }) 46 | -------------------------------------------------------------------------------- /man/rcorr_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rcorr_tidiers.R 3 | \name{rcorr_tidiers} 4 | \alias{rcorr_tidiers} 5 | \alias{tidy.rcorr} 6 | \title{Tidying methods for rcorr objects} 7 | \usage{ 8 | \method{tidy}{rcorr}(x, diagonal = FALSE, ...) 9 | } 10 | \arguments{ 11 | \item{x}{An object of class "rcorr"} 12 | 13 | \item{diagonal}{Whether to include diagonal elements (where 14 | \code{estimate} is 1 and \code{p.value} is NA), default FALSE} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | A data.frame with one row for each pairing 20 | in the correlation matrix. Columns are: 21 | \item{column1}{Name or index of the first column being described} 22 | \item{column2}{Name or index of the second column being described} 23 | \item{estimate}{Estimate of Pearson's r or Spearman's rho} 24 | \item{n}{Number of observations used to compute the correlation} 25 | \item{p.value}{P-value of correlation} 26 | } 27 | \description{ 28 | Tidies a correlation matrix from the \code{rcorr} function in the 29 | "Hmisc" package, including correlation estimates, p-values, 30 | and the number of observations in each pairwise correlation. 31 | Note that it returns these in "long", or "melted", format, 32 | with one row for each pair of columns being compared. 33 | } 34 | \details{ 35 | Only half the symmetric matrix is shown. 36 | } 37 | \examples{ 38 | 39 | if (require("Hmisc", quietly = TRUE)) { 40 | mat <- replicate(52, rnorm(100)) 41 | # add some NAs 42 | mat[sample(length(mat), 2000)] <- NA 43 | # also column names 44 | colnames(mat) <- c(LETTERS, letters) 45 | 46 | rc <- rcorr(mat) 47 | 48 | td <- tidy(rc) 49 | head(td) 50 | 51 | library(ggplot2) 52 | ggplot(td, aes(p.value)) + 53 | geom_histogram(binwidth = .1) 54 | 55 | ggplot(td, aes(estimate, p.value)) + 56 | geom_point() + 57 | scale_y_log10() 58 | } 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /man/Arima_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/arima_tidiers.R 3 | \name{Arima_tidiers} 4 | \alias{Arima_tidiers} 5 | \alias{glance.Arima} 6 | \alias{tidy.Arima} 7 | \title{Tidying methods for ARIMA modeling of time series} 8 | \usage{ 9 | \method{tidy}{Arima}(x, conf.int = FALSE, conf.level = 0.95, ...) 10 | 11 | \method{glance}{Arima}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{An object of class "Arima"} 15 | 16 | \item{conf.int}{whether to include a confidence interval} 17 | 18 | \item{conf.level}{confidence level of the interval, used only if 19 | \code{conf.int=TRUE}} 20 | 21 | \item{...}{extra arguments (not used)} 22 | } 23 | \value{ 24 | All tidying methods return a data.frame without rownames, whose 25 | structure depends on the method chosen. 26 | 27 | \code{tidy} returns one row for each coefficient in the model, 28 | with five columns: 29 | \item{term}{The term in the nonlinear model being estimated and tested} 30 | \item{estimate}{The estimated coefficient} 31 | \item{std.error}{The standard error from the linear model} 32 | 33 | If \code{conf.int = TRUE}, also returns 34 | \item{conf.low}{low end of confidence interval} 35 | \item{conf.high}{high end of confidence interval} 36 | 37 | \code{glance} returns one row with the columns 38 | \item{sigma}{the square root of the estimated residual variance} 39 | \item{logLik}{the data's log-likelihood under the model} 40 | \item{AIC}{the Akaike Information Criterion} 41 | \item{BIC}{the Bayesian Information Criterion} 42 | } 43 | \description{ 44 | These methods tidy the coefficients of ARIMA models of univariate time 45 | series. 46 | } 47 | \details{ 48 | \code{augment} is not currently implemented, as it is not clear 49 | whether ARIMA predictions can or should be merged with the original 50 | data frame. 51 | } 52 | \examples{ 53 | 54 | fit <- arima(lh, order = c(1, 0, 0)) 55 | tidy(fit) 56 | glance(fit) 57 | 58 | } 59 | \seealso{ 60 | \link{arima} 61 | } 62 | 63 | -------------------------------------------------------------------------------- /man/htest_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/htest_tidiers.R 3 | \name{htest_tidiers} 4 | \alias{glance.htest} 5 | \alias{htest_tidiers} 6 | \alias{tidy.htest} 7 | \title{Tidying methods for an htest object} 8 | \usage{ 9 | \method{tidy}{htest}(x, ...) 10 | 11 | \method{glance}{htest}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{An object of class \code{"htest"}} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | Both \code{tidy} and \code{glance} return the same output, 20 | a one-row data frame with one or more of the following columns: 21 | \item{estimate}{Estimate of the effect size} 22 | \item{statistic}{Test statistic used to compute the p-value} 23 | \item{p.value}{P-value} 24 | \item{parameter}{Parameter field in the htest, typically degrees of 25 | freedom} 26 | \item{conf.low}{Lower bound on a confidence interval} 27 | \item{conf.high}{Upper bound on a confidence interval} 28 | \item{estimate1}{Sometimes two estimates are computed, such as in a 29 | two-sample t-test} 30 | \item{estimate2}{Sometimes two estimates are computed, such as in a 31 | two-sample t-test} 32 | \item{method}{Method used to compute the statistic as a string} 33 | \item{alternative}{Alternative hypothesis as a string} 34 | 35 | Which columns are included depends on the hypothesis test used. 36 | } 37 | \description{ 38 | Tidies hypothesis test objects, such as those from \code{cor.test}, 39 | \code{t.test}, and \code{wilcox.test}, into a one-row data frame. 40 | } 41 | \details{ 42 | No \code{augment} method is provided for \code{"htest"}, 43 | since there is no sense in which a hypothesis test generates one 44 | value for each observation. 45 | } 46 | \examples{ 47 | 48 | tt <- t.test(rnorm(10)) 49 | tidy(tt) 50 | glance(tt) # same output for all htests 51 | 52 | tt <- t.test(mpg ~ am, data = mtcars) 53 | tidy(tt) 54 | 55 | wt <- wilcox.test(mpg ~ am, data = mtcars) 56 | tidy(wt) 57 | 58 | ct <- cor.test(mtcars$wt, mtcars$mpg) 59 | tidy(ct) 60 | 61 | } 62 | 63 | -------------------------------------------------------------------------------- /R/gamlss_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for gamlss objects 2 | #' 3 | #' Tidying methods for "gamlss" objects from the gamlss package. 4 | #' 5 | #' @param x A "gamlss" object 6 | #' @param quick Whether to perform a fast version, and return only the coefficients 7 | #' @param ... Extra arguments (not used) 8 | #' 9 | #' @name gamlss_tidiers 10 | #' 11 | #' @template boilerplate 12 | #' 13 | #' @return A data.frame with one row for each coefficient, containing columns 14 | #' \item{parameter}{Type of coefficient being estimated: \code{mu}, \code{sigma}, 15 | #' \code{nu}, or \code{tau}} 16 | #' \item{term}{The term in the model being estimated and tested} 17 | #' \item{estimate}{The estimated coefficient} 18 | #' \item{std.error}{The standard error from the linear model} 19 | #' \item{statistic}{t-statistic} 20 | #' \item{p.value}{two-sided p-value} 21 | #' 22 | #' if (requireNamespace("gamlss", quietly = TRUE)) { 23 | #' data(abdom) 24 | #' mod<-gamlss(y~pb(x),sigma.fo=~pb(x),family=BCT, data=abdom, method=mixed(1,20)) 25 | #' 26 | #' tidy(mod) 27 | #' } 28 | #' 29 | #' @export 30 | tidy.gamlss <- function(x, quick = FALSE, ...){ 31 | if (quick) { 32 | co <- stats::coef(x) 33 | return(data.frame(term = names(co), estimate = unname(co))) 34 | } 35 | 36 | # need gamlss for summary to work 37 | if (!requireNamespace("gamlss", quietly = TRUE)) { 38 | stop("gamlss package not installed, cannot tidy gamlss") 39 | } 40 | 41 | # use capture.output to prevent summary from being printed to screen 42 | utils::capture.output(s <- summary(x, type = "qr")) 43 | 44 | # tidy the coefficients much as would be done for a linear model 45 | nn <- c("estimate", "std.error", "statistic", "p.value") 46 | ret <- fix_data_frame(s, nn) 47 | 48 | # add parameter types. This assumes each coefficient table starts 49 | # with "(Intercept)": unclear if this is guaranteed 50 | parameters <- x$parameters[cumsum(ret$term == "(Intercept)")] 51 | cbind(parameter = parameters, ret) 52 | } 53 | -------------------------------------------------------------------------------- /man/robust_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/robust_tidiers.R 3 | \name{robust_tidiers} 4 | \alias{augment.glmRob} 5 | \alias{augment.lmRob} 6 | \alias{glance.glmRob} 7 | \alias{glance.lmRob} 8 | \alias{robust_tidiers} 9 | \alias{tidy.glmRob} 10 | \alias{tidy.lmRob} 11 | \title{Tidiers for lmRob and glmRob objects} 12 | \usage{ 13 | \method{tidy}{lmRob}(x, ...) 14 | 15 | \method{augment}{lmRob}(x, ...) 16 | 17 | \method{glance}{lmRob}(x, ...) 18 | 19 | \method{tidy}{glmRob}(x, ...) 20 | 21 | \method{augment}{glmRob}(x, ...) 22 | 23 | \method{glance}{glmRob}(x, ...) 24 | } 25 | \arguments{ 26 | \item{x}{An lmRob or glmRob object with a robust regression} 27 | 28 | \item{...}{Extra arguments, not used} 29 | } 30 | \value{ 31 | All tidying methods return a data.frame without rownames, whose 32 | structure depends on the method chosen. 33 | 34 | \code{tidy} and \code{augment} return the same results as \code{\link{lm_tidiers}}. 35 | 36 | On an \code{lmRob} \code{glance} returns a one-row data frame with the following columns: 37 | \item{r.squared}{R-squared} 38 | \item{deviance}{Robust deviance} 39 | \item{sigma}{Residual scale estimate} 40 | \item{df.residual}{Number of residual degrees of freedom} 41 | 42 | On an \code{lmRob} \code{glance} returns a one-row data frame with the following columns: 43 | \item{deviance}{Robust deviance} 44 | \item{null.deviance}{Deviance under the null model} 45 | \item{df.residual}{Number of residual degrees of freedom} 46 | } 47 | \description{ 48 | Tidying robust regression objects from the robust package. The tidy and augment 49 | methods simply pass it on to the linear model tidiers. 50 | } 51 | \examples{ 52 | 53 | if (require("robust", quietly = TRUE)) { 54 | m <- lmRob(mpg ~ wt, data = mtcars) 55 | 56 | tidy(m) 57 | augment(m) 58 | glance(m) 59 | 60 | gm <- glmRob(am ~ wt, data = mtcars, family = "binomial") 61 | glance(gm) 62 | } 63 | 64 | } 65 | \seealso{ 66 | \code{\link{lm_tidiers}}, \code{\link[robust]{lmRob}}, \code{\link[robust]{glmRob}} 67 | } 68 | 69 | -------------------------------------------------------------------------------- /man/boot_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boot_tidiers.R 3 | \name{boot_tidiers} 4 | \alias{boot_tidiers} 5 | \alias{tidy.boot} 6 | \title{Tidying methods for bootstrap computations} 7 | \usage{ 8 | \method{tidy}{boot}(x, conf.int = FALSE, conf.level = 0.95, 9 | conf.method = "perc", ...) 10 | } 11 | \arguments{ 12 | \item{x}{\code{\link{boot}} object} 13 | 14 | \item{conf.int}{whether to include a confidence interval} 15 | 16 | \item{conf.level}{confidence level for CI} 17 | 18 | \item{conf.method}{method for computing confidence intervals (see \code{\link{boot.ci}})} 19 | 20 | \item{\dots}{extra arguments (not used)} 21 | } 22 | \value{ 23 | The \code{tidy} method returns a data frame with one row per 24 | bootstrapped statistic that was calculated, and the 25 | following columns: 26 | \item{term}{Name of the computed statistic, if present} 27 | \item{statistic}{The original values of the statistic} 28 | \item{bias}{The bias of the original statistic value} 29 | \item{std.error}{Standard error of the statistic} 30 | 31 | If weights were provided to the \code{boot} function, an \code{estimate} 32 | column is included showing the weighted bootstrap estimate, and the 33 | standard error is of that estimate. 34 | 35 | If there are no original statistics in the "boot" object, such as with a 36 | call to \code{tsboot} with \code{orig.t = FALSE}, the \code{original} 37 | and \code{statistic} columns are omitted, and only \code{estimate} and 38 | \code{std.error} columns shown. 39 | } 40 | \description{ 41 | Tidying methods for "boot" objects from the "boot" package. 42 | } 43 | \examples{ 44 | if (require("boot")) { 45 | clotting <- data.frame( 46 | u = c(5,10,15,20,30,40,60,80,100), 47 | lot1 = c(118,58,42,35,27,25,21,19,18), 48 | lot2 = c(69,35,26,21,18,16,13,12,12)) 49 | 50 | g1 <- glm(lot2 ~ log(u), data = clotting, family = Gamma) 51 | 52 | bootfun <- function(d, i) { 53 | coef(update(g1, data= d[i,])) 54 | } 55 | bootres <- boot(clotting, bootfun, R = 999) 56 | tidy(g1, conf.int=TRUE) 57 | tidy(bootres, conf.int=TRUE) 58 | } 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /R/orcutt_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for Cochrane Orcutt object 2 | #' 3 | #' Tidies a Cochrane Orcutt object, which estimates autocorrelation 4 | #' and beta coefficients in a linear fit. 5 | #' 6 | #' @param x An "orcutt" object returned by \code{cochrane.orcutt} 7 | #' @param ... Extra arguments passed on to \code{\link{tidy.lm}} 8 | #' 9 | #' @template boilerplate 10 | #' 11 | #' @return \code{tidy} returns the same information as 12 | #' \code{\link{tidy.lm}}, though without confidence interval options. 13 | #' 14 | #' @return \code{glance}{} 15 | #' 16 | #' @name orcutt_tidiers 17 | #' 18 | #' @examples 19 | #' 20 | #' reg <- lm(mpg ~ wt + qsec + disp, mtcars) 21 | #' tidy(reg) 22 | #' 23 | #' if (require("orcutt", quietly = TRUE)) { 24 | #' co <- cochrane.orcutt(reg) 25 | #' co 26 | #' 27 | #' tidy(co) 28 | #' glance(co) 29 | #' } 30 | #' 31 | #' @export 32 | tidy.orcutt <- function(x, ...) { 33 | tidy.lm(x, ...) 34 | } 35 | 36 | 37 | #' @rdname orcutt_tidiers 38 | #' 39 | #' @return \code{glance} returns a one-row data frame with the following columns: 40 | #' \item{r.squared}{R-squared} 41 | #' \item{adj.r.squared}{Adjusted R-squared} 42 | #' \item{rho}{Spearman's rho autocorrelation} 43 | #' \item{number.interaction}{Number of interactions} 44 | #' \item{dw.original}{Durbin-Watson statistic of original fit} 45 | #' \item{p.value.original}{P-value of original Durbin-Watson statistic} 46 | #' \item{dw.transformed}{Durbin-Watson statistic of transformed fit} 47 | #' \item{p.value.transformed}{P-value of autocorrelation after transformation} 48 | #' 49 | #' @export 50 | glance.orcutt <- function(x, ...) { 51 | ret <- data.frame(r.squared = x$r.squared, 52 | adj.r.squared = x$adj.r.squared, 53 | rho = x$rho, 54 | number.interaction = x$number.interaction, 55 | dw.original = x$DW[1], 56 | p.value.original = x$DW[2], 57 | dw.transformed = x$DW[3], 58 | p.value.transformed = x$DW[4]) 59 | 60 | ret$rho <- x$rho 61 | ret$number.interaction <- x$number.interaction 62 | 63 | ret 64 | } 65 | -------------------------------------------------------------------------------- /R/rcorr_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for rcorr objects 2 | #' 3 | #' Tidies a correlation matrix from the \code{rcorr} function in the 4 | #' "Hmisc" package, including correlation estimates, p-values, 5 | #' and the number of observations in each pairwise correlation. 6 | #' Note that it returns these in "long", or "melted", format, 7 | #' with one row for each pair of columns being compared. 8 | #' 9 | #' @param x An object of class "rcorr" 10 | #' @param diagonal Whether to include diagonal elements (where 11 | #' \code{estimate} is 1 and \code{p.value} is NA), default FALSE 12 | #' @param ... extra arguments (not used) 13 | #' 14 | #' @return A data.frame with one row for each pairing 15 | #' in the correlation matrix. Columns are: 16 | #' \item{column1}{Name or index of the first column being described} 17 | #' \item{column2}{Name or index of the second column being described} 18 | #' \item{estimate}{Estimate of Pearson's r or Spearman's rho} 19 | #' \item{n}{Number of observations used to compute the correlation} 20 | #' \item{p.value}{P-value of correlation} 21 | #' 22 | #' @details Only half the symmetric matrix is shown. 23 | #' 24 | #' @examples 25 | #' 26 | #' if (require("Hmisc", quietly = TRUE)) { 27 | #' mat <- replicate(52, rnorm(100)) 28 | #' # add some NAs 29 | #' mat[sample(length(mat), 2000)] <- NA 30 | #' # also column names 31 | #' colnames(mat) <- c(LETTERS, letters) 32 | #' 33 | #' rc <- rcorr(mat) 34 | #' 35 | #' td <- tidy(rc) 36 | #' head(td) 37 | #' 38 | #' library(ggplot2) 39 | #' ggplot(td, aes(p.value)) + 40 | #' geom_histogram(binwidth = .1) 41 | #' 42 | #' ggplot(td, aes(estimate, p.value)) + 43 | #' geom_point() + 44 | #' scale_y_log10() 45 | #' } 46 | #' 47 | #' @name rcorr_tidiers 48 | #' 49 | #' @export 50 | tidy.rcorr <- function(x, diagonal = FALSE, ...) { 51 | ret <- reshape2::melt(x$r, varnames = c("column1", "column2"), 52 | value.name = "estimate") 53 | ret$n <- c(x$n) 54 | ret$p.value <- c(x$P) 55 | 56 | # include only half the symmetric matrix. 57 | ret <- ret[upper.tri(x$r, diag = diagonal), ] 58 | 59 | ret 60 | } 61 | -------------------------------------------------------------------------------- /man/gam_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/gam_tidiers.R 3 | \name{gam_tidiers} 4 | \alias{gam_tidiers} 5 | \alias{tidy.gam} 6 | \alias{glance.gam} 7 | \title{Tidying methods for a generalized additive model (gam)} 8 | \usage{ 9 | \method{tidy}{gam}(x, parametric = FALSE, ...) 10 | 11 | \method{glance}{gam}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{gam object} 15 | 16 | \item{parametric}{logical. Return parametric coefficients (\code{TRUE}) or 17 | information about smooth terms (\code{FALSE})?} 18 | 19 | \item{...}{extra arguments (not used)} 20 | } 21 | \value{ 22 | All tidying methods return a data.frame without rownames, whose 23 | structure depends on the method chosen. 24 | 25 | \code{tidy.gam} called on an object from the gam package, 26 | or an object from the mgcv package with \code{parametric = FALSE}, returns the 27 | tidied output of the parametric ANOVA with one row for each term in the formula. 28 | The columns match those in \link{anova_tidiers}. 29 | \code{tidy.gam} called on a gam object from the mgcv package with 30 | \code{parametric = TRUE} returns the fixed coefficients. 31 | 32 | \code{glance.gam} returns a one-row data.frame with the columns 33 | \item{df}{Degrees of freedom used by the coefficients} 34 | \item{logLik}{the data's log-likelihood under the model} 35 | \item{AIC}{the Akaike Information Criterion} 36 | \item{BIC}{the Bayesian Information Criterion} 37 | \item{deviance}{deviance} 38 | \item{df.residual}{residual degrees of freedom} 39 | } 40 | \description{ 41 | These methods tidy the coefficients of a "gam" object (generalized additive 42 | model) into a summary, augment the original data with information on the 43 | fitted values and residuals, and construct a one-row glance of the model's 44 | statistics. 45 | } 46 | \details{ 47 | The "augment" method is handled by \link{lm_tidiers}. 48 | } 49 | \examples{ 50 | 51 | if (require("gam", quietly = TRUE)) { 52 | data(kyphosis) 53 | g <- gam(Kyphosis ~ s(Age,4) + Number, family = binomial, data = kyphosis) 54 | tidy(g) 55 | augment(g) 56 | glance(g) 57 | } 58 | 59 | } 60 | \seealso{ 61 | \link{lm_tidiers}, \link{anova_tidiers} 62 | } 63 | -------------------------------------------------------------------------------- /man/loess_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/loess_tidiers.R 3 | \name{loess_tidiers} 4 | \alias{augment.loess} 5 | \alias{loess_tidiers} 6 | \title{Augmenting methods for loess models} 7 | \usage{ 8 | \method{augment}{loess}(x, data = stats::model.frame(x), newdata, ...) 9 | } 10 | \arguments{ 11 | \item{x}{A "loess" object} 12 | 13 | \item{data}{Original data, defaults to the extracting it from the model} 14 | 15 | \item{newdata}{If provided, performs predictions on the new data} 16 | 17 | \item{...}{extra arguments} 18 | } 19 | \value{ 20 | When \code{newdata} is not supplied \code{augment.loess} 21 | returns one row for each observation with three columns added 22 | to the original data: 23 | \item{.fitted}{Fitted values of model} 24 | \item{.se.fit}{Standard errors of the fitted values} 25 | \item{.resid}{Residuals of the fitted values} 26 | 27 | When \code{newdata} is supplied \code{augment.loess} returns 28 | one row for each observation with one additional column: 29 | \item{.fitted}{Fitted values of model} 30 | \item{.se.fit}{Standard errors of the fitted values} 31 | } 32 | \description{ 33 | This method augments the original data with information 34 | on the fitted values and residuals, and optionally the 35 | standard errors. 36 | } 37 | \details{ 38 | When the modeling was performed with \code{na.action = "na.omit"} 39 | (as is the typical default), rows with NA in the initial data are omitted 40 | entirely from the augmented data frame. When the modeling was performed 41 | with \code{na.action = "na.exclude"}, one should provide the original data 42 | as a second argument, at which point the augmented data will contain those 43 | rows (typically with NAs in place of the new columns). If the original data 44 | is not provided to \code{augment} and \code{na.action = "na.exclude"}, a 45 | warning is raised and the incomplete rows are dropped. 46 | } 47 | \examples{ 48 | 49 | lo <- loess(mpg ~ wt, mtcars) 50 | augment(lo) 51 | 52 | # with all columns of original data 53 | augment(lo, mtcars) 54 | 55 | # with a new dataset 56 | augment(lo, newdata = head(mtcars)) 57 | 58 | } 59 | \seealso{ 60 | \link{na.action} 61 | } 62 | 63 | -------------------------------------------------------------------------------- /R/list_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for return values from functions that aren't S3 objects 2 | #' 3 | #' This method handles the return values of functions that return lists 4 | #' rather than S3 objects, such as \code{optim}, \code{svd}, or 5 | #' \code{\link[akima]{interp}}, and therefore cannot be handled by 6 | #' S3 dispatch. 7 | #' 8 | #' @param x list object 9 | #' @param ... extra arguments, passed to the tidying function 10 | #' 11 | #' @details Those tiders themselves are implemented as functions of the 12 | #' form tidy_ or glance_ that are not exported. 13 | #' 14 | #' @seealso \link{optim_tidiers}, \link{xyz_tidiers}, 15 | #' \link{svd_tidiers}, \link{orcutt_tidiers} 16 | #' 17 | #' @name list_tidiers 18 | #' 19 | #' @export 20 | tidy.list <- function(x, ...) { 21 | if (all(c("par", "value", "counts", "convergence", "message") 22 | %in% names(x))) { 23 | # returned from optim 24 | tidy_optim(x, ...) 25 | } else if (all(c("x", "y", "z") %in% names(x)) & is.matrix(x$z)) { 26 | if ( length(x$x) != nrow(x$z) ) { 27 | stop("The list looks like an x,y,z list but is not. Element x of the list needs to be the same length as the number of rows of element z") 28 | } 29 | if ( length(x$y) != ncol(x$z) ) { 30 | stop("The list looks like an x,y,z list but is not. Element y of the list needs to be the same length as the number of columns of element z") 31 | } 32 | # xyz list suitable for persp, image, etc. 33 | tidy_xyz(x, ...) 34 | } else if (all(sort(names(x)) == c("d", "u", "v"))) { 35 | tidy_svd(x, ...) 36 | } else if ("Cochrane.Orcutt" %in% names(x)) { 37 | tidy.orcutt(x, ...) 38 | } else { 39 | stop("No tidying method recognized for this list") 40 | } 41 | } 42 | 43 | 44 | #' @rdname list_tidiers 45 | #' 46 | #' @export 47 | glance.list <- function(x, ...) { 48 | if (all(c("par", "value", "counts", "convergence", "message") %in% names(x))) { 49 | glance_optim(x, ...) 50 | } else if ("Cochrane.Orcutt" %in% names(x)) { 51 | glance.orcutt(x, ...) 52 | } else { 53 | stop("No glance method recognized for this list") 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/testthat/test-rowwise.R: -------------------------------------------------------------------------------- 1 | # test tidiers for rowwise data frames (that contain individual 2 | # objects as a list column, see ?rowwise_df_tidiers) 3 | 4 | context("rowwise tidiers") 5 | 6 | mods <- mtcars %>% 7 | group_by(cyl) %>% 8 | do(mod = lm(mpg ~ wt + qsec, .)) 9 | 10 | test_that("rowwise tidiers can be applied to sub-models", { 11 | expect_is(mods, "rowwise_df") 12 | 13 | tidied <- mods %>% tidy(mod) 14 | augmented <- mods %>% augment(mod) 15 | glanced <- mods %>% glance(mod) 16 | 17 | expect_equal(nrow(augmented), nrow(mtcars)) 18 | expect_equal(nrow(glanced), 3) 19 | expect_true(!("disp" %in% colnames(augmented))) 20 | }) 21 | 22 | test_that("rowwise tidiers can be given additional arguments", { 23 | augmented <- mods %>% augment(mod, newdata = head(mtcars, 5)) 24 | expect_equal(nrow(augmented), 3 * 5) 25 | }) 26 | 27 | test_that("rowwise augment can use a column as the data", { 28 | mods <- mtcars %>% 29 | group_by(cyl) %>% 30 | do(mod = lm(mpg ~ wt + qsec, .), data = (.)) 31 | 32 | expect_is(mods, "rowwise_df") 33 | augmented <- mods %>% augment(mod, data = data) 34 | # order has changed, but original columns should be there 35 | expect_true(!is.null(augmented$disp)) 36 | expect_equal(sort(mtcars$disp), sort(augmented$disp)) 37 | expect_equal(sort(mtcars$drat), sort(augmented$drat)) 38 | 39 | expect_true(!is.null(augmented$.fitted)) 40 | 41 | # column name doesn't have to be data 42 | mods <- mtcars %>% 43 | group_by(cyl) %>% 44 | do(mod = lm(mpg ~ wt + qsec, .), original = (.)) 45 | augmented <- mods %>% augment(mod, data = original) 46 | expect_true(!is.null(augmented$disp)) 47 | expect_equal(sort(mtcars$disp), sort(augmented$disp)) 48 | }) 49 | 50 | test_that("rowwise tidiers work even when an ungrouped data frame was used", { 51 | one_row <- mtcars %>% do(model = lm(mpg ~ wt, .)) 52 | 53 | tidied <- one_row %>% tidy(model) 54 | expect_equal(nrow(tidied), 2) 55 | 56 | augmented <- one_row %>% augment(model) 57 | expect_equal(nrow(augmented), nrow(mtcars)) 58 | 59 | glanced <- one_row %>% glance(model) 60 | expect_equal(nrow(glanced), 1) 61 | }) 62 | -------------------------------------------------------------------------------- /man/ridgelm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ridgelm_tidiers.R 3 | \name{ridgelm_tidiers} 4 | \alias{glance.ridgelm} 5 | \alias{ridgelm_tidiers} 6 | \alias{tidy.ridgelm} 7 | \title{Tidying methods for ridgelm objects from the MASS package} 8 | \usage{ 9 | \method{tidy}{ridgelm}(x, ...) 10 | 11 | \method{glance}{ridgelm}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{An object of class "ridgelm"} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy.ridgelm} returns one row for each combination of 23 | choice of lambda and term in the formula, with columns: 24 | \item{lambda}{choice of lambda} 25 | \item{GCV}{generalized cross validation value for this lambda} 26 | \item{term}{the term in the ridge regression model being estimated} 27 | \item{estimate}{estimate of scaled coefficient using this lambda} 28 | \item{scale}{Scaling factor of estimated coefficient} 29 | 30 | \code{glance.ridgelm} returns a one-row data.frame with the columns 31 | \item{kHKB}{modified HKB estimate of the ridge constant} 32 | \item{kLW}{modified L-W estimate of the ridge constant} 33 | \item{lambdaGCV}{choice of lambda that minimizes GCV} 34 | 35 | This is similar to the output of \code{select.ridgelm}, but it is returned 36 | rather than printed. 37 | } 38 | \description{ 39 | These methods tidies the coefficients of a ridge regression model 40 | chosen at each value of lambda into a data frame, or constructs 41 | a one-row glance of the model's choices of lambda (the ridge 42 | constant) 43 | } 44 | \examples{ 45 | 46 | names(longley)[1] <- "y" 47 | fit1 <- MASS::lm.ridge(y ~ ., longley) 48 | tidy(fit1) 49 | 50 | fit2 <- MASS::lm.ridge(y ~ ., longley, lambda = seq(0.001, .05, .001)) 51 | td2 <- tidy(fit2) 52 | g2 <- glance(fit2) 53 | 54 | # coefficient plot 55 | library(ggplot2) 56 | ggplot(td2, aes(lambda, estimate, color = term)) + geom_line() 57 | 58 | # GCV plot 59 | ggplot(td2, aes(lambda, GCV)) + geom_line() 60 | 61 | # add line for the GCV minimizing estimate 62 | ggplot(td2, aes(lambda, GCV)) + geom_line() + 63 | geom_vline(xintercept = g2$lambdaGCV, col = "red", lty = 2) 64 | } 65 | 66 | -------------------------------------------------------------------------------- /man/pyears_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/survival_tidiers.R 3 | \name{pyears_tidiers} 4 | \alias{glance.pyears} 5 | \alias{pyears_tidiers} 6 | \alias{tidy.pyears} 7 | \title{Tidy person-year summaries} 8 | \usage{ 9 | \method{tidy}{pyears}(x, ...) 10 | 11 | \method{glance}{pyears}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{a "pyears" object} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | \code{tidy} returns a data.frame with the columns 20 | \item{pyears}{person-years of exposure} 21 | \item{n}{number of subjects contributing time} 22 | \item{event}{observed number of events} 23 | \item{expected}{expected number of events (present only if a 24 | \code{ratetable} term is present)} 25 | 26 | If the \code{data.frame = TRUE} argument is supplied to \code{pyears}, 27 | this is simply the contents of \code{x$data}. 28 | 29 | \code{glance} returns a one-row data frame with 30 | \item{total}{total number of person-years tabulated} 31 | \item{offtable}{total number of person-years off table} 32 | 33 | This contains the values printed by \code{summary.pyears}. 34 | } 35 | \description{ 36 | These tidy the output of \code{pyears}, a calculation of the person-years 37 | of follow-up time contributed by a cohort of subject. Since the output of 38 | \code{pyears$data} is already tidy (if the \code{data.frame = TRUE} argument 39 | is given), this does only a little work and should rarely be necessary. 40 | } 41 | \examples{ 42 | 43 | if (require("survival", quietly = TRUE)) { 44 | temp.yr <- tcut(mgus$dxyr, 55:92, labels=as.character(55:91)) 45 | temp.age <- tcut(mgus$age, 34:101, labels=as.character(34:100)) 46 | ptime <- ifelse(is.na(mgus$pctime), mgus$futime, mgus$pctime) 47 | pstat <- ifelse(is.na(mgus$pctime), 0, 1) 48 | pfit <- pyears(Surv(ptime/365.25, pstat) ~ temp.yr + temp.age + sex, mgus, 49 | data.frame=TRUE) 50 | head(tidy(pfit)) 51 | glance(pfit) 52 | 53 | # if data.frame argument is not given, different information is present in 54 | # output 55 | pfit2 <- pyears(Surv(ptime/365.25, pstat) ~ temp.yr + temp.age + sex, mgus) 56 | head(tidy(pfit2)) 57 | glance(pfit2) 58 | } 59 | 60 | } 61 | \seealso{ 62 | \link{pyears} 63 | } 64 | 65 | -------------------------------------------------------------------------------- /man/data.frame_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.frame_tidiers.R 3 | \name{data.frame_tidiers} 4 | \alias{augment.data.frame} 5 | \alias{data.frame_tidiers} 6 | \alias{glance.data.frame} 7 | \alias{tidy.data.frame} 8 | \title{Tidiers for data.frame objects} 9 | \usage{ 10 | \method{tidy}{data.frame}(x, ...) 11 | 12 | \method{augment}{data.frame}(x, data, ...) 13 | 14 | \method{glance}{data.frame}(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{A data.frame} 18 | 19 | \item{...}{extra arguments: for \code{tidy}, these are passed on to 20 | \code{\link{describe}} from \code{psych} package} 21 | 22 | \item{data}{data, not used} 23 | } 24 | \value{ 25 | \code{tidy.data.frame} produces a data frame with one 26 | row per original column, containing summary statistics of each: 27 | \item{column}{name of original column} 28 | \item{n}{Number of valid (non-NA) values} 29 | \item{mean}{mean} 30 | \item{sd}{standard deviation} 31 | \item{median}{median} 32 | \item{trimmed}{trimmed mean, with trim defaulting to .1} 33 | \item{mad}{median absolute deviation (from the median)} 34 | \item{min}{minimum value} 35 | \item{max}{maximum value} 36 | \item{range}{range} 37 | \item{skew}{skew} 38 | \item{kurtosis}{kurtosis} 39 | \item{se}{standard error} 40 | 41 | \code{glance} returns a one-row data.frame with 42 | \item{nrow}{number of rows} 43 | \item{ncol}{number of columns} 44 | \item{complete.obs}{number of rows that have no missing values} 45 | \item{na.fraction}{fraction of values across all rows and columns that 46 | are missing} 47 | } 48 | \description{ 49 | These perform tidy summaries of data.frame objects. \code{tidy} produces 50 | summary statistics about each column, while \code{glance} simply reports 51 | the number of rows and columns. Note that \code{augment.data.frame} will 52 | throw an error. 53 | } 54 | \details{ 55 | The \code{tidy} method calls the psych method 56 | \code{\link{describe}} directly to produce its per-columns summary 57 | statistics. 58 | } 59 | \examples{ 60 | 61 | td <- tidy(mtcars) 62 | td 63 | 64 | glance(mtcars) 65 | 66 | library(ggplot2) 67 | # compare mean and standard deviation 68 | ggplot(td, aes(mean, sd)) + geom_point() + 69 | geom_text(aes(label = column), hjust = 1, vjust = 1) + 70 | scale_x_log10() + scale_y_log10() + geom_abline() 71 | 72 | } 73 | \seealso{ 74 | \code{\link{describe}} 75 | } 76 | 77 | -------------------------------------------------------------------------------- /man/plm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plm_tidiers.R 3 | \name{plm_tidiers} 4 | \alias{augment.plm} 5 | \alias{glance.plm} 6 | \alias{plm_tidiers} 7 | \alias{tidy.plm} 8 | \title{Tidiers for panel regression linear models} 9 | \usage{ 10 | \method{tidy}{plm}(x, conf.int = FALSE, conf.level = 0.95, 11 | exponentiate = FALSE, ...) 12 | 13 | \method{augment}{plm}(x, data = as.data.frame(stats::model.frame(x)), ...) 14 | 15 | \method{glance}{plm}(x, ...) 16 | } 17 | \arguments{ 18 | \item{x}{a "plm" object representing a panel object} 19 | 20 | \item{conf.int}{whether to include a confidence interval} 21 | 22 | \item{conf.level}{confidence level of the interval, used only if 23 | \code{conf.int=TRUE}} 24 | 25 | \item{exponentiate}{whether to exponentiate the coefficient estimates 26 | and confidence intervals} 27 | 28 | \item{...}{extra arguments, not used} 29 | 30 | \item{data}{original dataset} 31 | } 32 | \value{ 33 | All tidying methods return a data.frame without rownames, whose 34 | structure depends on the method chosen. 35 | 36 | \code{tidy.plm} returns a data frame with one row per 37 | coefficient, of the same form as \code{\link{tidy.lm}}. 38 | 39 | \code{augment} returns a data frame with one row for each 40 | initial observation, adding the columns 41 | \item{.fitted}{predicted (fitted) values} 42 | \item{.resid}{residuals} 43 | 44 | \code{glance} returns a one-row data frame with columns 45 | \item{r.squared}{The percent of variance explained by the model} 46 | \item{adj.r.squared}{r.squared adjusted based on the degrees of freedom} 47 | \item{statistic}{F-statistic} 48 | \item{p.value}{p-value from the F test, describing whether the full 49 | regression is significant} 50 | \item{deviance}{deviance} 51 | \item{df.residual}{residual degrees of freedom} 52 | } 53 | \description{ 54 | Tidiers for panel regression linear models 55 | } 56 | \examples{ 57 | 58 | if (require("plm", quietly = TRUE)) { 59 | data("Produc", package = "plm") 60 | zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, 61 | data = Produc, index = c("state","year")) 62 | 63 | summary(zz) 64 | 65 | tidy(zz) 66 | tidy(zz, conf.int = TRUE) 67 | tidy(zz, conf.int = TRUE, conf.level = .9) 68 | 69 | head(augment(zz)) 70 | 71 | glance(zz) 72 | } 73 | 74 | } 75 | \seealso{ 76 | \code{\link{lm_tidiers}} 77 | } 78 | 79 | -------------------------------------------------------------------------------- /R/arima_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for ARIMA modeling of time series 2 | #' 3 | #' These methods tidy the coefficients of ARIMA models of univariate time 4 | #' series. 5 | #' 6 | #' @param x An object of class "Arima" 7 | #' 8 | #' @details \code{augment} is not currently implemented, as it is not clear 9 | #' whether ARIMA predictions can or should be merged with the original 10 | #' data frame. 11 | #' 12 | #' @template boilerplate 13 | #' 14 | #' @seealso \link{arima} 15 | #' 16 | #' @examples 17 | #' 18 | #' fit <- arima(lh, order = c(1, 0, 0)) 19 | #' tidy(fit) 20 | #' glance(fit) 21 | #' 22 | #' @name Arima_tidiers 23 | NULL 24 | 25 | 26 | #' @rdname Arima_tidiers 27 | #' 28 | #' @param conf.int whether to include a confidence interval 29 | #' @param conf.level confidence level of the interval, used only if 30 | #' \code{conf.int=TRUE} 31 | #' 32 | #' @return \code{tidy} returns one row for each coefficient in the model, 33 | #' with five columns: 34 | #' \item{term}{The term in the nonlinear model being estimated and tested} 35 | #' \item{estimate}{The estimated coefficient} 36 | #' \item{std.error}{The standard error from the linear model} 37 | #' 38 | #' If \code{conf.int = TRUE}, also returns 39 | #' \item{conf.low}{low end of confidence interval} 40 | #' \item{conf.high}{high end of confidence interval} 41 | #' 42 | #' @export 43 | tidy.Arima <- function(x, conf.int=FALSE, conf.level=.95, ...) { 44 | coefs <- stats::coef(x) 45 | # standard errors are computed as in stats:::print.Arima 46 | ses <- rep.int(0, length(coefs)) 47 | ses[x$mask] <- sqrt(diag(x$var.coef)) 48 | 49 | ret <- unrowname(data.frame(term = names(coefs), 50 | estimate = coefs, 51 | std.error = ses)) 52 | 53 | if (conf.int) { 54 | ret <- cbind(ret, confint_tidy(x)) 55 | } 56 | ret 57 | } 58 | 59 | 60 | #' @rdname Arima_tidiers 61 | #' 62 | #' @param ... extra arguments (not used) 63 | #' 64 | #' @return \code{glance} returns one row with the columns 65 | #' \item{sigma}{the square root of the estimated residual variance} 66 | #' \item{logLik}{the data's log-likelihood under the model} 67 | #' \item{AIC}{the Akaike Information Criterion} 68 | #' \item{BIC}{the Bayesian Information Criterion} 69 | #' 70 | #' @export 71 | glance.Arima <- function(x, ...) { 72 | ret <- unrowname(data.frame(sigma = sqrt(x$sigma2))) 73 | finish_glance(ret, x) 74 | } 75 | -------------------------------------------------------------------------------- /R/robust_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for lmRob and glmRob objects 2 | #' 3 | #' Tidying robust regression objects from the robust package. The tidy and augment 4 | #' methods simply pass it on to the linear model tidiers. 5 | #' 6 | #' @param x An lmRob or glmRob object with a robust regression 7 | #' @param ... Extra arguments, not used 8 | #' 9 | #' @template boilerplate 10 | #' 11 | #' @return \code{tidy} and \code{augment} return the same results as \code{\link{lm_tidiers}}. 12 | #' 13 | #' On an \code{lmRob} \code{glance} returns a one-row data frame with the following columns: 14 | #' \item{r.squared}{R-squared} 15 | #' \item{deviance}{Robust deviance} 16 | #' \item{sigma}{Residual scale estimate} 17 | #' \item{df.residual}{Number of residual degrees of freedom} 18 | #' 19 | #' On an \code{lmRob} \code{glance} returns a one-row data frame with the following columns: 20 | #' \item{deviance}{Robust deviance} 21 | #' \item{null.deviance}{Deviance under the null model} 22 | #' \item{df.residual}{Number of residual degrees of freedom} 23 | #' 24 | #' @examples 25 | #' 26 | #' if (require("robust", quietly = TRUE)) { 27 | #' m <- lmRob(mpg ~ wt, data = mtcars) 28 | #' 29 | #' tidy(m) 30 | #' augment(m) 31 | #' glance(m) 32 | #' 33 | #' gm <- glmRob(am ~ wt, data = mtcars, family = "binomial") 34 | #' glance(gm) 35 | #' } 36 | #' 37 | #' @name robust_tidiers 38 | #' 39 | #' @seealso \code{\link{lm_tidiers}}, \code{\link[robust]{lmRob}}, \code{\link[robust]{glmRob}} 40 | #' 41 | #' @export 42 | tidy.lmRob <- function(x, ...) { 43 | tidy.lm(x) 44 | } 45 | 46 | #' @rdname robust_tidiers 47 | #' @export 48 | augment.lmRob <- function(x, ...) { 49 | augment.lm(x) 50 | } 51 | 52 | #' @rdname robust_tidiers 53 | #' @export 54 | glance.lmRob <- function(x, ...) { 55 | s <- robust::summary.lmRob(x) 56 | ret <- data.frame(r.squared = x$r.squared, sigma = s$sigma) 57 | 58 | if (!is.null(ret$dev)) { 59 | ret$deviance <- ret$dev 60 | } 61 | finish_glance(ret, x) 62 | } 63 | 64 | #' @name robust_tidiers 65 | #' 66 | #' @export 67 | tidy.glmRob <- function(x, ...) { 68 | tidy.lm(x) 69 | } 70 | 71 | #' @rdname robust_tidiers 72 | #' @export 73 | augment.glmRob <- function(x, ...) { 74 | augment.lm(x) 75 | } 76 | 77 | #' @rdname robust_tidiers 78 | #' @export 79 | glance.glmRob <- function(x, ...) { 80 | ret <- data.frame(deviance = x$deviance, 81 | null.deviance = x$null.deviance) 82 | finish_glance(ret, x) 83 | } 84 | 85 | -------------------------------------------------------------------------------- /man/svd_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/svd_tidiers.R 3 | \name{svd_tidiers} 4 | \alias{svd_tidiers} 5 | \alias{tidy_svd} 6 | \title{Tidying methods for singular value decomposition} 7 | \usage{ 8 | tidy_svd(x, matrix = "u", ...) 9 | } 10 | \arguments{ 11 | \item{x}{list containing d, u, v components, returned from \code{svd}} 12 | 13 | \item{matrix}{which of the u, d or v matrix to tidy} 14 | 15 | \item{...}{Extra arguments (not used)} 16 | } 17 | \value{ 18 | An SVD object contains a decomposition into u, d, and v matrices, such that 19 | \code{u \%\*\% diag(d) \%\*\% t(v)} gives the original matrix. This tidier gives 20 | a choice of which matrix to tidy. 21 | 22 | When \code{matrix = "u"}, each observation represents one pair of row and 23 | principal component, with variables: 24 | \item{row}{Number of the row in the original data being described} 25 | \item{PC}{Principal component} 26 | \item{loading}{Loading of this principal component for this row} 27 | 28 | When \code{matrix = "d"}, each observation represents one principal component, 29 | with variables: 30 | \item{PC}{Principal component} 31 | \item{d}{Value in the \code{d} vector} 32 | \item{percent}{Percent of variance explained by this PC, which is 33 | proportional to $d^2$} 34 | 35 | When \code{matrix = "v"}, each observation represents a pair of a principal 36 | component and a column of the original matrix, with variables: 37 | \item{column}{Column of original matrix described} 38 | \item{PC}{Principal component} 39 | \item{value}{Value of this PC for this column} 40 | } 41 | \description{ 42 | These methods tidy the U, D, and V matrices returned by the 43 | \code{\link{svd}} function into a tidy format. Because 44 | \code{svd} returns a list without a class, this function has to be 45 | called by \code{\link{tidy.list}} when it recognizes a list as an 46 | SVD object. 47 | } 48 | \examples{ 49 | 50 | mat <- as.matrix(iris[, 1:4]) 51 | s <- svd(mat) 52 | 53 | tidy_u <- tidy(s, matrix = "u") 54 | head(tidy_u) 55 | 56 | tidy_d <- tidy(s, matrix = "d") 57 | tidy_d 58 | 59 | tidy_v <- tidy(s, matrix = "v") 60 | head(tidy_v) 61 | 62 | library(ggplot2) 63 | library(dplyr) 64 | 65 | ggplot(tidy_d, aes(PC, percent)) + 66 | geom_point() + 67 | ylab("\% of variance explained") 68 | 69 | tidy_u \%>\% 70 | mutate(Species = iris$Species[row]) \%>\% 71 | ggplot(aes(Species, loading)) + 72 | geom_boxplot() + 73 | facet_wrap(~ PC, scale = "free_y") 74 | } 75 | \seealso{ 76 | \code{\link{svd}}, \code{\link{tidy.list}} 77 | } 78 | 79 | -------------------------------------------------------------------------------- /tests/testthat/test-lme4.R: -------------------------------------------------------------------------------- 1 | # test tidy, augment, glance methods from lme4-tidiers.R 2 | 3 | if (require(lme4, quietly = TRUE)) { 4 | context("lme4 models") 5 | 6 | d <- as.data.frame(ChickWeight) 7 | colnames(d) <- c("y", "x", "subj", "tx") 8 | fit <- lmer(y ~ tx*x + (x | subj), data=d) 9 | 10 | test_that("tidy works on lme4 fits", { 11 | td <- tidy(fit) 12 | }) 13 | 14 | test_that("scales works", { 15 | t1 <- tidy(fit,effects="ran_pars") 16 | t2 <- tidy(fit,effects="ran_pars",scales="sdcor") 17 | expect_equal(t1$estimate,t2$estimate) 18 | expect_error(tidy(fit,effects="ran_pars",scales="varcov"), 19 | "unrecognized ran_pars scale") 20 | t3 <- tidy(fit,effects="ran_pars",scales="vcov") 21 | expect_equal(t3$estimate[c(1,2,4)], 22 | t2$estimate[c(1,2,4)]^2) 23 | expect_error(tidy(fit,scales="vcov"), 24 | "must be provided for each effect") 25 | }) 26 | test_that("tidy works with more than one RE grouping variable", { 27 | dd <- expand.grid(f=factor(1:10),g=factor(1:5),rep=1:3) 28 | dd$y <- suppressMessages(simulate(~(1|f)+(1|g),newdata=dd, 29 | newparams=list(beta=1,theta=c(1,1)), 30 | family=poisson, seed=101))[[1]] 31 | gfit <- glmer(y~(1|f)+(1|g),data=dd,family=poisson) 32 | expect_equal(as.character(tidy(gfit,effects="ran_pars")$term), 33 | paste("sd_(Intercept)",c("f","g"),sep=".")) 34 | }) 35 | 36 | test_that("augment works on lme4 fits with or without data", { 37 | au <- augment(fit) 38 | au <- augment(fit, d) 39 | }) 40 | 41 | dNAs <- d 42 | dNAs$y[c(1, 3, 5)] <- NA 43 | 44 | test_that("augment works on lme4 fits with NAs", { 45 | fitNAs <- lmer(y ~ tx*x + (x | subj), data = dNAs) 46 | au <- augment(fitNAs) 47 | expect_equal(nrow(au), sum(complete.cases(dNAs))) 48 | }) 49 | 50 | test_that("augment works on lme4 fits with na.exclude", { 51 | fitNAs <- lmer(y ~ tx*x + (x | subj), data = dNAs, na.action = "na.exclude") 52 | 53 | #expect_error(suppressWarnings(augment(fitNAs))) 54 | au <- augment(fitNAs, dNAs) 55 | 56 | # with na.exclude, should have NAs in the output where there were NAs in input 57 | expect_equal(nrow(au), nrow(dNAs)) 58 | expect_equal(complete.cases(au), complete.cases(dNAs)) 59 | }) 60 | 61 | test_that("glance works on lme4 fits", { 62 | g <- glance(fit) 63 | }) 64 | } 65 | -------------------------------------------------------------------------------- /man/kmeans_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kmeans_tidiers.R 3 | \name{kmeans_tidiers} 4 | \alias{augment.kmeans} 5 | \alias{glance.kmeans} 6 | \alias{kmeans_tidiers} 7 | \alias{tidy.kmeans} 8 | \title{Tidying methods for kmeans objects} 9 | \usage{ 10 | \method{tidy}{kmeans}(x, col.names = paste0("x", 1:ncol(x$centers)), ...) 11 | 12 | \method{augment}{kmeans}(x, data, ...) 13 | 14 | \method{glance}{kmeans}(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{kmeans object} 18 | 19 | \item{col.names}{The names to call each dimension of the data in \code{tidy}. 20 | Defaults to \code{x1, x2...}} 21 | 22 | \item{...}{extra arguments, not used} 23 | 24 | \item{data}{Original data (required for \code{augment})} 25 | } 26 | \value{ 27 | All tidying methods return a \code{data.frame} without rownames. 28 | The structure depends on the method chosen. 29 | 30 | \code{tidy} returns one row per cluster, with one column for each 31 | dimension in the data describing the center, followed by 32 | \item{size}{The size of each cluster} 33 | \item{withinss}{The within-cluster sum of squares} 34 | \item{cluster}{A factor describing the cluster from 1:k} 35 | 36 | \code{augment} returns the original data with one extra column: 37 | \item{.cluster}{The cluster assigned by the k-means algorithm} 38 | 39 | \code{glance} returns a one-row data.frame with the columns 40 | \item{totss}{The total sum of squares} 41 | \item{tot.withinss}{The total within-cluster sum of squares} 42 | \item{betweenss}{The total between-cluster sum of squares} 43 | \item{iter}{The numbr of (outer) iterations} 44 | } 45 | \description{ 46 | These methods summarize the results of k-means clustering into three 47 | tidy forms. \code{tidy} describes the center and size of each cluster, 48 | \code{augment} adds the cluster assignments to the original data, and 49 | \code{glance} summarizes the total within and between sum of squares 50 | of the clustering. 51 | } 52 | \examples{ 53 | 54 | library(dplyr) 55 | library(ggplot2) 56 | 57 | set.seed(2014) 58 | centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), 59 | x1=c(5, 0, -3), x2=c(-1, 1, -2)) 60 | points <- centers \%>\% group_by(cluster) \%>\% 61 | do(data.frame(x1=rnorm(.$size[1], .$x1[1]), 62 | x2=rnorm(.$size[1], .$x2[1]))) 63 | 64 | k <- kmeans(points \%>\% dplyr::select(x1, x2), 3) 65 | tidy(k) 66 | head(augment(k, points)) 67 | glance(k) 68 | 69 | ggplot(augment(k, points), aes(x1, x2)) + 70 | geom_point(aes(color = .cluster)) + 71 | geom_text(aes(label = cluster), data = tidy(k), size = 10) 72 | 73 | } 74 | \seealso{ 75 | \code{\link{kmeans}} 76 | } 77 | 78 | -------------------------------------------------------------------------------- /man/btergm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/btergm_tidiers.R 3 | \name{btergm_tidiers} 4 | \alias{btergm_tidiers} 5 | \alias{tidy.btergm} 6 | \title{Tidying method for a bootstrapped temporal exponential random graph model} 7 | \usage{ 8 | \method{tidy}{btergm}(x, conf.level = 0.95, exponentiate = FALSE, 9 | quick = FALSE, ...) 10 | } 11 | \arguments{ 12 | \item{x}{a \code{\link[btergm]{btergm}} object} 13 | 14 | \item{conf.level}{confidence level of the bootstrapped interval} 15 | 16 | \item{exponentiate}{whether to exponentiate the coefficient estimates 17 | and confidence intervals} 18 | 19 | \item{quick}{whether to compute a smaller and faster version, containing 20 | only the \code{term} and \code{estimate} columns.} 21 | 22 | \item{...}{extra arguments (currently not used)} 23 | } 24 | \value{ 25 | A \code{data.frame} without rownames. 26 | 27 | \code{tidy.btergm} returns one row for each coefficient, 28 | with four columns: 29 | \item{term}{The term in the model being estimated and tested} 30 | \item{estimate}{The estimated coefficient} 31 | \item{conf.low}{The lower bound of the confidence interval} 32 | \item{conf.high}{The lower bound of the confidence interval} 33 | } 34 | \description{ 35 | This method tidies the coefficients of a bootstrapped temporal exponential 36 | random graph model estimated with the \pkg{xergm}. It simply returns the 37 | coefficients and their confidence intervals. 38 | } 39 | \details{ 40 | There is no \code{augment} or \code{glance} method 41 | for \pkg{ergm} objects. 42 | } 43 | \examples{ 44 | 45 | if (require("xergm")) { 46 | # Using the same simulated example as the xergm package 47 | # Create 10 random networks with 10 actors 48 | networks <- list() 49 | for(i in 1:10){ 50 | mat <- matrix(rbinom(100, 1, .25), nrow = 10, ncol = 10) 51 | diag(mat) <- 0 52 | nw <- network::network(mat) 53 | networks[[i]] <- nw 54 | } 55 | # Create 10 matrices as covariates 56 | covariates <- list() 57 | for (i in 1:10) { 58 | mat <- matrix(rnorm(100), nrow = 10, ncol = 10) 59 | covariates[[i]] <- mat 60 | } 61 | # Fit a model where the propensity to form ties depends 62 | # on the edge covariates, controlling for the number of 63 | # in-stars 64 | btfit <- btergm(networks ~ edges + istar(2) + 65 | edgecov(covariates), R = 100) 66 | 67 | # Show terms, coefficient estimates and errors 68 | tidy(btfit) 69 | 70 | # Show coefficients as odds ratios with a 99\% CI 71 | tidy(btfit, exponentiate = TRUE, conf.level = 0.99) 72 | } 73 | } 74 | \seealso{ 75 | \code{\link[btergm]{btergm}} 76 | } 77 | 78 | -------------------------------------------------------------------------------- /man/lmodel2_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lmodel2_tidiers.R 3 | \name{lmodel2_tidiers} 4 | \alias{glance.lmodel2} 5 | \alias{lmodel2_tidiers} 6 | \alias{tidy.lmodel2} 7 | \title{Tidiers for linear model II objects from the lmodel2 package} 8 | \usage{ 9 | \method{tidy}{lmodel2}(x, ...) 10 | 11 | \method{glance}{lmodel2}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{lmodel2 object} 15 | 16 | \item{...}{Extra arguments, not used} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy} returns a data frame with one row for each combination 23 | of method (OLS/MA/SMA/RMA) and term (always Intercept/Slope). Its columns 24 | are: 25 | \describe{ 26 | \item{method}{Either OLS/MA/SMA/RMA} 27 | \item{term}{Either "Intercept" or "Slope"} 28 | \item{estimate}{Estimated coefficient} 29 | \item{conf.low}{Lower bound of 95\% confidence interval} 30 | \item{conf.high}{Upper bound of 95\% confidence interval} 31 | } 32 | 33 | \code{glance} returns a one-row data frame with columns 34 | \describe{ 35 | \item{r.squared}{OLS R-squared} 36 | \item{p.value}{OLS parametric p-value} 37 | \item{theta}{Angle between OLS lines \code{lm(y ~ x)} and \code{lm(x ~ y)}} 38 | \item{H}{H statistic for computing confidence interval of major axis slope} 39 | } 40 | } 41 | \description{ 42 | Tidy or glance an lmodel2 object. An lmodel2 represents model II simple 43 | linear regression, where both variables in the regression equation are 44 | random. 45 | } 46 | \details{ 47 | Note that unlike linear regression, there are always only two terms 48 | in an lmodel2: Intercept and Slope. Furthermore, these are computed by four 49 | methods: OLS (ordinary least squares), MA (major axis), SMA (standard major 50 | axis), and RMA (ranged major axis). See the lmodel2 documentation for more. 51 | 52 | Note that there is no \code{augment} method for lmodel2 objects because 53 | lmodel2 does not provide a \code{predict} or {\code{residuals}} method 54 | (and since when both observations are random, fitted values and residuals 55 | have a less clear meaning). 56 | } 57 | \examples{ 58 | 59 | if (require("lmodel2", quietly = TRUE)) { 60 | data(mod2ex2) 61 | Ex2.res <- lmodel2(Prey ~ Predators, data=mod2ex2, "relative", "relative", 99) 62 | Ex2.res 63 | 64 | tidy(Ex2.res) 65 | glance(Ex2.res) 66 | 67 | # this allows coefficient plots with ggplot2 68 | library(ggplot2) 69 | ggplot(tidy(Ex2.res), aes(estimate, term, color = method)) + 70 | geom_point() + 71 | geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) + 72 | geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) 73 | } 74 | 75 | } 76 | 77 | -------------------------------------------------------------------------------- /man/geeglm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geeglm_tidiers.R 3 | \name{geeglm_tidiers} 4 | \alias{geeglm_tidiers} 5 | \alias{tidy.geeglm} 6 | \title{Tidying methods for generalized estimating equations models} 7 | \usage{ 8 | \method{tidy}{geeglm}(x, conf.int = FALSE, conf.level = 0.95, 9 | exponentiate = FALSE, quick = FALSE, ...) 10 | } 11 | \arguments{ 12 | \item{x}{An object of class \code{geeglm}, such as from \code{geeglm}} 13 | 14 | \item{conf.int}{whether to include a confidence interval} 15 | 16 | \item{conf.level}{confidence level of the interval, used only if 17 | \code{conf.int=TRUE}} 18 | 19 | \item{exponentiate}{whether to exponentiate the coefficient estimates 20 | and confidence intervals (typical for log distributions)} 21 | 22 | \item{quick}{whether to compute a smaller and faster version, containing 23 | only the \code{term} and \code{estimate} columns.} 24 | 25 | \item{...}{Additional arguments to be passed to other methods. Currently 26 | not used.} 27 | } 28 | \value{ 29 | All tidying methods return a \code{data.frame} without rownames. 30 | The structure depends on the method chosen. 31 | 32 | \code{tidy.geeglm} returns one row for each coefficient, with five columns: 33 | \item{term}{The term in the linear model being estimated and tested} 34 | \item{estimate}{The estimated coefficient} 35 | \item{std.error}{The standard error from the GEE model} 36 | \item{statistic}{Wald statistic} 37 | \item{p.value}{two-sided p-value} 38 | 39 | If \code{conf.int=TRUE}, it also includes columns for 40 | \code{conf.low} and \code{conf.high}, computed with 41 | \code{\link{confint.geeglm}} (included as part of broom). 42 | } 43 | \description{ 44 | These methods tidy the coefficients of generalized estimating 45 | equations models of the \code{geeglm} class from functions of the 46 | \code{geepack} package. 47 | } 48 | \details{ 49 | If \code{conf.int=TRUE}, the confidence interval is computed with 50 | the \code{\link{confint.geeglm}} function. 51 | 52 | While \code{tidy} is supported for "geeglm" objects, \code{augment} and 53 | \code{glance} are not. 54 | 55 | If you have missing values in your model data, you may need to 56 | refit the model with \code{na.action = na.exclude} or deal with the 57 | missingness in the data beforehand. 58 | } 59 | \examples{ 60 | 61 | if (require('geepack')) { 62 | data(state) 63 | ds <- data.frame(state.region, state.x77) 64 | 65 | geefit <- geeglm(Income ~ Frost + Murder, id = state.region, 66 | data = ds, family = gaussian, 67 | corstr = 'exchangeable') 68 | 69 | tidy(geefit) 70 | tidy(geefit, quick = TRUE) 71 | tidy(geefit, conf.int = TRUE) 72 | } 73 | 74 | } 75 | 76 | -------------------------------------------------------------------------------- /man/multinom_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/multinom_tidiers.R 3 | \name{multinom_tidiers} 4 | \alias{glance.multinom} 5 | \alias{multinom_tidiers} 6 | \alias{tidy.multinom} 7 | \title{Tidying methods for multinomial logistic regression models} 8 | \usage{ 9 | \method{tidy}{multinom}(x, conf.int = FALSE, conf.level = 0.95, 10 | exponentiate = TRUE, ...) 11 | 12 | \method{glance}{multinom}(x, ...) 13 | } 14 | \arguments{ 15 | \item{x}{A model object of class \code{multinom}} 16 | 17 | \item{conf.int}{whether to include a confidence interval} 18 | 19 | \item{conf.level}{confidence level of the interval, used only if 20 | \code{conf.int=TRUE}} 21 | 22 | \item{exponentiate}{whether to exponentiate the coefficient estimates 23 | and confidence intervals (typical for multinomial logistic regression)} 24 | 25 | \item{...}{extra arguments, not used} 26 | } 27 | \value{ 28 | All tidying methods return a \code{data.frame} without rownames. 29 | The structure depends on the method chosen. 30 | 31 | \code{tidy.multinom} returns one row for each coefficient at each 32 | level of the response variable, with six columns: 33 | \item{y.value}{The response level} 34 | \item{term}{The term in the model being estimated and tested} 35 | \item{estimate}{The estimated coefficient} 36 | \item{std.error}{The standard error from the linear model} 37 | \item{statistic}{Wald z-statistic} 38 | \item{p.value}{two-sided p-value} 39 | 40 | If \code{conf.int=TRUE}, it also includes columns for \code{conf.low} and 41 | \code{conf.high}, computed with \code{\link{confint}}. 42 | 43 | \code{glance.multinom} returns a 44 | 45 | \code{glance.multinom} returns a one-row data.frame with the columns 46 | \item{edf}{The effective degrees of freedom} 47 | \item{deviance}{deviance} 48 | \item{AIC}{the Akaike Information Criterion} 49 | } 50 | \description{ 51 | These methods tidy the coefficients of multinomial logistic regression 52 | models generated by \code{multinom} of the \code{nnet} package. 53 | } 54 | \details{ 55 | If \code{conf.int=TRUE}, the confidence interval is computed with 56 | the \code{\link{confint}} function. 57 | 58 | While \code{tidy} and \code{glance} are supported for "multinom" objects, 59 | \code{augment} is not. 60 | } 61 | \examples{ 62 | 63 | if (require(nnet) & require(MASS)){ 64 | example(birthwt) 65 | bwt.mu <- multinom(low ~ ., bwt) 66 | tidy(bwt.mu) 67 | glance(bwt.mu) 68 | 69 | #* This model is a truly terrible model 70 | #* but it should show you what the output looks 71 | #* like in a multinomial logistic regression 72 | 73 | fit.gear <- multinom(gear ~ mpg + factor(am), data=mtcars) 74 | tidy(fit.gear) 75 | glance(fit.gear) 76 | } 77 | 78 | } 79 | 80 | -------------------------------------------------------------------------------- /man/biglm_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/biglm_tidiers.R 3 | \name{biglm_tidiers} 4 | \alias{biglm_tidiers} 5 | \alias{glance.biglm} 6 | \alias{tidy.biglm} 7 | \title{Tidiers for biglm and bigglm object} 8 | \usage{ 9 | \method{tidy}{biglm}(x, conf.int = FALSE, conf.level = 0.95, 10 | exponentiate = FALSE, quick = FALSE, ...) 11 | 12 | \method{glance}{biglm}(x, ...) 13 | } 14 | \arguments{ 15 | \item{x}{a "biglm" object} 16 | 17 | \item{conf.int}{whether to include a confidence interval} 18 | 19 | \item{conf.level}{confidence level of the interval, used only if 20 | \code{conf.int=TRUE}} 21 | 22 | \item{exponentiate}{whether to exponentiate the coefficient estimates 23 | and confidence intervals (typical for logistic regression)} 24 | 25 | \item{quick}{whether to compute a smaller and faster version, containing 26 | only the \code{term} and \code{estimate} columns.} 27 | 28 | \item{...}{extra arguments (not used)} 29 | } 30 | \value{ 31 | All tidying methods return a data.frame without rownames, whose 32 | structure depends on the method chosen. 33 | 34 | \code{tidy.biglm} returns one row for each coefficient, with columns 35 | \item{term}{The term in the linear model being estimated and tested} 36 | \item{estimate}{The estimated coefficient} 37 | \item{std.error}{The standard error from the linear model} 38 | \item{p.value}{two-sided p-value} 39 | 40 | If \code{conf.int=TRUE}, it also includes columns for \code{conf.low} and 41 | \code{conf.high}, computed with \code{\link{confint}}. 42 | 43 | \code{glance.biglm} returns a one-row data frame, with columns 44 | \item{r.squared}{The percent of variance explained by the model} 45 | \item{AIC}{the Akaike Information Criterion} 46 | \item{deviance}{deviance} 47 | \item{df.residual}{residual degrees of freedom} 48 | } 49 | \description{ 50 | Tidiers for biglm object from the "biglm" package, which contains a linear model 51 | object that is limited in memory usage. Generally the behavior is as similar 52 | to the \code{\link{lm_tidiers}} as is possible. Currently no \code{augment} 53 | is defined. 54 | } 55 | \examples{ 56 | 57 | if (require("biglm", quietly = TRUE)) { 58 | bfit <- biglm(mpg ~ wt + disp, mtcars) 59 | tidy(bfit) 60 | tidy(bfit, conf.int = TRUE) 61 | tidy(bfit, conf.int = TRUE, conf.level = .9) 62 | 63 | glance(bfit) 64 | 65 | # bigglm: logistic regression 66 | bgfit <- bigglm(am ~ mpg, mtcars, family = binomial()) 67 | tidy(bgfit) 68 | tidy(bgfit, exponentiate = TRUE) 69 | tidy(bgfit, conf.int = TRUE) 70 | tidy(bgfit, conf.int = TRUE, conf.level = .9) 71 | tidy(bgfit, conf.int = TRUE, conf.level = .9, exponentiate = TRUE) 72 | 73 | glance(bgfit) 74 | } 75 | 76 | } 77 | 78 | -------------------------------------------------------------------------------- /man/betareg_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/betareg_tidiers.R 3 | \name{betareg_tidiers} 4 | \alias{augment.betareg} 5 | \alias{betareg_tidiers} 6 | \alias{glance.betareg} 7 | \alias{tidy.betareg} 8 | \title{Tidy betareg objects from the betareg package} 9 | \usage{ 10 | \method{tidy}{betareg}(x, conf.int = FALSE, conf.level = 0.95, ...) 11 | 12 | \method{augment}{betareg}(x, data = stats::model.frame(x), newdata, 13 | type.predict, type.residuals, ...) 14 | 15 | \method{glance}{betareg}(x, ...) 16 | } 17 | \arguments{ 18 | \item{x}{A "betareg" object} 19 | 20 | \item{conf.int}{whether to include a confidence interval} 21 | 22 | \item{conf.level}{confidence level of the interval, used only if 23 | \code{conf.int=TRUE}} 24 | 25 | \item{...}{Extra arguments, not used} 26 | 27 | \item{data}{Original data frame the regression was fit on} 28 | 29 | \item{newdata}{New data frame to use for prediction} 30 | 31 | \item{type.predict}{Type of predictions to calculate} 32 | 33 | \item{type.residuals}{Type of residuals to calculate} 34 | } 35 | \value{ 36 | All tidying methods return a data.frame without rownames, whose 37 | structure depends on the method chosen. 38 | 39 | tidy returns a data.frame with one row for each term used to predict 40 | the mean, along with at least one term used to predict phi (the inverse of 41 | the variance). It starts with the column \code{component} containing either 42 | "mean" or "precision" to describe which is being modeled, then has the same 43 | columns as tidied linear models or glm's (see \code{\link{lm_tidiers}}). 44 | 45 | augment returns the original data, along with new columns describing 46 | each observation: 47 | \item{.fitted}{Fitted values of model} 48 | \item{.resid}{Residuals} 49 | \item{.cooksd}{Cooks distance, \code{\link{cooks.distance}}} 50 | 51 | \code{glance} returns a one-row data.frame with the columns 52 | \item{pseudo.r.squared}{the deviance of the null model} 53 | \item{logLik}{the data's log-likelihood under the model} 54 | \item{AIC}{the Akaike Information Criterion} 55 | \item{BIC}{the Bayesian Information Criterion} 56 | \item{df.residual}{residual degrees of freedom} 57 | \item{df.null}{degrees of freedom under the null} 58 | } 59 | \description{ 60 | Tidy beta regression objects into summarized coefficients, add their fitted values 61 | and residuals, or find their model parameters. 62 | } 63 | \examples{ 64 | 65 | if (require("betareg", quietly = TRUE)) { 66 | data("GasolineYield", package = "betareg") 67 | 68 | mod <- betareg(yield ~ batch + temp, data = GasolineYield) 69 | 70 | mod 71 | tidy(mod) 72 | tidy(mod, conf.int = TRUE) 73 | tidy(mod, conf.int = TRUE, conf.level = .99) 74 | 75 | head(augment(mod)) 76 | 77 | glance(mod) 78 | } 79 | 80 | } 81 | 82 | -------------------------------------------------------------------------------- /R/multcomp_tidiers.R: -------------------------------------------------------------------------------- 1 | #' tidying methods for objects produced by \pkg{multcomp} 2 | #' 3 | #' These methods originated in ggplot2, as "fortify." In broom, 4 | #' they were renamed "tidy" because they summarize terms and 5 | #' tests, rather than adding columns to a dataset. 6 | #' 7 | #' @param x an object of class \code{glht}, \code{confint.glht}, 8 | #' \code{summary.glht} or \code{\link[multcomp]{cld}} 9 | #' @param ... extra arguments (not used) 10 | #' 11 | #' @name multcomp_tidiers 12 | #' @examples 13 | #' 14 | #' if (require("multcomp") && require("ggplot2")) { 15 | #' amod <- aov(breaks ~ wool + tension, data = warpbreaks) 16 | #' wht <- glht(amod, linfct = mcp(tension = "Tukey")) 17 | #' 18 | #' tidy(wht) 19 | #' ggplot(wht, aes(lhs, estimate)) + geom_point() 20 | #' 21 | #' CI <- confint(wht) 22 | #' tidy(CI) 23 | #' ggplot(CI, aes(lhs, estimate, ymin = lwr, ymax = upr)) + 24 | #' geom_pointrange() 25 | #' 26 | #' tidy(summary(wht)) 27 | #' ggplot(mapping = aes(lhs, estimate)) + 28 | #' geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) + 29 | #' geom_point(aes(size = p), data = summary(wht)) + 30 | #' scale_size(trans = "reverse") 31 | #' 32 | #' cld <- cld(wht) 33 | #' tidy(cld) 34 | #' } 35 | NULL 36 | 37 | #' @method tidy glht 38 | #' @rdname multcomp_tidiers 39 | #' @export 40 | tidy.glht <- function(x, ...) { 41 | unrowname(data.frame( 42 | lhs = rownames(x$linfct), 43 | rhs = x$rhs, 44 | estimate = stats::coef(x), 45 | check.names = FALSE, 46 | stringsAsFactors = FALSE)) 47 | } 48 | 49 | #' @rdname multcomp_tidiers 50 | #' @method tidy confint.glht 51 | #' @export 52 | tidy.confint.glht <- function(x, ...) { 53 | coef <- x$confint 54 | colnames(coef) <- c("estimate", "conf.low", "conf.high") 55 | 56 | unrowname(data.frame( 57 | lhs = rownames(coef), 58 | rhs = x$rhs, 59 | coef, 60 | check.names = FALSE, 61 | stringsAsFactors = FALSE)) 62 | } 63 | 64 | #' @method tidy summary.glht 65 | #' @rdname multcomp_tidiers 66 | #' @export 67 | tidy.summary.glht <- function(x, ...) { 68 | coef <- as.data.frame( 69 | x$test[c("coefficients", "sigma", "tstat", "pvalues")]) 70 | names(coef) <- c("estimate", "std.error", "statistic", "p.value") 71 | 72 | unrowname(data.frame( 73 | lhs = rownames(coef), 74 | rhs = x$rhs, 75 | coef, 76 | check.names = FALSE, 77 | stringsAsFactors = FALSE)) 78 | } 79 | 80 | 81 | #' @method tidy cld 82 | #' @rdname multcomp_tidiers 83 | #' @export 84 | tidy.cld <- function(x, ...) { 85 | unrowname(data.frame( 86 | lhs = names(x$mcletters$Letters), 87 | letters = x$mcletters$Letters, 88 | check.names = FALSE, 89 | stringsAsFactors = FALSE)) 90 | } 91 | -------------------------------------------------------------------------------- /man/glmnet_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/glmnet_tidiers.R 3 | \name{glmnet_tidiers} 4 | \alias{glance.glmnet} 5 | \alias{glmnet_tidiers} 6 | \alias{tidy.glmnet} 7 | \title{Tidiers for LASSO or elasticnet regularized fits} 8 | \usage{ 9 | \method{tidy}{glmnet}(x, ...) 10 | 11 | \method{glance}{glmnet}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{a "glmnet" object} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy} produces a data.frame with one row per combination of 23 | coefficient (including the intercept) and value of lambda for which the estimate 24 | is nonzero, with the columns: 25 | \item{term}{coefficient name (V1...VN by default, along with 26 | "(Intercept)")} 27 | \item{step}{which step of lambda choices was used} 28 | \item{estimate}{estimate of coefficient} 29 | \item{lambda}{value of penalty parameter lambda} 30 | \item{dev.ratio}{fraction of null deviance explained at each 31 | value of lambda} 32 | 33 | \code{glance} returns a one-row data.frame with the values 34 | \item{nulldev}{null deviance} 35 | \item{npasses}{total passes over the data across all lambda values} 36 | } 37 | \description{ 38 | Tidying methods for regularized fits produced by \code{glmnet}, summarizing 39 | the estimates across values of the penalty parameter lambda. 40 | } 41 | \details{ 42 | Note that while this representation of GLMs is much easier 43 | to plot and combine than the default structure, it is also much 44 | more memory-intensive. Do not use for extremely large, sparse matrices. 45 | 46 | No \code{augment} method is yet provided even though the model produces 47 | predictions, because the input data is not tidy (it is a matrix that 48 | may be very wide) and therefore combining predictions with it is not 49 | logical. Furthermore, predictions make sense only with a specific 50 | choice of lambda. 51 | } 52 | \examples{ 53 | 54 | if (require("glmnet", quietly = TRUE)) { 55 | set.seed(2014) 56 | x <- matrix(rnorm(100*20),100,20) 57 | y <- rnorm(100) 58 | fit1 <- glmnet(x,y) 59 | 60 | head(tidy(fit1)) 61 | glance(fit1) 62 | 63 | library(dplyr) 64 | library(ggplot2) 65 | 66 | tidied <- tidy(fit1) \%>\% filter(term != "(Intercept)") 67 | 68 | ggplot(tidied, aes(step, estimate, group = term)) + geom_line() 69 | ggplot(tidied, aes(lambda, estimate, group = term)) + 70 | geom_line() + scale_x_log10() 71 | 72 | ggplot(tidied, aes(lambda, dev.ratio)) + geom_line() 73 | 74 | # works for other types of regressions as well, such as logistic 75 | g2 <- sample(1:2, 100, replace=TRUE) 76 | fit2 <- glmnet(x, g2, family="binomial") 77 | head(tidy(fit2)) 78 | } 79 | 80 | } 81 | 82 | -------------------------------------------------------------------------------- /man/rowwise_df_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rowwise_df_tidiers.R 3 | \name{rowwise_df_tidiers} 4 | \alias{augment.rowwise_df} 5 | \alias{augment.tbl_df} 6 | \alias{augment_.rowwise_df} 7 | \alias{glance.rowwise_df} 8 | \alias{glance.tbl_df} 9 | \alias{glance_.rowwise_df} 10 | \alias{rowwise_df_tidiers} 11 | \alias{tidy.rowwise_df} 12 | \alias{tidy.tbl_df} 13 | \alias{tidy_.rowwise_df} 14 | \title{Tidying methods for rowwise_dfs from dplyr, for tidying each row and 15 | recombining the results} 16 | \usage{ 17 | \method{tidy}{rowwise_df}(x, object, ...) 18 | 19 | \method{tidy_}{rowwise_df}(x, object, ...) 20 | 21 | \method{augment}{rowwise_df}(x, object, ...) 22 | 23 | \method{augment_}{rowwise_df}(x, object, ...) 24 | 25 | \method{glance}{rowwise_df}(x, object, ...) 26 | 27 | \method{glance_}{rowwise_df}(x, object, ...) 28 | 29 | \method{tidy}{tbl_df}(x, ...) 30 | 31 | \method{augment}{tbl_df}(x, ...) 32 | 33 | \method{glance}{tbl_df}(x, ...) 34 | } 35 | \arguments{ 36 | \item{x}{a rowwise_df} 37 | 38 | \item{object}{the column name of the column containing the models to 39 | be tidied. For tidy, augment, and glance it should be the bare name; for 40 | _ methods it should be quoted.} 41 | 42 | \item{...}{additional arguments to pass on to the respective tidying method} 43 | } 44 | \value{ 45 | A \code{"grouped_df"}, where the non-list columns of the 46 | original are used as grouping columns alongside the tidied outputs. 47 | } 48 | \description{ 49 | These \code{tidy}, \code{augment} and \code{glance} methods are for 50 | performing tidying on each row of a rowwise data frame created by dplyr's 51 | \code{group_by} and \code{do} operations. They first group a rowwise data 52 | frame based on all columns that are not lists, then perform the tidying 53 | operation on the specified column. This greatly shortens a common idiom 54 | of extracting tidy/augment/glance outputs after a do statement. 55 | } 56 | \details{ 57 | Note that this functionality is not currently implemented for 58 | data.tables, since the result of the do operation is difficult to 59 | distinguish from a regular data.table. 60 | } 61 | \examples{ 62 | 63 | library(dplyr) 64 | regressions <- mtcars \%>\% 65 | group_by(cyl) \%>\% 66 | do(mod = lm(mpg ~ wt, .)) 67 | 68 | regressions 69 | 70 | regressions \%>\% tidy(mod) 71 | regressions \%>\% augment(mod) 72 | regressions \%>\% glance(mod) 73 | 74 | # we can provide additional arguments to the tidying function 75 | regressions \%>\% tidy(mod, conf.int = TRUE) 76 | 77 | # we can also include the original dataset as a "data" argument 78 | # to augment: 79 | regressions <- mtcars \%>\% 80 | group_by(cyl) \%>\% 81 | do(mod = lm(mpg ~ wt, .), original = (.)) 82 | 83 | # this allows all the original columns to be included: 84 | regressions \%>\% augment(mod) # doesn't include all original 85 | regressions \%>\% augment(mod, data = original) # includes all original 86 | 87 | } 88 | 89 | -------------------------------------------------------------------------------- /R/data.frame_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for data.frame objects 2 | #' 3 | #' These perform tidy summaries of data.frame objects. \code{tidy} produces 4 | #' summary statistics about each column, while \code{glance} simply reports 5 | #' the number of rows and columns. Note that \code{augment.data.frame} will 6 | #' throw an error. 7 | #' 8 | #' @param x A data.frame 9 | #' @param data data, not used 10 | #' @param ... extra arguments: for \code{tidy}, these are passed on to 11 | #' \code{\link{describe}} from \code{psych} package 12 | #' 13 | #' @details The \code{tidy} method calls the psych method 14 | #' \code{\link{describe}} directly to produce its per-columns summary 15 | #' statistics. 16 | #' 17 | #' @examples 18 | #' 19 | #' td <- tidy(mtcars) 20 | #' td 21 | #' 22 | #' glance(mtcars) 23 | #' 24 | #' library(ggplot2) 25 | #' # compare mean and standard deviation 26 | #' ggplot(td, aes(mean, sd)) + geom_point() + 27 | #' geom_text(aes(label = column), hjust = 1, vjust = 1) + 28 | #' scale_x_log10() + scale_y_log10() + geom_abline() 29 | #' 30 | #' @name data.frame_tidiers 31 | 32 | 33 | #' @rdname data.frame_tidiers 34 | #' 35 | #' @return \code{tidy.data.frame} produces a data frame with one 36 | #' row per original column, containing summary statistics of each: 37 | #' \item{column}{name of original column} 38 | #' \item{n}{Number of valid (non-NA) values} 39 | #' \item{mean}{mean} 40 | #' \item{sd}{standard deviation} 41 | #' \item{median}{median} 42 | #' \item{trimmed}{trimmed mean, with trim defaulting to .1} 43 | #' \item{mad}{median absolute deviation (from the median)} 44 | #' \item{min}{minimum value} 45 | #' \item{max}{maximum value} 46 | #' \item{range}{range} 47 | #' \item{skew}{skew} 48 | #' \item{kurtosis}{kurtosis} 49 | #' \item{se}{standard error} 50 | #' 51 | #' @importFrom psych describe 52 | #' 53 | #' @seealso \code{\link{describe}} 54 | #' 55 | #' @export 56 | tidy.data.frame <- function(x, ...) { 57 | ret <- psych::describe(x, ...) 58 | ret <- fix_data_frame(ret, newcol = "column") 59 | # remove vars column, which contains an index (not useful here) 60 | ret$vars <- NULL 61 | ret 62 | } 63 | 64 | 65 | #' @rdname data.frame_tidiers 66 | #' 67 | #' @export 68 | augment.data.frame <- function(x, data, ...) { 69 | stop(paste("augment's first argument should be a model, not a data.frame")) 70 | } 71 | 72 | 73 | #' @rdname data.frame_tidiers 74 | #' 75 | #' @return \code{glance} returns a one-row data.frame with 76 | #' \item{nrow}{number of rows} 77 | #' \item{ncol}{number of columns} 78 | #' \item{complete.obs}{number of rows that have no missing values} 79 | #' \item{na.fraction}{fraction of values across all rows and columns that 80 | #' are missing} 81 | #' 82 | #' @export 83 | glance.data.frame <- function(x, ...) { 84 | ret <- data.frame(nrow = nrow(x), ncol = ncol(x)) 85 | ret$complete.obs <- sum(stats::complete.cases(x)) 86 | ret$na.fraction <- mean(is.na(x)) 87 | return(ret) 88 | } 89 | -------------------------------------------------------------------------------- /R/plm_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidiers for panel regression linear models 2 | #' 3 | #' @param x a "plm" object representing a panel object 4 | #' @param data original dataset 5 | #' @param conf.int whether to include a confidence interval 6 | #' @param conf.level confidence level of the interval, used only if 7 | #' \code{conf.int=TRUE} 8 | #' @param exponentiate whether to exponentiate the coefficient estimates 9 | #' and confidence intervals 10 | #' 11 | #' @template boilerplate 12 | #' 13 | #' @return \code{tidy.plm} returns a data frame with one row per 14 | #' coefficient, of the same form as \code{\link{tidy.lm}}. 15 | #' 16 | #' @seealso \code{\link{lm_tidiers}} 17 | #' 18 | #' @name plm_tidiers 19 | #' 20 | #' @examples 21 | #' 22 | #' if (require("plm", quietly = TRUE)) { 23 | #' data("Produc", package = "plm") 24 | #' zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, 25 | #' data = Produc, index = c("state","year")) 26 | #' 27 | #' summary(zz) 28 | #' 29 | #' tidy(zz) 30 | #' tidy(zz, conf.int = TRUE) 31 | #' tidy(zz, conf.int = TRUE, conf.level = .9) 32 | #' 33 | #' head(augment(zz)) 34 | #' 35 | #' glance(zz) 36 | #' } 37 | #' 38 | #' @export 39 | tidy.plm <- function(x, conf.int = FALSE, conf.level = .95, 40 | exponentiate = FALSE, ...) { 41 | tidy.lm(x, conf.int = conf.int, conf.level = conf.level, 42 | exponentiate = exponentiate) 43 | } 44 | 45 | 46 | #' @rdname plm_tidiers 47 | #' 48 | #' @return \code{augment} returns a data frame with one row for each 49 | #' initial observation, adding the columns 50 | #' \item{.fitted}{predicted (fitted) values} 51 | #' \item{.resid}{residuals} 52 | #' 53 | #' @export 54 | augment.plm <- function(x, data = as.data.frame(stats::model.frame(x)), ...) { 55 | # Random effects and fixed effect (within model) have individual intercepts, 56 | # thus we cannot take the ususal procedure for augment(). 57 | # Also, there is currently no predict() method for plm objects. 58 | augment_columns(x, data, ...) 59 | } 60 | 61 | 62 | #' @rdname plm_tidiers 63 | #' 64 | #' @param ... extra arguments, not used 65 | #' 66 | #' @return \code{glance} returns a one-row data frame with columns 67 | #' \item{r.squared}{The percent of variance explained by the model} 68 | #' \item{adj.r.squared}{r.squared adjusted based on the degrees of freedom} 69 | #' \item{statistic}{F-statistic} 70 | #' \item{p.value}{p-value from the F test, describing whether the full 71 | #' regression is significant} 72 | #' \item{deviance}{deviance} 73 | #' \item{df.residual}{residual degrees of freedom} 74 | #' 75 | #' @export 76 | glance.plm <- function(x, ...) { 77 | s <- summary(x) 78 | ret <- with(s, data.frame(r.squared = r.squared[1], 79 | adj.r.squared = r.squared[2], 80 | statistic = fstatistic$statistic, 81 | p.value = fstatistic$p.value 82 | )) 83 | finish_glance(ret, x) 84 | } 85 | -------------------------------------------------------------------------------- /man/rf_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rf_tidiers.R 3 | \name{rf_tidiers} 4 | \alias{augment.randomForest} 5 | \alias{glance.randomForest} 6 | \alias{rf_tidiers} 7 | \alias{tidy.randomForest} 8 | \title{Tidying methods for a randomForest model} 9 | \usage{ 10 | \method{tidy}{randomForest}(x, ...) 11 | 12 | \method{augment}{randomForest}(x, data = NULL, ...) 13 | 14 | \method{glance}{randomForest}(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{randomForest object} 18 | 19 | \item{...}{Additional arguments (ignored)} 20 | 21 | \item{data}{Model data for use by \code{\link{augment.randomForest}}.} 22 | } 23 | \value{ 24 | All tidying methods return a \code{data.frame} without rownames. The 25 | structure depends on the method chosen. 26 | 27 | \code{tidy.randomForest} returns one row for each model term, with the following columns: 28 | \item{term}{The term in the randomForest model} 29 | \item{class_*}{One column for each model term; the relative importance of each term per class. Only present if the model was created with \code{importance = TRUE}} 30 | \item{MeanDecreaseAccuracy}{A measure of variable importance. See \code{\link[randomForest]{randomForest}} for more information. Only present if the model was created with \code{importance = TRUE}} 31 | \item{MeanDecreaseGini}{A measure of variable importance. See \code{\link[randomForest]{randomForest}} for more information.} 32 | \item{sd_*}{Sandard deviations for the preceding statistics. Only present if the model was created with \code{importance = TRUE}} 33 | 34 | \code{augment.randomForest} returns the original data with additional columns: 35 | \item{.oob_times}{The number of trees for which the given case was "out of bag". See \code{\link[randomForest]{randomForest}} for more details.} 36 | \item{.fitted}{The fitted value or class.} 37 | \item{.li_*}{The casewise variable importance for each term. Only present if the model was created with \code{importance = TRUE}} 38 | In addition, \code{augment} returns additional columns for classification and usupervised trees: 39 | \item{.votes_*}{For each case, the voting results, with one column per class.} 40 | 41 | \code{glance.randomForest} returns a one-row data.frame with 42 | the following columns: 43 | For regression trees, the following additional columns are present: 44 | \item{mse}{The average mean squared error across all trees.} 45 | \item{rsq}{The average pesudo-R-squared across all trees. See \code{\link[randomForest]{randomForest}} for more information.} 46 | For classification trees, the following columns are present for each class 47 | \item{*_precision}{} 48 | \item{*_recall}{} 49 | \item{*_accuracy}{} 50 | \item{*_f_measure}{} 51 | } 52 | \description{ 53 | These methods tidy the variable importance of a random forest model summary, 54 | augment the original data with information on the fitted 55 | values/classifications and error, and construct a one-row glance of the 56 | model's statistics. 57 | } 58 | 59 | -------------------------------------------------------------------------------- /man/mcmc_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mcmc_tidiers.R 3 | \name{mcmc_tidiers} 4 | \alias{mcmc_tidiers} 5 | \alias{tidy.rjags} 6 | \alias{tidy.stanfit} 7 | \alias{tidyMCMC} 8 | \title{Tidying methods for MCMC (Stan, JAGS, etc.) fits} 9 | \usage{ 10 | tidyMCMC(x, pars, estimate.method = "mean", conf.int = FALSE, 11 | conf.level = 0.95, conf.method = "quantile", droppars = "lp__", 12 | rhat = FALSE, ess = FALSE, ...) 13 | 14 | \method{tidy}{rjags}(x, pars, estimate.method = "mean", conf.int = FALSE, 15 | conf.level = 0.95, conf.method = "quantile", ...) 16 | 17 | \method{tidy}{stanfit}(x, pars, estimate.method = "mean", conf.int = FALSE, 18 | conf.level = 0.95, conf.method = "quantile", droppars = "lp__", 19 | rhat = FALSE, ess = FALSE, ...) 20 | } 21 | \arguments{ 22 | \item{x}{an object of class \sQuote{"stanfit"}} 23 | 24 | \item{pars}{(character) specification of which parameters to include} 25 | 26 | \item{estimate.method}{method for computing point estimate ("mean" or median")} 27 | 28 | \item{conf.int}{(logical) include confidence interval?} 29 | 30 | \item{conf.level}{probability level for CI} 31 | 32 | \item{conf.method}{method for computing confidence intervals 33 | ("quantile" or "HPDinterval")} 34 | 35 | \item{droppars}{Parameters not to include in the output (such 36 | as log-probability information)} 37 | 38 | \item{rhat, ess}{(logical) include Rhat and/or effective sample size estimates?} 39 | 40 | \item{...}{unused} 41 | } 42 | \description{ 43 | Tidying methods for MCMC (Stan, JAGS, etc.) fits 44 | } 45 | \examples{ 46 | 47 | \dontrun{ 48 | 49 | # Using example from "RStan Getting Started" 50 | # https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started 51 | 52 | model_file <- system.file("extdata", "8schools.stan", package = "broom") 53 | 54 | schools_dat <- list(J = 8, 55 | y = c(28, 8, -3, 7, -1, 1, 18, 12), 56 | sigma = c(15, 10, 16, 11, 9, 11, 10, 18)) 57 | 58 | if (requireNamespace("rstan", quietly = TRUE)) { 59 | set.seed(2015) 60 | rstan_example <- stan(file = model_file, data = schools_dat, 61 | iter = 100, chains = 2) 62 | } 63 | 64 | } 65 | 66 | if (requireNamespace("rstan", quietly = TRUE)) { 67 | # the object from the above code was saved as rstan_example.rda 68 | infile <- system.file("extdata", "rstan_example.rda", package = "broom") 69 | load(infile) 70 | 71 | tidy(rstan_example) 72 | tidy(rstan_example, conf.int = TRUE, pars = "theta") 73 | 74 | td_mean <- tidy(rstan_example, conf.int = TRUE) 75 | td_median <- tidy(rstan_example, conf.int = TRUE, estimate.method = "median") 76 | 77 | library(dplyr) 78 | library(ggplot2) 79 | tds <- rbind(mutate(td_mean, method = "mean"), 80 | mutate(td_median, method = "median")) 81 | 82 | ggplot(tds, aes(estimate, term)) + 83 | geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) + 84 | geom_point(aes(color = method)) 85 | } 86 | 87 | 88 | } 89 | 90 | -------------------------------------------------------------------------------- /R/ridgelm_tidiers.R: -------------------------------------------------------------------------------- 1 | #' Tidying methods for ridgelm objects from the MASS package 2 | #' 3 | #' These methods tidies the coefficients of a ridge regression model 4 | #' chosen at each value of lambda into a data frame, or constructs 5 | #' a one-row glance of the model's choices of lambda (the ridge 6 | #' constant) 7 | #' 8 | #' @param x An object of class "ridgelm" 9 | #' @param ... extra arguments (not used) 10 | #' 11 | #' @template boilerplate 12 | #' 13 | #' @name ridgelm_tidiers 14 | #' 15 | #' @examples 16 | #' 17 | #' names(longley)[1] <- "y" 18 | #' fit1 <- MASS::lm.ridge(y ~ ., longley) 19 | #' tidy(fit1) 20 | #' 21 | #' fit2 <- MASS::lm.ridge(y ~ ., longley, lambda = seq(0.001, .05, .001)) 22 | #' td2 <- tidy(fit2) 23 | #' g2 <- glance(fit2) 24 | #' 25 | #' # coefficient plot 26 | #' library(ggplot2) 27 | #' ggplot(td2, aes(lambda, estimate, color = term)) + geom_line() 28 | #' 29 | #' # GCV plot 30 | #' ggplot(td2, aes(lambda, GCV)) + geom_line() 31 | #' 32 | #' # add line for the GCV minimizing estimate 33 | #' ggplot(td2, aes(lambda, GCV)) + geom_line() + 34 | #' geom_vline(xintercept = g2$lambdaGCV, col = "red", lty = 2) 35 | NULL 36 | 37 | 38 | #' @rdname ridgelm_tidiers 39 | #' 40 | #' @return \code{tidy.ridgelm} returns one row for each combination of 41 | #' choice of lambda and term in the formula, with columns: 42 | #' \item{lambda}{choice of lambda} 43 | #' \item{GCV}{generalized cross validation value for this lambda} 44 | #' \item{term}{the term in the ridge regression model being estimated} 45 | #' \item{estimate}{estimate of scaled coefficient using this lambda} 46 | #' \item{scale}{Scaling factor of estimated coefficient} 47 | #' 48 | #' @export 49 | tidy.ridgelm <- function(x, ...) { 50 | if (length(x$lambda) == 1) { 51 | # only one choice of lambda 52 | ret <- data.frame(lambda = x$lambda, term = names(x$coef), 53 | estimate = x$coef, 54 | scale = x$scales, xm = x$xm) 55 | return(unrowname(ret)) 56 | } 57 | 58 | # otherwise, multiple lambdas/coefs/etc, have to tidy 59 | cotidy <- data.frame(plyr::unrowname(t(x$coef)), lambda = x$lambda, 60 | GCV = unname(x$GCV)) %>% 61 | tidyr::gather(term, estimate, -lambda, -GCV) %>% 62 | mutate(term = as.character(term)) %>% 63 | mutate(scale = x$scales[term]) 64 | 65 | cotidy 66 | } 67 | 68 | 69 | #' @rdname ridgelm_tidiers 70 | #' 71 | #' @return \code{glance.ridgelm} returns a one-row data.frame with the columns 72 | #' \item{kHKB}{modified HKB estimate of the ridge constant} 73 | #' \item{kLW}{modified L-W estimate of the ridge constant} 74 | #' \item{lambdaGCV}{choice of lambda that minimizes GCV} 75 | #' 76 | #' This is similar to the output of \code{select.ridgelm}, but it is returned 77 | #' rather than printed. 78 | #' 79 | #' @export 80 | glance.ridgelm <- function(x, ...) { 81 | ret <- data.frame(kHKB = x$kHKB, kLW = x$kLW, 82 | lambdaGCV = x$lambda[which.min(x$GCV)]) 83 | ret 84 | } 85 | -------------------------------------------------------------------------------- /man/mclust_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mclust_tidiers.R 3 | \name{mclust_tidiers} 4 | \alias{augment.Mclust} 5 | \alias{glance.Mclust} 6 | \alias{mclust_tidiers} 7 | \alias{tidy.Mclust} 8 | \title{Tidying methods for Mclust objects} 9 | \usage{ 10 | \method{tidy}{Mclust}(x, ...) 11 | 12 | \method{augment}{Mclust}(x, data, ...) 13 | 14 | \method{glance}{Mclust}(x, ...) 15 | } 16 | \arguments{ 17 | \item{x}{Mclust object} 18 | 19 | \item{...}{extra arguments, not used} 20 | 21 | \item{data}{Original data (required for \code{augment})} 22 | } 23 | \value{ 24 | All tidying methods return a data.frame without rownames, whose 25 | structure depends on the method chosen. 26 | 27 | \code{tidy} returns one row per component, with 28 | \item{component}{A factor describing the cluster from 1:k 29 | (or 0:k in presence of a noise term in x)} 30 | \item{size}{The size of each component} 31 | \item{proportion}{The mixing proportion of each component} 32 | \item{variance}{In case of one-dimensional and spherical models, 33 | the variance for each component, omitted otherwise. NA for noise component} 34 | \item{mean}{The mean for each component. In case of two- or more dimensional models, 35 | a column with the mean is added for each dimension. NA for noise component} 36 | 37 | \code{augment} returns the original data with two extra columns: 38 | \item{.class}{The class assigned by the Mclust algorithm} 39 | \item{.uncertainty}{The uncertainty associated with the classification} 40 | 41 | \code{glance} returns a one-row data.frame with the columns 42 | \item{model}{A character string denoting the model at which the optimal BIC occurs} 43 | \item{n}{The number of observations in the data} 44 | \item{G}{The optimal number of mixture components} 45 | \item{BIC}{The optimal BIC value} 46 | \item{logLik}{The log-likelihood corresponding to the optimal BIC} 47 | \item{df}{The number of estimated parameters} 48 | \item{hypvol}{The hypervolume parameter for the noise component if required, 49 | otherwise set to NA} 50 | } 51 | \description{ 52 | These methods summarize the results of Mclust clustering into three 53 | tidy forms. \code{tidy} describes the size, mixing probability, mean 54 | and variabilty of each class, \code{augment} adds the class assignments and 55 | their probabilities to the original data, and 56 | \code{glance} summarizes the model parameters of the clustering. 57 | } 58 | \examples{ 59 | 60 | library(dplyr) 61 | library(ggplot2) 62 | library(mclust) 63 | 64 | set.seed(2016) 65 | centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), 66 | x1=c(5, 0, -3), x2=c(-1, 1, -2)) 67 | points <- centers \%>\% group_by(cluster) \%>\% 68 | do(data.frame(x1=rnorm(.$size[1], .$x1[1]), 69 | x2=rnorm(.$size[1], .$x2[1]))) \%>\% 70 | ungroup() 71 | 72 | m = Mclust(points \%>\% dplyr::select(x1, x2)) 73 | 74 | tidy(m) 75 | head(augment(m, points)) 76 | glance(m) 77 | 78 | } 79 | \seealso{ 80 | \code{\link[mclust]{Mclust}} 81 | } 82 | 83 | -------------------------------------------------------------------------------- /man/ivreg_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ivreg_tidiers.R 3 | \name{ivreg_tidiers} 4 | \alias{ivreg_tidiers} 5 | \alias{tidy.ivreg} 6 | \alias{augment.ivreg} 7 | \alias{glance.ivreg} 8 | \title{Tidiers for ivreg models} 9 | \usage{ 10 | \method{tidy}{ivreg}(x, conf.int = FALSE, conf.level = 0.95, 11 | exponentiate = FALSE, ...) 12 | 13 | \method{augment}{ivreg}(x, data = as.data.frame(stats::model.frame(x)), 14 | newdata, ...) 15 | 16 | \method{glance}{ivreg}(x, diagnostics = FALSE, ...) 17 | } 18 | \arguments{ 19 | \item{x}{an "ivreg" object} 20 | 21 | \item{conf.int}{whether to include a confidence interval} 22 | 23 | \item{conf.level}{confidence level of the interval, used only if 24 | \code{conf.int=TRUE}} 25 | 26 | \item{exponentiate}{whether to exponentiate the coefficient estimates 27 | and confidence intervals} 28 | 29 | \item{...}{extra arguments, not used} 30 | 31 | \item{data}{original dataset} 32 | 33 | \item{diagnostics}{Logical. Return results of diagnostic tests.} 34 | } 35 | \value{ 36 | All tidying methods return a data.frame without rownames, whose 37 | structure depends on the method chosen. 38 | 39 | \code{tidy.ivreg} returns a data frame with one row per 40 | coefficient, of the same form as \code{\link{tidy.lm}}. 41 | 42 | \code{augment} returns a data frame with one row for each 43 | initial observation, adding the columns: 44 | \item{.fitted}{predicted (fitted) values} 45 | and if \code{newdata} is \code{NULL}: 46 | \item{.resid}{residuals} 47 | 48 | \code{glance} returns a one-row data frame with columns 49 | \item{r.squared}{The percent of variance explained by the model} 50 | \item{adj.r.squared}{r.squared adjusted based on the degrees of freedom} 51 | \item{statistic}{Wald test statistic} 52 | \item{p.value}{p-value from the Wald test} 53 | \item{df}{Degrees of freedom used by the coefficients} 54 | \item{sigma}{The square root of the estimated residual variance} 55 | \item{df.residual}{residual degrees of freedom} 56 | If \code{diagnostics} is \code{TRUE}, \code{glance} also returns: 57 | \item{p.value.Sargan}{P value of Sargan test} 58 | \item{p.value.Wu.Hausman}{P value of Wu-Hausman test} 59 | \item{p.value.weakinst}{P value of weak instruments test} 60 | } 61 | \description{ 62 | Tidiers for ivreg models 63 | } 64 | \examples{ 65 | 66 | if (require("AER", quietly = TRUE)) { 67 | data("CigarettesSW", package = "AER") 68 | CigarettesSW$rprice <- with(CigarettesSW, price/cpi) 69 | CigarettesSW$rincome <- with(CigarettesSW, income/population/cpi) 70 | CigarettesSW$tdiff <- with(CigarettesSW, (taxs - tax)/cpi) 71 | ivr <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi), 72 | data = CigarettesSW, subset = year == "1995") 73 | 74 | summary(ivr) 75 | 76 | tidy(ivr) 77 | tidy(ivr, conf.int = TRUE) 78 | tidy(ivr, conf.int = TRUE, exponentiate = TRUE) 79 | 80 | head(augment(ivr)) 81 | 82 | glance(ivr) 83 | } 84 | 85 | } 86 | \seealso{ 87 | \code{\link{lm_tidiers}} 88 | } 89 | -------------------------------------------------------------------------------- /tests/testthat/test-nlme.R: -------------------------------------------------------------------------------- 1 | # test tidy, augment, glance methods from nlme-tidiers.R 2 | 3 | if (suppressPackageStartupMessages(require(nlme, quietly = TRUE))) { 4 | context("nlme models") 5 | 6 | d <- as.data.frame(ChickWeight) 7 | colnames(d) <- c("y", "x", "subj", "tx") 8 | fit <- lme(y ~ tx * x, random = ~x | subj, data = d) 9 | 10 | test_that("tidy works on nlme/lme fits", { 11 | td <- tidy(fit) 12 | }) 13 | 14 | test_that("augment works on lme4 fits with or without data", { 15 | au <- augment(fit) 16 | au <- augment(fit, d) 17 | }) 18 | dNAs <- d 19 | dNAs$y[c(1, 3, 5)] <- NA 20 | 21 | test_that("augment works on lme fits with NAs and na.omit", { 22 | fitNAs <- lme(y ~ tx*x, random = ~x | subj, data = dNAs, 23 | na.action = "na.omit") 24 | au <- augment(fitNAs) 25 | expect_equal(nrow(au), sum(complete.cases(dNAs))) 26 | }) 27 | 28 | 29 | test_that("augment works on lme fits with na.omit", { 30 | fitNAs <- lme(y ~ tx*x, random = ~x | subj, data = dNAs, 31 | na.action = "na.exclude") 32 | 33 | au <- augment(fitNAs, dNAs) 34 | 35 | # with na.exclude, should have NAs in the output where there were NAs in input 36 | expect_equal(nrow(au), nrow(dNAs)) 37 | expect_equal(complete.cases(au), complete.cases(dNAs)) 38 | }) 39 | 40 | test_that("glance works on nlme fits", { 41 | g <- glance(fit) 42 | }) 43 | 44 | 45 | testFit <- function(fit, data = NULL){ 46 | test_that("Pinheiro/Bates fit works", { 47 | tidy(fit, "fixed") 48 | tidy(fit) 49 | glance(fit) 50 | if (is.null(data)) { 51 | augment(fit) 52 | } else { 53 | augment(fit, data) 54 | } 55 | }) 56 | } 57 | 58 | testFit(lme(score ~ Machine, data = Machines, random = ~1 | Worker)) 59 | testFit(lme(score ~ Machine, data = Machines, random = ~1 | Worker)) 60 | testFit(lme(score ~ Machine, data = Machines, random = ~1 | Worker / Machine)) 61 | testFit(lme(pixel ~ day + day ^ 2, data = Pixel, random = list(Dog = ~day, Side = ~1))) 62 | testFit(lme(pixel ~ day + day ^ 2 + Side, data = Pixel, 63 | random = list(Dog = ~day, Side = ~1))) 64 | 65 | testFit(lme(yield ~ ordered(nitro)*Variety, data = Oats, 66 | random = ~1/Block/Variety)) 67 | # There are cases where no data set is returned in the result 68 | # We can do nothing about this inconsitency but give a useful error message in augment 69 | fit = nlme(conc ~ SSfol(Dose, Time, lKe, lKa, lCl), data = Theoph, 70 | random = pdDiag(lKe + lKa + lCl ~ 1)) 71 | test_that( 72 | "Fit without data in returned structure works when data are given", { 73 | testFit(fit, Theoph) 74 | }) 75 | # When no data are passed, a meaningful message is issued 76 | expect_error(augment(fit), "explicit") 77 | } 78 | -------------------------------------------------------------------------------- /man/cv.glmnet_tidiers.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/glmnet_tidiers.R 3 | \name{cv.glmnet_tidiers} 4 | \alias{cv.glmnet_tidiers} 5 | \alias{glance.cv.glmnet} 6 | \alias{tidy.cv.glmnet} 7 | \title{Tidiers for glmnet cross-validation objects} 8 | \usage{ 9 | \method{tidy}{cv.glmnet}(x, ...) 10 | 11 | \method{glance}{cv.glmnet}(x, ...) 12 | } 13 | \arguments{ 14 | \item{x}{a "cv.glmnet" object} 15 | 16 | \item{...}{extra arguments (not used)} 17 | } 18 | \value{ 19 | All tidying methods return a data.frame without rownames, whose 20 | structure depends on the method chosen. 21 | 22 | \code{tidy} produces a data.frame with one row per choice of lambda, 23 | with columns 24 | \item{lambda}{penalty parameter lambda} 25 | \item{estimate}{estimate (median) of mean-squared error or other 26 | criterion} 27 | \item{std.error}{standard error of criterion} 28 | \item{conf.high}{high end of confidence interval on criterion} 29 | \item{conf.low}{low end of confidence interval on criterion} 30 | \item{nzero}{number of parameters that are zero at this choice of lambda} 31 | 32 | \code{glance} returns a one-row data.frame with the values 33 | \item{nulldev}{null deviance} 34 | \item{npasses}{total passes over the data across all lambda values} 35 | } 36 | \description{ 37 | Tidying methods for cross-validation performed by \code{glmnet.cv}, 38 | summarizing the mean-squared-error across choices of the penalty parameter 39 | lambda. 40 | } 41 | \details{ 42 | No \code{augment} method exists for this class. 43 | } 44 | \examples{ 45 | 46 | if (require("glmnet", quietly = TRUE)) { 47 | set.seed(2014) 48 | 49 | nobs <- 100 50 | nvar <- 50 51 | real <- 5 52 | 53 | x <- matrix(rnorm(nobs * nvar), nobs, nvar) 54 | beta <- c(rnorm(real, 0, 1), rep(0, nvar - real)) 55 | y <- c(t(beta) \%*\% t(x)) + rnorm(nvar, sd = 3) 56 | 57 | cvfit1 <- cv.glmnet(x,y) 58 | 59 | head(tidy(cvfit1)) 60 | glance(cvfit1) 61 | 62 | library(ggplot2) 63 | tidied_cv <- tidy(cvfit1) 64 | glance_cv <- glance(cvfit1) 65 | 66 | # plot of MSE as a function of lambda 67 | g <- ggplot(tidied_cv, aes(lambda, estimate)) + geom_line() + scale_x_log10() 68 | g 69 | 70 | # plot of MSE as a function of lambda with confidence ribbon 71 | g <- g + geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = .25) 72 | g 73 | 74 | # plot of MSE as a function of lambda with confidence ribbon and choices 75 | # of minimum lambda marked 76 | g <- g + geom_vline(xintercept = glance_cv$lambda.min) + 77 | geom_vline(xintercept = glance_cv$lambda.1se, lty = 2) 78 | g 79 | 80 | # plot of number of zeros for each choice of lambda 81 | ggplot(tidied_cv, aes(lambda, nzero)) + geom_line() + scale_x_log10() 82 | 83 | # coefficient plot with min lambda shown 84 | tidied <- tidy(cvfit1$glmnet.fit) 85 | ggplot(tidied, aes(lambda, estimate, group = term)) + scale_x_log10() + 86 | geom_line() + 87 | geom_vline(xintercept = glance_cv$lambda.min) + 88 | geom_vline(xintercept = glance_cv$lambda.1se, lty = 2) 89 | } 90 | 91 | } 92 | 93 | --------------------------------------------------------------------------------