📄 sktest.ado
字号:
*! version 2.1.5 13sep2000/28sep2004
* originally by P. Royston, modified by StataCorp
program define sktest, rclass
version 6, missing
global S_1 /* Pr(skew) */
global S_2 /* Pr(Kurtosis) */
global S_3 /* chi-square(2) */
global S_4 /* Pr(chi2) */
syntax varlist [if] [in] [aw fw] [, noAdjust]
tempvar touse
mark `touse' `if' `in' /* but do not markout varlist */
tokenize `varlist'
if "`adjust'" ~= "noadjust" {
local adj "adj"
}
else {
local adj " "
}
#delimit ;
di in smcl _n _col(20) in gr "Skewness/Kurtosis tests for Normality" _n
_col(50) "{hline 7} joint {hline 6}"_n
" Variable {c |} Pr(Skewness) Pr(Kurtosis)"
" `adj' chi2(2) Prob>chi2" _n
"{hline 13}{c +}{hline 55}" ;
#delimit cr
while ("`1'"!="") {
quietly sum `1' if `touse' [`weight'`exp'], detail
if r(N) < 8 {
local Z1 .
local Z2 .
local K2 .
local P .
}
else {
#delimit ;
local n = r(N);
local Y = r(skewness)*sqrt(
( (`n'+1)*(`n'+3)) / ( 6*(`n'-2) ) ) ;
local Beta2=
(
3*(`n'*`n' + 27*`n'-70)*(`n'+1)*(`n'+3)
) / (
(`n'-2)*(`n'+5)*(`n'+7)*(`n'+9)
) ;
local W2 = -1 + sqrt(2*(`Beta2'-1)) ;
local delta = 1/sqrt(log(sqrt(`W2'))) ;
local alpha = sqrt(2/(`W2'-1)) ;
local Z1= `delta'*log(
`Y'/`alpha' + sqrt((`Y'/`alpha')^2+1) ) ;
local Eb2=(3*(`n'-1)) / (`n'+1) ;
local Vb2 =
(
24*`n'*(`n'-2)*(`n'-3)
) / (
(`n'+1)^2 * (`n'+3)*(`n'+5)
) ;
local X = (r(kurtosis)-`Eb2')/sqrt(`Vb2') ;
local RBeta1 =
(
(
6*(`n'*`n'-5*`n'+2)
) / (
(`n'+7)*(`n'+9)
)
) * sqrt(
(
6*(`n'+3)*(`n'+5)
) / (
`n'*(`n'-2)*(`n'-3)
)
) ;
local A = 6 + (8/`RBeta1')*(
2/`RBeta1' + sqrt(1+4/((`RBeta1')*(`RBeta1')))
) ;
local Z2 = (
(1-2/(9*`A'))
- ( (1-2/`A')/(1+`X'*sqrt(2/(`A'-4))) )^(1/3)
)
/ sqrt(2/(9*`A')) ;
local K2 = `Z1'*`Z1' + `Z2'*`Z2' ;
/*
Start of P Royston modification 2.
*/
#delimit cr
if "`adjust'" ~= "noadjust" {
local ZC2 = -invnorm(exp(-0.5*`K2'))
local logn = log(`n')
local cut = .55*(`n'^.2)-.21
local a1 = (-5+3.46*`logn')*exp(-1.37*`logn')
local b1 = 1+(.854-.148*`logn')*exp(-.55*`logn')
local b2mb1 = 2.13/(1-2.37*`logn')
local a2 = `a1'-`b2mb1'*`cut'
local b2 = `b2mb1'+`b1'
if `ZC2'<-1 {
local Z = `ZC2'
}
else if `ZC2'<`cut' {
local Z = `a1'+`b1'*`ZC2'
}
else {
local Z=`a2'+`b2'*`ZC2'
}
local P = 1-normprob(`Z')
local K2 = -2*log(`P')
}
else {
local P = chiprob(2,`K2')
}
/*
End of P Royston modification 2.
*/
}
ret scalar P_skew = 2-2*normprob(abs(`Z1'))
ret scalar P_kurt = 2-2*normprob(abs(`Z2'))
ret scalar chi2 = `K2'
ret scalar P_chi2 = `P'
/* Double saves */
global S_1 `return(P_skew)'
global S_2 `return(P_kurt)'
global S_3 `return(chi2)'
global S_4 `return(P_chi2)'
#delimit ;
di in smcl in gr %12s abbrev("`1'",12) " {c |}" in ye
_col(21) %5.3f 2-2*normprob(abs(`Z1'))
_col(35) %5.3f 2-2*normprob(abs(`Z2'))
_col(47) %9.2f `K2'
_col(63) %6.4f `P' ; /* P Royston modification 3 */
#delimit cr
mac shift
}
end
exit
/*
modification 2.
Empirical adjustment to chi-square(2) statistic.
ZC2 is normal deviate corresponding to putative
chisq(2) K2. This is adjusted to a final normal
deviate Z, from which the P-value for the test
is calculated as its upper tail area.
The original chi-sq is adjusted so that the reported
P value is the same as would be obtained from tables.
Also the P value for kurtosis is adjusted.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -