-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlpdensity_illustration.R
137 lines (106 loc) · 4.77 KB
/
lpdensity_illustration.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
#--------------------------------------------------------------------------------
# lpdensity: Local Polynomial Density Estimation and Inference
# Matias D. Cattaneo, Michael Jansson, and Xinwei Ma
# Replication Code
#--------------------------------------------------------------------------------
rm(list=ls())
#--------------------------------------------------------------------------------
# Install and load the "lpdensity" and "ggplot2" packages
#--------------------------------------------------------------------------------
# install.packages("ggplot2")
# install.packages("lpdensity")
library("ggplot2")
library("lpdensity")
#----------------------------------------
# Generate data
#----------------------------------------
set.seed(42)
data <- as.data.frame(rnorm(4000, mean = -1))
data <- data[data < 0, 1, drop=FALSE]
data <- -1 * data[1:2000, 1, drop=FALSE]
colnames(data) <- c("v1")
#----------------------------------------
# Figure 1, panel (a)
#----------------------------------------
data$pdf <- dnorm(data$v1, mean = 1, sd = 1) / pnorm(0, mean = 1, sd = 1, lower.tail = FALSE)
ggplot() + geom_histogram(data=data, aes(x=v1, y=..density..), breaks=seq(0, 4, 0.2), fill=2, col="white", alpha=0.6) +
theme_bw() + labs(x = "") +
geom_line(data=data, aes(x=v1, y=pdf))
#----------------------------------------
# Figure 1, panel (b)
#----------------------------------------
model2 <- lpdensity(data$v1, bw = 0.5, grid = seq(0, 4, 0.05))
plot(model2, ylabel = "density") + theme(legend.position = "none")
#----------------------------------------
# lpdensity(): Estimation with bandwidth 0.5 on provided grid points
#----------------------------------------
model1 <- lpdensity(data$v1, bw = 0.5, grid = seq(0, 4, 0.5))
summary(model1)
#----------------------------------------
# lpdensity(): extracting estimation
# results
#----------------------------------------
model1$Estimate
#----------------------------------------
# lpdensity(): conventional inference
#----------------------------------------
summary(lpdensity(data$v1, bw = 0.5, p = 2, q = 2))
#----------------------------------------
# lpdensity(): customizing screen output
#----------------------------------------
set.seed(123) # fix the random seed for critical value simulation
summary(model1, alpha = 0.01, sep = 3, grid = c(0, 0.5, 1, 2), CIuniform = TRUE)
#----------------------------------------
# lpdensity(): inconsistent density
# estimation using partial sample
#----------------------------------------
lpdensity(data$v1[data$v1 < 1.5], bw = 0.5, grid = 1.5)$Estimate[, "f_p"]
lpdensity(data$v1[data$v1 > 1.5], bw = 0.5, grid = 1.5)$Estimate[, "f_p"]
dnorm(1.5, mean = 1, sd = 1) / pnorm(0, mean = 1, sd = 1, lower.tail = FALSE) # true density at 1.5
#----------------------------------------
# lpdensity(): consistent density
# estimation using partial sample and
# option "scale"
#----------------------------------------
lpdensity(data$v1[data$v1 < 1.5], bw = 0.5, grid = 1.5,
scale = sum(data$v1 < 1.5)/2000)$Estimate[, "f_p"]
lpdensity(data$v1[data$v1 > 1.5], bw = 0.5, grid = 1.5,
scale = sum(data$v1 > 1.5)/2000)$Estimate[, "f_p"]
#----------------------------------------
# plot(): customization
#----------------------------------------
plot(model2, CItype="line", ylabel = "density") +
theme(legend.position = "none")
plot(model2, type="points", CItype="ebar", grid = seq(0, 4, 0.5), ylabel = "density") +
theme(legend.position = "none")
plot(model2, hist = TRUE, histData = data$v1, histBreaks = seq(0, 4, 0.2), ylabel = "density") +
theme(legend.position = "none")
set.seed(123) # fix the random seed for critical value simulation
plot(model2, alpha=0.1, CIuniform = TRUE, ylabel = "density") +
theme(legend.position = "none")
#----------------------------------------
# lpbwdensity(): illustration
#----------------------------------------
model1bw <- lpbwdensity(data$v1, grid = seq(0, 4, 0.5))
summary(model1bw)
#----------------------------------------
# lpdensity(): automatic bandwidth
# selection
#----------------------------------------
model5 <- lpdensity(data$v1, grid = seq(0, 4, 0.5), bwselect = "imse-dpi")
summary(model5)
#----------------------------------------
# lpdensity(): undersmoothing
#----------------------------------------
# Estimation and plot using IMSE bandwidth
model6bwIMSE <- lpbwdensity(data$v1, grid = seq(0, 4, 0.05),
bwselect = "imse-dpi")
model6 <- lpdensity(data$v1, grid = seq(0, 4, 0.05),
bw = model6bwIMSE$BW[, "bw"])
plot(model6, ylabel = "density") +
theme(legend.position = "none")
# Estimation and plot using half IMSE bandwidth
model7 <- lpdensity(data$v1, grid = seq(0, 4, 0.05),
bw = model6bwIMSE$BW[, "bw"] / 2)
plot(model7, ylabel = "density") +
theme(legend.position = "none")