*! version 3.0.0 10/22/93 sed9: STB-24 program define running version 3.0 if "`*'"=="" { di in bl "-> running yvar [xvar] [if] [in] [,[noGraph] [gen(newvar)] [logit]" di in bl _skip(10) "[Knn(#)] [mean] [repeat(#)] [double] [graph_options]]" exit 198 } local varlist "req ex min(1) max(2)" local if "opt" local in "opt" #delimit ; local options "noGraph GEN(string) Knn(int 0) Double Repeat(int 1) Mean LOGit SPan(real 0) TItle(string) Symbol(string) Connect(string) *"; #delimit cr parse "`*'" if "`gen'"~="" { confirm new var `gen' } parse "`varlist'", parse(" ") tempvar Y X smooth sy rsy k quietly { gen `Y'=`1' `if' `in' _crcslbl `Y' `1' if "`2'"!=""{ gen `X'=`2' `if' `in' _crcslbl `X' `2' } else { gen `X'=_n `if' `in' lab var `X' "n" } quietly replace `X'=. if `Y'==. count if `X'!=. local cnt = _result(1) if `cnt'<3 { noisily error 2001 } * if "`nosort'"!=""{preserve} sort `X' local xcen=(`X'[1]+`X'[`cnt'])/2 replace `X'=`X'-`xcen' sum `Y' local ycen=_result(3) replace `Y'=`Y'-`ycen' if "`double'"!=""{local repeat =2*`repeat'} if `repeat'>11{ local repeat 11 noisily di in red "repeat set to 11" } if `span'!=0 { local knn=(`cnt'*`span'-1)/2 } else { if `knn' == 0 {local knn = (`cnt')^(.67) } local span=(2*`knn'+1)/`cnt' } global S_1 `knn' global S_2 `span' local kk = int(`knn'/sqrt(`repeat') +.5) if `kk'==0 { noisily di in red "Span too small. Increase span or knn." exit 2002 } if `kk'>`cnt'/2 { noisily di in red "Span too big. Decrease span or knn." exit 2003 } gen `sy'=(`X'==`X'[_n-1] |`X'==`X'[_n+1]) in 1/`cnt' gen `rsy'=sum(`sy') in 1/`cnt' gen `smooth'=`Y' local ties=`rsy'[`cnt'] if `ties'>0 { tempvar xc gen `xc'=`X' if `sy'==1 sort `xc' by `xc':replace `sy'=sum(`smooth') by `xc':replace `smooth'=`sy'[_N]/_N local ties=`ties'+1 if `ties'<=_N{ replace `smooth'=`Y' in `ties'/l } sort `X' } gen int `k'=2*`kk'+1 replace `k'=_n+`kk' in 1/`kk' local k2=`cnt'-`kk' replace `k'=(`cnt'-_n+1)+`kk' in `k2'/`cnt' local k1=`kk'+2 if "`mean'"=="" { local mean "line" tempvar sxy rsxy alpha beta rsx rsxx gen `sxy'=. gen `rsxy'=. gen `alpha'=sum(`X') gen `beta'=sum(`X'^2) #delimit ; gen `rsx'=cond(_n<`cnt'/2,`alpha'[`k']/`k', (`alpha'[`cnt']-`alpha'[`cnt'-`k'])/`k') ; replace `rsx'=(`alpha'[_n+`kk']-`alpha'[_n-`kk'-1])/`k' in `k1'/`k2' ; gen `rsxx'=cond(_n<`cnt'/2,`beta'[`k']/`k', (`beta'[`cnt']-`beta'[`cnt'-`k'])/`k') ; replace `rsxx'=(`beta'[_n+`kk']-`beta'[_n-`kk'-1])/`k' in `k1'/`k2'; #delimit cr } while `repeat'>0 { replace `sy'=sum(`smooth') #delimit ; replace `rsy'=cond(_n<`cnt'/2,`sy'[`k']/`k', (`sy'[`cnt']-`sy'[`cnt'-`k'])/`k') ; replace `rsy'=(`sy'[_n+`kk']-`sy'[_n-`kk'-1])/`k' in `k1'/`k2' ; #delimit cr if "`mean'"=="line" { replace `sxy'=sum(`X'*`smooth') #delimit ; replace `rsxy'=cond(_n<`cnt'/2,`sxy'[`k']/`k', (`sxy'[`cnt']-`sxy'[`cnt'-`k'])/`k') ; replace `rsxy'=(`sxy'[_n+`kk']-`sxy'[_n-`kk'-1])/`k' in `k1'/`k2' ; replace `beta'=(`rsxy'-`rsx'*`rsy') / (`rsxx'-`rsx'*`rsx') ; #delimit cr replace `alpha'=`rsy'-`beta'*`rsx' replace `smooth'=`alpha'+`beta'*`X' } else {replace `smooth'=`rsy'} local repeat=`repeat'-1 } if `ties'>0 { sort `xc' by `xc':replace `sy'=sum(`smooth') by `xc':replace `smooth'=`sy'[_N]/_N if `xc'!=. sort `X' } replace `X'=`X'+`xcen' replace `Y'=`Y'+`ycen' replace `smooth'=`smooth'+`ycen' if "`mean'"=="mean"{ local k2=`k2'+1 replace `smooth'=. in 1/`kk' replace `smooth'=. in `k2'/l } if "`logit'" == "logit" { local adj = 1/`cnt' replace `smooth' = `adj' if `smooth'<`adj' replace `smooth' = 1-`adj' if `smooth'>(1-`adj') &`smooth'!=. replace `smooth' = log(`smooth'/(1-`smooth')) sum `smooth' local adj=(_result(6)-_result(5))/25 #delimit ; replace `Y'=`Y'*(_result(6) +(1.3+uniform()) *`adj') +(1-`Y')*(_result(5) -(1.3+uniform()) *`adj'); #delimit cr } * GRAPH (If required) if "`graph'" ~= "nograph" { if "`title'" ==""{ local title "Running `mean' smoother" } if "`symbol'"==""{ local symbol "oi" } if "`connect'"==""{ local connect ".l" } #delimit ; noisily graph `Y' `smooth' `X', `options' ti(`title') s(`symbol') c(`connect') ; #delimit cr } if "`gen'"~=""{ rename `smooth' `gen' } } end