-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdescriptives.R
219 lines (204 loc) · 5.77 KB
/
descriptives.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#' @title Describe a dataset
#' @description Provide descriptive statistics for a dataset.
#' @param x An object for which a method exists.
#' @param ... Additional arguments.
#' @return A \code{data.frame} with descriptive statistics for \code{x}.
#' @examples
#' descriptives(iris)
#' @rdname descriptives
#' @export
#' @importFrom stats median sd
descriptives <- function(x, ...) {
UseMethod("descriptives", x)
}
#' @method descriptives matrix
#' @export
descriptives.matrix <- function(x, ...) {
Args <- as.list(match.call()[-1])
Args$x <- data.frame(x)
do.call(descriptives, Args)
}
#' @method descriptives data.frame
#' @export
descriptives.data.frame <- function(x, ...) {
data_types <-
sapply(x, function(i) {
paste0(class(i), collapse = ", ")
})
out <- lapply(x, descriptives)
all_names <-
c(
"n",
"missing",
"unique",
"mean",
"median",
"mode",
"mode_value",
"sd",
"v",
"min",
"max",
"range",
"skew",
"skew_2se",
"kurt",
"kurt_2se"
)
out <-
do.call(rbind, c(lapply(out, function(x)
data.frame(c(
x, sapply(setdiff(all_names, names(x)),
function(y)
NA)
))),
make.row.names = FALSE))
out <- out[, all_names]
out <- cbind(name = names(x),
type = data_types,
out)
rownames(out) <- NULL
out
}
#' @method descriptives numeric
#' @export
descriptives.numeric <- function(x, ...) {
rng <- range(x, na.rm = TRUE)
sk <- skew_kurtosis(x)
cbind(
data.frame(
n = sum(!is.na(x)),
missing = sum(is.na(x))/length(x),
unique = length(unique(x)),
mean = mean(x, na.rm = TRUE),
median = median(x, na.rm = TRUE),
mode = median(x, na.rm = TRUE),
sd = sd(x, na.rm = TRUE),
min = rng[1],
max = rng[2],
range = diff(rng)
),
t(sk)
)
}
#' @method descriptives integer
#' @export
descriptives.integer <- descriptives.numeric
#' @method descriptives default
#' @export
descriptives.default <- function(x, ...) {
if(is.factor(x)) x <- droplevels(x)
if(!is.vector(x)) x <- tryCatch(as.vector(x), error = function(e){NA})
tb <- tryCatch(table(x, useNA = "always"), error = function(e){NA})
data.frame(
n = tryCatch({sum(!is.na(x))}, error = function(e){NA}),
missing = sum(is.na(x))/length(x),
unique = tryCatch(length(tb), error = function(e){NA}),
mode = tryCatch({
unname(tb[which.max(tb)])
}, error = function(e){NA}),
mode_value = tryCatch(names(tb)[which.max(tb)], error = function(e){NA}),
v = tryCatch(var_cat(x), error = function(e){NA})
)
}
#' @method descriptives factor
#' @export
descriptives.factor <- descriptives.default
# Agresti's V for categorical data variability
# Agresti, Alan (1990). Categorical Data Analysis. John Wiley and Sons, Inc. 24-25
var_cat <- function(x) {
x <- x[!is.na(x)]
if (!length(x))
return(NA)
p <- prop.table(table(x))
#-1 * sum(p*log(p)) Shannon entropy
1 - sum(p ^ 2)
}
#' @title Calculate skew and kurtosis
#' @description Calculate skew and kurtosis, standard errors for both, and the
#' estimates divided by two times the standard error. If this latter quantity
#' exceeds an absolute value of 1, the skew/kurtosis is significant. With very
#' large sample sizes, significant skew/kurtosis is common.
#' @param x An object for which a method exists.
#' @param verbose Logical. Whether or not to print messages to the console,
#' Default: FALSE
#' @param se Whether or not to return the standard errors, Default: FALSE
#' @param ... Additional arguments to pass to and from functions.
#' @return A \code{matrix} of skew and kurtosis statistics for \code{x}.
#' @examples
#' skew_kurtosis(datasets::anscombe)
#' @rdname skew_kurtosis
#' @export
skew_kurtosis <- function(x, verbose = FALSE, se = FALSE, ...) {
UseMethod("skew_kurtosis", x)
}
#' @method skew_kurtosis matrix
#' @export
skew_kurtosis.matrix <-
function(x, verbose = FALSE, se = FALSE, ...) {
Args <- as.list(match.call()[-1])
Args$x <- data.frame(x)
do.call(skew_kurtosis, Args)
}
#' @method skew_kurtosis data.frame
#' @export
skew_kurtosis.data.frame <-
function(x, verbose = FALSE, se = FALSE, ...) {
t(sapply(x, skew_kurtosis))
}
#' @method skew_kurtosis matrix
#' @export
skew_kurtosis.matrix <-
function(x, verbose = FALSE, se = FALSE, ...) {
t(apply(x, 2, skew_kurtosis))
}
#' @method skew_kurtosis numeric
#' @export
skew_kurtosis.numeric <-
function(x, verbose = FALSE, se = FALSE, ...) {
x <- x[!is.na(x)]
n <- length(x)
out <- tryCatch({
if (n > 3) {
if (n > 5000 &
verbose)
message("Sample size > 5000; skew and kurtosis will likely be significant.")
skew <- sum((x - mean(x)) ^ 3) / (n * sqrt(var(x)) ^ 3)
skew_se <- sqrt(6 * n * (n - 1) / (n - 2) / (n + 1) / (n + 3))
skew_2se <- skew / (2 * skew_se)
kurt <- sum((x - mean(x)) ^ 4) / (n * var(x) ^ 2) - 3
kurt_se <- sqrt(24 * n * ((n - 1) ^ 2) / (n - 3) / (n - 2) / (n + 3) /
(n + 5))
kurt_2se <- kurt / (2 * kurt_se)
c(skew,
skew_se,
skew_2se,
kurt,
kurt_se,
kurt_2se
)
} else {
stop()
}
}, error = function(e){ rep(NA, 6) })
names(out) <-
c("skew", "skew_se", "skew_2se", "kurt", "kurt_se", "kurt_2se")
if (se) {
return(out)
} else {
return(out[c(1, 3, 4, 6)])
}
}
#' @method skew_kurtosis default
#' @export
skew_kurtosis.default <-
function(x, verbose = FALSE, se = FALSE, ...) {
out <- rep(NA, 6)
names(out) <-
c("skew", "skew_se", "skew_2se", "kurt", "kurt_se", "kurt_2se")
if (se) {
return(out)
} else {
return(out[c(1, 3, 4, 6)])
}
}