专栏名称: momo
Stata+R,从入门到提高
目录
相关文章推荐
白鲸出海  ·  百度完成对YY直播的收购,Anthropic ... ·  16 小时前  
海外独角兽  ·  OpenAI 都在用的 AI 招聘,2 ... ·  17 小时前  
阿里开发者  ·  select `*` from table ... ·  20 小时前  
白鲸出海  ·  中国互联网出海一周头条 ... ·  2 天前  
51好读  ›  专栏  ›  momo

Stata学习:如何构建中外政治关系指数变量?

momo  · 知乎专栏  ·  · 2023-10-04 21:39

正文

Stata学习

  • 网页型数据的清洗

文献来源

  1. Du, Y., Ju, J., Ramirez, C. D., & Yao, X. (2017).Bilateral trade and shocks in political relations: Evidence from China and some of its major trading partners, 1990–2013. Journal of International Economics, 108, 211–225.

数据来源

调整至“适合页面”,逐页右键“Web 选择”,将赋值的内容直接粘贴到do文件( 中外关系raw.do )中。

清洗数据

clear
input strL a 
*
* 这里省略了贴入的数据部分3-16074
*
end 
compress
g f = substr(a,1,1) == "-" 
g g = substr(a,1,1) == "."
g n = _n
tsset n
g       b = a+a[_n+1] if f == 1 
replace b = a+a[_n-1] if g == 1 & mi(b)
g Lf = L.f
drop if Lf == 1
g Fg = F.g
drop if Fg == 1
replace b = a if mi(b)
drop a *f *g 
g c = b
destring c, force replace
g y = c if c > 1900
g i = !mi(y)
carryforward y, replace
g m = 0 if i == 1
replace  m = m[_n-1]+1 if mi(m)
drop if c == y
drop i
cap drop r
g r = 1 if c == 1
replace r = r[_n-1]+1 if !mi(r[_n-1]) & r[_n-1] <= 11
g w = b if regexm(b,"关系")
carryforward w, replace
drop if c == r
drop r
ren c PRI
ren y year
ren m month 
ren w var 
keep P y m v
drop if mi(v)
drop if m > 12
replace v = subinstr(v,"月中","",.)
replace v = subinstr(v,"关系分值","",.)
compress
g q = m
tostring y m, replace
replace m = "0"+m if q < 10
g YearMon = y+"-"+m
drop y m q
duplicates drop v Y, force 
cap pr drop A
pr def A
args a b
replace v = "`a'" if regexm(v,"`b'")
end A
A E 
A N 
A Y 
A B 
A D 
A R 
A F 
A O 
A M 
A I 
A U 
A H 
spread v P
cap pr drop A
pr def A
args a b
ren `a' `b'
end A
A E 
A N 印尼
A Y 
A B 
A D 
A R 
A F 
A O 
A M 
A I 
A U 
A H 
foreach v of var -{
la var `v' "中`v'关系分值"
}
g year = substr(Y,1,4)
g month = substr(Y,6,2)
destring y m, force replace
compress
save 中外政治关系指数, replace
d
ta y
mkdensity -

得到结果

  • 注:巴=巴基斯坦
Contains data from 中外政治关系指数.dta
 Observations:           849                  
    Variables:            15                  4 Oct 2023 21:35
------------------------------------------------------------------------------------------
Variable      Storage   Display    Value
    name         type    format    label      Variable label
------------------------------------------------------------------------------------------
YearMon         str7    %9s                   
巴              double  %10.0g                中巴关系分值
德              double  %10.0g                中德关系分值
俄              double  %10.0g                中俄关系分值
法              double  %10.0g                中法关系分值
韩              double  %10.0g                中韩关系分值
英              double  %10.0g                中英关系分值
美              double  %10.0g                中美关系分值
印尼            double  %10.0g                中印尼关系分值
澳              double  %10.0g                中澳关系分值
日              double  %10.0g                中日关系分值
越              double  %10.0g                中越关系分值
印              double  %10.0g                中印关系分值
year            int     %10.0g                
month           byte    %10.0g                
------------------------------------------------------------------------------------------
Sorted by: YearMon

. ta y

       year |      Freq.     Percent        Cum.
------------+-----------------------------------
       1950 |         12        1.41        1.41
       1951 |         12        1.41        2.83
       1952 |         12        1.41        4.24
       1953 |         12        1.41        5.65
       1954 |         12        1.41        7.07
       1955 |         12        1.41        8.48
       1956 |         12        1.41        9.89
       1957 |         12        1.41       11.31
       1958 |         12        1.41       12.72
       1959 |         12        1.41       14.13
       1960 |         12        1.41       15.55
       1961 |         12        1.41       16.96
       1962 |         12        1.41       18.37
       1963 |         12        1.41       19.79
       1964 |         12        1.41       21.20
       1965 |         12        1.41       22.61
       1966 |         12        1.41       24.03
       1967 |         12        1.41       25.44
       1968 |         12        1.41       26.86
       1969 |         12        1.41       28.27
       1970 |         12        1.41       29.68
       1971 |         12        1.41       31.10
       1972 |         12        1.41       32.51
       1973 |         12        1.41       33.92
       1974 |         12        1.41       35.34
       1975 |         12        1.41       36.75
       1976 |         12        1.41       38.16
       1977 |         12        1.41       39.58
       1978 |         12        1.41       40.99
       1979 |         12        1.41       42.40
       1980 |         12        1.41       43.82
       1981 |         12        1.41       45.23
       1982 |         12        1.41       46.64
       1983 |         12        1.41       48.06
       1984 |         12        1.41       49.47
       1985 |         12        1.41       50.88
       1986 |         12        1.41       52.30
       1987 |         12        1.41       53.71
       1988 |         12        1.41       55.12
       1989 |         12        1.41       56.54
       1990 |         12        1.41       57.95
       1991 |         12        1.41       59.36
       1992 |         12        1.41       60.78
       1993 |         12        1.41       62.19
       1994 |         12        1.41       63.60
       1995 |         12        1.41       65.02
       1996 |         12        1.41       66.43
       1997 |         12        1.41       67.84
       1998 |         12        1.41       69.26
       1999 |         12        1.41       70.67
       2000 |         12        1.41       72.08
       2001 |         12        1.41       73.50
       2002 |         12        1.41       74.91
       2003 |         12        1.41       76.33
       2004 |         12        1.41       77.74
       2005 |         12        1.41       79.15
       2006 |         12        1.41       80.57
       2007 |         12        1.41       81.98
       2008 |         12        1.41       83.39
       2009 |         12        1.41       84.81
       2010 |         12        1.41       86.22
       2011 |         12        1.41       87.63
       2012 |         12        1.41       89.05
       2013 |         12        1.41       90.46
       2014 |         12        1.41       91.87
       2015 |         12        1.41       93.29
       2016 |         12        1.41       94.70
       2017 |         12        1.41       96.11
       2018 |         12        1.41       97.53
       2019 |         12        1.41       98.94
       2020 |          9        1.06      100.00
------------+-----------------------------------
      Total |        849      100.00






请到「今天看啥」查看全文