-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
261 lines (191 loc) · 6.66 KB
/
README.Rmd
File metadata and controls
261 lines (191 loc) · 6.66 KB
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
---
title: "R Utility for Pairwise Comparisons of Margins Computed with Functions in Package 'effects'"
author: "Simo Goshev"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
toc: true
toc_float: true
collapsed: false
smooth_scroll: false
number_sections: true
theme: default
highlight: textmate
---
Introduction
======
Package 'effects' in R offers excellent functionality for
computing the type of marginal effects known as average partial
effects. An unfortunate shotfall of the package is that it does not
offer capabilities for computing and testing of pairwise differences
in the margins. The goal of this project is to fill this void.
Installation
======
Download and source *effcomp.R* into R.
Functionality
======
File *effcomp.R* contains one primary function and several utility
functions. Users would want to use only the primary function.
Primary function
------
The primary function is `effcomp`.
`effcomp` computes pairwise differences in margins and also reports
significance tests of differences in the margins, if requested. Its
syntax is:
effcomp(effects_obj, lincon, all = FALSE, tests = FALSE, ...)
and its arguments are:
+ `effects_obj`: the object created by running either `effect` or
`Effect` from Package 'effects'
+ `lincon`: stands for "linear contrasts" and is a matrix of
pairwise linear contrasts which is provided by the user. This
argument can be omitted if `all = TRUE`
+ `all`: a logical flag which indicates whether all possible
pairwise contrasts are to be computed. At its default value
`FALSE`, the user has to provide a matrix of pairwise linear
contrasts.
+ `tests`: a logical flag which indicates whether tests of
significance of the differences in margins are to be computed. By
default `tests = FALSE`
+ `...`: if `tests = TRUE`, the user may wish pass specify a method
for adjusting the p-values of the tests for multiple comparisons.
This is done using the argument `adjmethod` which takes all methods
described in `p.adjust` of base R as well as `"sidak"` and
`"scheffe"`. For no adjustment, use `"none"`. The default method is
`"bonferroni"`.
Utility functions
-------
Function `testpwcomp` computes the significance tests, `p_adjust`
computes the p-values adjusted for multiple comparisons.
Examples of usage
=======
We start by removing all objects in memory and loading the effect package.
```{r}
## Clear memory and load package effects
rm(list=ls())
library(effects)
```
Next, we source *effcomp.R*
```{r}
# Source in effcomp.R
source("effcomp.R")
```
Finally, we load an example dataset provided in the _effects_ package and make
some minor changes to the variables
```{r}
## Making minor changes to the data
Prestige$educ <- round(Prestige$education)
Prestige$educ <- ifelse(Prestige$educ <= 12, 12, Prestige$educ)
Prestige$educ <- ifelse(Prestige$educ >= 14, 14, Prestige$educ)
Prestige$educ <- as.factor(Prestige$educ)
Prestige$income <- Prestige$income/1000
```
We are now ready to illustrate the usage of `effcomp`.
Contrasts of margins, one variable
------
We run our model and compute the margins of variable *type*.
```{r}
fit_lm <- lm(prestige ~ income + type + educ, data = Prestige)
type_eff <- effect("type", fit_lm) # compute effects
as.data.frame(type_eff) # print effects
```
To obtain all possible contrasts of the margins of variable *`type`*, we
execute
```{r}
comps <- effcomp(type_eff, all = TRUE) # compute all contrasts
summary(comps)
```
If we also want to test whether the margins are different from each
other, we run
```{r}
comps <- effcomp(type_eff, all = TRUE, tests = TRUE) # Bonferroni adjustment, default
summary(comps)
```
In case we want to use a different adjustment, for example Scheffe's, we
specify the command in the following way:
```{r}
## Scheffe adjustment
comps <- effcomp(type_eff, all = TRUE, tests = TRUE, adjmethod ="scheffe")
summary(comps)
```
All standard methods for adjustment are available. Sidak's method has
also be added.
`effcomp` also accepts a user provided matrix for constructing desired contrasts.
```{r}
contr_mat <- matrix(c(-1, 1, 0,
-1, 0, 1),
nrow = 2, byrow = TRUE)
comps <- effcomp(type_eff,contr_mat) # compute contrasts
summary(comps)
```
To test the differences in the margins (and apply adjustments for
multiple comparisons), we use the following syntax:
+ No adjustment for multiple comparisons
```{r}
## No adjustment for multiple comparisons
comps <- effcomp(type_eff, contr_mat, tests = TRUE, adjmethod = "none")
summary(comps)
```
+ Bonferroni adjustment for multiple comparisons
```{r}
## Bonferroni adjustment
comps <- effcomp(type_eff, contr_mat, tests = TRUE)
summary(comps)
```
+ Scheffe adjustment for multiple comparisons
```{r}
## Scheffe's adjustment
comps <- effcomp(type_eff, contr_mat, tests = TRUE, adjmethod = "scheffe")
summary(comps)
```
Contrasts of margins, two or more variables
-----
`effcomp` can be used to construct contrasts of margins computed
over multiple variables.
Here, we compute margins over *`type`* and *`educ`*:
```{r}
type_educ <- Effect(c("type","educ"), fit_lm) # compute effects
as.data.frame(type_educ) # print effects
```
To obtain the contrasts of these margins, we use
```{r}
comps <- effcomp(type_educ, all = TRUE) # compute all contrasts
summary(comps)
```
If we need to test for significance of the differences, we run:
+ No adjustment for multiple comparisons
```{r}
## Compute statistical tests, no adjustment for mulitple comparisons
comps <- effcomp(type_educ, all = TRUE, tests = TRUE, adjmethod = "none")
summary(comps)
```
+ Bonferroni adjustment for multiple comparisons
```{r}
## Bonferroni adjustment for multimple comparisons
comps <- effcomp(type_educ, all = TRUE, tests = TRUE)
summary(comps)
```
+ Scheffe adjustment for multiple comparisons
```{r}
## Scheffe's adjustment for multimple comparisons
comps <- effcomp(type_educ, all = TRUE, tests = TRUE, adjmethod = "scheffe")
summary(comps)
```
As before, `effcomp` accepts a matrix of user-specified contrasts.
We first create the matrix of contrasts
```{r}
contr_mat <- matrix(c(-1, 1, 0, 0, 0, 0, 0, 0, 0,
0,-1, 0, 0, 0, 0, 0, 1, 0,
0, 0,-1, 0, 0, 1, 0, 0, 0),
nrow = 3, byrow = TRUE)
```
and then feed it to `effcomp`:
```{r}
comps <- effcomp(type_educ, contr_mat) # compute all contrasts
summary(comps)
```
Again, to test the differences (and adjust for multiple comparisons
using Scheffe's method) we run
```{r}
comps <- effcomp(type_educ, contr_mat, tests = TRUE, adjmethod = "scheffe")
summary(comps)
```