-
Notifications
You must be signed in to change notification settings - Fork 0
/
pro.py
372 lines (330 loc) · 10.5 KB
/
pro.py
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
360
361
362
363
364
365
366
367
368
369
370
371
372
import pandas
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
import pandas as pd
def pro():
nobs = 100
X = np.random.random((nobs, 2))
X = sm.add_constant(X)
beta = [1, .1, .5]
e = np.random.random(nobs)
y = np.dot(X, beta) + e
results = sm.OLS(y, X).fit()
print(results.summary())
import csv
import xlrd
PROV_LIST = ['北京', '天津', '上海', '重庆市', '湖北省', '广东省']
def merge():
data = list()
# [prov, time, gdp, pergdp, ei, il, lnpergdp, ui, pilot, post, lnlctfp]
energy = open('control variables/能源消费总量.csv')
f = open('control variables/外商固定资产投资.csv')
ind = open('control variables/第二产业占比.csv')
pergdp = open('control variables/人均GDP.csv')
gdp = open('control variables/地区GDP.csv')
urban = open('control variables/城镇人口比.csv')
# gdp
reader = list(csv.reader(gdp))
for line in reader[1:331]:
if int(line[2]) <= 2016:
data.append([
line[1], int(line[2]), float(line[3])
])
# per gdp
cnt = 0
reader = list(csv.reader(pergdp))
for idx, line in enumerate(reader[1:331]):
if int(line[2]) <= 2016:
data[cnt].append(float(line[3]))
cnt += 1
# ei
cnt = 0
reader = list(csv.reader(energy))
for idx, line in enumerate(reader[1:331]):
if int(line[1]) <= 2016:
data[cnt].append(float(line[2]) / data[cnt][2])
cnt += 1
cnt = 0
# fdi
reader = list(csv.reader(f))
for idx, line in enumerate(reader[1:301]):
if int(line[2]) <= 2016:
data[cnt].append( float(line[3]) / data[cnt][2])
cnt += 1
# il
cnt = 0
reader = list(csv.reader(ind))
for idx, line in enumerate(reader[1:331]):
if int(line[2]) <= 2016:
data[cnt].append( float(line[3]) / 100)
cnt += 1
# lnpergdp
for i in range(len(data)):
data[i].append(np.log(data[i][3]))
# ui
cnt = 0
reader = list(csv.reader(urban))
for idx, line in enumerate(reader[1:331]):
if int(line[2]) <= 2016:
data[cnt].append( float(line[3]) / 100)
cnt += 1
# pilot, post
for i in range(len(data)):
prov = data[i][0]
if prov in PROV_LIST:
data[i].append(1)
else:
data[i].append(0)
prov = data[i][-1]
year = data[i][1]
# if year < 2013 and year >= 2008:
# data[i].append(0)
# elif year >= 2013 and year <= 2016:
# data[i].append(1)
# else:
# print('illegal data')
if year < 2009:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2010:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2011:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2012:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2013:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2014:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2015:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
if year < 2016:
data[i].append(0)
else:
data[i].append(1)
data[i].append(data[i][-1] * prov)
# lnlctfp
worksheet = xlrd.open_workbook('lnlctfp.xlsx')
sheet = worksheet.sheet_by_name('Sheet1')
cnt = 0
for row in range(2, 29):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(np.log(c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# lnlci
worksheet = xlrd.open_workbook('lnlci.xlsx')
sheet = worksheet.sheet_by_name('Sheet1')
cnt = 0
for row in range(2, 29):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(np.log(c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# eps
worksheet = xlrd.open_workbook('eps.xlsx')
sheet = worksheet.sheet_by_name('Sheet1')
cnt = 0
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(c.value)
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# ind_num1
worksheet = xlrd.open_workbook('industrial_num.xls')
sheet = worksheet.sheet_by_name('Sheet1')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(np.log(c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# ind_num2
worksheet = xlrd.open_workbook('industrial_num.xls')
sheet = worksheet.sheet_by_name('Sheet2')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(np.log(c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# ind_num3
worksheet = xlrd.open_workbook('industrial_num.xls')
sheet = worksheet.sheet_by_name('Sheet3')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(np.log(c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# ind_num4
worksheet = xlrd.open_workbook('industrial_num.xls')
sheet = worksheet.sheet_by_name('Sheet4')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append(np.log(c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# forest1
worksheet = xlrd.open_workbook('forest.xls')
sheet = worksheet.sheet_by_name('Sheet1')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append((c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# forest2
worksheet = xlrd.open_workbook('forest.xls')
sheet = worksheet.sheet_by_name('Sheet2')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append((c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
# forest3
worksheet = xlrd.open_workbook('forest.xls')
sheet = worksheet.sheet_by_name('Sheet3')
cnt = 0
print(len(data))
for row in range(1, 28):
for col in range(2, 11):
c = sheet.cell(row, col)
data[cnt].append((c.value))
cnt += 1
if cnt == 20*9:
cnt = 22*9
if cnt == 27*9:
cnt = 28*9
outfile = open('did_data.csv', 'w')
writer = csv.writer(outfile)
writer.writerow(['prov', 'time', 'gdp', 'pergdp', 'ei', 'fdi', 'il', 'lnpergdp', 'ui',
'pilot', 'post09', 'cross09', 'post10', 'cross10', 'post11', 'cross11', 'post12', 'cross12', 'post13', 'cross13',
'post14', 'cross14', 'post15', 'cross15', 'post16', 'cross16',
'lnlctfp', 'lnlci', 'eps', 'ind_num1', 'ind_num2', 'ind_num3', 'ind_num4', 'forest1', 'forest2', 'forest3'])
writer.writerows(data)
EAST = ['北京', '天津', '河北省', '辽宁省', '上海', '江苏省', '浙江省', '福建省', '山东省', '广东省', '海南']
MIDDLE = ['山西省', '吉林省', '黑龙江省', '安徽省', '江西省', '河南省', '湖北省', '湖南省']
WEST = ['内蒙古自治区', '广西壮族自治区', '重庆', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省', '甘肃省', '青海省', '宁夏回族自治区', '新疆维吾尔自治区']
def split():
file = open('did_input.csv')
reader = list(csv.reader(file))
east = list()
mid = list()
west = list()
for line in reader[1:]:
if line[0] in EAST:
east.append(line)
elif line[0] in MIDDLE:
mid.append(line)
elif line[0] in WEST:
west.append(line)
else:
print(line[0])
exit(0)
fout = open('east.csv', 'w')
writer = csv.writer(fout)
writer.writerow(reader[0])
writer.writerows(east)
fout = open('middle.csv', 'w')
writer = csv.writer(fout)
writer.writerow(reader[0])
writer.writerows(mid)
fout = open('west.csv', 'w')
writer = csv.writer(fout)
writer.writerow(reader[0])
writer.writerows(west)
def main():
merge()
split()
df = pd.read_csv('did_input.csv')
print(df.tail)
# prov_dummy = pd.get_dummies(df['prov'], prefix="Prov_dum")
# print(prov_dummy.head())
# df = df.join(prov_dummy)
# print(df)
prov = df['prov']
new_prov = list()
rcd = dict()
cnt = 0
for item in prov:
if item not in rcd:
rcd[item] = cnt
cnt += 1
new_prov.append(rcd[item])
new_prov = pandas.DataFrame({"id_prov": new_prov})
df = df.join(new_prov)
idx = list(range(243))
id = pandas.DataFrame({"id": idx})
df = df.join(id)
print(df)
est = smf.ols(formula='lnlctfp ~ forest1 + cross13 + ei + fdi + il + lnpergdp + ui + C(id_prov, Treatment) + C(time, Treatment)', data=df).fit()
print(est.summary())
print(est.params)
if __name__ == '__main__':
main()