-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetrics-Project.R
190 lines (146 loc) · 7.92 KB
/
Metrics-Project.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
library(tmap)
library(maptools)
library(raster)
library(SpatialRDD)
library(ggplot2)
library(sf)
library(stars)
library(rgdal)
library(rdrobust)
library(xtable)
library(stargazer)
library(lwgeom)
library(margins)
#loading tif files into stata
hungary <-raster("~/Desktop/DFP/tif files/Hungary_lights_no0.tif")
hungary
plot(hungary)
col = heat.colors(63)
cellStats(hungary, range)
image(hungary, col=col)
# creating plot of hungary lights
hungary2 <- as.data.frame(hungary)
hist(hungary2$Hungary_lights_no0)
# plot of the brightest regions
image(hungary, zlim=c(50,63), col=col)
# loading as a stars object to convert to sf object
hungary_stars <- read_stars("~/Desktop/DFP/tif files/Hungary_lights_no0.tif")
plot(hungary_stars)
# this converts all pixels to point geometries
hungary.sf <- st_as_sf(hungary_stars, as_points = TRUE, merge = FALSE, long = TRUE, crs = 4326, coords = c("x", "y"))
plot(hungary.sf)
#importing discontinuity/danube. For some reason "~" doesn't work here
danube <- readOGR(dsn="/Users/andrewfox/Desktop/DFP/danube", layer="discont")
plot(danube)
danube.sf <- st_read("/Users/andrewfox/Desktop/DFP/danube", layer = "discont")
ggplot()+geom_sf(data=danube.sf)
# loading elevation data and merging with luminosity
hungary_elev_stars <- read_stars("~/Desktop/DFP/tif files/Hungary_elevation.tif")
plot(hungary_elev_stars)
hungary.elev.sf <- st_as_sf(hungary_elev_stars, as_points = TRUE, merge = FALSE, long = TRUE, crs = 4326, coords = c("x", "y"))
hungary.sf <- st_join(hungary.sf, hungary.elev.sf, join = st_nearest_feature)
# loading and merging population density data
hungary_pop_stars <- read_stars("~/Desktop/DFP/hungary.pop.tif")
hungary.pop.sf <- st_as_sf(hungary_pop_stars, as_points = TRUE, merge = FALSE, long = TRUE, crs = 4326, coords = c("x", "y"))
hungary.sf <- st_join(hungary.sf, hungary.pop.sf, join = st_nearest_feature)
# loading and merging soil data
hungary_soil_stars <- read_stars("~/Desktop/DFP/soil.tif")
hungary.soil.sf <- st_as_sf(hungary_soil_stars, as_points = TRUE, merge = FALSE, long = TRUE, crs = 4326, coords = c("x", "y"))
hungary.sf <- st_join(hungary.sf, hungary.soil.sf, join = st_nearest_feature)
# coding as factor variable
hungary.sf$soil.tf.f <- factor(hungary.sf$soil.tif)
is.factor(hungary.sf$soil.tf.f)
# a plot with the boundary
plot(hungary)
plot(danube, add = TRUE)
#importing the treatment polygon cut in QGIS
treat <- readOGR(dsn="/Users/andrewfox/Desktop/DFP/treatment", layer="treat")
plot(hungary)
plot(treat, add = TRUE)
treat.sf <- st_read("/Users/andrewfox/Desktop/DFP/treatment", layer = "treat")
ggplot()+geom_sf(data=treat.sf)
# setting the coordinate system, so they are consistent
st_crs(treat.sf) = 4326
st_crs(hungary.sf) = 4326
st_crs(danube.sf) = 4326
# creating the treatment area
hungary.sf$treated <- assign_treated(hungary.sf, treat.sf, id = "geometry")
# a warning appears, but for this small area, it shouldn't be a problem
summary(hungary.sf$treated)
# creating ln of luminosity
hungary.sf$log_lights <- log(hungary.sf$Hungary_lights_no0.tif)
# a first simple pooled binary variable regression
lm1 <- lm(log_lights ~ treated, data = hungary.sf)
lm2 <- lm(log_lights ~ treated + Hungary_elevation.tif + hungary.pop.tif + soil.tf.f , data = hungary.sf)
# calculating distance between each point and the danube
hungary.sf$dist2cutoff <- as.numeric(sf::st_distance(hungary.sf, danube.sf))
summary(hungary.sf$dist2cutoff)
# simple pooled regression within 3,5,10 km distance
lm4 <- lm(log_lights ~ treated + Hungary_elevation.tif + hungary.pop.tif, data = hungary.sf[hungary.sf$dist2cutoff < 5000, ])
lm5 <- lm(log_lights ~ treated + Hungary_elevation.tif + hungary.pop.tif + soil.tf.f, data = hungary.sf[hungary.sf$dist2cutoff < 10000, ])
lm6 <- lm(log_lights ~ treated + Hungary_elevation.tif + hungary.pop.tif + soil.tf.f, data = hungary.sf[hungary.sf$dist2cutoff < 3000, ])
lm7 <- lm(log_lights ~ treated + Hungary_elevation.tif + hungary.pop.tif + soil.tf.f, data = hungary.sf[hungary.sf$dist2cutoff < 5000, ])
lm8 <- lm(log_lights ~ treated + Hungary_elevation.tif + hungary.pop.tif + soil.tf.f, data = hungary.sf[hungary.sf$dist2cutoff < 10000, ])
stargazer(lm1, lm2, lm6, lm7, lm8, title="Pooled Linear Regressions", align=TRUE)
# note that around the border, the effect is positive
lm3 <- lm(Hungary_lights_no0.tif ~ treated, data = hungary.sf[hungary.sf$dist2cutoff < 15000, ])
coef(summary(lm3))[, "Std. Error"]
# re-centering data at zero
hungary.sf$distrunning <- hungary.sf$dist2cutoff
hungary.sf$distrunning[hungary.sf$treated == 0] <- -1 * hungary.sf$distrunning[hungary.sf$treated == 0]
# plot of lights around Danube
ggplot(data = hungary.sf, aes(x = distrunning, y = Hungary_lights_no0.tif)) + geom_point() + geom_vline(xintercept = 0, col = "red")
# "naive" rdd with local linear estimation
covs1 <- cbind(hungary.sf$Hungary_elevation.tif, hungary.sf$hungary.pop.tif)
covs2 <- cbind(hungary.sf$Hungary_elevation.tif, hungary.sf$hungary.pop.tif, hungary.sf$soil.tf.f)
rd1 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, c = 0)
rd1.1 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, c = 0, p = 2)
rd2 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, covs = hungary.sf$Hungary_elevation.tif, c = 0)
rd3 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs1, c = 0)
rd4 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs2, c = 0)
rd5 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs1, c = 0, p=2)
rd6 <- rdrobust(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs2, c = 0, p =2)
rdplot(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs2, c = 0, p =2,
y.label = "Luminosity", x.label = "distance to border")
rdplot(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs2, c = 0, p =1,
y.label = "Luminosity", x.label = "distance to border")
rdplot(hungary.sf$log_lights, hungary.sf$distrunning, covs = covs2, c = 0, p =6, h = 60,
y.label = "Luminosity", x.label = "distance to border")
rdplot(hungary.sf$Hungary_lights_no0.tif, hungary.sf$distrunning, c = 0, ci = 95,
kernel = "triangular", y.label = "Luminosity", x.label = "distance to border")
# for a nice 4-part plot
plot(hungary.sf)
# discretize border (approx every 6 kilometers)
borderpoints.sf <- discretise_border(cutoff = danube.sf, n = 50)
borderpoints.sf$id <- 1:nrow(borderpoints.sf)
# approx every 1 kilometer
borderpoints.sf2 <- discretise_border(cutoff = danube.sf, n = 10)
borderpoints.sf2$id <- 1:nrow(borderpoints.sf2)
# looping over boundary points with spatialrd(), with 50 sections. non-parametric estimate
bdd1 <- spatialrd(y = "log_lights", data = hungary.sf, cutoff.points = borderpoints.sf,
treated = "treated", minobs = 10, spatial.object = F)
knitr::kable(bdd1)
bdd2 <- spatialrd(y = "log_lights", data = hungary.sf, cutoff.points = borderpoints.sf,
treated = "treated", minobs = 10, covs = covs1, spatial.object = F)
bdd3 <- spatialrd(y = "log_lights", data = hungary.sf, cutoff.points = borderpoints.sf,
treated = "treated", minobs = 10, covs = covs2, spatial.object = F)
plotspatialrd(bdd2, map = T)
#calculating average treatment effect
mean(bdd1$Estimate)
mean(bdd1$pvalR)
# poisson regressions
m1 <- glm(Hungary_lights_no0.tif ~ treated + Hungary_elevation.tif + hungary.pop.tif + soil.tf.f, family="poisson", data=hungary.sf)
m2 <- glm(Hungary_lights_no0.tif ~ treated + Hungary_elevation.tif
+ hungary.pop.tif + soil.tf.f, family="poisson", data=hungary.sf[hungary.sf$dist2cutoff < 3000, ])
m3 <- glm(Hungary_lights_no0.tif ~ treated + Hungary_elevation.tif
+ hungary.pop.tif + soil.tf.f, family="poisson", data=hungary.sf[hungary.sf$dist2cutoff < 5000, ])
m4 <- glm(Hungary_lights_no0.tif ~ treated + Hungary_elevation.tif
+ hungary.pop.tif + soil.tf.f, family="poisson", data=hungary.sf[hungary.sf$dist2cutoff < 10000, ])
# seeing marginal effects
margins(m1)
margins(m2)
margins(m3)
margins(m4)
# compiling to LaTeX table!
stargazer(m1,m2,m3,m4)
# woohoo!