-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
weighted_stats.R
424 lines (397 loc) · 14.2 KB
/
weighted_stats.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#' Compute various weighted statistics
#'
#' \itemize{
#' \item{{\code{w_mean}}{ weighted mean of a numeric vector}}
#' \item{{\code{w_sd}}{ weighted sample standard deviation of a numeric vector}}
#' \item{{\code{w_var}}{ weighted sample variance of a numeric vector}}
#' \item{{\code{w_se}}{ weighted standard error of a numeric vector}}
#' \item{{\code{w_median}}{ weighted median of a numeric vector}}
#' \item{{\code{w_mad}}{ weighted mean absolute deviation from median of a numeric vector}}
#' \item{{\code{w_sum}}{ weighted sum of a numeric vector}}
#' \item{{\code{w_n}}{ weighted number of values of a numeric vector}}
#' \item{{\code{w_cov}}{ weighted covariance matrix of a numeric matrix/data.frame}}
#' \item{{\code{w_cor}}{ weighted Pearson correlation matrix of a numeric matrix/data.frame}}
#' \item{{\code{w_pearson}}{ shortcut for \code{w_cor}. Weighted Pearson
#' correlation matrix of a numeric matrix/data.frame}}
#' \item{{\code{w_spearman}}{ weighted Spearman correlation matrix of a numeric matrix/data.frame}}
#' }
#'
#' @details
#' If argument of correlation functions is data.frame with variable labels then
#' variables names will be replaced with labels. If this is undesirable behavior
#' use \link{drop_var_labs} function: \code{w_cor(drop_var_labs(x))}. Weighted
#' Spearman correlation coefficients are calculated with weights rounded to nearest
#' integer. It gives the same result as in SPSS Statistics software. By
#' now this algorithm is not memory efficient.
#'
#' @param x a numeric vector (matrix/data.frame for correlations) containing the
#' values whose weighted statistics is to be computed.
#' @param weight a vector of weights to use for each element of x. Cases with
#' missing, zero or negative weights will be removed before calculations. If
#' \code{weight} is missing then unweighted statistics will be computed.
#' @param na.rm a logical value indicating whether NA values should be stripped
#' before the computation proceeds. Note that contrary to base R statistic
#' functions the default value is TRUE (remove missing values).
#' @param use \code{"pairwise.complete.obs"} (default) or \code{"complete.obs"}.
#' In the first case the correlation or covariance between each pair of
#' variables is computed using all complete pairs of observations on those
#' variables. If \code{use} is \code{"complete.obs"} then missing values are
#' handled by casewise deletion.
#' @return a numeric value of length one/correlation matrix
#' @export
#'
#' @examples
#' data(mtcars)
#' dfs = mtcars %>% columns(mpg, disp, hp, wt)
#'
#' with(dfs, w_mean(hp, weight = 1/wt))
#'
#' # apply labels
#' mtcars = mtcars %>%
#' apply_labels(
#' mpg = "Miles/(US) gallon",
#' cyl = "Number of cylinders",
#' disp = "Displacement (cu.in.)",
#' hp = "Gross horsepower",
#' drat = "Rear axle ratio",
#' wt = "Weight (lb/1000)",
#' qsec = "1/4 mile time",
#' vs = "Engine",
#' vs = c("V-engine" = 0,
#' "Straight engine" = 1),
#' am = "Transmission",
#' am = c(automatic = 0,
#' manual=1),
#' gear = "Number of forward gears",
#' carb = "Number of carburetors"
#' )
#'
#' # weighted correlations with labels
#' w_cor(dfs, weight = 1/dfs$wt)
#'
#' # without labels
#' w_cor(drop_var_labs(dfs), weight = 1/dfs$wt)
w_mean = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = matrixStats::weightedMean)
}
#' @export
#' @rdname w_mean
w_median = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = function(x, w, na.rm){
matrixStats::weightedMedian(x = x, w = w, na.rm = na.rm, interpolate = FALSE, ties = "weighted")
})
}
#' @export
#' @rdname w_mean
w_var = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, check_weight_sum = TRUE, fun = matrixStats::weightedVar)
}
#' @export
#' @rdname w_mean
w_sd = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, check_weight_sum = TRUE, fun = matrixStats::weightedSd)
}
#' @export
#' @rdname w_mean
w_se = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, check_weight_sum = TRUE, fun = function(x, w, na.rm){
if(is.null(w)){
if(na.rm) x = x[!is.na(x)]
matrixStats::weightedSd(x, w = w, na.rm = na.rm)/sqrt(length(x))
} else {
if(na.rm) {
w = w[!is.na(x)]
x = x[!is.na(x)]
}
matrixStats::weightedSd(x, w = w, na.rm = na.rm)/sqrt(sum(w, na.rm = TRUE))
}
})
}
#' @export
#' @rdname w_mean
w_mad = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = function(x, w, na.rm){
matrixStats::weightedMad(x = x, w = w, na.rm = na.rm,
center = w_median(x = x, weight = w, na.rm = na.rm)
)
})
}
#' @export
#' @rdname w_mean
w_sum = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = function(x, w, na.rm){
if(is.null(w)) sum(x, na.rm = na.rm) else sum(x*w, na.rm = na.rm)
})
}
#' @export
#' @rdname w_mean
w_n = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = function(x, w, na.rm){
if(is.null(w)){
if(na.rm){
sum(!is.na(x))
} else {
length(x)
}
} else {
if(na.rm){
sum(w[!is.na(x)])
} else {
sum(w)
}
}
})
}
#' @export
#' @rdname w_mean
unweighted_valid_n = function(x, weight = NULL) {
res = valid(x)
if(is.null(weight)){
sum(res, na.rm = TRUE)
} else {
weight = set_negative_and_na_to_zero(weight)
res = recycle_if_single_row(res, NROW(weight))
weight = recycle_if_single_row(weight, NROW(res))
sum(res[weight>0], na.rm = TRUE)
}
}
#' @export
#' @rdname w_mean
valid_n = function(x, weight = NULL) {
res = valid(x)
if(is.null(weight)){
sum(res, na.rm = TRUE)
} else {
weight = set_negative_and_na_to_zero(weight)
res = recycle_if_single_row(res, NROW(weight))
weight = recycle_if_single_row(weight, NROW(res))
sum(weight[res], na.rm = TRUE)
}
}
#' @export
#' @rdname w_mean
w_max = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = function(x, w, na.rm){
res = max(x, na.rm = TRUE)
res[!is.finite(res)] = NA_real_
res
})
}
#' @export
#' @rdname w_mean
w_min = function(x, weight = NULL, na.rm = TRUE){
internal_w_stat(x = x, weight = weight, na.rm = na.rm, fun = function(x, w, na.rm){
res = min(x, na.rm = TRUE)
res[!is.finite(res)] = NA_real_
res
})
}
#' @export
#' @rdname w_mean
w_cov = function(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs")){
if (is.data.frame(x)) {
x = as.matrix(names2labels(x))
} else {
is.matrix(x) || stop("'x' must be a matrix or a data frame")
}
if(NROW(x)==0){
return(matrix_of_na(x))
}
use = match.arg(use)
if(!is.null(weight)){
if(length(weight) == 1L){
weight = rep(weight, NROW(x))
}
(length(weight) == NROW(x)) || stop(
"length of 'weight' must equal to the length of 'x' but NROW(x) == ", NROW(x),
" and length(weight) == ", length(weight))
weight = set_negative_and_na_to_zero(weight)
x = x[weight>0, , drop = FALSE]
weight = weight[weight>0]
if(anyNA(x)){
if(use == "complete.obs"){
completes = stats::complete.cases(x)
weight = weight[completes]
x = x[completes, ,drop = FALSE]
internal_w_cov(x = x, weight = weight)
} else {
seq_ncol_x = seq_len(ncol(x))
res = matrix(NA, ncol = ncol(x), nrow = ncol(x))
for(i in seq_ncol_x){
for(j in seq_ncol_x[seq_ncol_x>=i]){
two_col = x[, c(i, j)]
complete_pair = stats::complete.cases(two_col)
curr_cov = internal_w_cov(two_col[complete_pair,], weight = weight[complete_pair])[1,2]
res[i,j] = curr_cov
if(i!=j){
res[j,i] = curr_cov
}
}
}
colnames(res) = colnames(x)
rownames(res) = colnames(x)
res
}
} else {
internal_w_cov(x = x, weight = weight)
}
} else {
suppressWarnings(stats::cov(x = x, use = use, method = "pearson"))
}
}
#' @export
#' @rdname w_mean
w_cor = function(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs")){
use = match.arg(use)
if (is.data.frame(x)) {
x = as.matrix(names2labels(x))
} else {
is.matrix(x) || stop("'x' must be a matrix or a data frame")
}
if(NROW(x)==0){
return(matrix_of_na(x))
}
if(!is.null(weight)){
if(length(weight) == 1L){
weight = rep(weight, NROW(x))
}
(length(weight) == NROW(x)) || stop(
"length of 'weight' must equal to the length of 'x' but NROW(x) == ", NROW(x),
" and length(weight) == ", length(weight))
weight = set_negative_and_na_to_zero(weight)
x = x[weight>0, , drop = FALSE]
weight = weight[weight>0]
cov_mat = w_cov(x = x, weight = weight, use = use)
if((use == "complete.obs") || !anyNA(x)){
if(!all(is.na(cov_mat))) {
res = suppressWarnings(stats::cov2cor(cov_mat))
} else {
res = cov_mat
}
} else {
seq_ncol_x = seq_len(ncol(x))
sds = internal_pairwise_sd(x = x, weight = weight)
for(i in seq_ncol_x){
for(j in seq_ncol_x){
cov_mat[i,j] = cov_mat[i,j]/sds[i,j]/sds[j,i]
}
}
res = cov_mat
}
} else {
res = suppressWarnings(stats::cor(x = x, use = use, method = "pearson"))
}
diag(res) = 1
res
}
#' @export
#' @rdname w_mean
w_pearson = w_cor
#' @export
#' @rdname w_mean
w_spearman = function(x, weight = NULL, use = c("pairwise.complete.obs", "complete.obs")){
use = match.arg(use)
if (is.data.frame(x)) {
x = as.matrix(names2labels(x))
} else {
is.matrix(x) || stop("'x' must be a matrix or a data frame")
}
if(NROW(x)==0){
return(matrix_of_na(x))
}
if(!is.null(weight)){
if(length(weight) == 1L){
weight = rep(weight, NROW(x))
}
(length(weight) == NROW(x)) || stop(
"length of 'weight' must equal to the length of 'x' but NROW(x) == ", NROW(x),
" and length(weight) == ", length(weight))
weight = set_negative_and_na_to_zero(weight)
x = x[weight>0, , drop = FALSE]
weight = weight[weight>0]
weight = trunc(weight+0.5)
if(sum(weight)<1){
# warning("Sum of weights is less than one. NA will be returned.")
res = matrix_of_na(x)
} else {
x = x[rep(seq_len(nrow(x)), times = weight), ]
res = suppressWarnings(stats::cor(x = x, use = use, method = "spearman"))
}
} else {
res = suppressWarnings(stats::cor(x = x, use = use, method = "spearman"))
}
diag(res) = 1
res
}
internal_pairwise_sd = function(x, weight){
seq_ncol_x = seq_len(ncol(x))
res = matrix(NA, ncol = ncol(x), nrow = ncol(x))
for(i in seq_ncol_x){
seq_ncol_j = seq_ncol_x[seq_ncol_x>=i]
for(j in seq_ncol_j){
complete_pair = which(stats::complete.cases(x[, c(i, j)]))
sds = suppressWarnings(
matrixStats::colWeightedSds(x, w = weight, rows = complete_pair, cols = c(i,j)))
res[i,j] = sds[1]
if(i!=j){
res[j,i] = sds[2]
}
}
}
colnames(res) = colnames(x)
rownames(res) = colnames(x)
res
}
internal_w_cov = function (x, weight){
weights_sum = sum(weight)
if(weights_sum < 1){
# warning("Sum of weights is less than one. NA will be returned.")
return(matrix_of_na(x))
}
weight = weight/weights_sum
center = colSums(weight * x)
x = sqrt(weight) * sweep(x, 2, center, check.margin = FALSE)
res = crossprod(x)*weights_sum/(weights_sum - 1)
res
}
matrix_of_na = function(x){
res = matrix(NA_real_, nrow = ncol(x), ncol = ncol(x))
colnames(res) = colnames(x)
rownames(res) = colnames(x)
res
}
internal_w_stat = function(x, weight, na.rm, check_weight_sum = FALSE, fun){
(NCOL(x) == 1) || stop("'x' should be vector or single column object.")
if(is.data.frame(x)) x = x[[1]]
if(is.logical(x)) x = as.numeric(x)
if(!is.null(weight)){
if(length(weight) == 1L){
weight = rep(weight, NROW(x))
}
(length(weight) == NROW(x)) || stop(
"length of 'weight' must equal to the length of 'x' but NROW(x) == ", NROW(x),
" and length(weight) == ", length(weight))
weight = set_negative_and_na_to_zero(weight)
x = x[weight>0]
weight = weight[weight>0]
if(check_weight_sum && (sum(weight[!is.na(x)])<1)) {
# warning("Sum of weights is less than one. NA will be returned.")
return(NA_real_) # for data.table - it cannot combine logical and numeric in single column
}
}
fun(x = x, w = weight, na.rm = na.rm)
}
## fun is one of the functions from matrixStats with fun(x, w, na.rm)
weight_helper = function(fun){
return(
function(x, weight = NULL, na.rm = TRUE){
res = suppressWarnings(fun(x, w = weight, na.rm = na.rm))
if(is.nan(res)){
# for data.table - it cannot combine logical and numeric in single column
NA_real_
} else {
res
}
}
)
}