📄 gr_example2.ado
字号:
*! version 1.1.13 27apr2005
program gr_example2
version 8.2
if (_caller() < 8.2) version 8
else version 8.2
set more off
`0'
end
program NotSmall
if "`c(flavor)'"=="Small" {
window stopbox stop ///
"Dataset used in this example" ///
"too large for Small Stata"
}
end
program Msg
di as txt
di as txt "-> " as res `"`0'"'
end
program Xeq
di as txt
di as txt `"-> "' as res `"`0'"'
`0'
end
program markerlabel1
Msg preserve
preserve
Xeq sysuse lifeexp, clear
Xeq gen pos = 3
Xeq replace pos = 9 if country=="Honduras"
#delimit ;
Xeq
scatter lexp gnppc if region==2,
mlabel(country) mlabv(pos)
xsca(range(35000))
;
#delimit cr
Msg restore
end
program markerlabel2
Msg preserve
preserve
Xeq sysuse lifeexp, clear
Xeq gen pos = 3
Xeq replace pos = 9 if country=="Honduras"
#delimit ;
Xeq
scatter lexp gnppc if region==2,
mlabel(country) mlabv(pos)
xsca(range(35000))
plotregion(margin(l+9))
;
#delimit cr
Msg restore
end
program markerlabel3
Msg preserve
preserve
Xeq sysuse lifeexp, clear
Xeq keep if region==2 | region==3
Xeq replace gnppc = gnppc / 1000
Xeq label var gnppc "GNP per capita (thousands of dollars)"
Xeq gen lgnp = log(gnp)
Xeq qui reg lexp lgnp
Xeq predict hat
Xeq label var hat "Linear prediction"
Xeq replace country = "Trinidad" if country=="Trinidad and Tobago"
Xeq replace country = "Para" if country == "Paraguay"
Xeq gen pos = 3
Xeq replace pos = 9 if lexp > hat
Xeq replace pos = 3 if country == "Colombia"
Xeq replace pos = 3 if country == "Para"
Xeq replace pos = 3 if country == "Trinidad"
Xeq replace pos = 9 if country == "United States"
#delimit ;
Xeq twoway
(scatter lexp gnppc, mlabel(country) mlabv(pos))
(line hat gnppc, sort)
, xsca(log) xlabel(.5 5 10 15 20 25 30, grid) legend(off)
title("Life expectancy vs. GNP per capita")
subtitle("North, Central, and South America")
note("Data source: World bank, 1998")
ytitle("Life expectancy at birth (years)")
;
#delimit cr
Msg restore
end
program markerlabel4
Msg preserve
preserve
Xeq sysuse lifeexp, clear
Xeq keep if region==2 | region==3
Xeq replace gnppc = gnppc / 1000
Xeq label var gnppc "GNP per capita (thousands of dollars)"
Xeq gen lgnp = log(gnp)
Xeq qui reg lexp lgnp
Xeq predict hat
Xeq label var hat "Linear prediction"
Xeq replace country = "Trinidad" if country=="Trinidad and Tobago"
Xeq replace country = "Para" if country == "Paraguay"
Xeq gen pos = 3
Xeq replace pos = 9 if lexp > hat
Xeq replace pos = 3 if country == "Colombia"
Xeq replace pos = 3 if country == "Para"
Xeq replace pos = 3 if country == "Trinidad"
Xeq replace pos = 9 if country == "United States"
#delimit ;
Xeq twoway
(scatter lexp gnppc, mlabel(country) mlabv(pos))
(line hat gnppc, sort)
, xsca(log) xlabel(.5 5 10 15 20 25 30, grid) legend(off)
title("Life expectancy vs. GNP per capita")
subtitle("North, Central, and South America")
note("Data source: World bank, 1998")
ytitle("Life expectancy at birth (years)")
scale(1.1)
;
#delimit cr
Msg restore
end
/*
program scatter1
preserve
quietly {
use "`c(sysdir_stata)'census", clear
gen mrate = marriage/pop18p
gen drate = divorce/pop18p
}
scatter mrate drate medage if state!="Nevada"
end
*/
program scatterlog
Msg preserve
preserve
Xeq sysuse lifeexp, clear
Xeq gen gnp000 = gnppc/1000
Xeq label var gnp000 "GNP per capita, thousands of dollars"
Xeq scatter lexp gnp000, xsca(log) ///
xlabel(.5 2.5 10(10)40, grid)
Msg restore
end
program scatterwgt
Msg preserve
preserve
Xeq sysuse census, clear
Xeq gen drate = divorce / pop18p
Xeq label var drate "Divorce rate"
Xeq scatter drate medage [w=pop18p] if state!="Nevada", ///
m(Oh) ///
note("State data excluding Nevada" ///
"Area of symbol proportional to state's population aged 18+")
Msg restore
end
program line1
tempname esti
_est hold `esti', nullok
Msg preserve
preserve
Xeq sysuse auto, clear
Xeq quietly regress mpg weight
Xeq predict hat
Xeq predict stf, stdf
Xeq gen lo = hat - 1.96*stf
Xeq gen hi = hat + 1.96*stf
Xeq scatter mpg weight || line hat lo hi weight, ///
pstyle(p2 p3 p3) sort
Msg restore
restore
_estimates unhold `esti'
end
program line2
Msg preserve
preserve
Xeq sysuse uslifeexp, clear
Xeq gen diff = le_wm - le_bm
Xeq label var diff "Difference"
#delimit ;
Xeq
line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
ylabel(0(5)20, axis(2) gmin angle(horizontal))
ylabel(0 20(10)80, gmax angle(horizontal))
ytitle("", axis(2))
xlabel(1918, axis(2)) xtitle("", axis(2))
ylabel(, axis(2) grid)
ytitle("Life expectancy at birth (years)")
title("White and black life expectancy")
subtitle("USA, 1900-1999")
note("Source: National Vital Statistics, Vol 50, No. 6"
"(1918 dip caused by 1918 Influenza Pandemic)")
;
#delimit cr
Msg restore
end
program line3
Msg preserve
preserve
Xeq sysuse uslifeexp, clear
Xeq gen diff = le_wm - le_bm
Xeq label var diff "Difference"
#delimit ;
Xeq
line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
ylabel(0(5)20, axis(2) gmin angle(horizontal))
ylabel(0 20(10)80, gmax angle(horizontal))
ytitle("", axis(2))
xlabel(1918, axis(2)) xtitle("", axis(2))
ylabel(, axis(2) grid)
ytitle("Life expectancy at birth (years)")
title("White and black life expectancy")
subtitle("USA, 1900-1999")
note("Source: National Vital Statistics, Vol 50, No. 6"
"(1918 dip caused by 1918 Influenza Pandemic)")
legend(label(1 "White males") label(2 "Black males"))
;
#delimit cr
Msg restore
end
program line3a
Msg preserve
preserve
Xeq sysuse uslifeexp, clear
Xeq gen diff = le_wm - le_bm
Xeq label var diff "Difference"
#delimit ;
Xeq
line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
ylabel(0(5)20, axis(2) grid gmin angle(horizontal))
ylabel(0 20(10)80, gmax angle(horizontal))
ytitle("", axis(2))
xlabel(1918, axis(2)) xtitle("", axis(2))
ylabel(, axis(2) grid)
ytitle("Life expectancy at birth (years)")
title("White and black life expectancy")
subtitle("USA, 1900-1999")
note("Source: National Vital Statistics, Vol 50, No. 6"
"(1918 dip caused by 1918 Influenza Pandemic)")
legend(label(1 "White males") label(2 "Black males"))
;
#delimit cr
Msg restore
end
program line4
Msg preserve
preserve
Xeq sysuse uslifeexp, clear
Xeq gen diff = le_wm - le_bm
Xeq label var diff "Difference"
#delimit ;
Xeq
line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
ylabel(0(5)20, axis(2) gmin angle(horizontal))
ylabel(0 20(10)80, gmax angle(horizontal))
ytitle("", axis(2))
xlabel(1918, axis(2)) xtitle("", axis(2))
ylabel(, axis(2) grid)
ytitle("Life expectancy at birth (years)")
title("White and black life expectancy")
subtitle("USA, 1900-1999")
note("Source: National Vital Statistics, Vol 50, No. 6"
"(1918 dip caused by 1918 Influenza Pandemic)")
legend(label(1 "White males") label(2 "Black males"))
legend(col(1) pos(3))
;
#delimit cr
Msg restore
end
program twobar
Msg preserve
preserve
Xeq sysuse sp500, clear
#delimit ;
Xeq twoway
line close date, yaxis(1)
||
bar change date, yaxis(2)
||
in 1/52,
ysca(axis(1) r(1000 1400)) ylab(1200(50)1400, axis(1))
ysca(axis(2) r(-50 300)) ylab(-50 0 50, axis(2))
ytick(-50(25)50, axis(2) grid)
legend(off)
xtitle("Date")
title("S&P 500")
subtitle("January - March 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc.")
yline(1150, axis(1) lstyle(foreground))
;
#delimit cr
Msg restore
end
program twobar2
Msg preserve
preserve
Xeq sysuse pop2000, clear
Xeq replace maletotal = -maletotal/1e+6
Xeq replace femtotal = femtotal/1e+6
#delimit ;
Xeq twoway
bar maletotal agegrp, horizontal xvarlab(Males)
||
bar femtotal agegrp, horizontal xvarlab(Females)
||
, ylabel(1(1)17, angle(horizontal) valuelabel labsize(*.8))
xtitle("Population in millions") ytitle("")
xlabel(-10 "10" -7.5 "7.5" -5 "5" -2.5 "2.5" 2.5 5 7.5 10)
legend(label(1 Males) label(2 Females))
title("US Male and Female Population by Age")
subtitle("Year 2000")
note("Source: U.S. Census Bureau, Census 2000, Tables 1, 2 and 3",
span) ;
#delimit cr
Msg restore
end
program twobar3
Msg preserve
preserve
Xeq sysuse pop2000, clear
Xeq replace maletotal = -maletotal
Xeq twoway bar maletotal agegrp, horizontal || bar femtotal agegrp, horizontal
Msg restore
end
program twobar4
Msg preserve
preserve
Xeq sysuse pop2000, clear
Xeq replace maletotal = -maletotal/1e+6
Xeq replace femtotal = femtotal/1e+6
Xeq gen zero = 0
#delimit ;
Xeq twoway
bar maletotal agegrp, horizontal xvarlab(Males)
||
bar femtotal agegrp, horizontal xvarlab(Females)
||
sc agegrp zero , mlabel(agegrp) mlabcolor(black) msymbol(i)
||
,
xtitle("Population in millions") ytitle("")
plotregion(style(none))
ysca(noline) ylabel(none)
xsca(noline titlegap(-3.5))
xlabel(-12 "12" -10 "10" -8 "8" -6 "6" -4 "4" 4(2)12 , tlength(0)
grid gmin gmax)
legend(label(1 Males) label(2 Females)) legend(order(1 2))
title("US Male and Female Population by Age, 2000")
note("Source: U.S. Census Bureau, Census 2000, Tables 1, 2 and 3")
;
#delimit cr
Msg restore
end
program twospike
Msg preserve
preserve
Xeq sysuse sp500, clear
#delimit ;
Xeq twoway
line close date, yaxis(1)
||
spike change date, yaxis(2)
||,
ysca(axis(1) r(700 1400)) ylab(1000(100)1400, axis(1))
ysca(axis(2) r(-50 300)) ylab(-50 0 50, axis(2))
ytick(-50(25)50, axis(2) grid)
legend(off)
xtitle(Date)
title("S&P 500")
subtitle("January - December 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc.")
yline(950, axis(1) lstyle(foreground))
;
#delimit cr
Msg restore
end
program twospike2
Msg preserve
preserve
Xeq sysuse sp500, clear
#delimit ;
Xeq twoway
line close date, yaxis(1)
||
spike change date, yaxis(2)
||,
ysca(axis(1) r(700 1400)) ylab(1000(100)1400, axis(1))
ysca(axis(2) r(-50 300)) ylab(-50 0 50, axis(2))
ytick(-50(25)50, axis(2) grid)
legend(off)
xtitle("Date")
title("S&P 500")
subtitle("January - December 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc.")
yline(950, axis(1) lstyle(foreground))
;
#delimit cr
Msg restore
end
program twodropline
Msg preserve
preserve
Xeq sysuse lifeexp, clear
Xeq keep if region==3
Xeq gen lngnp = ln(gnppc)
Xeq quietly regress le lngnp
Xeq predict r, resid
Xeq twoway dropline r gnp, ///
yline(0, lstyle(foreground)) mlabel(country) mlabpos(9) ///
ylab(-6(1)6) ///
subtitle("Regression of life expectancy on ln(gnp)" ///
"Residuals:" " ", pos(11)) ///
note("Residuals in years; positive values indicate" ///
"longer than predicted life expectancy")
Msg restore
end
program hist1
Msg preserve
preserve
Xeq sysuse sp500, clear
#delimit ;
Xeq
histogram volume, freq
xaxis(1 2)
ylabel(0(10)60, grid)
xlabel(12321 "mean"
9735 "-1 s.d."
14907 "+1 s.d."
7149 "-2 s.d."
17493 "+2 s.d."
20078 "+3 s.d."
22664 "+4 s.d."
, axis(2) grid gmax)
xtitle("", axis(2))
subtitle("S&P 500, January 2001 - December 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc.")
;
#delimit cr
Msg restore
end
program hist2
Msg preserve
preserve
Xeq sysuse sp500, clear
#delimit ;
Xeq
histogram volume, freq normal
xaxis(1 2)
ylabel(0(10)60, grid)
xlabel(12321 "mean"
9735 "-1 s.d."
14907 "+1 s.d."
7149 "-2 s.d."
17493 "+2 s.d."
20078 "+3 s.d."
22664 "+4 s.d."
, axis(2) grid gmax)
xtitle("", axis(2))
subtitle("S&P 500, January 2001 - December 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc.")
;
#delimit cr
Msg restore
end
program tworspike
Msg preserve
preserve
Xeq sysuse sp500, clear
Xeq replace volume = volume/1000
#delimit ;
Xeq
twoway
rspike hi low date ||
line close date ||
bar volume date, barw(.25) yaxis(2) ||
in 1/57
, ysca(axis(1) r(900 1400))
ysca(axis(2) r( 9 45))
ylabel(, axis(2) grid)
ytitle(" Price -- High, Low, Close")
ytitle(" Volume (millions)", axis(2) bexpand just(left))
legend(off)
subtitle("S&P 500", margin(b+2.5))
note("Source: Yahoo!Finance and Commodity Systems, Inc.")
;
#delimit cr
Msg restore
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -