Professional Documents
Culture Documents
ni dung
hhsize
6
5
10
incomepc
2100
3210
1200
Kiu
S nguyn
S nguyn
S nguyn
S thc
2
double
8
-10^308
10^308
S thc
Cc bin s c th bao gm cc bin ri rc v lin tc. Cc bin nh l quy
m h gia nh, gii tnh ch h, vng a l, trnh gio dc l cc
bin ri rc (discrete) (hay cn gi l bin phn loi (categorical)). Cc bin
ny c th c lu tr di dng byte, int, v long. Cc bin lin tc
(continuous) nh thu nhp, chi tiu ca h th lu tr di dng float hoc
double.
Bin k t (string) dng lu tr cc loi k t. V d bin headname l
bin kiu k t dng lu tr tn ca ch h.
Kiu lu tr
dng ch
str1
str2
...
str80
Byte
di ln nht
1
2
1
2
80
80
Phin bn Stata 8 c giao din tng t nh phin bn Stata 7. Khc bit ln nht l
Stata 8 c thm tu chn Statistics trong thanh thc n. Tu chn ny cho php
thc hin cc mt s lnh thng k bng cc tu chn qua giao din ca s m
khng phi g cc lnh trong ca s Command.
1
Cc ca s ca Stata
Cc ca s ca Stata c m ra bng vic la chn cc tu chn thanh
thc n Windows (menu bar). Cc ca s ny bao gm:
Results
Hin th cc lnh v kt qu
Graph
Hin th th
Viewer
Command
Dng g cc cu lnh
Review
Variables
Data editor
Do-file editor
File
Open
M file s liu
View
Save
Lu file s liu
Save as
Lu file s liu di tn mi
File name
Log
Save graph
Lu gi file th
Print graph
In th
Print results
In kt qu
Exit
Edit
Copy text
Copy tables
Paste
Dn
Prefs
c
Windows
Results
M ca s kt qu
Graph
M ca s th
Log
M ca s log file
Viewer
Command
M ca s cu lnh
Review
Variables
Help/Search
M ca s tr gip (help)
Data editor
Do-file editor
Help
Save
Lu tr file s liu ra a
Print results
In ni dung ca ca s kt qu
Begin log
Start viewer
M ca s tr tr (help)
Data editor
M ca s sa cha s liu
Data browser
M ca s xem s liu
Tt lnh more
Break
replace
text
smcl
V d:
log using baitap1
To tp baitap2 ti a D, di dng vn
bn (text) (phn m rng l log)
log using
append
C php:
use (ng dn\tn tp)
Lnh ny m tp Stata, vi phn rng l .dta, c ch ra tn tp.
V d:
use ho1.dta
name
income
(commas) hoc du cch (tab). Sau dng lnh insheet nhp s liu
ny vo Stata.
C php:
insheet [danh sch bin] using (tn tp text) [, [no]names comma
tab clear]
Lnh ny s c vo b nh ca Stata cc quan st ca tp text, v ch ra
tn cc bin s c to ra.
Cc tu chn:
[no]names
comma
tab
clear
V d:
. insheet using c:\income.txt
(3 vars, 4 obs)
. insheet maho hoten thunhap using c:\income.txt
(note: variable names in file ignored)
(3 vars, 4 obs)
Lu tr s liu
C php:
save (ng dn\tn tp) [,replace]
Lnh ny lu tr s liu ang trong b nh ca Stata thnh tp ch nh
di tn tp. Nu tu chn replace c ch ra th tp s liu ny s ghi
ln tp hin thi (tt nhin tn tp s liu l ging nhau).
Vic lu tr s liu c th thc hin bng cc ty chn Save v Save as
trong thanh thc n (menu bar); hoc nt Save trn thanh cng c (tool
bar).
Ch : Xem thm lnh infile v outfile
by danh sch bin (by varlist): Stata s thc hin cu lnh vi theo tng
gi tr c ch ra bi danh sch bin. Bin c ch ra bi danh sch bin
c yu cu sp xp trc khi thc hin lnh.
V d:
. sort sex
. by sex: sum
rlpcex1
-> sex = 1
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------rlpcex1 |
4375
2980.906
2430.648
357.318
45801.71
-> sex = 2
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------rlpcex1 |
1624
3748.368
3231.241
376.9805
30624.77
rlpcex1
11
poor if reg7==1
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------poor |
859
.4982538
.5002882
0
1
sum poor in 10
sum
poor
10/100
sum
f/100
poor
sum
100/l
poor
Quyn s (weight)
Cho php tnh ton s dng quyn s. Tu chn v quyn s s c trnh
by k mc 5 ca chng ny.
Cc tu chn (Options)
Nhiu cu lnh Stata cho php cc tu chn ring. Cc tu chn ny c
ch ra sau du phy.
V d:
Lnh sum c tu chn l detail, cho php tnh ton thm mt s thng k
khc ngoi gi tr trung bnh v lnh chun.
. sum
rlpcex1, detail
comp.M&Reg price adj.pc tot exp
------------------------------------------------------------Percentiles
Smallest
1%
682.9575
357.318
5%
1012.433
366.2792
10%
1238.088
376.9805
Obs
5999
25%
1671.054
381.3502
Sum of Wgt.
5999
50%
2397.042
75%
90%
95%
99%
3711.917
5940.803
8045.32
14163.04
Largest
26944.64
30624.77
31066.5
45801.71
Mean
Std. Dev.
3188.667
2692.567
Variance
Skewness
Kurtosis
7249918
3.791027
29.21398
Ch :
13
ngha
S hc
+
Cng
Tr
Nhn
Chia
Lu tha
Quan h
>
Ln hn
<
Nh hn
>=
Ln hn hoc bng
<=
Nh hn hoc bng
==
Bng
~=
!=
Lgc
~
Khng
Hoc
&
Ch :
Trong biu thc du == c dng cho vic kim nh biu thc, v d nh
c dng sau lnh if. Cn du = c dng cho lnh to bin.
V d:
gen RRD=0
replace RRD=1 if reg8==1
Cc hm s (function)
Hm s thng c dng trong biu thc (exp) ca cu lnh Stata. Nu coi Y
l mt hm s ca f(X1, X2,, Xn) th lnh v hm s trong Stata s tnh gi
tr ca Y nu cho cc gi tr ca Xi. Stata c 8 loi hm s:
Mathematical functions
Cc hm ton hc
Statistical functions
Hm thng k
Random numbers
String functions
Hm lin quan n dy k t
Special functions
Hm c bit
14
Date functions
Hm ngy thng
Time-series functions
Matrix functions
Hm ma trn
V d:
gen absx=abs(x)
gen log_exp=log(rlpcex1)
Cc k hiu c th v cc hm s ny c th xem mc help functions.
3. M t d liu (Data reporting)
3.1. Xo b nh ca Stata
C php:
clear
Lnh ny xo cc d liu trong b nh ca Stata, bt u cho mt file lm
vic mi.
3.2. Hng dn s dng lnh Stata
C php:
help <Cu lnh Stata>
Lnh ny hin th hng dn s dng cc lnh Stata, lnh Stata cn phi c g y v chnh xc.
V d:
. help sum
help for sum not found
try help contents or search sum
. help summarize
----------------------------------------------------------------------------------------help for summarize
----------------------------------------------------------------------------------------Summary statistics
.
Ch :
Chng ta c th tm hng dn s dng theo t kho bng lnh search. Lnh
search c th c thc hin bng tu chn Search thc n help.
3.3. M t d liu
C php:
15
storage display
value
variable name
type
format
label
variable label
-----------------------------------------------------------------------------househol
long
%12.0g
household code
year
float %9.0g
Year of interview
month
float %9.0g
Month of interview
vlssmphs
byte
%8.0g
1 if vlss, 2 if mphs
source
1.
2.
3.
4.
5.
. list
1.
2.
3.
4.
5.
farm
farm
farm
farm
non farm
non farm
farm
1
1
1
0
0
16
. dis 120*100/30
400
3.6. Sa cha, xem s liu
C php:
edit
3.8. Thng k c bn
C php:
summarize [danh sch bin] [quyn s] [iu kin] [phm vi] [,
detail]
Lnh ny tnh ton v hin th nhng thng k c bn ca cc bin c
ch ra bi danh sch bin. Tu chn detail cho php hin th thm mt
s thng k nh nhn, lnh v cc gi tr ca thp v phn.
V d:
. sum
rlpcex1
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------rlpcex1 |
5999
3188.667
2692.567
357.318
45801.71
. sum
rlpcex1, detail
2397.042
3711.917
5940.803
8045.32
14163.04
Largest
26944.64
30624.77
31066.5
45801.71
Mean
Std. Dev.
3188.667
2692.567
Variance
Skewness
Kurtosis
7249918
3.791027
29.21398
18
x:
---|
#
|
#
|
#
|
#
|
#
#
#
| .
#
#
#
.
+----------------------3.918931
3.641588
(More than 99 unique values)
Negative
Zero
Positive
Total
Missing
Number of Observations
NonTotal
Integers
Integers
2964
2964
3035
3035
------------5999
5999
----5999
nolabel
phi nhn bin
V d:
. tab sex
Gender of |
HH.head |
(1:M;2:F) |
Freq.
Percent
Cum.
------------+----------------------------------1 |
4375
72.93
72.93
2 |
1624
27.07
100.00
------------+----------------------------------Total |
5999
100.00
. tab1 urban98 reg7
-> tabulation of urban98
19
1:urban 98; |
0:rural 98 |
Freq.
Percent
Cum.
------------+----------------------------------Rural |
4269
71.16
71.16
Urban |
1730
28.84
100.00
------------+----------------------------------Total |
5999
100.00
-> tabulation of reg7
Code by 7 |
regions |
Freq.
Percent
Cum.
------------+----------------------------------region1 |
859
14.32
14.32
region2 |
1175
19.59
33.91
region3 |
708
11.80
45.71
region4 |
754
12.57
58.28
region5 |
368
6.13
64.41
region6 |
1023
17.05
81.46
region7 |
1112
18.54
100.00
------------+----------------------------------Total |
5999
100.00
Cc tu chn:
chi2
missing
nofreq
cell
column
row
V d:
. tab
V d:
. tab
Cc tu chn:
means
standard
freq
Hin th mi gi tr tn sut
missing
V d:
. replace poor=poor*100
(1777 real changes made)
. format poor %4.2f
. tab reg7 urban98, sum(poor) means
Means of poor
| 1:urban 98; 0:rural
Code by 7 |
98
regions |
Rural
Urban |
Total
-----------+----------------------+---------region1 |
61.46
8.02 |
49.83
region2 |
32.57
5.87 |
23.66
region3 |
44.83
10.19 |
39.55
region4 |
37.25
11.51 |
28.65
region5 |
47.28
. |
47.28
region6 |
12.45
2.16 |
7.33
region7 |
35.78
10.28 |
29.32
-----------+----------------------+---------Total |
38.86
6.82 |
29.62
22
Cc tu chn:
statistics(statname
[...])
by(tn bin)
Missing
format[(%fmt)]
ngha
mean
count
m s quan st
sum
Tng cng
max
Gi tr ln nht
min
Gi tr nh nht
range
sd
lch chun
sdmean
skewness
kurtosis
nhn
median
p1
1% phn v
p5
5% phn v
p10
10% phn v
p25
25% phn v
p50
p75
75% phn v
p90
90% phn v
p95
95% phn v
p99
99% phn v
iqr
p75 - p25
V d:
. tabstat
24
region1 |
2174.8
1265.1
1328.0
1792.1
2710.8
region2 |
3294.0
2511.9
1816.7
2532.5
3822.0
region3 |
2503.3
1918.0
1489.7
2001.2
2808.1
region4 |
2933.7
2260.5
1697.9
2362.2
3471.4
region5 |
2087.3
1285.4
1217.3
1850.8
2700.5
region6 |
5257.5
4005.7
2676.7
4154.1
6431.8
region7 |
2931.1
2137.2
1680.1
2321.9
3414.7
----------------------------------------------------------Total |
3188.7
2692.6
1671.1
2397.0
3711.9
-----------------------------------------------------------
Cc tu chn:
Contents(ni dung)
row
col
format(%fmt)
missing
loi
V d:
. table reg7 urban98 farm, contents(mean poor) row col format(%4.2f)
-----------------------------------------------------| Type of HH (1:farm; 0:nonfarm) and 1:urban
|
98; 0:rural 98
Code by 7 | ----- non farm ---------- farm -----regions
| Rural Urban Total
Rural Urban Total
----------+------------------------------------------region1 | 19.35
6.02 10.26
65.74 12.96 61.45
region2 | 26.67
4.62 11.29
33.97 15.22 32.70
region3 | 40.98 10.11 27.96
45.82 10.53 44.47
region4 | 21.60 11.64 15.13
42.44 10.00 40.81
region5 | 30.77
30.77
49.24
49.24
region6 | 15.04
2.20
6.43
10.07
0.00
9.78
region7 | 38.63 10.04 25.39
34.36 11.63 32.72
|
Total | 27.91
6.17 14.84
42.30 12.11 40.63
-----------------------------------------------------. table urban98 farm, contents(mean poor sd poor) row col format(%4.2f)
---------------------------------------1:urban
|
98;
|
Type of HH (1:farm;
0:rural
|
0:nonfarm)
98
| non farm
farm
Total
----------+----------------------------Rural |
27.91
42.30
38.86
|
44.88
49.41
48.75
|
Urban |
6.17
12.11
6.82
|
24.07
32.71
25.22
|
Total |
14.84
40.63
29.62
|
35.55
49.12
45.66
---------------------------------------. table urban98
format(%4.2f)
farm,
contents(mean
rlpcex1
mean
rlhhex1)
row
col
---------------------------------------1:urban
|
98;
|
Type of HH (1:farm;
0:rural
|
0:nonfarm)
98
| non farm
farm
Total
----------+----------------------------Rural | 2835.83
2212.12
2361.29
| 13242.03 10120.89 10867.36
|
Urban | 5476.86
3232.17
5230.33
26
To bin mi
27
gen region7=(reg7==7)
To bin bng lnh egen
C php:
egen <bin mi> = fcn(tham s) [iu kin] [phm vi] [, by(bin)]
Lnh ny cho php to bin mi theo gi tr ca hm s c ch ra bi fcn.
Bin mi ny s nhn gi tr c nh cho mi quan st. Hm s y c
th l:
count(exp)
mean(exp)
median(exp)
sd(exp)
28
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------sumexp |
5999
1.91e+07
0
1.91e+07
1.91e+07
. egen g=median( food+ nonfood1)
. sum g
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------g |
5999
11063.6
0
11063.6
11063.6
Thay th gi tr ca bin
C php:
replace <bin> = biu thc [iu kin] [phm vi]
Lnh ny thay th gi tr ca bin hin c bng gi tr mi xc nh bi
biu thc exp.
V d:
replace poor=poor*100
replace pcexp = hhexp/hhsize
To bin phn loi bng lnh encode
C php:
encode <bin> [iu kin] [phm vi], generate(bin mi)
Lnh ny cho php to bin phn loi mi (categorical) kiu s tng ng vi
cc gi tr ca bin kiu ch ch ra bi tn bin (c xp theo vn ch
ci).
V d:
. gen str15(mucsong) = "Kha"
. drop
mucsong
rlpcex1<1790 &
rlpcex1>1290
rlpcex1>=1790
. tab mucsong
29
mucsong |
Freq.
Percent
Cum.
----------------+----------------------------------Khong ngheo |
4222
70.38
70.38
Ngheo |
1087
18.12
88.50
Rat ngheo |
690
11.50
100.00
----------------+----------------------------------Total |
5999
100.00
. sum mucsong
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------mucsong |
0
. encode mucsong, gen(ma_ms)
. tab ma_ms
ma_ms |
Freq.
Percent
Cum.
------------+----------------------------------Khong ngheo |
4222
70.38
70.38
Ngheo |
1087
18.12
88.50
Rat ngheo |
690
11.50
100.00
------------+----------------------------------Total |
5999
100.00
. sum ma_ms
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------ma_ms |
5999
1.411235
.6871957
1
3
Trong
30
5 |
1199
19.99
100.00
------------+----------------------------------Total |
5999
100.00
. tab
4.2.
i tn bin
C php:
rename <tn bin c> <tn bin mi>
Lnh ny thc hin vic i tn c ca mt bin sang tn mi.
V d:
rename poor nguoingheo
rename rpcexp1 chitieu
4.3. Lnh xo bin, xo quan st
C php:
drop <danh sch bin>
sch bin
keep househol
xo i
keep in f/50
khc b xo i
32
. tab urban98
1:urban 98; |
0:rural 98 |
Freq.
Percent
Cum.
------------+----------------------------------Rural |
1730
28.84
28.84
Urban |
4269
71.16
100.00
------------+----------------------------------Total |
5999
100.00
33
Freq.
Percent
Cum.
34
------------+----------------------------------0 |
4222
70.38
70.38
1 |
1777
29.62
100.00
------------+----------------------------------Total |
5999
100.00
. list ngheo in 1/5
1.
2.
3.
4.
5.
ngheo
1
0
1
1
0
4.6. Sp xp s liu
C php:
sort <danh sch bin> [phm vi]
gsort [+|-]tn bin [[+|-]tn bin [...]]
Lnh sort sp xp quan st theo th t tng dn ca gi tr ca cc bin
c ch ra trong danh sch bin.
Lnh gsort cho php sp xp cc quan st theo th t tng dn ca ca
cc bin (danh sch bin), nu du + c ch ra (y cng l gi tr
ngm nh), hoc theo th t gim dn, nu du - c ch ra.
V d:
35
. gen quimo=1
. collapse (mean) thunhap (mean) chitieu (sum) quimo, by(ma_ho)
Tp s liu mi c dng:
ma_ho thunhap chitieu
quimo
101
350
325
4
102
1533.33
340
3
103
600
337.5
4
104
1350
416.667
6
Kt hp s liu - lnh merge
C php:
merge [danh sch bin] using <tn tp s dng> [, update replace]
Lnh merge s ni cc quan st ca tp s liu ang m trong Stata (gi l
tp ch (master dataset)) vi cc quan st tng ng ca tp s liu khc c ch ra sau t kho using (gi l tp s dng (using dataset)) thnh 1 tp
mi. Cc bin ch ra trong danh sch bin c gi l bin xc nh
(identifying variables), v phi c sp xt bng lnh sort (hoc gsort) trc
khi thc hin lnh merge.
V d:
Chng ta c 2 tp s liu nh sau:
thunhap.dta
ma_ho thunhap chitieu
101
350
325
102
1533.33
340
103
600
337.5
104
1350
416.667
dialy.dta
quimo
4
3
4
6
ma_ho
thanhthi
vung
204
0
1
102
1
4
103
0
3
104
0
6
Lnh merge s c thc hin nh sau:
. use "C:\dialy.dta", clear
. sort ma_ho
. save "C:\dialy.dta"
file C:\dialy.dta saved
. use "C:\thunhap.dta", clear
. sort ma_ho
. merge ma_ho using "C:\dialy.dta"
37
Nu nh quan st ch c to t s dng
_merge==3
Nu nh quan st c to t c tp ch v tp s dng
Cc tu chn:
Trong trng hp hai tp s liu c cc bin trng nhau, cc tu chn sau
y cho php x l s liu theo cc cch khc nhau:
update
replace
<tn
bin>,
i(danh
sch
bin)
j(tn
bin
reshape wide
reshape long
Lnh ny cho php chuyn s liu t dng ngang sang s liu dng dc
(tu chn long), v t dng dc sang dng ngang (tu chn wide). i(danh
sch bin) ch ra bin xc nh (indentifying variables) dng phn
bit cc quan st vi nhau trong s liu dng ngang (gi l quan st cp 1).
j(tn bin) ch ra bin dng phn bit gia cc quan st cp 2 s
liu dng dc.
V d 1:
Chng ta c th s liu dng bng ngang nh mt ma trn nh sau:
-i-
-------------------- xj
------------------mah quim thunhap thunhap thunhap
o
o
95
96
97
101
5
4500
4400
5400
102
4
3400
3300
3700
103
6
5000
5400
5500
s liu ny s c chuyn sang dng bng dc nh sau:
-imaho
101
quimo
5
-jnam
95
- xji thunhap
4500
39
101
5
96
101
5
97
102
4
95
102
4
96
102
4
97
103
6
95
103
6
96
103
6
97
V lnh reshape s c vit nh sau:
4400
5400
3400
3300
3700
5000
5400
5500
V d 2:
Chng ta c s liu dng bng sau y:
maho
101
sotien
1
1200
nguon1
sotien
2
2000
Ngan hang
A
102
1300
Ngan hang
.
B
103
2500
Ngan hang
1000
A
104
3000
Ngan hang
2000
A
Bng ny c chuyn sang bng dng dc nh
nguon2
Ngan hang A
.
Ngan hang C
Ngan hang B
sau:
40
(note: j = 1 2)
Data
wide
->
long
--------------------------------------------------------------------Number of obs.
4
->
8
Number of variables
5
->
4
j variable (2 values)
->
lanvay
xij variables:
sotien1 sotien2
->
sotien
nguon1 nguon2
->
nguon
---------------------------------------------------------------------
lanvay
1
2
1
2
1
2
1
2
sotien
1200
2000
1300
nguon
Ngan hang A
Ngan hang A
Ngan hang B
2500
1000
3000
2000
Ngan hang A
Ngan hang C
Ngan hang A
Ngan hang B
pweights:
aweights
iweights
tab
reg7 urban98
.
.
tab
43
Code by 7 |
98
regions |
Rural
Urban |
Total
-----------+----------------------+---------region1 | 11993763
1570583 | 13564346
region2 | 11057932
3812871 | 14870803
region3 |
9582621
906048 | 10488669
region4 |
5618709
2520372 |
8139081
region5 |
2783821
0 |
2783821
region6 |
4545303
5119702 |
9665005
region7 | 13220727
3074190 | 16294917
-----------+----------------------+---------Total | 58802876
17003766 | 75806642
44
|
900302
1108764 |
2009066
-----------+----------------------+---------region7 | 5.1494132 4.3925283 | 4.9872852
|
2567424
699868 |
3267292
-----------+----------------------+---------Total | 4.8003065 4.3841133 | 4.7002214
| 12249817
3878496 | 16128313
.
. table reg7 urban98 , c(mean poor) col row format(%4.1f)
------------------------------| 1:urban 98; 0:rural
Code by 7 |
98
regions
| Rural Urban Total
----------+-------------------region1 | 61.5
8.0
49.8
region2 | 32.6
5.9
23.7
region3 | 44.8
10.2
39.5
region4 | 37.3
11.5
28.6
region5 | 47.3
47.3
region6 | 12.5
2.2
7.3
region7 | 35.8
10.3
29.3
|
Total | 38.9
6.8
29.6
------------------------------. table reg7 urban98 [pw=hhsizewt], c(mean poor) col row format(%4.1f)
------------------------------| 1:urban 98; 0:rural
Code by 7 |
98
regions
| Rural Urban Total
----------+-------------------region1 | 65.2
8.3
58.6
region2 | 36.1
7.0
28.7
region3 | 51.3
14.3
48.1
region4 | 43.6
16.6
35.2
region5 | 52.4
52.4
region6 | 13.0
2.9
7.6
region7 | 42.0
15.3
36.9
|
Total | 45.5
9.2
37.4
-------------------------------
C php:
45
binomial
poisson
exposure(tn bin)
total
dng khi ma by prefix c s dung, yu cu c lng khong tin cy cho ton b nhm.
V d:
. ci
poor
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
5999
29.6216
.5895501
28.46587
30.77733
.
.
. sort reg7
. by reg7: ci poor, total
_______________________________________________________________________________
-> reg7 = region1
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
859
49.82538
1.706961
46.47507
53.17569
_______________________________________________________________________________
-> reg7 = region2
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
1175
23.65957
1.240357
21.22601
26.09314
_______________________________________________________________________________
-> reg7 = region3
46
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
708
39.54802
1.838899
35.93767
43.15838
_______________________________________________________________________________
-> reg7 = region4
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
754
28.64721
1.64759
25.4128
31.88163
_______________________________________________________________________________
-> reg7 = region5
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
368
47.28261
2.606121
42.1578
52.40741
_______________________________________________________________________________
-> reg7 = region6
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
1023
7.331378
.8153306
5.731465
8.931292
_______________________________________________________________________________
-> reg7 = region7
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
1112
29.31655
1.365709
26.63689
31.99621
_______________________________________________________________________________
-> Total
Variable |
Obs
Mean
Std. Err.
[95% Conf. Interval]
-------------+------------------------------------------------------------poor |
5999
29.6216
.5895501
28.46587
30.77733
Ch :
Cc lnh c lng c th c s dng khi bit cc tham s v mu. y c
th c gi l cc lnh s dng tham s trc tip (Commands using
immediate arguments). Cc lnh ny rt hu dng khi chng ta khng c s
liu gc v bin.
cii <s quan st> <gi tr trungbnh> < lch chun> [,
level(#) ] (phn phi chun)
cii <s quan st> <s ln thnh cng ca quan st> [, level(#) ]
(phn phi nh thc)
47
1.2.
859
---------------------------------------------------------------------------Variable |
Mean
Std. Err.
z
P>|z|
[95% Conf. Interval]
---------+-----------------------------------------------------------------poor | .4982538
.0170597
29.2065
0.0000
.4648174 .5316901
---------------------------------------------------------------------------Ho: proportion(poor) = .44
Ha: poor < .44
z = 3.440
P < z = 0.9997
prtest <bin 1> = <tn bin2> [iu kin] [phm vi] [, level(#)]
48
1175
754
-----------------------------------------------------------------------------Variable |
Mean
Std. Err.
z
P>|z|
[95% Conf. Interval]
---------+-------------------------------------------------------------------poor2 | .2365957
.0123983
19.0829
0.0000
.2122955
.2608959
poor4 | .2864721
.016465
17.3989
0.0000
.2542014
.3187429
---------+-------------------------------------------------------------------diff | -.0498764
.020611
-.0902732
-.0094796
| under Ho:
.0203666 -2.44893
0.0143
-----------------------------------------------------------------------------Ho: proportion(poor2) - proportion(poor4) = diff = 0
Ha: diff < 0
z = -2.449
P < z = 0.0072
prtest <bin>
[level(#)]
Ha: diff ~= 0
z = -2.449
P > |z| = 0.0143
[iu
kin]
[phm
vi],
by(bin
phn
nhm)
1: Number of obs =
2: Number of obs =
4375
1624
-----------------------------------------------------------------------------Variable |
Mean
Std. Err.
z
P>|z|
[95% Conf. Interval]
---------+-------------------------------------------------------------------1 |
.3248
.00708
45.8755
0.0000
.3109234
.3386766
2 | .2192118
.0102661
21.353
0.0000
.1990906
.239333
---------+-------------------------------------------------------------------diff | .1055882
.0124708
.0811459
.1300304
| under Ho:
.0132673
7.95855
0.0000
-----------------------------------------------------------------------------Ho: proportion(1) - proportion(2) = diff = 0
49
Ha: diff ~= 0
z = 7.959
P > |z| = 0.0000
(one-sided test)
(one-sided test)
(two-sided test)
(one-sided test)
(one-sided test)
(two-sided test)
ttest
rlpcex1=3200
One-sample t test
-----------------------------------------------------------------------------Variable |
Obs
Mean
Std. Err.
Std. Dev.
[95% Conf. Interval]
50
---------+-------------------------------------------------------------------rlpcex1 |
5999
3188.667
34.76379
2692.567
3120.518
3256.817
-----------------------------------------------------------------------------Degrees of freedom: 5998
Ho: mean(rlpcex1) = 3200
Ha: mean < 3200
t = -0.3260
P < t =
0.3722
ttest <bin 1> = <bin 2> [iu kin] [phm vi] [, unpaired
unequal level(#) ]
Lnh ny thc hin kim nh gi thuyt rng hai bin c gi tr trung
bnh bng nhau. (Ho:
= Y).
Cc tu chn:
unpaired
unequal
V d:
. ttest poor2=poor4, unpaired unequal
Two-sample t test with unequal variances
-----------------------------------------------------------------------------Variable |
Obs
Mean
Std. Err.
Std. Dev.
[95% Conf. Interval]
---------+-------------------------------------------------------------------poor2 |
1175
.2365957
.0124036
.425173
.2122601
.2609314
poor4 |
754
.2864721
.0164759
.4524128
.254128
.3188163
---------+-------------------------------------------------------------------combined |
1929
.2560912
.0099404
.436586
.2365962
.2755863
---------+-------------------------------------------------------------------diff |
-.0498764
.0206229
-.0903285
-.0094243
-----------------------------------------------------------------------------Satterthwaite's degrees of freedom: 1532.64
Ho: mean(poor2) - mean(poor4) = diff = 0
Ha: diff < 0
t = -2.4185
P < t =
0.0079
Ha: diff ~= 0
t = -2.4185
P > |t| =
0.0157
ttest <bin> [iu kin] [phm vi], by(bin phn nhm) [ unequal
level(#) ]
Lnh ny thc hin kim nh gi thuyt v s bng nhau ca gi tr
trung bnh ca hai nhm c ch ra bi bin phn nhm (Ho:
X1
X2
).
V d:
51
. ttest
rlpcex1, by(sex)
Ha: diff ~= 0
t = -9.8880
P > |t| =
0.0000
rlpcex1
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+----------------------------------------------------rlpcex1 |
5999
3188.667
2692.567
357.318
45801.71
. sdtest rlpcex1=2700
One-sample test of variance
-----------------------------------------------------------------------------Variable |
Obs
Mean
Std. Err.
Std. Dev.
[95% Conf. Interval]
---------+-------------------------------------------------------------------rlpcex1 |
5999
3188.667
34.76379
2692.567
3120.518
3256.817
-----------------------------------------------------------------------------Ho: sd(rlpcex1) = 2700
chi2(5998) = 5965.022
Ha: sd(rlpcex1) < 2700
52
covariance
_coef
wrap
V d:
. corr hhsize poor
(obs=5999)
rlpcex1 sex
|
hhsize
poor rlpcex1
sex
-------------+-----------------------------------hhsize |
1.0000
poor |
0.2425
1.0000
rlpcex1 | -0.2172 -0.4452
1.0000
sex | -0.2570 -0.1028
0.1267
1.0000
Variable |
Mean
Std. Dev.
Min
Max
-------------+---------------------------------------------------hhsize |
4.752292
1.954292
1
19
poor |
.296216
.4566255
0
1
rlpcex1 |
3188.667
2692.567
357.318
45801.71
sex |
1.270712
.4443645
1
2
|
hhsize
poor rlpcex1
sex
-------------+------------------------------------
53
hhsize
poor
rlpcex1
sex
| 3.81926
| .216435 .208507
| -1142.93 -547.335
| -.223195 -.020849
7.2e+06
151.543
.19746
pwcorr
[danh sch bin] [quyn s] [iu kin] [phm vi] [, obs
sig print(#) star(#)]
Lnh ny tnh h s tng quan cho tng cp bin c ch ra bi danh
sch bin.
Cc tu chn:
obs
sig
print(#)
star(#)
V d:
. pwcorr hhsize poor rlpcex1 sex, obs sig star(5)
|
hhsize
poor rlpcex1
sex
-------------+-----------------------------------hhsize |
1.0000
|
|
5999
|
poor |
0.2425* 1.0000
|
0.0000
|
5999
5999
|
rlpcex1 | -0.2172* -0.4452* 1.0000
|
0.0000
0.0000
|
5999
5999
5999
|
sex | -0.2570* -0.1028* 0.1267* 1.0000
|
0.0000
0.0000
0.0000
|
5999
5999
5999
5999
|
pcorr <bin> <danh sch bin> [quyn s] [iu kin] [phm vi]
Lnh ny tnh h s tng quan ca bin c ch ra bi tn bin vi cc
bin c trong danh sch bin
V d:
54
rlpcex1 sex
|
poor
hhsize rlpcex1
sex
-------------+-----------------------------------poor |
1.0000
hhsize |
0.2425
1.0000
rlpcex1 | -0.4452 -0.2172
1.0000
sex | -0.1028 -0.2570
0.1267
1.0000
rlpcex1
reg7
sex
hhsize
Source |
SS
df
MS
-------------+-----------------------------Model | 3.8639e+09
3 1.2880e+09
Residual | 3.9621e+10 5995 6609032.15
-------------+-----------------------------Total | 4.3485e+10 5998 7249918.40
Number of obs
F( 3, 5995)
Prob > F
R-squared
Adj R-squared
Root MSE
=
=
=
=
=
=
5999
194.88
0.0000
0.0889
0.0884
2570.8
-----------------------------------------------------------------------------rlpcex1 |
Coef.
Std. Err.
t
P>|t|
[95% Conf. Interval]
-------------+---------------------------------------------------------------reg7 |
240.9633
15.5905
15.46
0.000
210.4003
271.5263
sex |
403.2984
77.38324
5.21
0.000
251.5994
554.9974
hhsize | -305.6382
17.70692
-17.26
0.000
-340.3501
-270.9263
_cons |
3160.201
155.6576
20.30
0.000
2855.056
3465.346
------------------------------------------------------------------------------
Cc tu chn:
level(#)
noconstant
noheader
beta
poor
Iteration
Iteration
Iteration
Iteration
0:
1:
2:
3:
reg7 sex
log
log
log
log
hhsize
likelihood
likelihood
likelihood
likelihood
=
=
=
=
-3645.1363
-3367.2185
-3364.8032
-3364.8025
Probit estimates
Number of obs
LR chi2(3)
Prob > chi2
Pseudo R2
=
=
=
=
5999
560.67
0.0000
0.0769
-----------------------------------------------------------------------------poor |
Coef.
Std. Err.
z
P>|z|
[95% Conf. Interval]
-------------+---------------------------------------------------------------reg7 |
-.116342
.0084551
-13.76
0.000
-.1329136
-.0997703
sex | -.1284525
.0422247
-3.04
0.002
-.2112113
-.0456937
hhsize |
.1808115
.0095806
18.87
0.000
.1620338
.1995892
_cons | -.8088731
.0824798
-9.81
0.000
-.9705306
-.6472157
------------------------------------------------------------------------------
stdp
redid
c lng gi tr phn d:
e i = Yi Y
i
V d:
56
predict exphat, xb
To ra bin mi exphat c gi tr c lng ca bin ph thuc (fitted value)
theo h s thu c t hm hi quy.
predict expres, resid
To ra bin expres c gi tr ca phn d.
Kim nh v h s ca hm hi quy
C php:
test [gi tr biu thc]
test [danh sch bin]
testparm <danh sch bin> [, equal ]
Lnh test kim nh cc gi thit v h s ca hm hi quy va mi c c
lng
V d:
test urban98 =2000
Kim nh gi thit h s ca bin urban98 = 0
test region1 = region2
Kim nh gi thit h s ca bin region1 bng h s ca bin region2
test region1 = (region2+region3)/2
Kim nh gi thit v quan h gia cc h s ca bin region1, region2,
va region3
test region1 region2 region3
Kim nh gi thit h s ca bin region1, region2, va region3 u
bng 0
testparm region*
Kim nh gi thit v ca h s ca bin region1 n region7 u
bng 0
57
------------+----------------------------------Total |
5999
100.00
. reg
educyr98 hhsize
Source |
SS
df
MS
-------------+-----------------------------Model | 1.6960e+10
10 1.6960e+09
Residual | 2.6525e+10 5988 4429712.49
-------------+-----------------------------Total | 4.3485e+10 5998 7249918.40
Number of obs
F( 10, 5988)
Prob > F
R-squared
Adj R-squared
Root MSE
=
=
=
=
=
=
5999
382.87
0.0000
0.3900
0.3890
2104.7
-----------------------------------------------------------------------------rlpcex1 |
Coef.
Std. Err.
t
P>|t|
[95% Conf. Interval]
-------------+---------------------------------------------------------------urban98 |
1995.163
66.46943
30.02
0.000
1864.859
2125.467
region1 | -923.7066
132.8334
-6.95
0.000
-1184.108
-663.3052
region2 | -362.6047
130.2254
-2.78
0.005
-617.8934
-107.316
region3 | -558.0354
137.1551
-4.07
0.000
-826.9089
-289.1619
region4 | -100.7586
135.8372
-0.74
0.458
-367.0486
165.5313
region5 | (dropped)
region6 |
1742.688
131.9928
13.20
0.000
1483.934
2001.441
region7 |
151.9854
128.0272
1.19
0.235
-98.99396
402.9648
sex |
270.9142
66.61031
4.07
0.000
140.3339
401.4944
educyr98 |
153.3281
6.836934
22.43
0.000
139.9253
166.731
hhsize |
-257.691
14.73741
-17.49
0.000
-286.5816
-228.8004
_cons |
2362.355
178.3197
13.25
0.000
2012.784
2711.926
-----------------------------------------------------------------------------. test
( 1)
urban98 =2000
urban98 = 2000.0
F(
. test
( 1)
( 1)
( 1)
( 2)
( 3)
1, 5988) =
Prob > F =
34.57
0.0000
region1 = (region2+region3)/2
region1 - .5 region2 - .5 region3 = 0.0
F(
. test
0.01
0.9420
region1 = region2
F(
. test
1, 5988) =
Prob > F =
1, 5988) =
Prob > F =
27.80
0.0000
3, 5988) =
Prob > F =
20.22
0.0000
58
. testparm
(
(
(
(
(
(
(
1)
2)
3)
4)
5)
6)
7)
region*
region1 = 0.0
region2 = 0.0
region3 = 0.0
region4 = 0.0
region5 = 0.0
region6 = 0.0
region7 = 0.0
Constraint 5 dropped
F(
6, 5988) =
Prob > F =
148.55
0.0000
Chng IV: V th
1. V th (graph)
C php:
graph [danh sch bin] [quyn s] [iu kin] [phm vi] [,
loi__th tu_chn_ring tu_chn_chung]
Trong :
loi__th (graph_type)
Ch ra loi th cn v
tu_chn_ring (specific_options)
th
tu_chn_chung (common_options)
Cc tu chn c th s dng chung
cho cc loi th nh tu chn v nh
nhn trn cc trc ca th
45801.7
357.318
16
95
95
19
45801.7
comp.M&Reg price
adj.pc tot exp
357.318
95
Age of household
head
16
22
schooling year
of HH.head
0
19
Household size
1
357.318
45801.7
22
60
Fraction
.329888
0
357.318
45801.7
357.318
45801.71
61
357.318
63
Audi 5000
Audi Fox
BMW 320i
Datsun 200
Datsun 210
Price
Mileage (mpg)
Repair Record 1978
Datsun 510
Datsun 810
Fiat Strada
Honda Accord
Honda Civic
Headroom (in.)
Trunk space (cu. ft.)
Weight (lbs.)
Length (in.)
Mazda GLC
Renault
Subaru
Toyota Celica
Toyota Corolla
Toyota Corona
VW Dasher
VW Diesel
VW Rabbit
VW Scirocco
Volvo 260
hhsize, sum
(rlpcex1)
sum(educyr98)
Summary of schooling year of
HH.head
Mean
Std. Dev.
Freq.
64
------------+-----------------------------------1 |
3.7897196
4.3956537
214
2 |
5.7545272
4.7225549
497
3 |
7.3023256
4.6396425
731
4 |
8.2578348
4.2659841
1404
5 |
7.7243298
4.2998488
1318
6 |
6.8788927
4.0778062
867
7 |
6.3348958
4.1241759
480
8 |
5.7333333
3.9623557
255
9 |
5.7936508
3.4878474
126
10 |
6.1724138
3.1851516
58
11 |
4.7931034
3.1665586
29
12 |
4.4444444
3.6438685
9
13 |
5
5.0990195
4
14 |
3
2.1602469
4
16 |
4
1.4142136
2
19 |
2
0
1
------------+-----------------------------------Total |
7.0944185
4.4160917
5999
. replace meanexp= meanexp/1000
(16 real changes made)
. replace meanexp= meanexp/1000
. rename var71 ahhsize
. rename var72 meanexp
. rename var73 meanedu
. replace meanexp= meanexp/1000
. label var meanexp Chi tieu binh quan
. label var meanedu So nam hoc
. label var ahhsize Quy mo ho
meanedu
8.25783
1.57978
1
ahhsize
19
65
* La chn v tiu :
title("chui k t") t1title("chui k t") t2title("chui k t")
b1title("chui k t") b2title("chui k t") l1title("chui k t")
l2title("chui k t") r1title("chui k t") r2title("chui k t")
Lnh ny ghi cc tiu trn pha trn (top), pha di (bottom), bn tri
(left) v bn phi (right) th.
V d:
gr meanexp meanedu ahhsize, title (Do thi chi tieu va hoc van chu ho)
l1title(Chi tieu binh quan (tr dong)) l2title(So nam hoc cua chu ho) b2title
(Quy mo ho gia dinh)
Chi tieu binh quan
So nam hoc
8.25783
1.57978
1
19
* Hin th gi tr trc th
xlabel[(gi tr s)] ylabel[(gi tr s)] rlabel[(gi tr s)] tlabel[(gi
tr s)]
V d:
gr meanexp meanedu ahhsize, title (Do thi chi tieu va hoc van chu ho)
l1title(Chi tieu binh quan (tr dong)) l2title(So nam hoc cua chu ho) b2title
(Quy mo ho gia dinh) xlabel ylabel
66
So nam hoc
2
0
10
Quy mo ho gia dinh
15
20
67
So nam hoc
2
0
10
Quy mo ho gia dinh
15
20
68
So nam hoc
8
4000
6
3000
So nam hoc
5000
4
2000
1000
2
0
10
Quy mo ho gia dinh
15
20
2.2. th tn sut
C php:
graph [bin] [quyn s] [iu kin] [phm vi],
[tu_chn_chung bin(#) freq normal[(#,#)] density(#)]
histogram
Cc tu chn:
bin(#)
Freq
normal[(#,#)]
density(#)]
V d:
th tn sut ca chi tiu binh qun u ngi
. gr rlpcex1, hist bin(20) normal
69
Fraction
.56026
0
357.318
45801.7
Frequency
1979
0
357.318
45801.7
70
region1
region2
region3
region4
region5
region6
415
Frequency
415
0
357.318
region7
45801.7
357.318
45801.7
415
0
357.318
45801.7
2.3. th hnh ct
C php:
graph [danh sch bin] [quyn s] [iu kin] [phm vi], bar
[tu_chn_chung [no]alt means stack]
V d:
th gi tr trung bnh hc vn ca ch h v quy m h gia nh theo
7 vng
. gr educyr98 hhsize, bar means by(reg7)
schooling year of HH.head
Household size
8.64426
71
Household size
10
region1
region2
region3
region4
region5
region6
region7
La chn stack
. gen persons=1
. gr persons urban98, bar ylabel by(reg7) stack alt
72
persons
1500
1000
500
region1
region2
region3
region4
region5
region6
region7
V d:
Hy v th sau:
foodpoor
poor
600
400
200
region1
region2
region3
region4
region5
region6
region7
. gr poor1-poor7, pie
24% poor1
16% poor2
16% poor3
12% poor4
10% poor5
4% poor6
18% poor7
74
region1
region2
region3
12% foodpoor
18% poor but still above food povert
70% nonpoor
region4
region5
region7
region6
Total
region2
region3
persons
12% foodpoor
18% poor but still above food povert
1500
70% nonpoor
region4
region7
region5
region6
1000
500
Total
region1
region2
region3
region4
region5
region6
region7
24% poor1
16% poor2
16% poor3
12% poor4
10% poor5
4% poor6
18% poor7
76
Sau khi son tho, do-file s c lu tr bng tu chn Save as trong thc
n File ca ca s do-file editor. Tn ca do-file c th c ch ra ngay
ti lnh doedit nh sau:
doedit (tn do-file)
Tp do-file c phn m rng l do.
v d trn chng ta c th lu tr on chng trnh di tn l chng
trnh 1 ti th mc Vlss98 trn a C.
77
Freq.
Percent
Cum.
------------+----------------------------------Rural |
Urban |
4269
1730
71.16
28.84
71.16
100.00
------------+----------------------------------Total |
5999
100.00
. sum hhsize
Variable |
Obs
Min
Max
-------------+----------------------------------------------------hhsize |
5999
4.752292 1.954292
19
. gen new=hhsizet
hhsizet not found
r(111);
78
end of do-file
r(111);
Vi tu chn nostop
. do "c:\vlss98\chuong trinh 1", nostop
. clear
. set mem 32m
(32768k)
. use "C:\VLSS98\Hhexp98n.dta", clear
. tab urban98
1:urban 98; |
0:rural 98 |
Freq.
Percent
Cum.
------------+----------------------------------Rural |
4269
Urban |
1730
71.16
28.84
71.16
100.00
------------+----------------------------------Total |
5999
100.00
. sum hhsize
Variable |
Obs
Min
Max
-------------+----------------------------------------------------hhsize |
5999
4.752292 1.954292
19
. gen new=hhsizet
hhsizet not found
r(111);
. gen new=hhsize
. end of do-file
Thc hin (chy) bng lnh run
. run "c:\vlss98\chuong trinh 1", nostop
hhsizet not found
79
graph meanexp meanedu ahhsize, title (Do thi chi tieu va hoc van chu
ho) /*
*/ l1title(Chi tieu binh quan (tr dong)) l2title(So nam hoc cua chu
ho) /*
*/ b2title (Quy mo ho gia dinh) xlabel ylabel xline (5 10 to 20)
yline(2 4 to 8) connect(ll);
-
age
hhsize
rlpcex1
. `tb' `hogd'
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+-----------------------------------------------------------82
16
95
1
19
357.318 45801.71
. macro list _a
local macro `a' not found
r(111);
Trong khi i vi global macros
. do "C:\WINDOWS\TEMP\STD010000.tmp"
. global b "chuong trinh thong ke Stata"
. end of do-file
. macro list b
b:
A[3,3]
c1 c2 c3
r1
r2
r3 10 11 14
y ma trn A bao gm 9 phn t (element): 1, 2, 4, 3, 4, 7, 10, 11, 14.
Cc ct c t tn l c1, c2, v c3, v cc hng l r1, r2, v r3. Phn t l
giao im ca dng 1 v ct 2 c k hiu l A[1, 2]. Trong v d ny A[1,
2] cha gi tr bng 2.
3.2. Tch v hng (scalar)
Tch v hng cha 1 phn t l s. Tch v hng c nh ngha bng lnh
sau:
scalar scalar_name = expression
V d:
. scalar a = 10
. scalar list a
85
a = 10
. scalar b = a* 2
. scalar list b
b=
20
= To ra vct ct
quymo
thunhap
1. 101 6 1200
2. 103 5 1400
3. 105 5 3200
4. 107 9 1000
5. 109 4 2500
6. end
86
quymo thunhap
r1
101
1200
r2
103
1400
r3
105
3200
r4
107
1000
r5
109
2500
=B
matrix C
= (C+C)/2
matrix D = A*A
Xo ma trn
Ma trn v tch v hng c th xo khi b nh bng lnh:
matrix drop <ma trn>
scalar drop <tch v hng>
V d:
. matrix drop A
. scalar drop B
4. Lnh iu kin v vng lp
4.1. Lnh ifelse
C php:
iu kin (iu kin logic) {
Nhm cu lnh 1
}
else Cu lnh
Stata s kim tra iu kin logic (expression), nu iu kin ny ng
th cc lnh Nhm cu lnh 1 s c thc hin, nu iu kin sai th
lnh ng sau else s c thc hin, trong trng hp else khng c ch
ra th Stata s thc hin cc lnh sau lnh if {}.
V d:
87
----------------local a=invnorm(uniform())
if `a'>=0 {
display "So ngau nhien tao ra lon hon hoac bang 0"
}
else di "So ngau nhien tao ra nho hon 0"
macro list _a
Ch :
}
else {
comands 2
}
-
}
else iu kin (iu kin) {
.
4.2. Lnh while
C php:
while <iu kin logic> {
Nhm cu lnh
}
Stata s kim tra iu kin logic (expression), nu iu kin ny ng
th cc lnh Nhm cu lnh s c thc hin, nu iu kin sai th
cc lnh ny s khng c thc hin.
V d:
local i=1
while `i<= 10 {
88
if mod(`i',2) {
display "`i' is odd"
}
else {
display "`i' is even"
}
local i=`i+1
}
Ch :
Vng lp c th c dng li nu s dng tu chn sau y gia vng
lp:
continue [, break]
Nu gp lnh continue, Stata s b qua cc lnh sau v quay li lnh
u tin ca vng lp. Nu c tu chn break c ch ra th Stata s
thot khi vng lp.
V d: Tm tch s chung nh nht ca 2, 3 v 5
local i=1
while `i<= 1000 {
if mod(`i',2)==0 & mod(`i',3)==0 & mod(`i',5)==0 {
V d:
quietly program define povline
display as text _col(3) "Poverty line" _col(16) "{c |}" _col(20) "Food"
_col(30) "Overall"
di as text _col(2) "{hline 14}{c +}{hline 26}"
di as text _col(8) "Value" _col(16) "{c |}" as result _col(20) "1380"
_col(33) "1920"
end
Sau khi chng ta chy lnh ny bng run hoc do, th ti ca s
command, chng ta g:
. povline
Poverty line | Food
Overall
---------------+-------------------------Value
| 1380
1920
Ch :
Nu chng ta chy li lnh program define povline, v nhn c thng
bo:
povline already defined
r(110);
Tc l chng trnh povline c to ra ri, xo chng ny i th
chng ta dng lnh:
program drop poveline
hoc xo tt c cc chng trnh
program drop _all
Ado-file
Cc ado-file to ra cc lnh ca Stata. Trong Stata c hai loi lnh. Loi th
nht c vit trong Stata, v d nh lnh summarize. Loi th hai c
nh ngha bi cc tp ado, v d nh lnh ci.
bit c lnh Stata thuc loi no, g lnh which:
. which sum
built-in command: summarize
. which ci
C:\STATA\ado\base\c\ci.ado
90
Ph lc
Cc thng k c bn ca mu tun theo quy lut chun
Trung bnh:
n
x=
i =1
Phng sai:
n
s2 =
(x
i =1
x)2
n 1
lch chun:
s =
s2
MAD =
i =1
lch:
n
Skewness =
(x
x)3 / n
i =1
s3
nhn:
n
Kurtosis =
(x
x)4 / n
i =1
s4
92