'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' 役立つサブルーチン集 ' produced by Osamu Kayasono ' ' 0.1 1997/06/16 機能追加(ダミーをセットするサブルーチン(setdum)を作成) ' 0.2 1997/08/01 機能追加(2つの変数交換サブルーチン(replvar)を作成) ' 0.3 1999/01/23 機能追加(2データの接続を行うサブルーチン(conjunction)、 '      将来値を置くサブルーチン(setexog)を作成) ' 仕様変更(ダミー作成を年次データに対応) ' 0.3.1 1999/06/02 仕様変更(将来値設定を四半期データに対応、 '      ダミー作成、2変数接続の引数を変更) ' 0.3.2 1999/06/15 仕様変更(将来値設定を最終年平均に対応) ' 0.3.3 1999/12/02 仕様変更(@nan関数を使用して簡略化) ' 0.4 2000/03/29 機能追加(平均伸び率で将来に外挿するサブルーチン(setavc) ' 0.5 2001/06/13 機能追加(与えた伸び率で将来に外装する(setgr) ' 0.6 2002/01/25 機能追加(与えた差分で将来に外装する(setgd) ' 0.7 2005/07/04 機能追加(将来値をおく際に何期分の平均をとるかを指定する setexog2 を追加) ' 仕様変更(setexogは四半期前提だが、いきなり4で割らずにデータの個数をカウントして割るよう変更) ' 0.7.1 2006/01/12 バグ修正(EViews 5でSetDumがエラーになる不具合を修正) '******************************************* 'ダミーを立てるサブルーチン '%X : ダミー系列名 '%smp1 : ダミーをセットする始期 '%smp2 : ダミーをセットする終期 subroutine SetDum(string %X, string %smp1, string %smp2) smpl @all series {%X}=0 if %smp1 ="" and %smp2="" then smpl @FIRST @LAST else if %smp1="" then smpl @FIRST %smp2 else if %smp2="" then smpl %smp1 @LAST else smpl %smp1 %smp2 endif endif endif genr {%X}=1 smpl @all endsub '******************************************* '2つの変数を交換するサブルーチン 'ただし途中でtmpという時系列を使うので、本体ではtmpを使っちゃダメ subroutine REPLVAR(series X,series Y) smpl @all series tmp=0 tmp=X X=Y Y=tmp delete tmp endsub '******************************************* '前年同期比の系列を作るサブルーチン '%a :元系列名( GRY_%a が前年同期比の変数名となる) '(EViews3.0以降では、@pchy関数がサポートされたので不要) subroutine gry(string %a) %b="gry_"+%a genr %b=({%a}-{%a}(-4))/{%a}(-4)*100 endsub '******************************************* '2つのデータを接続するサブルーチン 'SIGMAで a = a ++ bとするのと同じ 'EViews3.1では@nan(a,b)という関数があるので、既に不要 subroutine conjunction(series a, series b) '!noel=@rows(a) 'for !xx=1 to !noel ' if a(!xx)=na then ' if b(!xx)<>na then ' a(!xx)=b(!xx) ' endif ' endif 'next a=@nan(a,b) endsub '******************************************* '将来値を設定するサブルーチン 'a(series) : 対象系列変数 '!smp(scalar) : 将来値として使用する時点(1時点) '%md(string) : 想定値のモード(av:最終期、last:最終年平均、no:ゼロ) subroutine setexog(series a,scalar !smp,string %md) if @left(%md,1)="l" or @left(%md,1)="L" then while a(!smp) = na !smp=!smp-1 wend !xx=a(!smp) else if @left(%md,1)="a" or @left(%md,1)="A" then !n=0 for !i=0 to 3 if a(!smp) <> na then !n=!n+1 endif next !xx=(a(!smp-3)+a(!smp-2)+a(!smp-1)+a(!smp))/!n else !xx=0 endif endif ' !ed=@rows(a) ' for !i=!smp+1 to !ed ' a(!i)=!xx ' next 'ループを使うより多分この方が早かろう genr a=@nan(a,!xx) endsub subroutine setexog2(series a,scalar !smp,string %md, scalar !prd) if @left(%md,1)="l" or @left(%md,1)="L" then while a(!smp) = na !smp=!smp-1 wend !xx=a(!smp) else if @left(%md,1)="a" or @left(%md,1)="A" then !n=0 !xx=0 for !i=0 to !prd-1 if a(!smp-!i) <> na then !n=!n+1 !xx=!xx+a(!smp-!i) endif next !xx=!xx/!n else !xx=0 endif endif ' !ed=@rows(a) ' for !i=!smp+1 to !ed ' a(!i)=!xx ' next 'ループを使うより多分この方が早かろう genr a=@nan(a,!xx) endsub '******************************************* '平均伸び率で将来値を設定するサブルーチン 'あらかじめ平均をとりたい期間をsmplで設定しておく 'a(series) : 対象系列変数 'b(series) : 結果を格納する系列変数 subroutine setavc(series a,series b) equation eq_tmpx1.ls dlog(a) c eq_tmpx1.forecast tmpx1 series b=@nan(a,tmpx1) delete eq_tmpx1 tmpx1 endsub '与えた伸び率で将来値を設定 subroutine setgr(series a,scalar !smp,scalar c) !ed=@rows(a) for !zzz=!smp+1 to !ed a(!zzz)=a(!zzz-1)*(1+c/100) next endsub '与えた差分で将来値を設定 subroutine setgd(series a,scalar !smp,scalar c) !ed=@rows(a) for !zzz=!smp+1 to !ed a(!zzz)=a(!zzz-1)+c next endsub subroutine ddd endsub