-
Notifications
You must be signed in to change notification settings - Fork 19
/
difference_in_means.Rd
230 lines (195 loc) · 8.13 KB
/
difference_in_means.Rd
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
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/estimatr_difference_in_means.R
\name{difference_in_means}
\alias{difference_in_means}
\title{Design-based difference-in-means estimator}
\usage{
difference_in_means(formula, data, blocks, clusters, weights, subset,
se_type = c("default", "none"), condition1 = NULL, condition2 = NULL,
ci = TRUE, alpha = 0.05)
}
\arguments{
\item{formula}{an object of class formula, as in \code{\link{lm}}, such as
\code{Y ~ Z} with only one variable on the right-hand side, the treatment.}
\item{data}{A \code{data.frame}.}
\item{blocks}{An optional bare (unquoted) name of the block variable. Use
for blocked designs only.}
\item{clusters}{An optional bare (unquoted) name of the variable that
corresponds to the clusters in the data; used for cluster randomized
designs. For blocked designs, clusters must nest within blocks.}
\item{weights}{the bare (unquoted) names of the weights variable in the
supplied data.}
\item{subset}{An optional bare (unquoted) expression specifying a subset of
observations to be used.}
\item{se_type}{An optional string that can be one of \code{c("default", "none")}. If "default" (the default), it will use the default standard error estimator for the design, and if "none" then standard errors will not be computed which may speed up run time if only the point estimate is required.}
\item{condition1}{value in the treatment vector of the condition
to be the control. Effects are
estimated with \code{condition1} as the control and \code{condition2} as the
treatment. If unspecified, \code{condition1} is the "first" condition and
\code{condition2} is the "second" according to levels if the treatment is a
factor or according to a sortif it is a numeric or character variable (i.e
if unspecified and the treatment is 0s and 1s, \code{condition1} will by
default be 0 and \code{condition2} will be 1). See the examples for more.}
\item{condition2}{value in the treatment vector of the condition to be the
treatment. See \code{condition1}.}
\item{ci}{logical. Whether to compute and return p-values and
confidence intervals, TRUE by default.}
\item{alpha}{The significance level, 0.05 by default.}
}
\value{
Returns an object of class \code{"difference_in_means"}.
The post-estimation commands functions \code{summary} and \code{\link{tidy}}
return results in a \code{data.frame}. To get useful data out of the return,
you can use these data frames, you can use the resulting list directly, or
you can use the generic accessor functions \code{coef} and
\code{confint}.
An object of class \code{"difference_in_means"} is a list containing at
least the following components:
\item{coefficients}{the estimated difference in means}
\item{std.error}{the estimated standard error}
\item{df}{the estimated degrees of freedom}
\item{p.value}{the p-value from a two-sided t-test using \code{coefficients}, \code{std.error}, and \code{df}}
\item{ci.lower}{the lower bound of the \code{1 - alpha} percent confidence interval}
\item{ci.upper}{the upper bound of the \code{1 - alpha} percent confidence interval}
\item{term}{a character vector of coefficient names}
\item{alpha}{the significance level specified by the user}
\item{N}{the number of observations used}
\item{outcome}{the name of the outcome variable}
\item{design}{the name of the design learned from the arguments passed}
}
\description{
Difference-in-means estimators that selects the appropriate
point estimate, standard errors, and degrees of freedom for a variety of
designs: unit randomized, cluster randomized, block randomized,
block-cluster randomized, matched-pairs, and matched-pair cluster
randomized designs
}
\details{
This function implements a difference-in-means estimator, with
support for blocked, clustered, matched-pairs, block-clustered, and
matched-pair clustered designs. One specifies their design by passing
the blocks and clusters in their data and this function chooses which
estimator is most appropriate.
If you pass only \code{blocks}, if all blocks are of size two, we will
infer that the design is a matched-pairs design. If they are all size four
or larger, we will infer that it is a regular blocked design. If you pass
both \code{blocks} and \code{clusters}, we will similarly
infer whether it is a matched-pairs clustered design or a block-clustered
design the number of clusters per block. If the user passes only
\code{clusters}, we will infer that the design was cluster-randomized. If
the user specifies neither the \code{blocks} nor the \code{clusters},
a regular Welch's t-test will be performed.
Importantly, if the user specifies weights, the estimation is handed off
to \code{\link{lm_robust}} with the appropriate robust standard errors
as weighted difference-in-means estimators are not implemented here.
More details of the about each of the estimators can be found in the
\href{https://declaredesign.org/R/estimatr/articles/mathematical-notes.html}{mathematical notes}.
}
\examples{
library(fabricatr)
library(randomizr)
# Get appropriate standard errors for unit-randomized designs
# ----------
# 1. Unit randomized
# ----------
dat <- fabricate(
N = 100,
Y = rnorm(100),
Z_comp = complete_ra(N, prob = 0.4),
)
table(dat$Z_comp)
difference_in_means(Y ~ Z_comp, data = dat)
# ----------
# 2. Cluster randomized
# ----------
# Accurates estimates and standard errors for clustered designs
dat$clust <- sample(20, size = nrow(dat), replace = TRUE)
dat$Z_clust <- cluster_ra(dat$clust, prob = 0.6)
table(dat$Z_clust, dat$clust)
summary(difference_in_means(Y ~ Z_clust, clusters = clust, data = dat))
# ----------
# 3. Block randomized
# ----------
dat$block <- rep(1:10, each = 10)
dat$Z_block <- block_ra(dat$block, prob = 0.5)
table(dat$Z_block, dat$block)
difference_in_means(Y ~ Z_block, blocks = block, data = dat)
# ----------
# 4. Block cluster randomized
# ----------
# Learns this design if there are two clusters per block
dat$small_clust <- rep(1:50, each = 2)
dat$big_blocks <- rep(1:5, each = 10)
dat$Z_blcl <- block_and_cluster_ra(
blocks = dat$big_blocks,
clusters = dat$small_clust
)
difference_in_means(
Y ~ Z_blcl,
blocks = big_blocks,
clusters = small_clust,
data = dat
)
# ----------
# 5. Matched-pairs
# ----------
# Matched-pair estimates and standard errors are also accurate
# Specified same as blocked design, function learns that
# it is matched pair from size of blocks!
dat$pairs <- rep(1:50, each = 2)
dat$Z_pairs <- block_ra(dat$pairs, prob = 0.5)
table(dat$pairs, dat$Z_pairs)
difference_in_means(Y ~ Z_pairs, blocks = pairs, data = dat)
# ----------
# 6. Matched-pair cluster randomized
# ----------
# Learns this design if there are two clusters per block
dat$small_clust <- rep(1:50, each = 2)
dat$cluster_pairs <- rep(1:25, each = 4)
table(dat$cluster_pairs, dat$small_clust)
dat$Z_mpcl <- block_and_cluster_ra(
blocks = dat$cluster_pairs,
clusters = dat$small_clust
)
difference_in_means(
Y ~ Z_mpcl,
blocks = cluster_pairs,
clusters = small_clust,
data = dat
)
# ----------
# Other examples
# ----------
# Also works with multi-valued treatments if users specify
# comparison of interest
dat$Z_multi <- simple_ra(
nrow(dat),
conditions = c("Treatment 2", "Treatment 1", "Control"),
prob_each = c(0.4, 0.4, 0.2)
)
# Only need to specify which condition is treated `condition2` and
# which is control `condition1`
difference_in_means(
Y ~ Z_multi,
condition1 = "Treatment 2",
condition2 = "Control",
data = dat
)
difference_in_means(
Y ~ Z_multi,
condition1 = "Treatment 1",
condition2 = "Control",
data = dat
)
# Specifying weights will result in estimation via lm_robust()
dat$w <- runif(nrow(dat))
difference_in_means(Y ~ Z_comp, weights = w, data = dat)
lm_robust(Y ~ Z_comp, weights = w, data = dat)
}
\references{
Gerber, Alan S, and Donald P Green. 2012. Field Experiments: Design, Analysis, and Interpretation. New York: W.W. Norton.
Imai, Kosuke, Gary King, Clayton Nall. 2009. "The Essential Role of Pair Matching in Cluster-Randomized Experiments, with Application to the Mexican Universal Health Insurance Evaluation." Statistical Science 24 (1). Institute of Mathematical Statistics: 29-53. \url{https://doi.org/10.1214/08-STS274}.
}
\seealso{
\code{\link{lm_lin}}
}