| 1 |
#' @templateVar class nls |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `nls` object returned from [stats::nls()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @template param_unused_dots |
| 7 |
#' |
| 8 |
#' @evalRd return_tidy(regression = TRUE) |
| 9 |
#' |
| 10 |
#' @examplesIf rlang::is_installed("ggplot2") |
| 11 |
#' |
| 12 |
#' # fit model |
| 13 |
#' n <- nls(mpg ~ k * e^wt, data = mtcars, start = list(k = 1, e = 2)) |
| 14 |
#' |
| 15 |
#' # summarize model fit with tidiers + visualization |
| 16 |
#' tidy(n) |
| 17 |
#' augment(n) |
| 18 |
#' glance(n) |
| 19 |
#' |
| 20 |
#' library(ggplot2) |
| 21 |
#' |
| 22 |
#' ggplot(augment(n), aes(wt, mpg)) + |
| 23 |
#' geom_point() + |
| 24 |
#' geom_line(aes(y = .fitted)) |
| 25 |
#' |
| 26 |
#' newdata <- head(mtcars) |
| 27 |
#' newdata$wt <- newdata$wt + 1 |
| 28 |
#' |
| 29 |
#' augment(n, newdata = newdata) |
| 30 |
#' |
| 31 |
#' @aliases nls_tidiers |
| 32 |
#' @export |
| 33 |
#' @seealso [tidy], [stats::nls()], [stats::summary.nls()] |
| 34 |
#' @family nls tidiers |
| 35 |
tidy.nls <- function(x, conf.int = FALSE, conf.level = .95, ...) { |
| 36 |
check_ellipses("exponentiate", "tidy", "nls", ...) |
| 37 |
|
| 38 |
ret <- as_tidy_tibble( |
| 39 |
stats::coef(summary(x)), |
| 40 |
new_names = c("estimate", "std.error", "statistic", "p.value") |
| 41 |
) |
| 42 |
|
| 43 |
if (conf.int) { |
| 44 |
# avoid "Waiting for profiling to be done..." message |
| 45 |
CI <- suppressMessages(stats::confint(x, level = conf.level)) |
| 46 |
if (is.null(dim(CI))) { |
| 47 |
CI <- matrix(CI, nrow = 1) |
| 48 |
} |
| 49 |
colnames(CI) <- c("conf.low", "conf.high") |
| 50 |
ret <- cbind(ret, unrowname(CI)) |
| 51 |
} |
| 52 |
as_tibble(ret) |
| 53 |
} |
| 54 |
|
| 55 |
#' @templateVar class nls |
| 56 |
#' @template title_desc_augment |
| 57 |
#' |
| 58 |
#' @inherit tidy.nls params examples |
| 59 |
#' @template param_data |
| 60 |
#' @template param_newdata |
| 61 |
#' |
| 62 |
#' @evalRd return_augment() |
| 63 |
#' |
| 64 |
#' @details augment.nls does not currently support confidence intervals due to |
| 65 |
#' a lack of support in stats::predict.nls(). |
| 66 |
#' |
| 67 |
#' @export |
| 68 |
#' @seealso [tidy], [stats::nls()], [stats::predict.nls()] |
| 69 |
#' @family nls tidiers |
| 70 |
#' |
| 71 |
augment.nls <- function(x, data = NULL, newdata = NULL, ...) { |
| 72 |
if (is.null(data) && is.null(newdata)) { |
| 73 |
pars <- names(x$m$getPars()) |
| 74 |
env <- as.list(x$m$getEnv()) |
| 75 |
data <- as_tibble(env[!(names(env) %in% pars)]) |
| 76 |
} |
| 77 |
|
| 78 |
augment_columns(x, data, newdata, .se_fit = FALSE) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
#' @templateVar class nls |
| 83 |
#' @template title_desc_glance |
| 84 |
#' |
| 85 |
#' @inherit tidy.nls params examples |
| 86 |
#' |
| 87 |
#' @evalRd return_glance( |
| 88 |
#' "sigma", |
| 89 |
#' "isConv", |
| 90 |
#' "finTol", |
| 91 |
#' "logLik", |
| 92 |
#' "AIC", |
| 93 |
#' "BIC", |
| 94 |
#' "deviance", |
| 95 |
#' "df.residual", |
| 96 |
#' "nobs" |
| 97 |
#' ) |
| 98 |
#' |
| 99 |
#' @export |
| 100 |
#' @seealso [tidy], [stats::nls()] |
| 101 |
#' @family nls tidiers |
| 102 |
glance.nls <- function(x, ...) { |
| 103 |
s <- summary(x) |
| 104 |
|
| 105 |
as_glance_tibble( |
| 106 |
sigma = s$sigma, |
| 107 |
isConv = s$convInfo$isConv, |
| 108 |
finTol = s$convInfo$finTol, |
| 109 |
logLik = as.numeric(stats::logLik(x)), |
| 110 |
AIC = stats::AIC(x), |
| 111 |
BIC = stats::BIC(x), |
| 112 |
deviance = stats::deviance(x), |
| 113 |
df.residual = stats::df.residual(x), |
| 114 |
nobs = stats::nobs(x), |
| 115 |
na_types = "rlrrrrrii" |
| 116 |
) |
| 117 |
} |