Skip to content

Commit da7112b

Browse files
authored
Merge pull request #25 from mthulin/master
Add convenience functions for residuals and distributions
2 parents c15e41d + b7434f3 commit da7112b

13 files changed

+880
-11
lines changed

DESCRIPTION

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Authors@R: c(
88
person("Hadley", "Wickham", email = "[email protected]", role = c("aut", "ctb"), comment = c(ORCID = "0000-0003-4757-117X")),
99
person("Niladri Roy", "Chowdhury", email = "[email protected]", role = c("aut", "ctb")),
1010
person("Di", "Cook", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3813-7155")),
11-
person("Heike", "Hofmann", email = "[email protected]", role = c("aut", "ctb"), comment = c(ORCID = "0000-0001-6216-5183"))
11+
person("Heike", "Hofmann", email = "[email protected]", role = c("aut", "ctb"), comment = c(ORCID = "0000-0001-6216-5183")),
12+
person("Måns", "Thulin", email = "[email protected]", role = c("aut", "ctb"))
1213
)
1314
Maintainer: Di Cook <[email protected]>
1415
License: GPL (>= 2)
@@ -23,7 +24,8 @@ Imports:
2324
purrr,
2425
tidyr,
2526
tibble,
26-
magrittr
27+
magrittr,
28+
stats
2729
Suggests:
2830
forecast,
2931
viridis,
@@ -32,5 +34,5 @@ LazyData: true
3234
Type: Package
3335
LazyLoad: false
3436
VignetteBuilder: knitr
35-
RoxygenNote: 7.3.1
37+
RoxygenNote: 7.3.2
3638
Encoding: UTF-8

NAMESPACE

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export(decrypt)
88
export(distmet)
99
export(distplot)
1010
export(lineup)
11+
export(lineup_histograms)
12+
export(lineup_qq)
13+
export(lineup_residuals)
1114
export(null_dist)
1215
export(null_lm)
1316
export(null_permute)
@@ -17,6 +20,7 @@ export(pvisual)
1720
export(reg_dist)
1821
export(resid_boot)
1922
export(resid_pboot)
23+
export(resid_perm)
2024
export(resid_rotate)
2125
export(resid_sigma)
2226
export(rorschach)
@@ -33,9 +37,21 @@ importFrom(dplyr,filter)
3337
importFrom(dplyr,group_by)
3438
importFrom(dplyr,mutate)
3539
importFrom(dplyr,summarise)
40+
importFrom(ggplot2,.data)
3641
importFrom(ggplot2,aes)
42+
importFrom(ggplot2,after_stat)
43+
importFrom(ggplot2,facet_wrap)
44+
importFrom(ggplot2,geom_abline)
45+
importFrom(ggplot2,geom_density)
46+
importFrom(ggplot2,geom_histogram)
47+
importFrom(ggplot2,geom_line)
48+
importFrom(ggplot2,geom_point)
49+
importFrom(ggplot2,geom_qq)
50+
importFrom(ggplot2,geom_qq_line)
51+
importFrom(ggplot2,geom_smooth)
3752
importFrom(ggplot2,geom_tile)
3853
importFrom(ggplot2,ggplot)
54+
importFrom(ggplot2,labs)
3955
importFrom(ggplot2,last_plot)
4056
importFrom(ggplot2,scale_fill_gradient)
4157
importFrom(ggplot2,xlab)
@@ -46,15 +62,21 @@ importFrom(stats,as.ts)
4662
importFrom(stats,coef)
4763
importFrom(stats,cutree)
4864
importFrom(stats,density)
65+
importFrom(stats,deviance)
66+
importFrom(stats,df.residual)
4967
importFrom(stats,dist)
68+
importFrom(stats,fitted)
69+
importFrom(stats,formula)
5070
importFrom(stats,hclust)
5171
importFrom(stats,lm)
72+
importFrom(stats,lm.influence)
5273
importFrom(stats,pbinom)
5374
importFrom(stats,predict)
5475
importFrom(stats,qbinom)
5576
importFrom(stats,quantile)
5677
importFrom(stats,rbinom)
5778
importFrom(stats,resid)
79+
importFrom(stats,residuals)
5880
importFrom(stats,rnorm)
5981
importFrom(stats,runif)
6082
importFrom(stats,sd)

R/method-model.r

+25-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#' 'rotate', 'perm', 'pboot' and 'boot' are defined by \code{\link{resid_rotate}},
1010
#' \code{\link{resid_perm}}, \code{\link{resid_pboot}} and \code{\link{resid_boot}}
1111
#' respectively
12-
#' @param ... other arguments passedd onto \code{method}.
12+
#' @param additional whether to compute additional measures: standardized
13+
#' residuals and leverage
14+
#' @param ... other arguments passed onto \code{method}.
1315
#' @return a function that given \code{data} generates a null data set.
1416
#' For use with \code{\link{lineup}} or \code{\link{rorschach}}
1517
#' @export
16-
#' @importFrom stats lm predict
18+
#' @importFrom stats lm predict deviance df.residual lm.influence
1719
#' @seealso null_permute, null_dist
1820
#' @examples
1921
#' data(tips)
@@ -23,7 +25,7 @@
2325
#' ggplot(lineup(null_lm(tip ~ total_bill, method = 'rotate'), tips.reg)) +
2426
#' geom_point(aes(x = total_bill, y = .resid)) +
2527
#' facet_wrap(~ .sample)
26-
null_lm <- function(f, method = "rotate", ...) {
28+
null_lm <- function(f, method = "rotate", additional = FALSE, ...) {
2729
n <- NULL
2830
if (is.character(method)) {
2931
method <- match.fun(paste("resid", method, sep = "_"))
@@ -33,9 +35,15 @@ null_lm <- function(f, method = "rotate", ...) {
3335
resp_var <- all.vars(f[[2]])
3436

3537
resid <- method(model, df, ...)
36-
fitted <- stats::predict(model, df)
38+
fitted <- predict(model, df)
3739
df[".resid"] <- resid
3840
df[".fitted"] <- fitted
41+
if(additional){
42+
s <- sqrt(deviance(model)/df.residual(model))
43+
hii <- lm.influence(model, do.coef = FALSE)$hat
44+
df[".leverage"] <- dropInf(hii, hii)
45+
df[".stdresid"] <- dropInf(resid/(s * sqrt(1 - hii)), hii)
46+
}
3947
df[[resp_var]] <- fitted + resid
4048
df
4149
}
@@ -103,7 +111,20 @@ resid_boot <- function(model, data) {
103111
#'
104112
#' @param model to extract residuals from
105113
#' @importFrom stats resid
114+
#' @param data used to fit model
106115
#' @export
107116
resid_perm <- function(model, data) {
108117
sample(stats::resid(model))
109118
}
119+
120+
121+
# Helper function for leverages, adapted from plot.lm
122+
dropInf <- function(x, h) {
123+
if (any(isInf <- h >= 1)) {
124+
warning(gettextf("not plotting observations with leverage greater than one:\n %s",
125+
paste(which(isInf), collapse = ", ")), call. = FALSE,
126+
domain = NA)
127+
x[isInf] <- NaN
128+
}
129+
x
130+
}

0 commit comments

Comments
 (0)