📄 tssmooth.ado
字号:
if "`samp0'" != "" & "`s0'" != "" {
di as err "specify samp0() or s0(), not both"
exit 198
}
if "`samp0'" != "" {
local s0samp "s0samp(`samp0')"
}
if "`s0'" != "" {
tokenize `s0'
if "`1'" == "" | "`2'" =="" {
di as err "s0(`s0') invalid"
exit 198
}
local s10 "s10(`1')"
local s20 "s20(`2')"
}
if "`forecast'" != "" {
local fcast "fcast(`forecast')"
}
if "`parms'" != "" {
qui gen `new'=.
capture noi `byp' _ts_dexp `new' `old', /*
*/ a(`parms') `s0samp' touse(`touse') /*
*/ `fcast' `s10' `s20' `display' `fcomp'
ret add
if _rc > 0 {
local rc = _rc
capture drop `nname'
exit `rc'
}
ret local timevar `tvar'
ret local panelvar `pvar'
local myexp2 : subinstr local myexp2 "= " ""
local myexp2 : subinstr local myexp2 "=" ""
ret local exp "`myexp2'"
ret local method "dexponential"
exit
}
/* if here then opt via bisection assumed */
qui gen `new'=.
capture noi `byp' _ts_dexp `new' `old', a(opt) /*
*/ `s0samp' touse(`touse') `fcast' /*
*/ `s10' `s20' `display' /*
*/ `fcomp'
ret add
if _rc > 0 {
local rc = _rc
capture drop `nname'
exit `rc'
}
local lbl : var label `nname'
char `nname'[tssmoother] `lbl'
ret local timevar `tvar'
ret local panelvar `pvar'
local myexp2 : subinstr local myexp2 "= " ""
local myexp2 : subinstr local myexp2 "=" ""
ret local exp "`myexp2'"
ret local method "dexponential"
exit
}
if "`hwinters'" != "" {
local 0 " `opts'"
syntax , [ Parms(numlist min=2 max=2 >=0 <=1) /*
*/ SAmp0(numlist max=1 >1 <=`Nobs' /*
*/ integer) /*
*/ s0(numlist min=2 max=2 ) /*
*/ Forecast(numlist max=1 >=1 <=500 /*
*/ integer ) /*
*/ Diff /*
*/ FRom(numlist min=2 max=2 >=0 <=1) /*
undocumented */ noDIsplay /*
*/ noLOG /*
*/ replace /*
*/ NODIFficult /*
undoc (err checking) */ DIFficult /*
*/ * ]
mlopts mlopts, `options'
if "`nodifficult'" != "" & "`difficult'" != "" {
di as err "nodifficult cannot be specified " /*
*/ "with difficult"
exit 198
}
if "`nodifficult'" == "" {
local difficult difficult
}
if "`samp0'" != "" & "`s0'" != "" {
di as err "specify samp0() or s0(), not both"
exit 198
}
if "`s0'" != "" & "`diff'" != "" {
di as err "specify diff or s0(), not both"
exit 198
}
if "`samp0'" != "" {
local s0samp "s0samp(`samp0')"
}
if "`s0'" != "" {
tokenize `s0'
if "`1'" == "" | "`2'" =="" {
di as err "s0(`s0') invalid"
exit 198
}
local s10 "s10(`1')"
local s20 "s20(`2')"
}
if "`parms'" != "" & "`from'" != "" {
di as err "from() cannot be specified with parms()"
exit 198
}
if "`forecast'" != "" {
local fcast "fcast(`forecast')"
}
if "`from'" == "" {
local from "init( .5 .5 ) "
}
else {
local from "init( `from' ) "
}
if "`parms'" != "" {
local ap "a(`parms')"
local from ""
}
if "`parms'" == "" {
local ap "a(opt)"
}
qui gen `new'=.
capture noi `byp' _ts_hw `new' `old', `ap' /*
*/ `s0samp' touse(`touse') `fcast' /*
*/ `s10' `s20' `diff' `from' `log' /*
*/ `display' /*
*/ `mlopts' `difficult'
ret add
if _rc > 0 {
local rc = _rc
capture drop `nname'
exit `rc'
}
local lbl : var label `nname'
char `nname'[tssmoother] `lbl'
ret local timevar `tvar'
ret local panelvar `pvar'
local myexp2 : subinstr local myexp2 "= " ""
local myexp2 : subinstr local myexp2 "=" ""
ret local exp "`myexp2'"
ret local method "hwinters"
exit
}
if "`shwinters'" != "" {
local 0 " `opts'"
syntax , [PERiod(numlist max=1 integer >=2 <=`Nobs2') /*
*/ Parms(numlist min=3 max=3 >=0 <=1) /*
*/ SAmp0(numlist max=1 >1 <=`Nobs') /*
*/ s0(numlist min=2 max=2 ) /*
*/ Forecast(numlist max=1 >=1 <=500 integer) /*
*/ FRom(numlist min=3 max=3 >=0 <=1) /*
*/ sn0_0(varname) /*
*/ sn0_v(string) /*
*/ snt_v(string) /*
*/ replace /*
*/ ADDitive /*
*/ ALTstarts /*
*/ Normalize /*
*/ noLOG /*
undocumented */ noDISplay /*
*/ NODIFficult /*
undocumented */ DIFficult /*
*/ * ]
mlopts mlopts , `options'
if "`display'" != "" {
local log nolog
}
if "`nodifficult'" != "" & "`difficult'" != "" {
di as err "nodifficult cannot be specified " /*
*/ "with difficult"
exit 198
}
if "`nodifficult'" == "" {
local difficult difficult
}
if "`altstarts'" != "" & "`sn0_0'" != "" {
di as err "specify either altstarts or " /*
*/ "sn0_0(), not both"
exit 198
}
if "`altstarts'" != "" & "`s0'" != "" {
di as err "specify either altstarts or " /*
*/ "s0(), not both"
exit 198
}
if "`period'" == "" {
qui tsset
local fmt "`r(unit1)'"
if "`fmt'" == "d" {
local period 365
}
if "`fmt'" == "w" {
local period 52
}
if "`fmt'" == "m" {
local period 12
}
if "`fmt'" == "q" {
local period 4
}
if "`fmt'" == "h" {
local period 2
}
if "`fmt'" == "y" {
di as err "period set by tsset is invalid, "/*
*/ period must be greater than 1"
exit 198
}
if "`period'" == "" {
di as err "period not set by tsset, specify "/*
*/ "period()"
exit 198
}
}
/* check that sn0_0, which is name
* of varible containing
* starting values for seasonal
* process, exists and it numeric
*/
if "`sn0_0'" != "" {
capture confirm numeric variable `sn0_0'
if _rc >0 {
di as error "sn0_0() does not specify a "/*
*/ "numeric variable"
exit 198
}
local sn0_0m "sn0_0(`sn0_0')"
}
/* check that sn0_v, which is name
* of varible that will contain
* starting values computed
* for seasonal
* process, does not exist
*/
if "`sn0_v'" != "" {
capture confirm new variable `sn0_v'
if _rc >0 {
di as error "sn0_v() does not specify a "/*
*/ "new variable"
exit 198
}
local sn0_vm "sn0_v(`sn0_v')"
}
if "`sn0_v'" != "" & "`sn0_0'" != "" {
di as err "sn0_0() and sn0_v() cannot both be"/*
*/ " specified"
exit 198
}
/* check that snt_v, which is name
* of varible that will contain
* the computed values
* for the seasonal
* process, does not exist
*/
if "`snt_v'" != "" {
capture confirm new variable `snt_v'
if _rc >0 {
di as error "snt_v() does not specify a "/*
*/ "new variable"
exit 198
}
local snt_vm "snt_v(`snt_v')"
}
if "`samp0'" != "" & "`s0'" != "" {
di as err "specify samp0() or s0(), not both"
exit 198
}
if "`samp0'" != "" {
local s0samp "s0samp(`samp0')"
}
if "`s0'" != "" {
tokenize `s0'
if "`1'" == "" | "`2'" =="" {
di as err "s0(`s0') invalid"
exit 198
}
local s10 "s10(`1')"
local s20 "s20(`2')"
}
if "`parms'" != "" & "`from'" != "" {
di as err "from() cannot be specified with parms()"
exit 198
}
if "`forecast'" != "" {
local fcast "fcast(`forecast')"
}
if "`from'" == "" {
local from "init( .5 .5 .5 ) "
}
else {
local from "init( `from' ) "
}
if "`parms'" != "" {
local am "a(`parms')"
}
if "`parms'" == "" {
/* if parms are empty
* then opt via ml assumed
*/
local am "a(opt)"
}
if "`additive'" == "" {
local func "_ts_hwsm"
}
else {
local func "_ts_hwsa"
}
local periodm "period(`period')"
qui gen `new'=.
capture noi `byp' `func' `new' `old', `am' /*
*/ `s0samp' touse(`touse') `fcast' /*
*/ `s10' `s20' `from' `log' `display' /*
*/ `sn0_vm' `sn0_0m' `snt_vm' `periodm' /*
*/ `altstarts' `normalize' `mlopts' /*
*/ `difficult'
ret add
if _rc > 0 {
local rc = _rc
capture drop `nname'
exit `rc'
}
local lbl : var label `nname'
char `nname'[tssmoother] `lbl'
ret local timevar `tvar'
ret local panelvar `pvar'
local myexp2 : subinstr local myexp2 "= " ""
local myexp2 : subinstr local myexp2 "=" ""
ret local exp "`myexp2'"
if "`additive'" != "" {
ret local method "shwinters, additive"
}
else {
ret local method "shwinters, multiplicative"
}
if "`normalize'" != "" {
ret local normalize "normalize"
}
exit
}
/* if here smoother not found */
capture drop `old'
di as error "`opts' does not define a known smoother "
exit 198
end
program define _ts_nlsmooth, rclass
/* syntax is _ts_nlsmooth type new old, SMoother(string) touse(byte varname)
*/
gettoken vars left : 0 , parse(",") match(parns)
tokenize `vars'
local type "`1'"
local newvar "`2'"
local oldvar "`3'"
tempname N
if "`type'" == "" {
di as err "type not specified"
exit 198
}
if "`oldvar'" == "" {
di as err "old variable not specified"
exit 198
}
if "`newvar'" == "" {
di as err "new variable not specified"
exit 198
}
local 0 "`left'"
syntax , touse(varlist) SMoother(string)
local smoother : subinstr local smoother " " ""
markout `touse' `oldvar'
tsreport if `touse'
if r(N_gaps) > 0 {
di as err "cannot apply nl smoothers to variables with "/*
*/ "missing values"
exit 198
}
qui count if `touse' ==1
scalar `N' = r(N)
smooth `smoother' `oldvar' if `touse', gen(`newvar') /*
*/ type(`type') vlabel
ret scalar N = `N'
end
program define cknew_old
syntax , [ma Wma nl Exponential Dexponential Hwinters Shwinters *]
if "`ma'`nl'`exponential'`dexponential'`hwinters'`shwinters'" /*
*/ != "" {
di as err "More than one smoother found"
exit 198
}
end
program define cknew
syntax , [double float byte int long *]
if "`byte'`int'`long'" != "" {
di as err "type invalid"
exit 198
}
local n : word count `options'
if `n' > 1 {
di as err "smoother invalid"
exit 198
}
end
program define myadd, sortp
syntax varlist(min=1 max=1 numeric) [if] [in] , fcast(integer) [ /*
*/ panelp(numlist integer min=1 max=1) ]
if "`panelp'" != "" {
local panelp "panel(`panelp')"
}
marksample touse
tempvar mtvar outz out outvar
qui tsset
local tvar `r(timevar)'
qui gen `mtvar' = -1*`tvar'
if "`r(panelvar)'" != "" {
local pvar "`r(panelvar)'"
local pbyp "by `pvar': "
}
qui sort `pvar' `mtvar'
qui gen `outz' = (`touse'==1)
qui `pbyp' replace `outz' = sum(`outz')
qui gen `out' = (`outz' == 0)
qui `pbyp' gen `outvar' = sum(`out')
qui `pbyp' replace `outvar' = `outvar'[_N]
qui sum `outvar'
if r(min)<`fcast' {
local add = `fcast' - r(min)
tsappend , add(`add') `panelp'
}
end
program define trs2space, sclass
syntax , s0(string)
local s2 = "`s0'"
local len = length("`s2'")
while `len' > 0 {
local --len
local space = substr("`s2'",-1,1)
local s2 = substr("`s2'",1,`len')
if "`space'" == "" {
sret local string "`s2'"
exit
}
}
sret local string "`s0'"
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -