-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexamples.R
351 lines (265 loc) · 12.4 KB
/
examples.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
## panelview: Visualizing Panel Data
## User's Guide
## Hongyu Mou(PKU); Licheng Liu (MIT); Yiqing Xu (Stanford)
## Feb 12, 2023
## version 1.1.16
## install.packages('devtools', repos = 'http://cran.us.r-project.org') # if not already installed
## devtools::install_github('xuyiqing/panelview')
## require(ggplot2)
## require(gridExtra)
## require(grid)
## require(dplyr)
library(panelView)
data(panelView)
ls()
#############################################
## Plot treatment status
#############################################
## Example: EDR and voter turnout
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"),
xlab = "Year", ylab = "State")
## by treatment timing: treated on the top
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"),
xlab = "Year", ylab = "State", by.timing = TRUE,
legend.labs = c("No EDR", "EDR"), background = "white",
cex.main = 20, cex.axis= 8, cex.lab = 12, cex.legend = 12)
## distinguish pre-treatment and post-treatment for the treated
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"),
xlab = "Year", ylab = "State", pre.post = TRUE)
## adjust axis labels
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"),
xlab = "Year", ylab = "State", by.timing = TRUE,
pre.post = TRUE, legend.labs = c("Control States",
"Treated States (before EDR)", "Treated States (after EDR)"))
## not showing unit labels
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"),
main = "EDR Reform", axis.lab = "time")
## Give the variable name instead
panelview(D = "policy_edr", data = turnout, index = c("abb","year"),
xlab = "Year", ylab = "State", pre.post = TRUE,
main = "EDR Reform", axis.lab = "time")
## change color
library(dplyr)
turnout %>% panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
color = c("#B0C4DE","white"), by.timing = TRUE,
index = c("abb","year"), xlab = "Year", ylab = "State")
## leave gap if time is not evenly distributed
turnout2<-turnout[!(turnout$year=="1924" | turnout$year=="1928" | turnout$year == "1940"),]
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout2, index = c("abb","year"),
type = "treat", leave.gap = T)
## Example: democracy and state capacity
panelview(Capacity ~ demo + lnpop + lngdp,
data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2,10), main = "Democracy and State Capacity")
## by treatment timing
panelview(Capacity ~ demo + lnpop + lngdp,
data = capacity, index = c("ccode", "year"),
axis.lab.angle = 90, main = "Democracy and State Capacity: Treatment Status",
by.timing = TRUE, axis.lab = "time")
########################################
## Collapse by treatment history
#########################################
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), axis.lab.angle = 90,
by.timing = TRUE, collapse.history = "TRUE")
# sorted by treatment timing
panelview(Capacity ~ demo + lnpop + lngdp,
data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2,0), axis.lab.angle = 90,
collapse.history = "TRUE")
# sorted by cohort size
################
## plot subset
################
panelview(Capacity ~ demo + lnpop + lngdp,
data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2,0), main = "Democracy and State Capacity",
show.id = c(1:25))
## plot with treatment variable name
panelview(D = "demo", data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2,0), main = "Democracy and State Capacity",
show.id = c(1:25), type = "treat")
panelview(Capacity ~ demo + lnpop + lngdp, data = capacity,
index = c("ccode", "year"), axis.lab.gap = c(2,0),
main = "Democracy and State Capacity",
id = unique(capacity$ccode)[26:50], by.timing = TRUE)
#############################################
## Ignore treatment status (missingness)
#############################################
## ignore treatment status
library(dplyr)
capacity %>% panelview(Capacity ~ 1, index = c("ccode","year"),
axis.lab="off", type = "miss")
capacity %>% panelview(Capacity ~ demo, ignore.treat = TRUE,
index = c("ccode","year"), axis.lab= "off")
## plot with outcome variable
capacity %>% panelview(Y = "Capacity", index = c("ccode", "year"),
axis.lab.gap = c(2,10), type = "miss")
## leave gap if time is not evenly distributed
capacity$demo[capacity$year == "1960" | capacity$year == "1980"] <- NA
capacity$lngdp[capacity$year == "1990"] <- NA
panelview(1 ~ demo + lngdp, data = capacity, index = c("ccode","year"),
type = "missing", leave.gap = TRUE, axis.lab.gap = c(2,10))
#############################################
##### More than 2 treatment levels
#############################################
demo2 <- rep(0, dim(capacity)[1])
demo2[which(capacity$polity2 < -0.5)] <- -1
demo2[which(capacity$polity2 > 0.5)] <- 1
capacity$demo2 <- demo2
panelview(Capacity ~ demo2 + lngdp, data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2,10), main = "Regime Type")
library(RColorBrewer)
mycol<-brewer.pal(3,"Set1")[c(1,3,2)]
panelview(Capacity ~ demo2, data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2), main = "Regime Type", axis.lab = c("time"),
color = mycol, legend.labs = c("Autocracy", "Hybrid", "Democracy"))
panelview(D = "demo2", data = capacity, index = c("ccode", "year"),
axis.lab.gap = c(2), main = "Regime Type", axis.lab = c("time"),
color = mycol, legend.labs = c("Autocracy", "Hybrid", "Democracy"))
## continuous treatment levels (>5 values)
panelview(Capacity ~ polity2 + lngdp, data = capacity,
index = c("ccode", "year"), axis.lab.gap = c(2,10),
main = "Regime Type", gridOff = TRUE)
## change colors
panelview(Capacity ~ polity2 + lngdp, data = capacity,
index = c("ccode", "year"), axis.lab.gap = c(2,10), main = "Regime Type",
color = c("yellow", "red","blue","green","gray"),
background = "white")
########################################################################
## Plot the outcome variable (or any variable in a panel dataset)
########################################################################
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome",
main = "EDR Reform and Turnout", ylim = c(0,100),
xlab = "Year", ylab = "Turnout")
## change legend
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome",
main = "EDR Reform and Turnout",
legend.labs = c("Control States","Treated States (before EDR)",
"Treated States (after EDR)"))
## not use black and white theme
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome",
main = "EDR Reform and Turnout", theme.bw = FALSE)
## change colors
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome",
main = "EDR Reform and Turnout", color = c("#FC8D6280", "red", "#99999950"),
legend.labs = c("Control States","Treated States (before EDR)", "Treated States (after EDR)"),
theme.bw = FALSE)
## specify units
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome",
main = "EDR Reform and Turnout (AL, AR, CT)", id = c("AL", "AR", "CT"))
## by group (in a column by default)
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome", main = "",
by.group = TRUE, cex.main = 20, cex.main.sub = 15)
## by group (in a row)
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "outcome",
main = "", by.group.side = TRUE, cex.main = 20, cex.main.sub = 15)
## ignore treatment status
panelview(turnout ~ 1, data = turnout, index = c("abb","year"),
type = "outcome", main = "Turnout", ylim = c(0,100),
xlab = "Year", ylab = "Turnout")
# same as above
panelview(Y = "turnout", data = turnout, index = c("abb","year"),
type = "outcome", main = "Turnout", ylim = c(0,100),
xlab = "Year", ylab = "Turnout")
## by cohort (based on treatment timing)
panelview(turnout ~ policy_edr + policy_motor, data = turnout,
index = c("abb","year"), type = "outcome", main = "EDR Reform and Turnout",
by.cohort = TRUE, pre.post = FALSE)
#####################
## discrete outcomes
#####################
panelview(Y ~ D, data = simdata, index = c("id", "time"),
by.group = FALSE, outcome.type = "discrete",
type = "outcome", xlim = c(8, 15))
## by group
panelview(Y ~ D, data = simdata, index = c("id", "time"),
by.group = TRUE, outcome.type = "discrete",
type = "outcome", xlim = c(8, 15))
## ignore treatment status
panelview(Capacity ~ polity2 + lngdp, data = capacity,
index = c("ccode", "year"), main = "Measuring State Capacity",
type = "outcome", legendOff = TRUE)
########################################################################
## Plotting Y and D against time in the same graph
########################################################################
##########################################
## Plot average time series for all units
##########################################
# 1. Continuous Y, discrete D:
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor,
data = turnout, index = c("abb","year"), type = "bivariate",
style = c("c","b"), main = "EDR Reform and Turnout", ylab = "Turnout")
panelview(lnpop ~ demo, data = capacity, index = c("country", "year"),
ylim = list(c(8, 9.4), c(0.25, 0.6)), type = "bivar")
# 2. Discrete Y, discrete D:
panelview(Y ~ D, data = simdata, index = c("id", "time"),
type = "bivar", theme.bw = FALSE, outcome.type = "discrete")
# 3. Continuous Y, continuous D:
panelview(lnpop ~ polity2, data = capacity, index = c("country", "year"),
ylim = list(c(8.75,9.4), c(-0.2, 0.35)), type = "bivar")
# 4. Discrete Y, continuous D
simdata$x <- seq(0.001,0.9,0.001)
panelview(Y ~ x, data = simdata, index = c("id", "time"),
type = "bivar", outcome.type = "discrete")
## Line the discete treatment
# 1. Y: continuous; D: discrete
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor, data = turnout,
index = c("abb","year"),
type = "bivar",
style = c("line","connected"),
theme.bw = FALSE,
ylab = "Turnout")
# 2. Y: Discrete; D: discrete
panelview(Y ~ D, data = simdata, index = c("id", "time"),
type = "bivar", style = "line", theme.bw = FALSE, lwd = 0.4)
##########################################
## Plot by each unit
##########################################
# 1. Continuous Y, discrete D:
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor, data = turnout,
index = c("abb","year"), type = "bivar", by.unit = TRUE,
show.id = c(1:12), ylab = "Turnout")
panelview(lnpop ~ demo, data = capacity,
index = c("country","year"), type = "bivar",
by.unit = TRUE, ylim = list(c(8,10), c(-2, 2)),
show.id = c(1:16))
# 2. Discrete Y, discrete D:
panelview(Y ~ D, data = simdata, index = c("id", "time"),
type = "bivariate", by.unit = TRUE, theme.bw = FALSE,
outcome.type = "discrete", id = unique(simdata$id)[1:12])
# 3. Continuous Y, continuous D:
panelview(lnpop ~ polity2, data = capacity,
index = c("country","year"), type = "bivar",
by.unit = TRUE, show.id = c(1:12))
# 4. Discrete Y, continuous D
simdata$x <- seq(0.001,0.9,0.001)
panelview(Y ~ x, data = simdata, index = c("id", "time"),
type = "bivar", by.unit = TRUE,
outcome.type = "discrete",
lwd = 0.4, id = unique(simdata$id)[1:12])
## Line the discete treatment
# 1. Y: continuous; D: discrete
panelview(turnout ~ policy_edr + policy_mail_in + policy_motor, data = turnout,
index = c("abb","year"), type = "bivar",
by.unit = TRUE, style = "line", theme.bw = FALSE, lwd = 0.5,
show.id = c(1:12), ylab = "Turnout")
# 2. Y: Discrete; D: discrete
panelview(Y ~ D, data = simdata, index = c("id", "time"),
type = "bivar", by.unit = TRUE,
outcome.type = "discrete", style = "line",
theme.bw = FALSE, lwd = 0.4,
id = unique(simdata$id)[1:20])