Skip to content

Commit

Permalink
Added tidiers for drc::drc() (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
Edild authored and alexpghayes committed Apr 8, 2019
1 parent f865aee commit 63c3b9f
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 1 deletion.
11 changes: 10 additions & 1 deletion DESCRIPTION
Expand Up @@ -221,7 +221,11 @@ Authors@R:
family = "Gegzna",
email = "GegznaV@gmail.com",
role = "ctb",
comment = c(ORCID = "0000-0002-9500-5167")))
comment = c(ORCID = "0000-0002-9500-5167")),
person(given = "Eduard",
family = "Szoecs",
email = "eduardszoecs@gmail.com",
role = "ctb"))
Description: Summarizes key information about statistical
objects in tidy tibbles. This makes it easy to report results, create
plots and consistently work with large numbers of models at once.
Expand Down Expand Up @@ -327,6 +331,10 @@ Suggests:
tseries,
xergm,
zoo,
modeltests,
leaps,
lm.beta,
drc
metafor
VignetteBuilder:
knitr
Expand All @@ -353,6 +361,7 @@ Collate:
'car-tidiers.R'
'caret-tidiers.R'
'data-frame-tidiers.R'
'drc-tidiers.R'
'emmeans-tidiers.R'
'ergm-tidiers.R'
'gam-tidiers.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -7,6 +7,7 @@ S3method(augment,clm)
S3method(augment,coxph)
S3method(augment,decomposed.ts)
S3method(augment,default)
S3method(augment,drc)
S3method(augment,factanal)
S3method(augment,felm)
S3method(augment,glm)
Expand Down Expand Up @@ -49,6 +50,7 @@ S3method(glance,coxph)
S3method(glance,cv.glmnet)
S3method(glance,data.frame)
S3method(glance,default)
S3method(glance,drc)
S3method(glance,durbinWatsonTest)
S3method(glance,ergm)
S3method(glance,factanal)
Expand Down Expand Up @@ -131,6 +133,7 @@ S3method(tidy,cv.glmnet)
S3method(tidy,default)
S3method(tidy,density)
S3method(tidy,dist)
S3method(tidy,drc)
S3method(tidy,durbinWatsonTest)
S3method(tidy,emmGrid)
S3method(tidy,ergm)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Expand Up @@ -86,6 +86,13 @@ TODO: sort out what happens to `glance.aov()`
- Added `tidy.regsubsets()` for best subsets linear regression from the `leaps` package

- Added method `tidy.lm.beta()` to tidy `lm.beta` class models (#545 by @mattle24)

- Add feature for glance.biglm to return df.residual

- Patch bug in glance.lavaan (#577)

- Added tidiers for `drc::drm` models (#574 by @edild)

- `tidy.prcomp()` parameter `matrix` gained new options `"scores"`, `"loadings"`, and `"eigenvalues"` (#557 by @GegznaV)

- `tidy.kmeans()` now uses the names of the input variables in the output by
Expand Down
186 changes: 186 additions & 0 deletions R/drc-tidiers.R
@@ -0,0 +1,186 @@
#' @templateVar class drc
#' @template title_desc_tidy
#'
#' @param x A `drc` object produced by a call to [drc::drm()].
#' @template param_confint
#' @template param_unused_dots
#' @param quick whether to compute a smaller and faster version, containing
#' only the \code{term}, \code{curveid} and \code{estimate} columns.
#' @evalRd return_tidy(
#' curveid = "Id of the curve",
#' "term",
#' "estimate",
#' "std.error",
#' "statistic",
#' "p.value",
#' "conf.low",
#' "conf.high"
#' )
#' @details The tibble has one row for each curve and term in the regression. The
#' `curveid` column indicates the curve.
#' @author Eduard Szoecs, \email{eduardszoecs@@gmail.com}
#' @examples
#' library(drc)
#' mod <- drm(dead/total~conc, type,
#' weights = total, data = selenium, fct = LL.2(), type = "binomial")
#' mod
#'
#' tidy(mod)
#' tidy(mod, conf.int = TRUE)
#' tidy(mod, quick = TRUE)

#' glance(mod)

#' # augment(mod)
#' @export
#' @seealso [tidy()], [drc::drm()]
#' @family drc tidiers
#' @aliases drc_tidiers
tidy.drc <- function(x, conf.int = FALSE, conf.level = 0.95, quick = FALSE, ...) {
if (quick) {
co <- coef(x)
nam <- names(co)
term <- gsub("^(.*):(.*)$", "\\1", nam)
curves <- x[["dataList"]][["curveid"]]
if (length(unique(curves)) > 1) {
curveid <- gsub("^(.*):(.*)$", "\\2", nam)
} else {
curveid <- unique(curves)
}
ret <- tibble(term = term,
curveid = curveid,
estimate = unname(co))
return(ret)
}

co <- coef(summary(x))

nam <- rownames(co)
term <- gsub("^(.*):(.*)$", "\\1", nam)
curves <- x[["dataList"]][["curveid"]]
if (length(unique(curves)) > 1) {
curveid <- gsub("^(.*):(.*)$", "\\2", nam)
} else {
curveid <- unique(curves)
}
ret <- data.frame(term = term,
curveid = curveid,
co, stringsAsFactors = FALSE)
names(ret) <- c("term", "curveid", "estimate", "std.error", "statistic",
"p.value")
rownames(ret) <- NULL

if (conf.int) {
conf <- confint(x, level = conf.level)
colnames(conf) <- c("conf.low", "conf.high")
rownames(conf) <- NULL
ret <- cbind(ret, conf)
}

return(as_tibble(ret))
}

#' @templateVar class drc
#' @template title_desc_glance
#'
#' @inherit tidy.drc params examples
#' @template param_unused_dots
#'
#' @evalRd return_glance(
#' "logLik",
#' "AIC",
#' "AICc" = "AIC corrected for small samples",
#' "BIC",
#' "df.residual"
#' )
#' @seealso [glance()], [drc::drm()]
#' @export
#' @family drc tidiers
glance.drc <- function(x, ...) {
ret <- data.frame(AIC = AIC(x),
BIC = BIC(x),
logLik = logLik(x),
df.residual = x$df.residual)
return(as_tibble(ret))
}

#' @templateVar class drc
#' @template title_desc_augment

#' @inherit tidy.drc params examples
#' @template param_data
#' @template param_newdata
#' @template param_confint
#' @template param_se_fit
#' @template param_unused_dots
#'
#' @evalRd return_augment(".conf.low" = "Lower Confidence Interval",
#' ".conf.high" = "Upper Confidence Interval",
#' ".se.fit",
#' ".fitted",
#' ".resid",
#' ".cooksd")
#'
#' @seealso [augment()], [drc::drm()]
#' @export
#' @author Eduard Szoecs, \email{eduardszoecs@@gmail.com}
#' @family drc tidiers
augment.drc <- function(x, data = NULL, newdata = NULL,
se_fit = FALSE, conf.int = FALSE, conf.level = 0.95, ...) {

if (is.null(data) && is.null(newdata)) {
stop("Must specify either `data` or `newdata` argument.", call. = FALSE)
}

# drc doesn't like tibbles
if (inherits(newdata, "tbl")) {
newdata <- data.frame(newdata)
}

# drc doesn't like NA in the type
if (!missing(newdata) || is.null(newdata)) {
original <- newdata
original$.rownames <- rownames(original)
}

if (!missing(newdata) && x$curveVarNam %in% names(newdata) &&
any(is.na(newdata[[x$curveVarNam]]))) {
newdata <- newdata[!is.na(newdata[[x$curveVarNam]]), ]
}

ret <- augment_columns(x, data, newdata, se.fit = FALSE)

if (!is.null(newdata)) {
if (conf.int) {
preds <- data.frame(predict(x, newdata = newdata, interval = "confidence",
level = conf.level))
ret[[".conf.low"]] <- preds[["Lower"]]
ret[[".conf.high"]] <- preds[["Upper"]]
}
if (se_fit) {
preds <- data.frame(predict(x, newdata = newdata, se.fit = TRUE))
ret[[".se.fit"]] <- preds[["SE"]]
}
}

# join back removed rows
if (!".rownames" %in% names(ret)) {
ret$.rownames <- rownames(ret)
}

if (!is.null(original)) {
reto <- ret %>% select(starts_with("."))
ret <- merge(reto, original, by = ".rownames", all.y = TRUE)
}

# reorder to line up with original
ret <- ret[order(match(ret$.rownames, rownames(original))), ]
rownames(ret) <- NULL

# if rownames are just the original 1...n, they can be removed
if (all(ret$.rownames == seq_along(ret$.rownames))) {
ret$.rownames <- NULL
}

as_tibble(ret)
}
117 changes: 117 additions & 0 deletions man/augment.drc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/broom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63c3b9f

Please sign in to comment.