-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcss_stats.R
78 lines (70 loc) · 2.03 KB
/
css_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
library(lmtest)
library(Hmisc)
library(foreach)
# code by dean eckles and eytan bakshy
freq.weighted.vcov <- function (object, vcov. = vcov, freq = NA, ...)
{
if (missing(freq)) {
freq <- object$weights
}
if (class(vcov) == "function") {
vcov. <- vcov.(object, ...)
}
vcov. * length(freq)/sum(freq)
}
coeftest.freq <- function (object, vcov. = vcov, freq = NA, df = NA, ...)
{
if (!require(lmtest)) {
stop("This requires the 'lmtest' package.")
}
if (missing(freq)) {
freq <- object$weights
}
if (is.na(df)) {
df <- sum(freq) - (length(freq) - df.residual(object))
}
coeftest(object, freq.weighted.vcov(object, vcov., freq,
...), df)
}
## the bootstrap
r.double.or.nothing <- function(n) {
2 * rbinom(n, 1, .5)
}
clustered.bootstrap <- function(data, clusters, statistic, .R=250,
.verbose = FALSE,
.RNG = r.double.or.nothing,
.progress = "none",
.parallel = FALSE,
...) {
if(length(clusters) == 0) {
return(iid.bootstrap(data, statistic, .R, .RNG, ...))
}
# lists containing information for each group
cluster.ids <- list()
num.clusters <- list()
for(i in clusters) {
groups.factor <- as.factor(data[[i]])
cluster.ids[[i]] <- as.numeric(groups.factor)
num.clusters[[i]] <- length(levels(groups.factor))
}
replicates <- foreach(r=1:.R, .combine=c) %do% {
# generate and multiply weight vectors
w <- foreach(i = clusters, .combine = `*`) %do%
{.RNG(num.clusters[[i]])[cluster.ids[[i]]]}
statistic(mutate(data, .weights=w), ...)
}
replicates
}
iid.bootstrap <- function(data, statistic, .R=250,
.RNG = r.double.or.nothing) {
replicates <- foreach(r=1:.R, .combine=c) %do% {
statistic(mutate(data, .weights=.RNG(nrow(data))), ...)
}
replicates
}
norm.intervals <- function(x, alpha=0.05) {
data.frame(
lower=qnorm(alpha/2, mean(x), sd(x)),
mean=mean(x),
upper=qnorm(1-alpha/2, mean(x), sd(x)))
}