发现 IPC 和行业对照那里的处理有点问题,需要更新下~
新版本在这里:https://rstata.duanshu.com/#/brief/course/92b5c171ace949ec921b0570f421675f
今天给大家分享一下「新质生产力背景下数实融合的测算与时空比较——基于专利共分类方法的研究」一文中的数实融合水平指标的 Stata 计算方法,不过由于论文中关于指标计算的一些细节描述的较为含糊,所以计算过程中的一些处理方法还有待探讨,感兴趣的小伙伴也可以说说自己的想法。
按照论文的介绍,数实融合水平的测算过程如下所示:
一个简单的示例 准备示例数据 按照上面的示意图。计算数实融合需要下面三个数据:
一是各个专利的分类号:
clear all input str2 id str20 ipc "P1" "IPC1;IPC3" "P2" "IPC1;IPC2" "P3" "IPC1;IPC2;IPC3" end compress save 专利示例数据_示例, replace
二是 IPC 分类号与产业对照表:
clear all input str4 uniq_ipc str20 industry "IPC1" "I1" "IPC2" "I2;I3" "IPC3" "I3;I4" end compress save IPC与产业对照表_示例, replace
三是产业分类表:
clear all input str4 uniq_industry str20 class "I1" "数字产业" "I2" "数字产业" "I3" "实体产业" "I4" "实体产业" end compress save 产业分类表_示例, replace
计算 IPC 融合矩阵 根据文献的介绍,IPC 融合矩阵的结果就是如果两个 IPC 号出现在一个专利的分类号上,返回 1,否则返回 0。因此我们可以使用 Mata 的代码循环各个专利,分别获取每个分类号里面的 IPC 组合,合并再统计数量:
use 专利示例数据_示例, clear gen _freq = 1mata : ipc = st_sdata(., "ipc" ) value = st_data(., "_freq" ) fullcombinations = J (0, 3, "" ) for (r = 1; r <= rows(ipc); r++) { class_list = colshape(ustrsplit(ipc[r, 1], ";" ), 1) n_classes = rows(class_list) if (n_classes == 2) { fullcombinations = fullcombinations \ (class_list[1,1], class_list[2,1], strofreal (value[r,1])) } if (n_classes > 2) { combinations = J (n_classes * n_classes, 3, "" ) row_index = 1 for (i = 1; i <= n_classes; i++) { for (j = 1; j <= n_classes; j++) { combinations[row_index, 1] = class_list[i,1] combinations[row_index, 2] = class_list[j,1] combinations[row_index, 3] = strofreal (value[r,1]) row_index++ } } fullcombinations = fullcombinations \ combinations } } st_matrix("obs" , rows(fullcombinations)) stata("clear" ) st_addvar("str100" , "ipc1" ) st_addvar("str100" , "ipc2" ) st_addvar("str100" , "value" ) stata("set obs `=obs[1,1]'" ) st_sstore(., ("ipc1" , "ipc2" , "value" ), fullcombinations) end destring , replace compress save temp1, replace list in 1/10
汇总统计各组合的总共现次数:
use temp1, clear collapse (sum ) value, by (ipc1 ipc2)replace value = 0 if ipc1 == ipc2save temp1a, replace
不过这个结果并不是一个对称矩阵,下面我们再把这个数据转换成对称矩阵(对称矩阵转换成长数据就是一个平衡面板):
use temp1a, clear gen id = _ndrop valuegather ipc1 ipc2 keep valueduplicates drop value, forcesave temp1b, replace ren value value2cross using temp1bren value ipc1ren value2 ipc2merge 1:1 ipc1 ipc2 using temp1adrop _mreplace value = 0 if mi (value)save temp1c, replace ren ipc1 tempren ipc2 ipc1ren temp ipc2ren value value2merge 1:1 ipc1 ipc2 using temp1cegen value3 = rowmax(value value2)replace value = value3drop value2 _m value3replace value = 0 if ipc1 == ipc2drop if value == 0save IPC融合数量计算结果, replace list in 1/6, sep(0)
转换成矩阵就是这样的:
spread ipc2 value list in 1/3 *> +---------------------------+ *> | ipc1 IPC1 IPC2 IPC3 | *> |---------------------------| *> 1. | IPC1 . 2 2 | *> 2. | IPC2 2 . 1 | *> 3. | IPC3 2 1 . | *> +---------------------------+
然后我们把 IPC 分类号和 IPC与产业对照表 进行匹配,进而将 IPC 替换成产业分类编号:
use IPC融合数量计算结果, clear ren ipc1 uniq_ipcren ipc2 uniq_ipc2merge m :1 uniq_ipc using IPC与产业对照表_示例drop if _m == 2drop _mren industry industry1drop uniq_ipcren uniq_ipc2 uniq_ipcmerge m :1 uniq_ipc using IPC与产业对照表_示例drop if _m == 2drop _mren industry industry2drop uniq_ipcorder industry*foreach i of varlist _all { cap format `i' %10s } drop if mi (industry1) | mi (industry2)drop if industry1 == ";" replace industry1 = subinstr (industry1, ";" , " " , .)replace industry1 = strtrim (industry1)replace industry1 = subinstr (industry1, " " , ";" , .)drop if industry2 == ";" replace industry2 = subinstr (industry2, ";" , " " , .)replace industry2 = strtrim (industry2)replace industry2 = subinstr (industry2, " " , ";" , .)save temp2, replace list , sep(0)
计算产业融合矩阵 使用类似上面的 Mata 代码就可以计算产业融合矩阵了:
use temp2, clear collapse (sum ) value, by (industry1 industry2)mata : mata clear fullcombinations = J (0, 3, "" ) value = st_data(., "value" ) industry1 = st_sdata(., "industry1" ) industry2 = st_sdata(., "industry2" ) for (z = 1; z <= rows(industry1); z++) { a = ustrsplit(industry1[z,1], ";" ) b = ustrsplit(industry2[z,1], ";" ) combinations = J (cols(a) * cols(b), 3, "" ) row_index = 1 for (i = 1; i <= cols(a); i++) { for (j = 1; j <= cols(b); j++) { combinations[row_index, 1] = a[1, i] combinations[row_index, 2] = b[1, j] combinations[row_index, 3] = strofreal (value[z,1]) row_index++ } } fullcombinations = fullcombinations \ combinations } st_matrix("obs" , rows(fullcombinations)) stata("clear" ) st_addvar("str100" , "industry1" ) st_addvar("str100" , "industry2" ) st_addvar("str100" , "value" ) stata("set obs `=obs[1,1]'" ) st_sstore(., ("industry1" , "industry2" , "value" ), fullcombinations) end destring , replace compress collapse (sum ) value, by (industry1 industry2)save temp3, replace list , sep(0)
然后我们统计下数字产业和实体产业的数量:
use temp3, clear gen id = _ndrop valuegather industry1 industry2 keep valueduplicates drop _all, forcesave temp3a, replace ren value uniq_industrysave temp3b, replace merge 1:m uniq_industry using 产业分类表_示例keep if _m == 3drop _mcontract class save 融合的各产业数量, replace
和前面一样,这里的 temp3 结果并不是一个对称矩阵,我们也要使用类似的方法把该结果处理成对称矩阵(平衡面板):
use temp3, clear gen id = _ndrop valuegather industry1 industry2 keep valueduplicates drop _all, forcesave temp3a, replace ren value uniq_industrysave temp3b, replace merge 1:m uniq_industry using 产业分类表_示例keep if _m == 3drop _mcontract class save 融合的各产业数量, replace use temp3a, clear cross using temp3bren value industry1ren uniq_industry industry2merge 1:1 industry1 industry2 using temp3drop _mreplace value = 0 if mi (value)save temp3c, replace ren industry1 tempren industry2 industry1ren temp industry2ren value value2merge 1:1 industry1 industry2 using temp3cegen value3 = rowmax(value value2)replace value = value3drop value2 _m value3replace value = 0 if industry1 == industry2save 行业融合数量计算结果, replace list , sep(0)
然后我们就可以根据公式计算数实融合水平了:
use 行业融合数量计算结果, clear ren industry1 uniq_industrymerge m :1 uniq_industry using 产业分类表_示例keep if _m == 3drop _mren uniq_industry industry1ren class class1ren industry2 uniq_industrymerge m :1 uniq_industry using 产业分类表_示例keep if _m == 3drop _mren uniq_industry industry2ren class class2drop if value == 0save temp4, replace collapse (sum ) value, by (class1 class2)save temp5, replace use temp4, clear ren class1 class merge m :1 class using 融合的各产业数量drop _mren _freq freq1ren class class1ren class2 class merge m :1 class using 融合的各产业数量drop _mren _freq freq2ren class class2collapse (sum ) value (first) freq1 (first) freq2, by (class1 class2)gen RH = value / (freq1 * freq2)gen class = "数实融合" if class1 != class2replace class = "数数融合" if class1 == "数字产业" & class2 == "数字产业" replace class = "实实融合" if class1 == "实体产业" & class2 == "实体产业" keep class RHduplicates drop _all, forcelist
这样就计算得到了数实融合水平是 2,数数融合水平是 1,实实融合水平是 0.5。
基于真实数据的计算 数字经济及其核心产业统计分类(2021) 从国家知识产权局的官网上就可以下载到「数字经济及其核心产业统计分类(2021).docx」文件了,使用下面的代码即可提取里面的表格:
library( tidyverse) library( docxtractr) doc <- read_docx( "数字经济及其核心产业统计分类(2021).docx" ) doc %>% docx_extract_all_tbls( ) %>% .[[ 1 ] ] %>% slice( - 1 , - 2 ) %>% set_names( "大类" , "中类" , "小类" , "名称" , "说明" , "国民经济行业代码及名称" ) %>% type_convert( ) %>% mutate( 国民经济行业代码 = str_extract( 国民经济行业代码及名称, "\\d{4}" ) ) %>% haven:: write_dta( "数字经济及其核心产业统计分类.dta" ) haven:: read_dta( "数字经济及其核心产业统计分类.dta" ) %>% DT:: datatable( )
根据论文的建议,我们选取数字经济前四类核心产业作为测算数实融合的数字产业部分:
use 数字经济及其核心产业统计分类.dta, clear keep if substr (小类, 1, 2) != "05" & !mi (小类)drop if mi (国民经济行业代码)gen 数字经济产业 = "" replace 数字经济产业 = "数字产品制造业" if substr (小类, 1, 2) == "01" replace 数字经济产业 = "数字产品服务业" if substr (小类, 1, 2) == "02" replace 数字经济产业 = "数字技术应用业" if substr (小类, 1, 2) == "03" replace 数字经济产业 = "数字要素驱动业" if substr (小类, 1, 2) == "04" keep 国民经济行业代码 数字经济产业ren 国民经济行业代码 industry2save 数字经济核心产业, replace list in 1/10, sep(0)
国民经济分类与IPC分类号对照表 为了建立 IPC 分类号和国民经济行业分类的关系,我们还需要处理下「国民经济分类与IPC分类号对照表」,该文件也是来自国家知识产权局:
library( tidyverse) readxl:: read_xlsx( "国际专利分类与国民经济行业分类参照关系表(2018).xlsx" ) %>% set_names( "国民经济行业代码" , "国民经济行业名称" , "国际专利分类号" , "国际专利分类号类名" ) %>% mutate( 行业门类代码 = if_else( str_detect( 国民经济行业代码, "[A-Z]" ) , 国民经济行业代码, "" ) ) %>% type_convert( ) %>% fill( 行业门类代码) %>% mutate( 国民经济行业代码 = if_else( str_length( 国民经济行业代码) == 3 & ! str_detect( 国民经济行业代码, "^0" ) , paste0( "0" , 国民经济行业代码) , 国民经济行业代码) ) %>% filter( str_length( 国民经济行业代码) == 4 | is.na ( 国民经济行业代码) ) %>% fill( 国民经济行业代码, 国民经济行业名称) %>% filter( ! is.na ( 国际专利分类号) ) %>% mutate_all( ~ str_remove_all( .x, "\\r|\\n" ) ) %>% mutate( 国民经济行业代码 = paste0( 行业门类代码, 国民经济行业代码) ) %>% select( - 行业门类代码) %>% haven:: write_dta( "国民经济分类与IPC分类号对照表.dta" , label = "数据处理:微信公众号 RStata" ) haven:: read_dta( "国民经济分类与IPC分类号对照表.dta" ) %>% DT:: datatable( )
实体产业分类 按照论文的介绍,实体产业可以分为下面四类:
use 国民经济分类与IPC分类号对照表.dta, clear gen 实体产业 = "" gen 行业门类 = substr (国民经济行业代码, 1, 1)order 行业门类replace 实体产业 = "制造业" if 行业门类 == "C" replace 实体产业 = "农业" if 行业门类 == "A" replace 实体产业 = "建筑业及其他工业" if inlist (行业门类, "B" , "D" , "E" )replace 实体产业 = "服务业" if inlist (行业门类, "I" , "O" )save 实体产业分类, replace
不过这个在本次课中用不到。
专利数据中所有的 IPC 号 虽然和国民经济行业代码对照的国际专利分类号有差不多 18000 个,不过并不是每个 IPC 号都在专利数据中出现过。因此我们首先需要统计所有专利数据中互不相同的 IPC 号,然后和这个「国民经济分类与IPC分类号对照表」进行匹配:
专利数据太大了,这里就不再放在附件了。大家重现下面的代码可以从这里下载:https://rstata.duanshu.com/#/brief/course/edef981592a64257aa41e2b6d6d21746
local url = "/Volumes/A10R2copy/A10T/大数据/1985~2022 年专利申请数据(已更新,含授权信息、申请人地址的经纬度及其所处的省市区县信息)/newpatentdata_coord2/" use "`url'/1985patent_v2_coord.dta" , clear ren 分类号 IPCreplace IPC = subinstr (IPC, "//" , "" , .)replace IPC = subinstr (IPC, "(" , "" , .)replace IPC = subinstr (IPC, ")" , "" , .)replace IPC = subinstr (IPC, "=" , "" , .)replace IPC = subinstr (IPC, "," , ";" , .)drop if 专利类型 == "外观设计" drop if !index (IPC, ";" )keep newzlid IPCreplace IPC = subinstr (IPC, " " , "" , .)mata :ipc = st_sdata(., "IPC" ) ipc2 = rowshape(ipc, cols(ipc)) ipc3 = invtokens(ipc2, ";" ) ipc4 = ustrsplit(ipc3, ";" ) ipc5 = colshape(ipc4, 1) ipc6 = uniqrows(ipc5) st_matrix("obs" , rows(ipc6)) stata("clear" ) stata("cap drop uniq_ipc" ) st_addvar("strL" , "uniq_ipc" ) stata("set obs `=obs[1,1]'" ) st_sstore(., "uniq_ipc" , ipc6) end
循环所有年份的:
cap mkdir "uniq_ipc" local url = "/Volumes/A10R2copy/A10T/大数据/1985~2022 年专利申请数据(已更新,含授权信息、申请人地址的经纬度及其所处的省市区县信息)/newpatentdata_coord2/" forval y = 1985/2022 { di "`y'" qui { use "`url'/`y'patent_v2_coord.dta" , clear ren 分类号 IPC replace IPC = subinstr (IPC, "//" , "" , .) replace IPC = subinstr (IPC, "," , ";" , .) replace IPC = subinstr (IPC, "=" , "" , .) drop if 专利类型 == "外观设计" drop if !index (IPC, ";" ) keep newzlid IPC replace IPC = subinstr (IPC, " " , "" , .) mata : ipc = st_sdata(., "IPC" ) mata : ipc2 = rowshape(ipc, cols(ipc)) mata : ipc3 = invtokens(ipc2, ";" ) mata : ipc4 = ustrsplit(ipc3, ";" ) mata : ipc5 = colshape(ipc4, 1) mata : ipc6 = uniqrows(ipc5) mata : st_matrix("obs" , rows(ipc6)) mata : stata("clear" ) mata : stata("cap drop uniq_ipc" ) mata : st_addvar("strL" , "uniq_ipc" ) mata : stata("set obs `=obs[1,1]'" ) mata : st_sstore(., "uniq_ipc" , ipc6) save uniq_ipc/`y' , replace } }
合并所有的:
use uniq_ipc/1985, clear forval y = 1986/2022 { append using uniq_ipc/`y' } duplicates drop uniq_ipc, forcereplace uniq_ipc = subinstr (uniq_ipc, "(" , "" , .) if ustrregexm(uniq_ipc, "^\(" )replace uniq_ipc = ustrregexs(1) if ustrregexm(uniq_ipc, "(.*)\(" )replace uniq_ipc = subinstr (uniq_ipc, ")" , "" , .)replace uniq_ipc = ustrregexs(1) if ustrregexm(uniq_ipc, "(.*):" )replace uniq_ipc = subinstr (uniq_ipc, "C07D209/14C07D209" , "C07D209/14" , .)replace uniq_ipc = subinstr (uniq_ipc, "A61K31/52A61K31" , "A61K31/52" , .)drop if mi (uniq_ipc)drop if !ustrregexm(substr (uniq_ipc, 1, 1), "[A-Z]" )save uniq_ipc, replace
筛选能够和国民经济分类对应上的 IPC 分类号 「国民经济分类与IPC分类号对照表.dta」数据中给出了非常多的分类号和行业小类的对照,不过并不是所有的 IPC 号都在专利数据中出现过,因此我们可以筛选能够匹配上行业小类的:
use 国民经济分类与IPC分类号对照表.dta, clear gen dzid = _norder dzidren 国际专利分类号 IPCreplace IPC = subinstr (IPC, "*" , "" , .)replace IPC = subinstr (IPC, " " , "" , .)gen len = strlen (IPC)tab lencap mkdir "对照表" forval i = 3/11 { preserve keep if len == `i' drop len save 对照表/len`i' , replace restore } use uniq_ipc, clear cap mkdir "行业匹配结果" gen len = strlen (uniq_ipc)tab lengsort -lendrop if len > 11 | len < 3forval i = 3/11 { preserve keep if len == `i' drop len gen IPC = substr (uniq_ipc, 1, `i' ) joinby IPC using 对照表/len`i' save 行业匹配结果/`i' , replace restore } use 行业匹配结果/3, clear forval i = 4/11 { append using 行业匹配结果/`i' } duplicates drop _all, forcekeep uniq_ipc 国民经济行业代码save 专利数据与行业小类代码简易对照表, replace list in 1/10
可以看到每个分类号是可能对应多个行业小类的,我们把这些行业小类收集起来:
use 专利数据与行业小类代码简易对照表, clear recast str11 uniq_ipcbysort uniq_ipc: gen id = _ntostring id, replace replace id = "m" + idspread id 国民经济行业代码 unite m *, gen (国民经济行业代码) sep(";" ) drop m *forval i = 1/30 { replace 国民经济行业代码 = subinstr (国民经济行业代码, ";;" , ";" , .) } replace 国民经济行业代码 = subinstr (国民经济行业代码, ";" , " " , .)replace 国民经济行业代码 = strtrim (国民经济行业代码)replace 国民经济行业代码 = subinstr (国民经济行业代码, " " , ";" , .)save 专利数据与行业小类代码简易对照表2, replace
也就是大概只有 3000 多个 IPC 分类号是可以对应上行业小类的。
产业数实分类 基于上面的数字经济产业分类和实体产业分类,我们就可以对所有的行业小类进行分类了:
use 数字经济核心产业.dta, clear use 国民经济分类与IPC分类号对照表.dta, clear keep 国民经济行业代码duplicates drop 国民经济行业代码, forcegen industry2 = substr (国民经济行业代码, 2, .)joinby industry2 using 数字经济核心产业.dta, unmatched(master)drop if mi (国民经济行业代码)drop _mjoinby 国民经济行业代码 using 实体产业分类.dta, unmatched(master)drop industry2 _mkeep 国民经济行业代码 数字经济产业 实体产业gen class = (!mi (数字经济产业))collapse (sum ) class , by (国民经济行业代码)gen 类别 = "数字经济产业" if class > 0replace 类别 = "实体产业" if class == 0drop class tab 类别save 产业数实分类, replace
如果某个行业小类同时对应了实体产业和数字产业,归类到数字产业中。
以 2012 年的专利数据为例进行计算 这样我们就已经有了所需的所有数据了。
首先,保留所需的变量:
*- use 2012patent_v2_coord.dta, clear *- keep newzlid 公开公告号 专利类型 分类号 *- ren 分类号 IPC *- save 2012patent_small, replace
去除重复的专利以及删除用不到的外观设计专利:
use 2012patent_small, clear drop if 专利类型 == "外观设计" gen temp = substr (公开公告号, -1, 1)replace 公开公告号 = subinstr (公开公告号, temp, "" , .) if ustrregexm(temp, "[A-Z]" )drop tempduplicates drop 公开公告号, forcedrop if !index (IPC, ";" )keep newzlid IPCreplace IPC = subinstr (IPC, " " , "" , .)split IPC, parse (;)drop IPCkeep newzlid IPC1-IPC10save tempdata1, replace
把没有行业小类对照的都替换成空(这样可以减少下面的计算量):
use tempdata1, clear forval i = 1/10 { ren IPC`i' uniq_ipc merge m :1 uniq_ipc using 专利数据与行业小类代码简易对照表2 drop if _m == 2 replace uniq_ipc = "" if _m != 3 drop _m ren uniq_ipc IPC`i' drop 国民经济行业代码 } save tempdata1a, replace use tempdata1a, clear unite IPC*, gen (国际专利分类号) sep(";" ) drop IPC*forval i = 1/30 { replace 国际专利分类号 = subinstr (国际专利分类号, ";;" , ";" , .) } drop if 国际专利分类号 == ";" replace 国际专利分类号 = subinstr (国际专利分类号, ";" , " " , .)replace 国际专利分类号 = strtrim (国际专利分类号)replace 国际专利分类号 = subinstr (国际专利分类号, " " , ";" , .)drop if !index (国际专利分类号, ";" )ren 国际专利分类号 ipcsave tempdata2, replace
统计 IPC 共现矩阵:
use tempdata2, clear contract ipcmata : ipc = st_sdata(., "ipc" ) value = st_data(., "_freq" ) fullcombinations = J (0, 3, "" ) for (r = 1; r <= rows(ipc); r++) { class_list = colshape(ustrsplit(ipc[r, 1], ";" ), 1) n_classes = rows(class_list) if (n_classes == 2) { fullcombinations = fullcombinations \ (class_list[1,1], class_list[2,1], strofreal (value[r,1])) } if (n_classes > 2) { combinations = J (n_classes * n_classes, 3, "" ) row_index = 1 for (i = 1; i <= n_classes; i++) { for (j = 1; j <= n_classes; j++) { combinations[row_index, 1] = class_list[i,1] combinations[row_index, 2] = class_list[j,1] combinations[row_index, 3] = strofreal (value[r,1]) row_index++ } } fullcombinations = fullcombinations \ combinations } } st_matrix("obs" , rows(fullcombinations)) stata("clear" ) st_addvar("str100" , "ipc1" ) st_addvar("str100" , "ipc2" ) st_addvar("str100" , "value" ) stata("set obs `=obs[1,1]'" ) st_sstore(., ("ipc1" , "ipc2" , "value" ), fullcombinations) end destring , replace compress save tempdata3, replace list in 1/10
汇总后再把这个结果转换成平衡面板(对称矩阵的长面板样式):
use tempdata3, clear collapse (sum ) value, by (ipc1 ipc2)replace value = 0 if ipc1 == ipc2save tempdata3a, replace use tempdata3a, clear gen id = _ndrop valuegather ipc1 ipc2 keep valueduplicates drop value, forcesave tempdata3b, replace ren value value2cross using tempdata3bren value ipc1ren value2 ipc2merge 1:1 ipc1 ipc2 using tempdata3adrop _mreplace value = 0 if mi (value)save tempdata3c, replace ren ipc1 tempren ipc2 ipc1ren temp ipc2ren value value2merge 1:1 ipc1 ipc2 using tempdata3cegen value3 = rowmax(value value2)replace value = value3drop value2 _m value3replace value = 0 if ipc1 == ipc2drop if value == 0save IPC融合数量计算结果, replace list in 1/10
根据 IPC 和行业小类的对照表就可以把上面的 IPC 号替换成行业小类了:
use IPC融合数量计算结果, clear ren ipc1 uniq_ipcmerge m :1 uniq_ipc using 专利数据与行业小类代码简易对照表2.dtadrop if _m == 2drop _mren 国民经济行业代码 industry1drop uniq_ipcren ipc2 uniq_ipcmerge m :1 uniq_ipc using 专利数据与行业小类代码简易对照表2.dtadrop if _m == 2drop _mren 国民经济行业代码 industry2drop uniq_ipcorder industry*foreach i of varlist _all { cap format `i' %10s } drop if mi (industry1) | mi (industry2)drop if industry1 == ";" replace industry1 = subinstr (industry1, ";" , " " , .)replace industry1 = strtrim (industry1)replace industry1 = subinstr (industry1, " " , ";" , .)drop if industry2 == ";" replace industry2 = subinstr (industry2, ";" , " " , .)replace industry2 = strtrim (industry2)replace industry2 = subinstr (industry2, " " , ";" , .)save tempdata4, replace
再使用 Mata 代码统计行业共现频次:
use tempdata4, clear collapse (sum ) value, by (industry1 industry2)mata : mata clear fullcombinations = J (0, 3, "" ) value = st_data(., "value" ) industry1 = st_sdata(., "industry1" ) industry2 = st_sdata(., "industry2" ) for (z = 1; z <= rows(industry1); z++) { a = ustrsplit(industry1[z,1], ";" ) b = ustrsplit(industry2[z,1], ";" ) combinations = J (cols(a) * cols(b), 3, "" ) row_index = 1 for (i = 1; i <= cols(a); i++) { for (j = 1; j <= cols(b); j++) { combinations[row_index, 1] = a[1, i] combinations[row_index, 2] = b[1, j] combinations[row_index, 3] = strofreal (value[z,1]) row_index++ } } fullcombinations = fullcombinations \ combinations } st_matrix("obs" , rows(fullcombinations)) stata("clear" ) st_addvar("str100" , "industry1" ) st_addvar("str100" , "industry2" ) st_addvar("str100" , "value" ) stata("set obs `=obs[1,1]'" ) st_sstore(., ("industry1" , "industry2" , "value" ), fullcombinations) end destring , replace compress collapse (sum ) value, by (industry1 industry2)save tempdata5, replace
同样,这个结果也并不是平衡面板(对称矩阵),下面还会需要再处理下。不过我们先统计下数字和实体行业的数量:
use tempdata5, clear replace value = 0 if industry1 == industry2gen id = _ndrop valuegather industry1 industry2 keep valueduplicates drop _all, forcesave tempdata5a, replace ren value 国民经济行业代码save tempdata5b, replace merge 1:m 国民经济行业代码 using 产业数实分类keep if _m == 3drop _mcontract 类别ren 类别 class save 融合的各产业数量, replace list
处理平衡:
use tempdata5a, clear cross using tempdata5bren value industry1ren 国民经济行业代码 industry2merge 1:1 industry1 industry2 using tempdata5replace value = 0 if mi (value)drop _mreplace value = 0 if industry1 == industry2save tempdata5c, replace ren industry1 tempren industry2 industry1ren temp industry2ren value value2merge 1:1 industry1 industry2 using tempdata5cegen value3 = rowmax(value value2)replace value = value3drop value2 _m value3replace value = 0 if industry1 == industry2
匹配上行业类别再分类计算总数量:
ren industry1 国民经济行业代码recast str5 国民经济行业代码merge m :1 国民经济行业代码 using 产业数实分类keep if _m == 3drop _mren 国民经济行业代码 industry1ren 类别 class1ren industry2 国民经济行业代码recast str5 国民经济行业代码merge m :1 国民经济行业代码 using 产业数实分类keep if _m == 3drop _mren 国民经济行业代码 industry2ren 类别 class2drop if value == 0collapse (sum ) value , by (class1 class2)save tempdata6, replace
最后就可以计算数实、数数和实实融合水平了:
use tempdata6, clear ren class1 class merge m :1 class using 融合的各产业数量drop _mren _freq freq1ren class class1ren class2 class merge m :1 class using 融合的各产业数量drop _mren _freq freq2ren class class2collapse (sum ) value (first) freq1 (first) freq2, by (class1 class2)gen RH = value / (freq1 * freq2)gen class = "数实融合" if class1 != class2replace class = "数数融合" if class1 == "数字经济产业" & class2 == "数字经济产业" replace class = "实实融合" if class1 == "实体产业" & class2 == "实体产业" keep class RHduplicates drop _all, forcelist
不过计算结果和原论文中的有些差异,令人费解。不过考虑到数字经济产业并不多,所以数实融合的专利不会很多,因此数实融合水平应该也不会很高。这个结果应该是合理的。
如果想分城市,只需要循环使用各城市的专利数据计算即可;如果想要分类别(农业、建筑业、制造业、建筑业及其他工业),只需要最后在分组计算的时候使用这个类别变量即可。
点击这里跳转到 RStata 短书平台获取附件:旧版本|名师讲堂|使用 Stata 测算数实融合水平
评论