This repository was archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetControl.R
More file actions
360 lines (290 loc) · 12.3 KB
/
getControl.R
File metadata and controls
360 lines (290 loc) · 12.3 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
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
352
353
354
355
356
357
358
359
###########################
# Independent and control variables
# CSSR Project
# Torben&Alex
# Date: 07.11.2016
###########################
# 0. Notes
# 1. Preparation
# 2. Population / State area
# 3. Population density
# 4. Unemployment rate
# 5. GDP of states
# 6. Beer tax
# 7. Youth unemployment
# 8. Education
# 9. Merge data
##########################
# 0. Notes
##########################
# ToDO
# 1. Clean script
# 2. Check beer
# https://de.statista.com/statistik/daten/studie/317586/umfrage/umsatz-im-gh-mit-nahrungs-und-genussmitteln-in-deutschland-nach-bundeslaendern/
# https://de.statista.com/statistik/daten/studie/28932/umfrage/bierabsatz-nach-bundeslaendern/
# 3. What to do with education?
###########################
# 1. Preparations
###########################
# Clear Global environment
#rm(list=ls())
# Setting Working directory
try(setwd("/home/torben/GIT/Pair_Assignment_2"), silent = TRUE)
try(setwd("D:/Eigene Datein/Dokumente/Uni/Hertie/Materials/Collaborative Social Science Data Analysis/CSSR_Project"), silent = TRUE)
# Collect packages/libraries we need:
packages <- c("readxl", "RCurl", "ckanr", "plyr", "reshape2", "ggplot2")
# install packages if not installed before
for (p in packages) {
if (p %in% installed.packages()[,1]) {
require(p, character.only=T)
}
else {
install.packages(p, repos="http://cran.rstudio.com", dependencies = TRUE)
require(p, character.only=T)
}
}
rm(p, packages)
# Generate list of states
statelist_name <- c("Baden-Württemberg", "Bayern", "Berlin", "Brandenburg",
"Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern",
"Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz",
"Saarland", "Sachsen", "Sachsen-Anhalt",
"Schleswig-Holstein", "Thüringen")
statelist_name_noU <- c("baden-wuerttemberg", "bayern", "berlin", "brandenburg",
"bremen", "hamburg", "hessen", "mecklenburg-vorpommern",
"niedersachsen", "nordrhein-westfalen", "rheinland-pfalz",
"saarland", "sachsen", "sachsen-anhalt",
"schleswig-holstein", "thueringen")
statelist_code <- c("DE-BW", "DE-BY", "DE-BE", "DE-BB", "DE-HB", "DE-HH",
"DE-HE", "DE-MV", "DE-NI", "DE-NW", "DE-RP", "DE-SL",
"DE-SN", "DE-ST", "DE-SH", "DE-TH")
###########################
# 2. Population / State Area
###########################
# Download state area (if not in directory)
if(class(try(read.csv("control/SA.csv")))=="try-error") {
url.SA <- "https://www-genesis.destatis.de/genesis/online?sequenz=tabelleDownload&selectionname=11111-0001®ionalschluessel=&format=csv"
write.csv(read.csv(url.SA, header = FALSE, sep=";", row.names=NULL), "control/SA.csv")
}
SA <- as.data.frame(read.csv("control/SA.csv", header = T,
fileEncoding ="ISO-8859-1",
stringsAsFactors = FALSE))
SA <- SA[-c(1:6, 23:nrow(SA)),-c(1)]
colnames(SA) <- c("STATE", "SA")
# Recode STATE and make it factor
SA$STATE <- mapvalues(as.matrix(SA$STATE), statelist_name, statelist_code)
SA$STATE <- factor(SA$STATE, levels = statelist_code)
# Make SA numeric
SA$SA <- as.numeric(sub(",", ".", as.character(SA$SA),
fixed = TRUE))
# Download population(if not in directory)
if(class(try(read.csv("control/PP.csv")))=="try-error") {
url.PP <- "https://www-genesis.destatis.de/genesis/online?sequenz=tabelleDownload&selectionname=12211-0005®ionalschluessel=&format=csv"
write.csv(read.csv(url.PP, header = FALSE, sep=";", row.names=NULL),
"control/PP.csv")
}
PP <- as.data.frame(read.csv("control/PP.csv", header = T,
fileEncoding ="ISO-8859-1",
stringsAsFactors = FALSE))
# remove source info and id column
PP <- PP[-c(1:7),-1]
# add variable names
colnames(PP) <- c("STATE", "YEAR", "PP", "PP.EM", "PP.UE", "PP.EA", "PP.EIA")
# streamline year column
PP[,2] <- as.numeric(substring(as.character(PP[,2]),0,4))
# Recode STATE and make it factor
PP$STATE <- mapvalues(as.matrix(PP$STATE), statelist_name, statelist_code)
PP$STATE <- factor(PP$STATE, levels = statelist_code)
PP$YEAR <- factor(PP$YEAR, levels = 1991:2015)
for (i in 3:ncol(PP)){
PP[,i] <- as.numeric(PP[,i])
}
#############################
# 3. Population Density
############################
# Download population density if not in directory
if(class(try(read.csv("control/PD.csv")))=="try-error") {
url.PD <- "https://www-genesis.destatis.de/genesis/online?sequenz=tabelleDownload&selectionname=12411-0050®ionalschluessel=&format=csv"
write.csv(read.csv(url.PD, header = FALSE, sep=";", row.names=NULL),
"control/PD.csv")
}
PD <- as.data.frame(read.csv("control/PD.csv", header = T,
fileEncoding ="ISO-8859-1",
stringsAsFactors = FALSE))
#Cleaning and Renaming PD
#ISO 3166-2:DE
# Delete leading and tailing rows / columns; rename them
PD <- PD[-c(1:5, 27:31),-c(1)]
colnames(PD) <- c("YEAR", PD[1,2:17])
PD <- PD[-1,]
# Rename YEAR rows and make it numeric
PD[,1] <- c(1995:2014)
PD$YEAR <- as.numeric(as.character(PD$YEAR))
rownames(PD) <- 1:nrow(PD)
# Transpose PD and rename columns
PD <- melt(PD, id = 1, measured = 2:17)
colnames(PD) <- c("YEAR", "STATE", "PD")
# Recode STATE and make it factor
PD$STATE <- mapvalues(as.matrix(PD$STATE), statelist_name, statelist_code)
PD$STATE <- factor(PD$STATE, levels = statelist_code)
# Make PD numeric
PD$PD <- as.numeric(sub(".", "", as.character(PD$PD),
fixed = TRUE))
###########################
# 4. Unemployment rate
###########################
# Download unemployment rate for states (if not in directory)
if(class(try(read.csv("control/UR.csv")))=="try-error") {
url.UR <- "https://www-genesis.destatis.de/genesis/online?sequenz=tabelleDownload&selectionname=13211-0007®ionalschluessel=&format=csv"
write.csv(read.csv(url.UR, header = FALSE, sep=";", row.names=NULL),
"control/UR.csv")
}
UR <- as.data.frame(read.csv("control/UR.csv", header = T,
fileEncoding ="ISO-8859-1",
stringsAsFactors = FALSE))
# Delete leading and tailing rows / columns; re-arrange and rename
UR <- UR[-c(1:5, 6, 407:421),-c(1)]
UR <- UR[,c(2, 1, 3:6)]
colnames(UR) <- c("YEAR", "STATE", "UTOTAL", "UR.LF", "UR", "VAC")
rownames(UR) <- 1:nrow(UR)
# Make YEAR numeric
UR$YEAR <- as.numeric(sub(".", "", as.character(UR$YEAR),
fixed = TRUE))
# Recode STATE and make it factor
UR$STATE <- mapvalues(as.matrix(UR$STATE), statelist_name, statelist_code)
UR$STATE <- factor(UR$STATE, levels = statelist_code)
# Make UR columns numeric
UR$UTOTAL <- as.numeric(sub(",", ".", as.character(UR$UTOTAL, fixed = TRUE)))
UR[UR == "-"] <- NA
UR$UR.LF <- as.numeric(sub(",", ".", as.character(UR$UR.LF, fixed = TRUE)))
UR$UR <- as.numeric(sub(",", ".", as.character(UR$UR, fixed = TRUE)))
UR$VAC <- as.numeric(sub(",", ".", as.character(UR$VAC, fixed = TRUE)))
############################
# 5. GDP of states
############################
# Download GDP for states (if not in directory)
if(class(try(read.csv("control/GDP.csv")))=="try-error") {
url.GDP <- "https://www-genesis.destatis.de/genesis/online?sequenz=tabelleDownload&selectionname=82111-0001®ionalschluessel=&format=csv"
write.csv(read.csv(url.GDP, header = FALSE, sep=";", row.names=NULL),
"control/GDP.csv")
}
GDP <- as.data.frame(read.csv("control/GDP.csv", header = T,
fileEncoding ="ISO-8859-1",
stringsAsFactors = FALSE))
# Delete leading and tailing rows / columns; rename them
GDP <- GDP[-c(1:6, 33:nrow(GDP)),-c(1, ncol(GDP))]
colnames(GDP) <- c("YEAR", GDP[1,2:ncol(GDP)])
GDP <- GDP[-1,]
rownames(GDP) <- 1:nrow(GDP)
# Rename YEAR rows and make it numeric
GDP$YEAR <- as.numeric(as.character(GDP$YEAR))
# Transpose GDP and rename columns
GDP <- melt(GDP, id = 1, measured = 2:17)
colnames(GDP) <- c("YEAR", "STATE", "GDP")
# Recode STATE and make it factor
GDP$STATE <- mapvalues(as.matrix(GDP$STATE), statelist_name, statelist_code)
GDP$STATE <- factor(GDP$STATE, levels = statelist_code)
# Make GDP numeric
GDP$GDP <- as.numeric(sub(".", "", as.character(GDP$GDP),
fixed = TRUE))
##############################
# 6. Beer Tax
############################
# Import Beer Tax for states
BTAX <- as.data.frame(read.csv2("control/BTAX.csv", header = F,
fileEncoding ="ISO-8859-1",
stringsAsFactors = FALSE))
# Delete leading and tailing rows / columns; rename them
BTAX <- BTAX[-c(1:7, 1160:nrow(BTAX)),]
colnames(BTAX) <- c("STATE", "YEAR", "Q", "BTAX")
rownames(BTAX) <- 1:nrow(BTAX)
# Sum quarterly numbers to yearly
BTAXSUM <- data.frame("STATE", "YEAR", "BTAX", stringsAsFactors = FALSE)
for (state in statelist_code){
for (year in 1999:2016){
BTAXSUM <- rbind(BTAXSUM, c(state, year, NA))
}
}
colnames(BTAXSUM) <- BTAXSUM[1,]
BTAXSUM <- BTAXSUM[-1,]
BTAX[BTAX == "..."] <- NA
BTAXSUM$BTAX <- aggregate(as.numeric(BTAX$BTAX), by = list(BTAX$YEAR, BTAX$STATE),
FUN = sum)[[3]]
BTAX <- BTAXSUM
rm(BTAXSUM)
# Recode STATE and make it factor
BTAX$STATE <- factor(BTAX$STATE, levels = statelist_code)
# Rename YEAR rows and make it numeric
BTAX$YEAR <- as.numeric(as.character(BTAX$YEAR))
# Make BTAX numeric
BTAX$BTAX <- as.numeric(sub(".", "", as.character(BTAX$BTAX),
fixed = TRUE))
#############################
# 7. Youth unemployment
#############################
# list of all excel files from statista
YUR.L <- list.files ("control",recursive = TRUE, pattern = "^statistic")
# add state column to link list
YUR.L <- cbind(LINK = YUR.L, STATE = gsub("-bis-2015.xlsx" ,"" , substring(YUR.L,71)))
YUR.L <- as.data.frame(YUR.L)
YUR.L$STATE <- mapvalues(as.matrix(YUR.L$STATE), statelist_name_noU, statelist_code)
# youth unemployment data frame
YUR <- data.frame(NA, NA, NA)
YUR[,1] <- as.character(YUR[,1])
YUR[,2] <- as.numeric(YUR[,2])
YUR[,3] <- as.numeric(YUR[,3])
colnames(YUR) <- c("STATE", "YEAR", "YUR")
# load, clean and merge 16 excel files
for (i in 1:16) {
tmp <- read_excel(paste("control/",YUR.L[YUR.L[,2] == statelist_code[i]][1],sep = ""),
sheet = 2)
tmp <- tmp[c(3:25), c(1:2)]
for (j in as.numeric(unlist(tmp[,1]))){
YUR <- rbind(YUR, c(statelist_code[i], j, as.numeric(tmp[tmp[,1] == j,2])))
}
remove(tmp)
}
YUR <- YUR[-1,]
YUR$YUR <- as.numeric(YUR$YUR)
#############################
# 8. Education
#############################
# Import Education for states
EDU <- as.data.frame(read.csv2("control/EDU.csv", header = F,
fileEncoding ="ISO-8859-1", stringsAsFactors = FALSE))
# Import Education for states
EDU <- as.data.frame(read.csv2("control/EDU.csv", header = F,
fileEncoding ="ISO-8859-1", stringsAsFactors = FALSE))
# Delete leading and tailing rows / columns; rename them
EDU <- EDU[-c(1:7, 9, 331:nrow(EDU)),-c(2)]
rownames(EDU) <- 1:nrow(EDU)
EDU <- EDU[,-c(4, 6, 8, 10, 12, 14)]
colnames(EDU) <- c("YEAR", "STATE", "EDU_TOTAL", "EDU_NO", "EDU_HS", "EDU_RS",
"EDU_FH", "EDU_AH")
EDU <- EDU[-c(1:2),]
EDU <- EDU[, c(2, 1, 3:ncol(EDU))]
# Rename YEAR rows and make it numeric
EDU$YEAR <- as.numeric(as.character(EDU$YEAR))
rownames(EDU) <- 1:nrow(EDU)
# Recode STATE and make it factor
EDU$STATE <- mapvalues(as.matrix(EDU$STATE), "Freistaat Sachsen", "Sachsen")
EDU$STATE <- mapvalues(as.matrix(EDU$STATE), statelist_name, statelist_code)
EDU$STATE <- factor(EDU$STATE, levels = statelist_code)
# Make EDU numeric
for (i in 3:8){
EDU[,i] <- as.numeric(sub(".", "", as.character(EDU[,i]),
fixed = TRUE))
}
###########################
# 9. Merge Data
###########################
# Merge and delete PD, UR, SA, GDP, BTAX
INDEP <- merge(PD, UR, by = c("STATE", "YEAR"))
INDEP <- merge(INDEP, PP, by= c("STATE", "YEAR"))
INDEP <- merge(INDEP, SA, by = c("STATE"))
INDEP <- merge(INDEP, GDP, by = c("STATE", "YEAR"))
INDEP <- merge(INDEP, BTAX, by = c("STATE", "YEAR"))
INDEP <- merge(INDEP, YUR, by = c("STATE", "YEAR"))
INDEP <- merge(INDEP, EDU, by = c("STATE", "YEAR"))
remove(PD, PP, UR, SA, GDP, BTAX, YUR, EDU, YUR.L, i, j, state, statelist_code,
statelist_name, statelist_name_noU, year)