-
Notifications
You must be signed in to change notification settings - Fork 0
/
industry_analysis.py
363 lines (297 loc) · 16.7 KB
/
industry_analysis.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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import cPickle
import numpy as np
import pandas as pd
from collections import Counter
import random
from itertools import combinations
from time import time
from scipy import sparse
import statsmodels.formula.api as sm
from sklearn.preprocessing import StandardScaler
#%matplotlib inline
#maybe try with 50 patents at least in industries, can try with median
file_open = open("dict_ind_org_30.plk", "rb")
dict_ind_org = cPickle.load(file_open)
sectors = dict_ind_org.keys()
file_open = open("dict_ind_org_50.plk", "rb")
dict_ind_org = cPickle.load(file_open)
sectors = dict_ind_org.keys()
#might not need this!!
#file_open = open("org_list_of_lists.plk", 'rb')
#org_list_of_lists = cPickle.load(file_open)
cols = ["org_id","year","fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching"]
total_org_yearly_df = pd.DataFrame(columns=cols)
dist_cols = ["org_id","year","min_inv_distance","av_inv_distance"]
total_dist_df = pd.DataFrame(columns=dist_cols)
#open all dataframes and concatenate together
for i in range(10):
name = 'yearly_dfrun' + str(i) + '.csv'
yearly_df = pd.DataFrame.from_csv(name)
total_org_yearly_df = total_org_yearly_df.append(yearly_df)
# inner join the distance as well - BIGDATAFRAME
for i in range(10):
name = "distance_df_run" + str(i) + ".csv"
dist_df = pd.DataFrame.from_csv(name)
total_dist_df = total_dist_df.append(dist_df)
learning_dist_df = pd.merge(total_org_yearly_df, total_dist_df, how='left', on=['year', 'org_id'])
learning_dist_df = learning_dist_df.dropna()
learning_dist_df["min_inv_distance"] = learning_dist_df["min_inv_distance"]*100000
learning_dist_df["av_inv_distance"] = learning_dist_df["av_inv_distance"]*100000
### Need to either ger rid of the av_inv_distance and min_inv_distance or scale them so that they are clear
#or separate plots?
def make_industry_plot(mean_df,std_df,industry_name):
fig,ax = plt.subplots()
ax.errorbar(mean_df["year"],mean_df["fract_org"],yerr=std_orgs["fract_org"],fmt='-', ecolor='k', color='b')
ax.errorbar(mean_df["year"],mean_df["fract_new_for_org"],yerr=std_orgs["fract_new_for_org"],fmt='-', ecolor='k', color='g')
ax.errorbar(mean_df["year"],mean_df["fract_inv_already"],yerr=std_orgs["fract_inv_already"],fmt='-', ecolor='k', color='r')
ax.errorbar(mean_df["year"],mean_df["fract_learning_from_org"],yerr=std_orgs["fract_learning_from_org"],fmt='-', ecolor='k', color='c')
ax.errorbar(mean_df["year"],mean_df["fract_learning_others"],yerr=std_orgs["fract_learning_others"],fmt='-', ecolor='k', color='m')
ax.errorbar(mean_df["year"],mean_df["fract_searching"],yerr=std_orgs["fract_searching"],fmt='-', ecolor='k', color='y')
ax.errorbar(mean_df["year"],mean_df["min_inv_distance"],yerr=std_orgs["min_inv_distance"],fmt='-', ecolor='k', color='0.75')
ax.errorbar(mean_df["year"],mean_df["av_inv_distance"],yerr=std_orgs["av_inv_distance"],fmt='-', ecolor='k', color='0.75')
ax.set_xlabel('year')
ax.set_ylabel('fraction')
cols = ["fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching","min_inv_distance","av_inv_distance"]
ax.legend(cols, loc='best', fontsize = 'xx-small')
name = "learning_" + industry_name + ".png"
plt.title(name)
plt.savefig(name)
return
def make_industry_plot_no_dist(mean_df,std_df,industry_name):
fig,ax = plt.subplots()
ax.errorbar(mean_df["year"],mean_df["fract_org"],yerr=std_orgs["fract_org"],fmt='-', ecolor='k', color='b')
ax.errorbar(mean_df["year"],mean_df["fract_new_for_org"],yerr=std_orgs["fract_new_for_org"],fmt='-', ecolor='k', color='g')
ax.errorbar(mean_df["year"],mean_df["fract_inv_already"],yerr=std_orgs["fract_inv_already"],fmt='-', ecolor='k', color='r')
ax.errorbar(mean_df["year"],mean_df["fract_learning_from_org"],yerr=std_orgs["fract_learning_from_org"],fmt='-', ecolor='k', color='c')
ax.errorbar(mean_df["year"],mean_df["fract_learning_others"],yerr=std_orgs["fract_learning_others"],fmt='-', ecolor='k', color='m')
ax.errorbar(mean_df["year"],mean_df["fract_searching"],yerr=std_orgs["fract_searching"],fmt='-', ecolor='k', color='y')
ax.set_xlabel('year')
ax.set_ylabel('fraction')
cols = ["fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching"]
ax.legend(cols, loc='best', fontsize = 'xx-small')
name = "learning_" + industry_name + ".png"
plt.title(name)
plt.savefig(name)
return
def distance_plot(mean_df,std_df,industry_name):
fig,ax = plt.subplots()
ax.errorbar(mean_df["year"],mean_df["min_inv_distance"],yerr=std_orgs["min_inv_distance"],fmt='-', ecolor='k', color='0.75')
ax.errorbar(mean_df["year"],mean_df["av_inv_distance"],yerr=std_orgs["av_inv_distance"],fmt='-', ecolor='k', color='k')
ax.set_xlabel('year')
ax.set_ylabel('inv distance')
cols = ["min_inv_distance","av_inv_distance"]
ax.legend(cols, loc='best', fontsize = 'xx-small')
name = "distance_" + industry_name + ".png"
plt.title(name)
plt.savefig(name)
return
###THEN WILL NEED TO DO THIS FOR ALL INDUSTRIES 1) ADD IN NAMING THING THAT IS AUTOMATIC!! 2) FIGURE OUT HOW CAN MAKE DISTANCES LARGE SO CAN
# SEE THEM OTHERWISE ITS SILLY
for sector in sectors:
splitted = sector.split()
sector_name = splitted[0]
list_of_orgs = list(dict_ind_org[sector])
sector_cols = ["year","fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching","min_inv_distance","av_inv_distance"]
sector_df = pd.DataFrame(columns=sector_cols)
sector_df = sector_df.append(learning_dist_df.loc[learning_dist_df['org_id'].isin(list_of_orgs)])
mean_orgs = sector_df.groupby("year").mean()
mean_orgs["year"] = mean_orgs.index.values
median_orgs = sector_df.groupby("year").median()
median_orgs["year"] = median_orgs.index.values
std_orgs = sector_df.groupby("year").std()
std_orgs["year"] = std_orgs.index.values
std_orgs.drop('org_id', axis=1, inplace=True)
make_industry_plot_no_dist(mean_orgs,std_orgs,sector_name)
distance_plot(median_orgs,std_orgs,sector_name)
plt.close("all")
## Figure out how to do the regression for distance (or if dont have distance use one of the other metrics just to test)
def regression(data_to_fit,target):
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std
X = sm.add_constant(data_to_fit)
model = sm.OLS(target, X)
results = model.fit()
print(results.summary())
return results
def GLM_poisson(data_to_fit,target):
import statsmodels.api as sm
X = sm.add_constant(data_to_fit)
poisson_model = sm.GLM(target, X, family=sm.families.Poisson(link=sm.families.links.log))
results = poisson_model.fit()
print(results.summary())
return results
#### WORK OUT HOW TO SAVE THE REGRESSION OUTPUT!!
def regression_to_dataframe(data_to_fit,target):
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std
X = sm.add_constant(data_to_fit)
model = sm.OLS(target, X)
model_result = model.fit()
print(model_result.summary())
statistics = pd.Series({'r2': model_result.rsquared,
'adj_r2': model_result.rsquared_adj})
# put them togher with the result for each term
result_df = pd.DataFrame({'params': model_result.params,
'pvals': model_result.pvalues,
'std': model_result.bse,
'statistics': statistics})
# add the complexive results for f-value and the total p-value
fisher_df = pd.DataFrame({'params': {'_f_test': model_result.fvalue},
'pvals': {'_f_test': model_result.f_pvalue}})
# merge them and unstack to obtain a hierarchically indexed series
res_series = pd.concat([result_df, fisher_df]).unstack()
return res_series.dropna()
def results_to_dataframe(model_result):
statistics = pd.Series({'r2': model_result.rsquared,
'adj_r2': model_result.rsquared_adj})
# put them togher with the result for each term
result_df = pd.DataFrame({'params': model_result.params,
'pvals': model_result.pvalues,
'std': model_result.bse,
'statistics': statistics})
# add the complexive results for f-value and the total p-value
fisher_df = pd.DataFrame({'params': {'_f_test': model_result.fvalue},
'pvals': {'_f_test': model_result.f_pvalue}})
# merge them and unstack to obtain a hierarchically indexed series
res_series = pd.concat([result_df, fisher_df]).unstack()
return res_series.dropna()
def missing_indicator(df, column_name):
""" add a missing indicator for a feature to the dataframe, 1 if missing and 0 otherwise. """
nul = df[[column_name]].isnull()
nul = nul.applymap(lambda x: 1 if x else 0)
name = column_name + "_missing"
df[name] = nul
return df
def run_missing_indicator(df, cols):
for col in cols:
df = missing_indicator(df,col)
return df
def reg_to_df_line(model_result,df):
result = list(model_result.params.values) + list(model_result.pvalues.values) + [model_result.rsquared] + [model_result.rsquared_adj] + list(model_result.bse.values)
return result
def poisson_to_df_line(model_result,df):
line = list(model_result.params.values) + list(model_result.pvalues.values) + [model_result.deviance] + [model_result.aic] + [model_result.pearson_chi2] + [model_result.nobs] + [model_result.llf] + list(model_result.bse.values)
return line
cols = ["param_c","param_1", "param_2", "param_3", "param_4","pv_c","pv_1", "pv_2", "pv_3", "pv_4","deviance", "AIC", "pearson_chi","data_points","log_likelihood","std_c", "std_1", "std_2", "std_3", "std_4"]
poisson_df = pd.DataFrame(columns=cols)
for sector in sectors:
splitted = sector.split()
sector_name = splitted[0]
list_of_orgs = list(dict_ind_org[sector])
sector_cols = ["year","fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching","min_inv_distance","av_inv_distance"]
sector_df = pd.DataFrame(columns=sector_cols)
sector_df = sector_df.append(learning_dist_df.loc[learning_dist_df['org_id'].isin(list_of_orgs)])
df_to_fit = sector_df[["year","fract_org","fract_new_for_org","fract_learning_from_org"]]
target = sector_df["av_inv_distance"]
df_to_fit['year'] = df_to_fit['year'] - 1976
scaler = StandardScaler()
variables = scaler.fit_transform(df_to_fit)
#results = regression(df_to_fit,target)
results = GLM_poisson(variables,target)
poisson_df.loc[sector_name] = poisson_to_df_line(results,poisson_df)
poisson_df.to_csv("distance_poisson_regression_results.csv")
#missing_indicator(sector_df,sector_scols)
####Count number of patents in industry per year ()
#this can only do with industry mean-ed data
file_open = open("wku_org_matrix_final.plk", "rb")
org_matrix = cPickle.load(file_open)
org_matrix = org_matrix.tocsr()
file_open = open("date_issue_array.plk", 'rb')
date_array = cPickle.load(file_open)
def no_of_patents_df(sector_df,org_matrix,date_array,list_of_orgs):
no_of_patents = pd.DataFrame(columns=[["year","no_of_patents"]])
for year in sector_df['year'].unique():
year = int(year)
patents = np.unique(org_matrix[:,list_of_orgs].nonzero()[0])
patent_in_year = len(patents[date_array[patents]==year])
no_of_patents.loc[(len(no_of_patents)+1)] = (year,patent_in_year)
return no_of_patents
def no_of_patents_per_org(sector_df,org_matrix,date_array,list_of_orgs):
no_of_patents = pd.DataFrame(columns=[["year","org_id","no_of_patents"]])
for year in sorted(sector_df['year'].unique()):
year = int(year)
print year
for org in list_of_orgs:
patents = org_matrix[:,org].nonzero()[0]
patent_in_year = len(patents[date_array[patents]==year])
no_of_patents.loc[(len(no_of_patents)+1)] = (year,float(org),patent_in_year)
return no_of_patents
from sklearn.preprocessing import StandardScaler
from time import time
cols = ["param_c","param_1", "param_2", "param_3", "param_4", "param_5", "pv_c","pv_1", "pv_2", "pv_3", "pv_4","pv_5","deviance", "AIC", "pearson_chi","data_points","log_likelihood","std_c", "std_1", "std_2", "std_3", "std_4", "std_5"]
poisson_df = pd.DataFrame(columns=cols)
for sector in sectors:
count = 33
toc = time()
splitted = sector.split()
sector_name = splitted[0]
list_of_orgs = list(dict_ind_org[sector])
sector_cols = ["year","org_id","fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching","min_inv_distance","av_inv_distance"]
sector_df = pd.DataFrame(columns=sector_cols)
sector_df = sector_df.append(learning_dist_df.loc[learning_dist_df['org_id'].isin(list_of_orgs)])
no_of_patents_company = no_of_patents_per_org(sector_df,org_matrix,date_array,list_of_orgs)
data_nop_org = pd.merge(sector_df, no_of_patents_company, how='left', on=['org_id','year'])
df_to_fit_nop = data_nop_org[["year","fract_org","fract_new_for_org","fract_learning_from_org","av_inv_distance"]]
df_to_fit_nop['year'] = df_to_fit_nop['year'] - 1976
scaler = StandardScaler()
variables = scaler.fit_transform(df_to_fit_nop)
target_nop = data_nop_org["no_of_patents"].astype(float)
results = GLM_poisson(variables,target_nop)
poisson_df.loc[sector_name] = poisson_to_df_line(results,poisson_df)
tic = time()
time_taken = tic - toc
count -=1
print "time for " + sector_name +" "+ str(time_taken) + " " + str(count)
poisson_df.to_csv("no_patents_poisson_regression_results.csv")
#results_patents = regression(df_to_fit_nop,target_nop)
#results_series = results_to_dataframe(results_patents)
'''
#industry level
for sector in sectors:
splitted = sector.split()
sector_name = splitted[0]
list_of_orgs = list(dict_ind_org[sector])
no_of_patents_industry = no_of_patents_df(sector_df,org_matrix,date_array,list_of_orgs)
sector_cols = ["year","fract_org","fract_new_for_org","fract_inv_already","fract_learning_from_org","fract_learning_others","fract_searching","min_inv_distance","av_inv_distance"]
sector_df = pd.DataFrame(columns=sector_cols)
sector_df = sector_df.append(learning_dist_df.loc[learning_dist_df['org_id'].isin(list_of_orgs)])
mean_orgs = sector_df.groupby("year").mean()
mean_orgs["year"] = mean_orgs.index.values
mean_orgs["av_inv_distance"] = mean_orgs["av_inv_distance"]
mean_orgs["min_inv_distance"] = mean_orgs["min_inv_distance"]
data_nop = pd.merge(mean_orgs, no_of_patents_industry, how='left', on=['year'])
#df_to_fit_nop = data_nop[["year","fract_org","fract_new_for_org","fract_learning_others","fract_learning_from_org","fract_searching","min_inv_distance","av_inv_distance"]]
df_to_fit_nop = data_nop[["year","fract_org","fract_new_for_org","fract_searching"]]
target_nop = data_nop["no_of_patents"]
#results_patents = regression(df_to_fit_nop,target_nop)
#results_series = results_to_dataframe(results_patents)
#SOMEHOW SAVE THE REGRESSION OUTPUT
#cols = ["param_0","param_1", "param_2", "param_3", "param_4", "param_5", "param_6","pv_0","pv_1", "pv_2", "pv_3", "pv_4", "pv_5", "pv_6", "rsqu", "adj_rsqu", "std_0","std_1", "std_2", "std_3", "std_4", "std_5", "std_6"]
#regression_df = pd.DataFrame(columns=cols)
'''
#SOMEHOW SAVE THE REGRESSION OUTPUT
'''
1) run graphs with median and with mean for 50_patent industries (okay, basically same - just tiny bit clearer trend, use 50)
2) make graphs of distance for all industries (do for 30 if the 50 patents are basically the same) --> used 50 to do graphs, but maybe should have done the median, std big so no super clear trend
(redo the graphs with the median maybe!!!)
3) re-scale distance for all regressions
Year make so that is year - earliest year....for all regressions --> ie work on the feature improvement thing
4) check variable shape distance (do groupby maybe but after scale it so makes more sense) - is poisson as well!!
5) check correlations between variables (learning metrics)
6) change regression to GLM Poisson
7) figure out how to post results so that are understandable/can see what is going on....
8) Write up
'''
'''
for sector in sectors:
list_of_orgs = list(dict_ind_org[sector])
sector_df = average_of_sector_inc_dist(list_of_orgs,total_org_yearly_df)
mean_orgs = sector_df.groupby("year").mean()
std_orgs = sector_df.groupby("year").std()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.errorbar(x,y,yerr=list_of_std,fmt='-', color='b')
'''