Skip to content

Commit

Permalink
Use summary.rq in quantreg tidiers, and return tibbles. Closes #373. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuehn10 authored and alexpghayes committed Jun 18, 2018
1 parent 03b182d commit fc34d28
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
14 changes: 7 additions & 7 deletions R/nls_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @param data original data this was fitted on; if not given this will
#' attempt to be reconstructed from nls (may not be successful)
#'
#' @return All tidying methods return a `data.frame` without rownames.
#' @return All tidying methods return a `tibble` without rownames.
#' The structure depends on the method chosen.
#'
#' @template augment_NAs
Expand Down Expand Up @@ -60,7 +60,7 @@ tidy.nls <- function(x, conf.int = FALSE, conf.level = .95,
term = names(co), estimate = unname(co),
stringsAsFactors = FALSE
)
return(ret)
return(tibble::as_tibble(ret))
}

nn <- c("estimate", "std.error", "statistic", "p.value")
Expand All @@ -75,7 +75,7 @@ tidy.nls <- function(x, conf.int = FALSE, conf.level = .95,
colnames(CI) <- c("conf.low", "conf.high")
ret <- cbind(ret, unrowname(CI))
}
ret
tibble::as_tibble(ret)
}


Expand All @@ -97,7 +97,7 @@ augment.nls <- function(x, data = NULL, newdata = NULL, ...) {
# use predictions on new data
newdata <- fix_data_frame(newdata, newcol = ".rownames")
newdata$.fitted <- stats::predict(x, newdata = newdata)
return(newdata)
return(tibble::as_tibble(newdata))
}

if (is.null(data)) {
Expand All @@ -113,14 +113,14 @@ augment.nls <- function(x, data = NULL, newdata = NULL, ...) {
# }
}

return(augment_columns(x, data))
return(tibble::as_tibble(augment_columns(x, data)))

# move rownames if necessary
data <- fix_data_frame(data, newcol = ".rownames")

data$.fitted <- stats::predict(x)
data$.resid <- stats::resid(x)
data
tibble::as_tibble(data)
}


Expand All @@ -145,5 +145,5 @@ glance.nls <- function(x, ...) {
sigma = s$sigma, isConv = s$convInfo$isConv,
finTol = s$convInfo$finTol
))
finish_glance(ret, x)
tibble::as_tibble(finish_glance(ret, x))
}
23 changes: 12 additions & 11 deletions R/rq_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ NULL
#' If `se.type != 'rank'` and `conf.int = TRUE`, confidence intervals
#' are standard t based intervals.
#'
#' @return `tidy.rq` returns a data frame with one row for each coefficient.
#' @return `tidy.rq` returns a tibble with one row for each coefficient.
#' The columns depend upon the confidence interval method selected.
#'
#' @export
tidy.rq <- function(x, se.type = "rank", conf.int = TRUE, conf.level = 0.95, alpha = 1 - conf.level, ...) {
tidy.rq <- function(x, se.type = "rank", conf.int = TRUE, conf.level = 0.9, alpha = 1 - conf.level, ...) {
# summary.rq often issues warnings when computing standard errors
rq_summary <- suppressWarnings(summary(x, se = se.type, alpha = alpha, ...))
rq_summary <- suppressWarnings(quantreg::summary.rq(x, se = se.type, alpha = alpha, ...))
process_rq(rq_obj = rq_summary, se.type = se.type, conf.int = conf.int, conf.level = conf.level, ...)
}

Expand All @@ -47,9 +47,9 @@ tidy.rq <- function(x, se.type = "rank", conf.int = TRUE, conf.level = 0.95, alp
#' method selected.
#'
#' @export
tidy.rqs <- function(x, se.type = "rank", conf.int = TRUE, conf.level = 0.95, alpha = 1 - conf.level, ...) {
tidy.rqs <- function(x, se.type = "rank", conf.int = TRUE, conf.level = 0.9, alpha = 1 - conf.level, ...) {
# summary.rq often issues warnings when computing standard errors
rq_summary <- suppressWarnings(summary(x, se = se.type, alpha = alpha, ...))
rq_summary <- suppressWarnings(quantreg::summary.rqs(x, se = se.type, alpha = alpha, ...))
plyr::ldply(rq_summary, process_rq, se.type = se.type, conf.int = conf.int, conf.level = conf.level, ...)
}

Expand All @@ -75,7 +75,7 @@ tidy.nlrq <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
ret[["conf.low"]] <- ret[["estimate"]] + (cv[1] * ret[["std.error"]])
ret[["conf.high"]] <- ret[["estimate"]] + (cv[2] * ret[["std.error"]])
}
ret
tibble::as_tibble(ret)
}

#' @rdname rq_tidiers
Expand All @@ -100,7 +100,7 @@ glance.rq <- function(x, ...) {
n <- length(fitted(x))
s <- summary(x)

data.frame(
tibble::tibble(
tau = x[["tau"]],
logLik = logLik(x),
AIC = AIC(x),
Expand Down Expand Up @@ -131,7 +131,7 @@ glance.rqs <- function(x, ...) {
glance.nlrq <- function(x, ...) {
n <- length(x[["m"]]$fitted())
s <- summary(x)
data.frame(
tibble::tibble(
tau = x[["m"]]$tau(),
logLik = logLik(x),
AIC = AIC(x),
Expand Down Expand Up @@ -215,15 +215,16 @@ augment.rqs <- function(x, data = model.frame(x), newdata, ...) {
pred <- setNames(as.data.frame(pred), x[["tau"]])
# pred <- reshape2::melt(pred,measure.vars = 1:ncol(pred),variable.name = ".tau",value.name = ".fitted")
pred <- tidyr::gather(data = pred, key = ".tau", value = ".fitted")
return(unrowname(cbind(original, pred[, -1, drop = FALSE])))
ret <- unrowname(cbind(original, pred[, -1, drop = FALSE]))
} else {
original <- newdata[rep(seq_len(nrow(newdata)), n_tau), ]
pred <- predict(x, newdata = newdata, stepfun = FALSE, ...)
pred <- setNames(as.data.frame(pred), x[["tau"]])
# pred <- reshape2::melt(pred,measure.vars = 1:ncol(pred),variable.name = ".tau",value.name = ".fitted")
pred <- tidyr::gather(data = pred, key = ".tau", value = ".fitted")
return(unrowname(cbind(original, pred)))
ret <- unrowname(cbind(original, pred))
}
tibble::as_tibble(ret)
}

#' @rdname rq_tidiers
Expand Down Expand Up @@ -273,5 +274,5 @@ process_rq <- function(rq_obj, se.type = "rank",
co[["conf.high"]] <- co[["estimate"]] + (cv[2] * co[["std.error"]])
}
co[["tau"]] <- rq_obj[["tau"]]
fix_data_frame(co, colnames(co))
tibble::as_tibble(fix_data_frame(co, colnames(co)))
}
2 changes: 1 addition & 1 deletion man/nls_tidiers.Rd

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

8 changes: 4 additions & 4 deletions man/rq_tidiers.Rd

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

0 comments on commit fc34d28

Please sign in to comment.