stata怎么做时间序列
2019-05-11
//lecture 9
//绘值散点图并添加图例:
scatter le year, c(l) xlabel(1990 1918 1940(20)2000, grid) legend(on)
//显示图例(自带的label,并未修改)
//le 变量的标签 图例是变量的标签,所以修改图例可以先修改标签
//不改变真正的label
scatter le_male le_female year, c(l) legend(label(1 "Male") label(2 "Female"))
//绘值le_m、le_f 散点图并添加图例,将图例分别改成“male”and“female”
scatter le_male le_female year, c(l) legend(on order(1 "male" 2 "female"))
//和上一行一样
twoway (line le_male year, lpattern(dash)) (line le_female year, lcolor(red) lpattern(dot)) (line le_w year, lcolor(green)), legend (on order(1 "Male" 2 "Female" 3 "White") col(1) ring(0) pos(4) title("标题") subtitle("子标题"))
//twoway(将几张图画在同一个里面):展示第一个图是什么(),第二是什么()第三个是什么。。。。。,用括号的形式展示出这个图形是什么,lpattern是线的形式 dash是虚线 (默认为实线),dot是点,lcolor是线的颜色
//其中pattern必须改 因为打印出黑白分不清颜色
//pos(4)是把图例放在四点钟方向(右下角)默认为六点钟方向, ring(0)是放在图形里面 col()是一行放几个元素
sysuse "auto.dta",clear
scatter mpg weight
//散点图
twoway(scatter mpg weight if foreign == 0)(scatter mpg weight if foreign == 1) , legend(on order(1 "国产车" 2 "进口车"))
scatter mpg weight, by(foreign)
//以foreign,domestic作区分 与twoway不同,这个是分别独立做图 散点图
scatter mpg weight, by(foreign, total)
//并添加一张包含所有观测值
scatter mpg weight, by(foreign, total rows(1))
//将绘制的图形改为一行排列
scatter mpg weight, by(foreign, total holes(3))
//将绘制图形的留白放在左下方
scatter mpg weight, by(foreign,total title("My Title"))
//将图形添加一个全标题
scatter mpg weight, subtitle(" ") by(foreign, total title("my title"))
//将每个小图的标题去掉
scatter mpg weight, subtitle(ring(0) pos(12) nobexpand) by(foreign, total title("my title"))
//将每个图的小标题位置更改
//如果foreign变量没有标签怎么办?
label drop origin
scatter mpg weight, by(foreign, total title("My Title"))
label define origin_v 0 "国产" 1 "进口"
label values foreign origin_v
scatter mpg weight, by(foreign, total title("My title"))
twoway (scatter mpg weight if foreign == 0) (scatter mpg weight if foreign == 1, msymbol(x)),legend(order(1 "国产" 2 "进口"))
scatter mpg weight || lfit mpg weight , by(foreign, row(1))
//绘制散点和线性复合图形,根据foreign分组,添加一个总体图 排成一行
scatter mpg weight || lfit mpg weight, legend(cols(1) ring(0)) by(foreign, legend(pos(4)))
//将图例变为一列显示,放置四点钟方向
scatter mpg weight || lfit mpg weight ||,legend(rows(1)) by(foreign, total legend(at(3) pos(0)))
//将图例放在2x2的留白处
scatter mpg weight || lfit mpg weight ||, by(foreign, total legend(off))
//将图例放在2x2的留白处
//scheme选项
help scheme
//轴线选择选项
help axis_choice_options
//example
sysuse auto,clear
scatter mpg price weight
//绘制散点图,要求两个y轴变量共用y轴
twoway (scatter mpg weight) (scatter price weight)
//一样
twoway (scatter mpg weight) (scatter price weight, yaxis(2))
//绘制散点图,并使用两个y轴
twoway ( scatter mpg weight ) (scatter price weight, yaxis(2)), xlabel(1000(500)5000) ytick(#10, axis(2)) ylabel(#8, axis(1))
//每一个坐标轴添加刻度和标识,x轴大约10个刻度,左边的y轴大约8个刻度,右边的大约10个刻度
//添加轴线选项
help added_line_options
//图形保存选项
sysuse auto,clear
help area
sysuse gnp96, clear
graph twoway area d.gnp96 date
//时间序列数据
gen dev_gnp = gnp96 - l.gnp96
twoway(line gnp96 date) (area d.gnp96 date, yaxis(2))
twoway(line gnp96 date) (area dev_gnp date, yaxis(2))
twoway(line gnp96 date) (line dev_gnp date, lpattern(dot) lcolor(1))
//绘制如下复杂图:
graph use area_gnp
sysuse gnp96,clear
#delimit ;
twoway area d.gnp96 date, xlabel(#20, angle(90))
ylabel(-100(50)200,angel(0))
ytitle("Billions of 1996 Dollars")
xtitle("")
subtitle("Change in US GNP",pos(11))
note("Source: US Department of Commerce, Bureau of Economics")
;
#delimit cr
//换行功能换回回车
graph save area_gp, replace
//lecture 10
sysuse lifeexp, clear
help histogram
//直方图📊,主要绘制连续变量,密度图,还要乘以宽度才是频数
twoway histogram le
//le是lexp的简称,直方图描述从多少到多少有多少个
//by 在有0,1变量时候可以用
twoway hist le, bin(10)
twoway hist le, bin(5)
//设置几个柱子
twoway hist le, width(5)
//设置柱子的宽度
twoway hist le, gap(10)
//柱子之间的间隙
twoway hist le, horizontal
//横过来,水平直方图
twoway hist le, percent
//以百分比形式显示
help graph bar
help graph hbar
//条图,主要绘制离散变量
sysuse citytemp, clear
graph bar (mean) tempjuly tempjan
graph bar tempjuly tempjan, over(region)
//默认设置为均值,按照地区(东南西北)划分
graph bar tempjuly tempjan, over(region) bargap(30)
//两个柱状之间的位置可以调,可以重叠,也可以分开一点 -30代表重叠 30代表分开
graph bar tempjuly tempjan, over(region) stack
//可以把两个变量堆积在一起,一般堆积的是个数 均值没啥意义
graph bar tempjuly tempjan, over(division)
//数据中有两个分类变量,可以根据division再分类
tab division if region == 1
tab division if region == 2
graph bar tempjan, over(division) over(region)
//每一个region下又分几个division
graph bar tempjuly tempjan, over(region, gap(10))
//条与条之间的间隔
graph bar tempjuly tempjan, over(region, descending)
//数据中west是4 所以按4321排列
graph bar tempjuly tempjan, by(region)
//按region分别绘制四个图
//图形保存选项:
sysuse auto,clear
scatter mpg price weight
graph save fig1, replace
graph use fig1
//读取已存储图形
graph export fig1.png, replace
//运行时要保证scatter mpg price weight 这个图是打开的 否则运行不了
cd /Victor/stata
//电子地图:
findit spmap
help spmap
unicode encoding set gb18030
unicode translate "china_label.dta"
//必须先清零数据,然后运行一遍路径名 才能运行这两行命令
use "china_label.dta", clear
//example 1
use china_label, clear
gen xx = uniform()
spmap xx using "china_map.dta", id(id) title("中国地图",size(*0.8)) label(label(ename) xcoord(x_coord) ycoord(y_coord) size(*.8)) plotregion(icolor(stone)) graphregion(icolor(stone)) fc(Greens) clnumber(8) oc(white ..) osize(medthin ..)
//clnumbers 代表8种不同的绿色
//example 2
tab name
replace name = subinstr(name, "省", "", .)
replace name = subinstr(name, "市", "", .)
replace name = subinstr(name, "回族自治区", "", .)
replace name = subinstr(name, "壮族自治区", "", .)
replace name = subinstr(name, "特别行政区", "", .)
replace name = subinstr(name, "自治区", "", .)
replace name = subinstr(name, "维吾尔", "", .)
tab name
//改名字
foreach x of numlist 1/5{
gen num`x'=uniform()
}
//产生0到1的随机数
format x %9.3g
foreach x of numlist 1/5{
spmap `x' using "china_map.dta",id(id) title("中国地图", size(*0.8)) label(label(ename) xcoord(x_coord) ycoord(y_coord) size(*.8)) plotregion(icolor(stone)) graphregion(icolor(stone)) fc(Greens) clnumber(8) oc(white ..) osize(medthin ..) graph export "china0`x'.png", replace
}
//lecture 11
sysuse auto, clear
summarize mpg weight
//summarize 后面可以接一个或多个变量,个数 均值 最小最大值
summarize mpg, detail
//会有关于数据其他的统计指标
help summarize
tabulate mpg, sort
//离散变量排序之后用表统计
tabulate foreign
//最好是分类变量去tabulate,展示各个种类有多少个,占多大比例(离散的)
help tabulate
sysuse nlsw88, clear
tab occ
//不同职业的样本在我的数据库里面分别有多少个,比例大小,总的样本数量是多少
tab industry
sysuse auto, clear
tabstat mpg price weight rep78 , stat(n mean sd min median max) c(s)
//c(s)是转置过来这个矩阵,默认阅读方式是:列是统计指标,行是变量名称
help tabstat
//下划线是代表可以简写,只写c(s)
//可以规定format 总长度多少个单位,小数点前面,后面有多少个单位,统一成一个格式
tabstat mpg price weight rep78 , by(foreign) stat(n mean sd min median max) c(s)
//by是以什么分类展示
//输出表格(不要复制):
ssc install logout
logout, save(summarize) tex word excel dec(3) replace: tabstat mpg price weight rep78 , stat(n mean sd min median max) column(s) long format
//不建议导出成tex word 因为在Excel还要进一步编辑,xml格式的可以在excel打开 rtf是可以从word打开 就可以应用在论文里面了。replace替换原来的 dec(3)代表小数点后统一保留三位数,replace后面与之前一模一样 ,column是列
logout, save(summarize) tex word excel dec(3) replace: tabstat mpg price weight rep78 , by(foreign) stat(n mean sd min median max) c(s)
//save(文件名)
use nei_sample.dta, clear
describe
duplicates tag newid year, gen(dup)
edit newid year if dup >= 195
duplicates drop newid year, force
help merge
duplicates drop newid year, force
//一个地方会有n个企业
merge m:1 fips year using "county_na.dta"
//根据county的代码和时间调用
//有三部分的merge,merge=1和2是不需要的地方 只保留3(matched) 因为没有企业的观测值(0),而merge=1则是有企业的观测值(1),而merge=2没有政策的观测值(0)(观测到了企业污染,却没有观察到关于政策的变量)
//我们关心企业所在的地区是否有环境政策
//做一个最简单的回归,政策对污染的影响:(regress)
foreach v of varlist reg_* {
replace `v'= 0 if `v' == .
}
//将missing变成0
reg co reg_co
gen lco = ln(co)
reg lco reg_co
//有0的问题
//add a set of dummies(虚拟变量), tear , industry, county
gen fips_st = substr(fips,1,2)
//state(截取fips编号的前两位)
gen sic2 = substr(sic,1,2)
//industry
gen sic1 = substr(sic2,1,1)
keep if sic1 == "2" | sic1 == "3"
//manufacturing only 或
gen lco = log(co)
//generate log
reg lco reg_co
//reg_co代表政府有无监管,有就是1(非常不准)表中的_cons代表截距
xi: reg lco reg_co i.year
//按照年份,每年加一个虚拟变量,是这一年就是一
//with year FE (根据每一年不一样回归 )
//年份前的系数是相对于基年的增加或减少,添加年份虚拟变量
//分离出宏观经济冲击的影响
bys year: egen id_sum = count(newid)
//?
xi : reg lco reg_co id_sum i.year
//with year FE, multicolinearity
//如果观测值是1996年的,那么iyear1996=1,这个统一的因素会影响所有的企业(宏观经济因素,所有企业都受影响),今年的这个企业和明年的这个企业外部环境是不一样的,是什么不重要,要capture这个东西
xi : reg lco reg_co i.year i.sic2
//with industry FE(不同产业的影响)
xi : reg lco reg_co i.year i.sic2 i.fips_st
//with state FE(省政府对环境保护的压力的影响)
xi : reg lco reg_co i.year i.sic2 i.fips
//with county FE
duplicates drop newid year, force
xtset newid year
//set panel
xi: reg lco reg_co i.newid
//通过添加dummy
xi: xtreg lco reg_co, fe
//先进行差分 (常用)
//这两行的结果相同
xi: xtreg lco reg_co i.year , fe
//year
xi: xtreg lco reg_co i.year i.fips_st, fe
//state fe
xi: xtreg lco reg_co i.year i.sic2, fe
//industry fe
//下标都是固定效益 用希腊字母带下标 c是位置 j是行业 t为第t年的宏观经济形势/技术进步(系统性) i表示企业自身的固定效益,是观察不到的个体特征因素(有些企业管理水平天生高,低)
sort newid sic2
by newid: gen newsic2 = sic2[_N]
xi: xtreg lco reg_co i.newsic2, fe
//企业不更改行业属性
//two-way fised effects with firm fixed effects
xi:xtreg lco reg_co i.teay*i.newsic2, fe
//industry-year FE
xi:xtreg lco reg_co i.teay*i.fips_st, fe
findit outreg2
//默默地回归quite
qui xi: xtreg lco reg_co i.newsic2,fe
outreg2 using result1.xls,excel keep(reg_co) dec(3) addtext(Firm FE, Y,Stata-Year FE,n,Industry-Year FE,n)
//keep只保留关心的系数,小数点后面保留多少位
//数据批量导入与导出,乱码处理,变量构造,数据的整理,数据变量的修改,相互转换
//数据可视化,在统计中summary statistic table 然后在fixeffect model 简单地看了一下
//将回归结果用excel报告