|
66 | 66 | return(NULL) |
67 | 67 | } |
68 | 68 |
|
69 | | - if (inherits(model, c("glm", "glmerMod")) || (inherits(model, "glmmTMB") && isFALSE(model_info$is_linear))) { |
| 69 | + if ( |
| 70 | + inherits(model, c("glm", "glmerMod")) || |
| 71 | + (inherits(model, "glmmTMB") && isFALSE(model_info$is_linear)) |
| 72 | + ) { |
70 | 73 | fitted_ <- stats::qnorm((stats::ppoints(length(res_)) + 1) / 2) |
71 | 74 | } else { |
72 | 75 | fitted_ <- stats::fitted(model) |
|
89 | 92 |
|
90 | 93 | # prepare data for random effects QQ plot ---------------------------------- |
91 | 94 |
|
92 | | -.model_diagnostic_ranef_qq <- function(model, level = 0.95, model_info = NULL, verbose = TRUE) { |
| 95 | +.model_diagnostic_ranef_qq <- function( |
| 96 | + model, |
| 97 | + level = 0.95, |
| 98 | + model_info = NULL, |
| 99 | + verbose = TRUE |
| 100 | +) { |
93 | 101 | # check if we have mixed model |
94 | 102 | if (is.null(model_info) || !model_info$is_mixed) { |
95 | 103 | return(NULL) |
|
110 | 118 | } |
111 | 119 | ) |
112 | 120 |
|
113 | | - |
114 | 121 | se <- tryCatch( |
115 | 122 | suppressWarnings(lapply(re, function(.x) { |
116 | 123 | pv <- attr(.x, var_attr, exact = TRUE) |
|
124 | 131 |
|
125 | 132 | if (is.null(se)) { |
126 | 133 | if (verbose) { |
127 | | - insight::format_alert("Could not compute standard errors from random effects for diagnostic plot.") |
| 134 | + insight::format_alert( |
| 135 | + "Could not compute standard errors from random effects for diagnostic plot." |
| 136 | + ) |
128 | 137 | } |
129 | 138 | return(NULL) |
130 | 139 | } |
131 | 140 |
|
132 | | - |
133 | | - Map(function(.re, .se) { |
134 | | - ord <- unlist(lapply(.re, order)) + rep((0:(ncol(.re) - 1)) * nrow(.re), each = nrow(.re)) |
135 | | - |
136 | | - df.y <- unlist(.re)[ord] |
137 | | - df.ci <- stats::qnorm((1 + level) / 2) * .se[ord] |
138 | | - |
139 | | - data.frame( |
140 | | - x = rep(stats::qnorm(stats::ppoints(nrow(.re))), ncol(.re)), |
141 | | - y = df.y, |
142 | | - conf.low = df.y - df.ci, |
143 | | - conf.high = df.y + df.ci, |
144 | | - facet = gl(ncol(.re), nrow(.re), labels = names(.re)), |
145 | | - stringsAsFactors = FALSE, |
146 | | - row.names = NULL |
147 | | - ) |
148 | | - }, re, se) |
| 141 | + Map( |
| 142 | + function(.re, .se) { |
| 143 | + ord <- unlist(lapply(.re, order)) + |
| 144 | + rep((0:(ncol(.re) - 1)) * nrow(.re), each = nrow(.re)) |
| 145 | + |
| 146 | + df.y <- unlist(.re)[ord] |
| 147 | + df.ci <- stats::qnorm((1 + level) / 2) * .se[ord] |
| 148 | + |
| 149 | + data.frame( |
| 150 | + x = rep(stats::qnorm(stats::ppoints(nrow(.re))), ncol(.re)), |
| 151 | + y = df.y, |
| 152 | + conf.low = df.y - df.ci, |
| 153 | + conf.high = df.y + df.ci, |
| 154 | + facet = gl(ncol(.re), nrow(.re), labels = names(.re)), |
| 155 | + stringsAsFactors = FALSE, |
| 156 | + row.names = NULL |
| 157 | + ) |
| 158 | + }, |
| 159 | + re, |
| 160 | + se |
| 161 | + ) |
149 | 162 | } |
150 | 163 |
|
151 | 164 |
|
|
163 | 176 | } |
164 | 177 |
|
165 | 178 | dat <- as.data.frame(bayestestR::estimate_density(r)) |
166 | | - dat$curve <- stats::dnorm(seq(min(dat$x), max(dat$x), length.out = nrow(dat)), mean(r), stats::sd(r)) |
| 179 | + dat$curve <- stats::dnorm( |
| 180 | + seq(min(dat$x), max(dat$x), length.out = nrow(dat)), |
| 181 | + mean(r), |
| 182 | + stats::sd(r) |
| 183 | + ) |
167 | 184 | dat |
168 | 185 | } |
169 | 186 |
|
|
198 | 215 | ) |
199 | 216 | plot_data$Index <- seq_len(nrow(plot_data)) |
200 | 217 | plot_data$Influential <- "OK" |
201 | | - plot_data$Influential[abs(plot_data$Cooks_Distance) >= max(cook_levels)] <- "Influential" |
| 218 | + plot_data$Influential[ |
| 219 | + abs(plot_data$Cooks_Distance) >= max(cook_levels) |
| 220 | + ] <- "Influential" |
202 | 221 |
|
203 | 222 | attr(plot_data, "cook_levels") <- cook_levels |
204 | 223 | attr(plot_data, "n_params") <- n_params |
|
304 | 323 | d$V <- insight::get_sigma(model)^2 * stats::family(model)$variance(predicted) |
305 | 324 | } else { |
306 | 325 | # for nbinom2, "sigma()" has "inverse meaning" (see #654) |
307 | | - d$V <- (1 / insight::get_sigma(model)^2) * stats::family(model)$variance(predicted) |
| 326 | + d$V <- (1 / insight::get_sigma(model)^2) * |
| 327 | + stats::family(model)$variance(predicted) |
308 | 328 | } |
309 | 329 | } else { |
310 | 330 | ## FIXME: this is not correct for glm.nb models? |
|
332 | 352 | } |
333 | 353 | d$Prob <- stats::predict(model, type = ptype) |
334 | 354 | d$Disp <- insight::get_sigma(model) |
335 | | - d$V <- predicted * (1 + predicted / d$Disp) * (1 - d$Prob) * (1 + predicted * (1 + predicted / d$Disp) * d$Prob) # nolint |
| 355 | + d$V <- predicted * |
| 356 | + (1 + predicted / d$Disp) * |
| 357 | + (1 - d$Prob) * |
| 358 | + (1 + predicted * (1 + predicted / d$Disp) * d$Prob) # nolint |
336 | 359 | } |
337 | 360 |
|
338 | 361 | # data for zero-inflated negative binomial models with dispersion |
|
345 | 368 | } |
346 | 369 | d$Prob <- stats::predict(model, type = ptype) |
347 | 370 | d$Disp <- stats::predict(model, type = "disp") |
348 | | - d$V <- predicted * (1 + predicted / d$Disp) * (1 - d$Prob) * (1 + predicted * (1 + predicted / d$Disp) * d$Prob) # nolint |
| 371 | + d$V <- predicted * |
| 372 | + (1 + predicted / d$Disp) * |
| 373 | + (1 - d$Prob) * |
| 374 | + (1 + predicted * (1 + predicted / d$Disp) * d$Prob) # nolint |
349 | 375 | } |
350 | 376 |
|
351 | 377 | d |
|
376 | 402 | d$V <- insight::get_sigma(model)^2 * stats::family(model)$variance(d$Predicted) |
377 | 403 | } else { |
378 | 404 | # for nbinom2, "sigma()" has "inverse meaning" (see #654) |
379 | | - d$V <- (1 / insight::get_sigma(model)^2) * stats::family(model)$variance(d$Predicted) |
| 405 | + d$V <- (1 / insight::get_sigma(model)^2) * |
| 406 | + stats::family(model)$variance(d$Predicted) |
380 | 407 | } |
381 | 408 | } else { |
382 | 409 | ## FIXME: this is not correct for glm.nb models? |
|
415 | 442 | } |
416 | 443 | d$Prob <- stats::predict(model, type = ptype) |
417 | 444 | d$Disp <- insight::get_sigma(model) |
418 | | - d$V <- d$Predicted * (1 + d$Predicted / d$Disp) * (1 - d$Prob) * (1 + d$Predicted * (1 + d$Predicted / d$Disp) * d$Prob) # nolint |
| 445 | + d$V <- d$Predicted * |
| 446 | + (1 + d$Predicted / d$Disp) * |
| 447 | + (1 - d$Prob) * |
| 448 | + (1 + d$Predicted * (1 + d$Predicted / d$Disp) * d$Prob) # nolint |
419 | 449 | d$StdRes <- insight::get_residuals(model, type = "pearson") |
420 | 450 | } |
421 | 451 |
|
|
431 | 461 | } |
432 | 462 | d$Prob <- stats::predict(model, type = ptype) |
433 | 463 | d$Disp <- stats::predict(model, type = "disp") |
434 | | - d$V <- d$Predicted * (1 + d$Predicted / d$Disp) * (1 - d$Prob) * (1 + d$Predicted * (1 + d$Predicted / d$Disp) * d$Prob) # nolint |
| 464 | + d$V <- d$Predicted * |
| 465 | + (1 + d$Predicted / d$Disp) * |
| 466 | + (1 - d$Prob) * |
| 467 | + (1 + d$Predicted * (1 + d$Predicted / d$Disp) * d$Prob) # nolint |
435 | 468 | d$StdRes <- insight::get_residuals(model, type = "pearson") |
436 | 469 | } |
437 | 470 |
|
|
446 | 479 | return(1) |
447 | 480 | } |
448 | 481 | betad <- model$fit$par["betadisp"] |
449 | | - switch(faminfo$family, |
| 482 | + switch( |
| 483 | + faminfo$family, |
450 | 484 | gaussian = exp(0.5 * betad), |
451 | 485 | Gamma = exp(-0.5 * betad), |
452 | 486 | exp(betad) |
|
0 commit comments