1 Star 1 Fork 0

张剑/COVID-19-and-AQI-in-China

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
picture.do 31.62 KB
一键复制 编辑 原始数据 按行查看 历史
张剑 提交于 2020-05-27 16:24 . no message
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
*** 新冠疫情冲击对于中国主要城市空气质量的影响
*** 请使用graph combine命令将画的图整合一下,文件位置写到自己的目录下
** Stata Code:1
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
! del *.png
* 删除所有后缀名以png命名的文件
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找平均PM2.5前5的城市并转换数据结构
bys City: egen pm25_avg = mean(pm25)
tab pm25_avg
drop if pm25_avg < 89.7
// 把PM2.5均值前5的城市保留
keep city year pm25 pm10 t
keep if t != .
destring year, replace
reshape wide pm25 pm10, i(city t) j(year)
// 把数据结构转换为宽型数据
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "乌鲁木齐 济南 石家庄 西安 郑州"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm252018 t, lpattern(solid) lcolor(black*0.4))
(line pm252019 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm252020 t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("PM2.5变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'.png, replace
}
//循环绘制PM2.5前5的城市图
** Stata Code: 2
**====================================================================================
**第一步 设置相对时间
cd C:\Users\jefeer\Documents\我的坚果云\工作数据
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找平均PM2.5前5的城市并转换数据结构
bys City: egen industry_avg = mean(ind_2)
tab industry_avg
drop if industry_avg < 0.44
// 把PM2.5均值前5的城市保留
keep city year pm25 pm10 t
keep if t != .
destring year, replace
reshape wide pm25 pm10, i(city t) j(year)
// 把数据结构转换为宽型数据
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "南昌 合肥 郑州 银川 长春"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm252018 t, lpattern(solid) lcolor(black*0.4))
(line pm252019 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm252020 t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("PM2.5变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'ind.png, replace
}
//循环绘制PM2.5前5的城市图
** Stata Code: 3
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找平均PM2.5前5的城市并转换数据结构
bys City: egen pm25_avg = mean(pm25)
tab pm25_avg
drop if pm25_avg < 89.7
// 把PM2.5均值前5的城市保留
keep city year pm25 pm10 t
keep if t != .
destring year, replace
reshape wide pm25 pm10, i(city t) j(year)
// 把数据结构转换为宽型数据
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "乌鲁木齐 济南 石家庄 西安 郑州"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm102018 t, lpattern(solid) lcolor(black*0.4))
(line pm102019 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm102020 t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("PM10变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_3.png, replace
}
//循环绘制PM2.5前5的城市图
** Stata Code: 4
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找平均PM2.5前5的城市并转换数据结构
bys City: egen industry_avg = mean(ind_2)
tab industry_avg
drop if industry_avg < 0.44
// 把PM2.5均值前5的城市保留
keep city year pm25 pm10 t
keep if t != .
destring year, replace
reshape wide pm25 pm10, i(city t) j(year)
// 把数据结构转换为宽型数据
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "南昌 合肥 郑州 银川 长春"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm102018 t, lpattern(solid) lcolor(black*0.4))
(line pm102019 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm102020 t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("PM10变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_4.png, replace
}
//循环绘制PM2.5前5的城市图
** Stata Code: 5
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找平均PM2.5前5的城市并转换数据结构
bys City: egen pm25_avg = mean(pm25)
tab pm25_avg
drop if pm25_avg < 89.7 & city != "武汉"
// 把PM2.5均值前5的城市保留
keep city year so2 t
keep if t != .
destring year, replace
reshape wide so2, i(city t) j(year)
// 把数据结构转换为宽型数据
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "乌鲁木齐 济南 石家庄 西安 郑州 武汉"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line so22018 t, lpattern(solid) lcolor(black*0.4))
(line so22019 t, lpattern(dash_dot) lcolor(black*0.6))
(line so22020 t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("SO2")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("SO2变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_5.png, replace
}
//循环绘制PM2.5前5的城市图
** Stata Code: 6
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找平均PM2.5前5的城市并转换数据结构
bys City: egen industry_avg = mean(ind_2)
tab industry_avg
drop if industry_avg < 0.44
// 把PM2.5均值前5的城市保留
keep city year so2 t
keep if t != .
destring year, replace
reshape wide so2, i(city t) j(year)
// 把数据结构转换为宽型数据
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "南昌 合肥 郑州 银川 长春"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line so22018 t, lpattern(solid) lcolor(black*0.4))
(line so22019 t, lpattern(dash_dot) lcolor(black*0.6))
(line so22020 t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("SO2")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("SO2变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_6.png, replace
}
//循环绘制PM2.5前5的城市图
** Stata Code: 7
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 换数据结构
bys City: egen pm25_avg = mean(pm25)
tab pm25_avg
drop if pm25_avg < 89.7
// 把PM2.5均值前5的城市保留
keep city year pm25 t
keep if t != .
destring year, replace
reshape wide pm25, i(city t) j(year)
// 把数据结构转换为宽型数据
gen diff = pm252020 - pm252019
**====================================================================================
**第三步 绘图
tab city
local Hcity "乌鲁木齐 济南 石家庄 西安 郑州"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (qfitci diff t, stdf ciplot(rline))(scatter diff t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5差值(2020年-2019年)")
yline(0 , lpattern(solid) lcolor(black*0.5))
ylabel(-500(500) 500)
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("PM2.5差值变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_7.png, replace
}
//循环绘图
** Stata Code: 8
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 寻找第二产业占比前5的城市并转换数据结构
bys City: egen industry_avg = mean(ind_2)
tab industry_avg
drop if industry_avg < 0.44
keep city year pm25 t
keep if t != .
destring year, replace
reshape wide pm25, i(city t) j(year)
// 把数据结构转换为宽型数据
gen diff = pm252020 - pm252019
**====================================================================================
**第三步 寻找平均PM2.5前5的城市
tab city
local Hcity "南昌 合肥 郑州 银川 长春"
//根据tab结果定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (qfitci diff t, stdf ciplot(rline))(scatter diff t, lpattern(solid) lcolor(red*1.0)) if city == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5差值(2020年-2019年)")
yline(0 , lpattern(solid) lcolor(black*0.5))
ylabel(-500(500) 500)
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("PM2.5差值变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export A`v'_8.png, replace
}
//循环绘图
** Stata Code: 9
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
bys City: egen virus = max(comfirm)
xtile comf = virus, nq(3)
// 按照地区确诊人数分类,分成三级,1——轻度感染,2——中度感染,3——重度感染
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 转换数据结构
keep if t != .
keep city year pm25 t comf
destring year, replace
reshape wide pm25, i(city t comf) j(year)
// 把数据结构转换为宽型数据
bys comf t: egen pm2517 = mean(pm252017)
bys comf t: egen pm2518 = mean(pm252018)
bys comf t: egen pm2519 = mean(pm252019)
bys comf t: egen pm2520 = mean(pm252020)
// 求出同一地区同一相对时间的PM2.5均值
duplicates drop comf t,force
// 删除重复项
**====================================================================================
**第三步 绘图
tostring comf, replace
replace comf = "轻度感染地区" if comf == "1"
replace comf = "中度感染地区" if comf == "2"
replace comf = "重度感染地区" if comf == "3"
local Hcity "轻度感染地区 中度感染地区 重度感染地区"
//定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm2518 t, lpattern(solid) lcolor(black*0.4))
(line pm2519 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm2520 t, lpattern(solid) lcolor(red*1.0)) if comf == "`v'",
xtitle("相对时间", place(right))
ytitle("地区平均PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("地区平均PM2.5变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_9.png, replace
}
// 绘图
** Stata Code: 10
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
bys City: egen pollution = mean(pm25)
xtile pollu = pollution, nq(3)
// 按照地区污染程度分类,分成三级,1——轻度污染,2——中度污染,3——重度污染
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 转换数据结构
keep if t != .
keep city year pm25 t pollu
destring year, replace
reshape wide pm25, i(city t pollu) j(year)
// 把数据结构转换为宽型数据
bys pollu t: egen pm2517 = mean(pm252017)
bys pollu t: egen pm2518 = mean(pm252018)
bys pollu t: egen pm2519 = mean(pm252019)
bys pollu t: egen pm2520 = mean(pm252020)
// 求出同一地区同一相对时间的PM2.5均值
duplicates drop pollu t,force
// 删除重复项
**====================================================================================
**第三步 绘图
tostring pollu, replace
replace pollu = "轻度污染地区" if pollu == "1"
replace pollu = "中度污染地区" if pollu == "2"
replace pollu = "重度污染地区" if pollu == "3"
local Hcity "轻度污染地区 中度污染地区 重度污染地区"
//定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm2518 t, lpattern(solid) lcolor(black*0.4))
(line pm2519 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm2520 t, lpattern(solid) lcolor(red*1.0)) if pollu == "`v'",
xtitle("相对时间", place(right))
ytitle("地区平均PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("地区平均PM2.5变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_10.png, replace
}
// 绘图
** Stata Code: 11
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
bys City: egen industry = mean(ind_2)
xtile ind = industry, nq(3)
// 按照地区第二产业占比分类,分成三级,1——第二产业占比低的地区,2——第二产业占比中等的地区,3——第二产业占比高的地区
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 转换数据结构
keep if t != .
keep city year pm25 t ind
destring year, replace
reshape wide pm25, i(city t ind) j(year)
// 把数据结构转换为宽型数据
bys ind t: egen pm2517 = mean(pm252017)
bys ind t: egen pm2518 = mean(pm252018)
bys ind t: egen pm2519 = mean(pm252019)
bys ind t: egen pm2520 = mean(pm252020)
// 求出同一地区同一相对时间的PM2.5均值
duplicates drop ind t,force
// 删除重复项
**====================================================================================
**第三步 绘图
tostring ind, replace
replace ind = "第二产业占比低地区" if ind == "1"
replace ind = "第二产业占比中等地区" if ind == "2"
replace ind = "第二产业占比高地区" if ind == "3"
local Hcity "第二产业占比低地区 第二产业占比中等地区 第二产业占比高地区"
//定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (line pm2518 t, lpattern(solid) lcolor(black*0.4))
(line pm2519 t, lpattern(dash_dot) lcolor(black*0.6))
(line pm2520 t, lpattern(solid) lcolor(red*1.0)) if ind == "`v'",
xtitle("相对时间", place(right))
ytitle("地区平均PM2.5")
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
legend(label(1 "2018年") label(2 "2019年") label(3 "2020年"))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("地区平均PM2.5变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export `v'_11.png, replace
}
// 绘图
** Stata Code: 12
**====================================================================================
**第一步 设置相对时间
cd D:\jianguoyun\我的坚果云\工作数据\
use data_0224.dta,clear
// 导入数据
tostring year month day,replace
gen date = year + "-" + month + "-" + day
gen Date = date(date, "YMD")
format %tdCCYY-NN-DD Date
// 将分开的年月日合成最终的日期
// 春节 2017-01-28
// 春节 2018-02-16
// 春节 2019-02-05
// 春节 2020-01-25
gen t = .
replace t = 10 if date == "2017-1-28" | date == "2018-2-16" | date == "2019-2-5" | ///
date == "2020-1-25"
// 设置开始日期
duplicates drop city date,force
encode city, gen(City)
xtset City Date
// 设置面板数据
bys City: egen industry = mean(ind_2)
xtile ind = industry, nq(3)
// 按照地区第二产业占比分类,分成三级,1——第二产业占比低的地区,2——第二产业占比中等的地区,3——第二产业占比高的地区
sort City Date
bys City : replace t = t[_n-1] + 1 if t == .
replace t = . if t > 38
// 以春节为起点,向后填充
gsort City -Date
bys City : replace t = t[_n-1] - 1 if t == .
replace t = . if t < 0
sort City Date
// 以春节为起点,向前填充
**====================================================================================
**第二步 转换数据结构
keep if t != .
keep city year pm25 t ind
destring year, replace
reshape wide pm25, i(city t ind) j(year)
// 把数据结构转换为宽型数据
bys ind t: egen pm2517 = mean(pm252017)
bys ind t: egen pm2518 = mean(pm252018)
bys ind t: egen pm2519 = mean(pm252019)
bys ind t: egen pm2520 = mean(pm252020)
// 求出同一地区同一相对时间的PM2.5均值
gen diff = pm252020 - pm252019
**====================================================================================
**第三步 绘图
tostring ind, replace
replace ind = "第二产业占比低地区" if ind == "1"
replace ind = "第二产业占比中等地区" if ind == "2"
replace ind = "第二产业占比高地区" if ind == "3"
local Hcity "第二产业占比低地区 第二产业占比中等地区 第二产业占比高地区"
//定义暂元
foreach v in `Hcity'{
#delimit ;
twoway (qfitci diff t, stdf ciplot(rline))(scatter diff t, lpattern(solid) lcolor(red*1.0)) if ind == "`v'",
xtitle("相对时间", place(right))
ytitle("PM2.5差值(2020年-2019年)")
ylabel(-300(300) 300)
xlabel( 0 "春节前十天" 10 "春节" 24 "元宵")
yline(0 , lpattern(solid) lcolor(black*0.5))
xline(0 10 24, lpattern(dash) lcolor(blue*0.5))
title("地区PM2.5差值变化趋势图")
subtitle("——`v'", place(right));
#delimit cr
graph export B`v'_12.png, replace
}
// 绘图
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jefeerzhang/COVID-19-and-AQI-in-China.git
git@gitee.com:jefeerzhang/COVID-19-and-AQI-in-China.git
jefeerzhang
COVID-19-and-AQI-in-China
COVID-19-and-AQI-in-China
master

搜索帮助