名师讲堂|使用 Stata 测算数实融合水平(三):分城市、产业计算数实融合

上次课程主页:https://rstata.duanshu.com/#/brief/course/427ec490765843a99825d3c412fff61e

继续上次的内容,今天我们再来学习如何基于真实专利数据分城市、产业计算数实融合水平。

分城市

由于需要分城市,所以在保留变量的时候需要保留城市变量:

use patent_small1/2012, clear
*> (数据处理:微信公众号 RStata)
list newipzlid IPC 市 in 1/10
*> +---------------------------------------------+
*> | newipzlid IPC 市 |
*> |---------------------------------------------|
*> 1. | 20120919840 A47C27/08 上海市 |
*> 2. | 20121507067 G02B5/30; G03F7/20 上海市 |
*> 3. | 20120134343 H05H7/18 上海市 |
*> 4. | 20120077260 G01B11/00 上海市 |
*> 5. | 20121693818 A61B5/055 上海市 |
*> |---------------------------------------------|
*> 6. | 20120132658 B65B21/24; B65B53/02 上海市 |
*> 7. | 20120114926 G01M13/00 上海市 |
*> 8. | 20120175154 B21K7/12 上海市 |
*> 9. | 20120029517 F22B1/28 上海市 |
*> 10. | 20121129726 E06B7/08 上海市 |
*> +---------------------------------------------+

按照同样的方式进行初步处理:

drop if !index(IPC, ";")
*> (541,410 observations deleted)
drop if mi(市)
*> (0 observations deleted)
keep newipzlid IPC 市
replace IPC = subinstr(IPC, " ", "", .)
*> (436,621 real changes made)
egen ipc = ipc_sub00(IPC), parse(;) choose(10)
drop IPC
ren 市 city
save ipcdata, replace
*> file ipcdata.dta saved

然后就可以分城市计算了:

clear all
use ipcdata, clear
contract city ipc
qui do matacode2.do

这里 matacode2.do 的代码如下:

此处代码需下载讲义材料查看~

采取的方法是先循环城市,每次处理一个城市的数据。结果都保存到 csv 文件里面。

读取结果:

import delimited using "res3.csv", clear
*> (encoding automatically selected: UTF-8)
*> (4 vars, 3,799,040 obs)
ren (v1 v2 v3 v4) (city ipc1 ipc2 value)
destring, replace
*> city: contains nonnumeric characters; no replace
*> ipc1: contains nonnumeric characters; no replace
*> ipc2: contains nonnumeric characters; no replace
*> value already numeric; no replace
compress
*> (0 bytes saved)
collapse (sum) value, by(city ipc1 ipc2)
drop if ipc1 == ipc2
*> (309,263 observations deleted)
save ipcmat, replace
*> file ipcmat.dta saved
list in 1/10
*> +------------------------------------------+
*> | city ipc1 ipc2 value |
*> |------------------------------------------|
*> 1. | 七台河市 A01C5/06 A01C7/06 1 |
*> 2. | 七台河市 A01C5/06 A01C7/20 1 |
*> 3. | 七台河市 A01C5/06 A01G25/09 1 |
*> 4. | 七台河市 A01C7/06 A01C5/06 1 |
*> 5. | 七台河市 A01C7/06 A01C7/20 1 |
*> |------------------------------------------|
*> 6. | 七台河市 A01C7/20 A01C5/06 1 |
*> 7. | 七台河市 A01C7/20 A01C7/06 1 |
*> 8. | 七台河市 A01D41/12 A01F12/22 1 |
*> 9. | 七台河市 A01F11/06 A01F12/18 1 |
*> 10. | 七台河市 A01F11/06 A01F12/44 1 |
*> +------------------------------------------+

后面的代码和前面的基本一样,就是分组的时候记得加上 city 的

*- 匹配行业小类
use ipcmat, clear
ren ipc1 uniq_ipc
joinby uniq_ipc using 专利数据与行业小类代码简易对照表.dta, unmatched(master)
drop _m uniq_ipc
ren 国民经济行业代码 industry1
ren ipc2 uniq_ipc
joinby uniq_ipc using 专利数据与行业小类代码简易对照表.dta, unmatched(master)
drop _m uniq_ipc
ren 国民经济行业代码 industry2
drop if mi(value)
*> (0 observations deleted)
collapse (sum) value, by(city industry1 industry2)
replace value = 0 if industry1 == industry2
*> (99,538 real changes made)
drop if mi(industry1) | mi(industry2)
*> (23,812 observations deleted)
save industrymat, replace
*> file industrymat.dta saved
*- 计算数实融合
use industrymat, clear
ren industry1 国民经济行业代码
recast str5 国民经济行业代码
merge m:1 国民经济行业代码 using 产业数实分类
*> (variable 国民经济行业代码 was str5, now str24 to accommodate using data's values)
*> Result Number of obs
*> -----------------------------------------
*> Not matched 0
*> Matched 4,963,532 (_merge==3)
*> -----------------------------------------
keep if _m == 3
*> (0 observations deleted)
drop _m
ren 国民经济行业代码 industry1
ren 类别 class1
ren industry2 国民经济行业代码
recast str5 国民经济行业代码
merge m:1 国民经济行业代码 using 产业数实分类
*> (variable 国民经济行业代码 was str5, now str24 to accommodate using data's values)
*> Result Number of obs
*> -----------------------------------------
*> Not matched 0
*> Matched 4,963,532 (_merge==3)
*> -----------------------------------------
keep if _m == 3
*> (0 observations deleted)
drop _m
ren 国民经济行业代码 industry2
ren 类别 class2
drop if value == 0
*> (99,362 observations deleted)
bysort class1: egen freq1 = nvals(industry1)
bysort class2: egen freq2 = nvals(industry2)
collapse (sum) value (first) freq1 (first) freq2, by(city class1 class2)
gen RH = value / (freq1 * freq2)
gen class = "数实融合" if class1 != class2
*> (648 missing values generated)
replace class = "数数融合" if class1 == "数字经济产业" & class2 == "数字经济产业"
*> (308 real changes made)
replace class = "实实融合" if class1 == "实体产业" & class2 == "实体产业"
*> (340 real changes made)
keep city class RH
duplicates drop _all, force
*> Duplicates in terms of city RH class
*> (329 observations deleted)
spread class RH
gsort -数实融合
list in 1/10
*> +-----------------------------------------+
*> | city 实实融合 数实融合 数数融合 |
*> |-----------------------------------------|
*> 1. | 深圳市 9.063931 34.77193 593.2258 |
*> 2. | 北京市 15.93919 29.50866 553.7612 |
*> 3. | 上海市 16.33119 16.85768 257.215 |
*> 4. | 苏州市 16.56216 10.55055 97.09481 |
*> 5. | 杭州市 6.971819 7.677135 111.6445 |
*> |-----------------------------------------|
*> 6. | 东莞市 3.583695 4.598668 57.16273 |
*> 7. | 南京市 4.514598 4.431203 69.5993 |
*> 8. | 成都市 3.482744 4.392675 59.55687 |
*> 9. | 无锡市 8.37444 4.348913 54.70042 |
*> 10. | 广州市 5.164196 4.312918 64.06716 |
*> +-----------------------------------------+

分行业计算数实融合

分行业就不需要重新构造 IPC 融合矩阵了,而是直接在分行业计算那里替换上更细致的行业分类:

use 国民经济分类与IPC分类号对照表.dta, clear
*> (数据处理:微信公众号 RStata)
keep 国民经济行业代码
duplicates drop 国民经济行业代码, force
*> Duplicates in terms of 国民经济行业代码
*> (17,178 observations deleted)
gen industry2 = substr(国民经济行业代码, 2, .)
merge 1:1 industry2 using 数字经济核心产业.dta
*> Result Number of obs
*> -----------------------------------------
*> Not matched 769
*> from master 732 (_merge==1)
*> from using 37 (_merge==2)
*> Matched 89 (_merge==3)
*> -----------------------------------------
tab _m
*> Matching result from |
*> merge | Freq. Percent Cum.
*> ------------------------+-----------------------------------
*> Master only (1) | 732 85.31 85.31
*> Using only (2) | 37 4.31 89.63
*> Matched (3) | 89 10.37 100.00
*> ------------------------+-----------------------------------
*> Total | 858 100.00
drop if _m == 2
*> (37 observations deleted)
drop _m
merge m:1 国民经济行业代码 using 实体产业分类.dta
*> Result Number of obs
*> -----------------------------------------
*> Not matched 0
*> Matched 821 (_merge==3)
*> -----------------------------------------
tab _m
*> Matching result from |
*> merge | Freq. Percent Cum.
*> ------------------------+-----------------------------------
*> Matched (3) | 821 100.00 100.00
*> ------------------------+-----------------------------------
*> Total | 821 100.00
drop industry2 _m
keep 国民经济行业代码 数字经济产业 实体产业
gen class = (!mi(数字经济产业))
duplicates drop _all, force
*> Duplicates in terms of 国民经济行业代码 数字经济产业 实体产业 class
*> (0 observations are duplicates)
gen 类别 = "数字经济产业" if !mi(数字经济产业)
*> (732 missing values generated)
replace 类别 = 实体产业 if mi(类别)
*> variable 类别 was str18 now str24
*> (732 real changes made)
keep 国民经济行业代码 类别
duplicates drop _all, force
*> Duplicates in terms of 国民经济行业代码 类别
*> (0 observations are duplicates)
save 产业数实分类2, replace
*> file 产业数实分类2.dta saved
list in 1/10
*> +----------------+
*> | 国民~码 类别 |
*> |----------------|
*> 1. | A0111 农业 |
*> 2. | A0112 农业 |
*> 3. | A0113 农业 |
*> 4. | A0119 农业 |
*> 5. | A0121 农业 |
*> |----------------|
*> 6. | A0122 农业 |
*> 7. | A0123 农业 |
*> 8. | A0131 农业 |
*> 9. | A0132 农业 |
*> 10. | A0133 农业 |
*> +----------------+

然后计算:

此处代码需下载讲义材料查看~

这样就实现了分产业的计算。

点击这里跳转到 RStata 短书平台获取附件:名师讲堂|使用 Stata 测算数实融合水平(三):分城市、产业计算数实融合

评论