Switching Regression Example

/*--------------------------------------------------------------

                    SAS Sample Library

        Name: modex13.sas
 Description: Example program from SAS/ETS User's Guide,
              The MODEL Procedure
       Title: Switching Regression Example
     Product: SAS/ETS Software
        Keys: nonlinear simultaneous equation models
        PROC: MODEL
       Notes:

--------------------------------------------------------------*/

data switch;
   input date monyy. bor    /* borrow */
                        rate   /* mortgage rates */
                        starts /* housing starts */
                        dep    /* deposit */
                        loans  /* real estate loans */ ;
   format date monyy7.;
   label bor    = "borrow";
   label rate   = "Mortgage Rates";
   label starts = "Housing Starts";
   label dep    = "Deposit";
   label loans  = "Real Estate Loans";
datalines;
Jan73 45.7   7.55  146.6 596.3  104.2
Feb73 49.2   7.56  138.0 598.4  105.6
Mar73 50.5   7.63  200.0 602.4  107.3
Apr73 50.1   7.73  205.0 611.8  109
May73 52.8   7.79  234.0 613.8  110.8
Jun73 55.8    7.89  202.6 622.5  112.9
Jul73 58.9    8.19  202.6 630.2  115
Aug73 60.2    8.85  197.2 629.3  117.1
Sep73 62.9    9.18  148.4 638    119.1
Oct73 65.2    8.97  147.1 647    120.8
Nov73 71.3    8.86  133.3 648.6  122.4
Dec73 71.3    8.78  90.4  663.2  123.6
Jan74 73.9    8.61  84.5  664.1  124.7
Feb74 75.7    8.54  109.4 663    125.6
Mar74 78   8.66  124.8 667.1  126.7
Apr74 75.7    9.17  159.5 684    128.4
May74 76.2    9.46  149.0 686.2  130
Jun74 78.3    9.46  147.6 695.3  131.5
Jul74 79.6    9.85  126.6 698.9  133.2
Aug74 79.2    10.3  111.1 696.6  134.3
Sep74 78.6    10.38 98.3  704.4  135.3
Oct74 78.7    10.13 96.7  709.4  136
Nov74 80.8    9.75  75.1  715.8  136.9
Dec74 77.3    9.51  55.1  730.9  137.1
Jan75 74.4    9.51  56.1  722.4  137.4
Feb75 75.9    8.99  54.7  714.7  137.3
Mar75 73.8    8.84  80.2  722.4  137.3
Apr75 72.9    8.69  97.9  729    137.6
May75 72.2    8.91  116.1 727.9  137.9
Jun75 73.7    9.16  110.3 739.8  138.4
Jul75 73.4    9.06  119.3 736    138.7
Aug75 70.1    9.13  117.3 734.2  139.2
Sep75 70.2    9.32  111.9 742.2  140
Oct75 70.6    9.74  123.6 750.4  141
Nov75 74.4    9.53  96.9  758.7  141.7
Dec75 73.3    9.41  76.1  771.9  142.3
Jan76 75.4    9.32  72.5  763.9  143.5
Feb76 76.7    9.06  89.9  760.7  144
Mar76 74.2    9.04  118.4 766.1  144.8
Apr76 78      9.0     137.2 770.1  145.9
May76 77.9    8.82  147.9 768.7  147.2
Jun76 76.1    9.03  154.2 783.1  148.4
Jul76 77.8    9.05  136.6 784.1  149.7
Aug76 82.3    8.99  145.9 778.2  151.2
Sep76 84.9    8.93  151.8 790.3  152.8
Oct76 87.9    8.82  148.4 800.3  154.1
Nov76 92.1    8.55  127.1 806.5  155.4
Dec76 97.5    8.45  107.4 828.7  156.4
Jan77 92.4    8.28  81.3  824.2  157.6
Feb77 93.1    8.45  112.5 815.9  159
Mar77 94.6    8.55  173.6 826.1  160.6
Apr77 97.5    8.65  182.2 839.2  162.8
May77 100.4  8.64  201.3 840.2  165.5
Jun77 99.1    8.77     197.6 854    168
Jul77 101.4  8.77  189.8 862.1  171
Aug77 101.3  8.77  194.0 858.8  174.2
Sep77 104.9  8.77  177.7 872.2  176.8
Oct77 106    8.74  193.1 882.3  179.7
Nov77 110.6  8.81  154.8 892.9  182.4
Dec77 114.3  8.81  129.2 914.3  184.4
Jan78 112.3  8.96  88.6  910.2  187.1
Feb78 113    9.18  101.3 909.9  189
Mar78 114.9  9.25     172.1 917.4  191.3
Apr78 118.5  9.35  197.5 934.3  193.9
May78 121.4  9.44  211.0 936.6  197.3
Jun78 121.5  9.74  216.0 950.4  200.8
Jul78 119.3  9.75  192.2 957.4  204.6
Aug78 122    9.96  190.9 952.2  208.5
Sep78 123.3  9.81  180.5 974.4  212.2
Oct78 128.7  9.81  192.1 980.9  215.9
Nov78 137.8  9.98  158.6 990.9  219.2
Dec78 141.6  10.04 119.5 1013.1 221.7
Jan79 137.2  10.23 88.2  1003.4 222.3
Feb79 138.6  10.24 84.5  997.6  223.9
Mar79 142.1  10.24 152.9 996.8  225.8
Apr79 147.1  10.26 161.0 1011.6 228.2
May79 151.3  10.30 189.1 1001.3 230.7
Jun79 159.7  10.61 191.8 1017.6 234.1
Jul79 164.7  10.49 164.2 1027.4 238.2
Aug79 167.4  10.46 170.3 1024   241.4
Sep79 175.3  10.58 163.7 1054.7 245.1
Oct79 174.9  11.37 169.0 1064.1 248.6
Nov79 167.7  12.1     118.7 1069.3 251.1
Dec79 175.7  12.41 91.6  1086.7 253.6
Jan80 179.7  12.24 73.1  1080.7 254.9
Feb80 183.6  12.6  79.9  1084.7 257.7
Mar80 179.8  13.     85.1  1089.7 259.5
Apr80 184.2  14.63 96.2  1101   261
May80 189.7  13.45 91.7  1093   261.6
Jun80 195.8  11.99 116.4 1102.6 262.2
Jul80 201.9  11.85 120.1 1106.9 263.7
Aug80 204.8  12.39 129.9 1114.2 265.4
Sep80 207.5  13.54 138.3 1135.3 267.2
Oct80 209.2  14.26 152.7 1153.1 269.8
Nov80 208.4  14.38 112.9 1178.6 272.1
Dec80 211    14.47 95.9  1200.3 274.1
Jan81 215.7  14.08 84.5  1198.7 274.9
Feb81 216    14.23 71.9  1200.3 275.9
Mar81 220.8  14.79 107.8 1208.2 277.3
Apr81 229.3  15.04 123.0 1225.3 279
May81 222.3  15.91 109.9 1226.5 281.6
Jun81 229    16.03 105.8 1247   283.7
Jul81 225.7  16.31 99.9  1241.6 285
Aug81 224.2  16.76 86.3  1253.1 287.7
Sep81 236.5  17.96 84.1  1271.7 290.1
Oct81 241.6  18.55 87.2  1229.4 292
Nov81 252.3  17.43 64.6  1244   293.5
Dec81 254.6  15.98 59.1  1264.2 295.8
Jan82 261.9  16.43 47.2  1260.5 296.3
Feb82 265.9  17.38 51.3  1257.3 297.7
Mar82 268    17.1  78.2  1263.8 299.3
Apr82 266.3  16.41 84.1  1277   300.7
May82 265.8  16.31 98.8  1280.6 302.6
Jun82 262.3  16.19 91.1  1293.1 304.5
Jul82 263.1  16.73 106.8 1302.7 305
Aug82 267.2  16.29 96.0  1315.2 306.7
Sep82 273.8  14.61 106.4 1330.9 308.4
Oct82 279    14.03 110.5 1345.5 309.1
Nov82 273.5  12.99 108.9 1351.6 309.7
Dec82 280.8  12.82 82.9  1382.8 310.3
Jan83 292.5  12.8  91.3  1388.3 309.8
Feb83 281.9  12.87 96.3  1392.2 309.9
Mar83 280.1  12.65 134.6 1398.6 310.6
Apr83 280.8  12.68 135.8 1412.6 312.5
May83 279.2  12.5  174.9 1415.2 314.7
Jun83 275.6  12.41 173.2 1428.7 317.4
Jul83 285    12.96 161.6 1436   320.7
Aug83 277.3  14.23 176.8 1433.7 324.2
Sep83 283.7  13.78 154.9 1445.6 328.5
Oct83 297.8  13.55 159.3 1460.6 332.1
Nov83 285.2  13.23 136.0 1472.8 335.5
Dec83 281.5  13.23 108.3 1496.9 338.7
Jan84 298.6  13.25 109.1 1495.3 341.5
Feb84 300.1  13.08 130.0 1488.9 344.6
Mar84 295.1  13.2  137.5 1497.3 348.4
Apr84 301.5  13.68 172.7 1518   352.6
May84 302.1  13.8  180.7 1525.6 356.4
Jun84 303.6  15.01 184.0 1545.5 360.8
Jul84 300.5  14.91 162.1 1555.6 364.2
Aug84 304.4  14.58 147.4 1552.8 368
Sep84 312.1  14.21 148.5 1568   372.8
Oct84 309.1  13.99 152.3 1577.8 376.3
Nov84 315.5  13.43 126.2 1594.8 381
Dec84 314.6  12.9  98.9  1630.7 384.6
Jan85 314.5  12.99 105.4 1624.9 385.7
Feb85 315.4  13.01 95.4  1627.2 388.7
Mar85 308.3  13.27 145.0 1636.7 392
Apr85 313.5  13.43 175.8 1660.6 395.4
May85 323.1  12.97 170.2 1662.4 399.4
Jun85 328.6  12.28 163.2 1683.5 404.2
Jul85 328.6  11.89 160.7 1685.2 409.3
Aug85 315.4  12.12 160.7 1692.2 413.4
Sep85 325.2  11.99 147.7 1713   418.1
Oct85 326.1  12.04 173.0 1722.2 423.7
Nov85 353.9  11.87 124.1 1744.9 428.7
Dec85 370.4  11.28 120.5 1778.5 433.5
Jan86 370.3  10.7  115.6 1774   435.9
Feb86 375.7  10.78 107.2 1764.9 439.4
Mar86 380.3  10.59 151.0 1776.5 444.2
Apr86 385.6  9.77  188.2 1798.5 449
May86 377.7  9.8   186.6 1796.1 454.7
Jun86 373.8  10.07 183.6 1816.5 459.8
Jul86 382.7  9.98  172.0 1826.1 465.7
Aug86 385.2  10.01 163.8 1840.5 473.2
Sep86 395.5  9.8   154.0 1860.1 479.3
Oct86 394.4  9.9   154.8 1867.4 485.3
Nov86 399.2  9.8   115.6 1896   491.4
Dec86 407.4  9.26  113.0 1941.5 502
Jan87 433.3  9.21  105.1 1925.7 510
Feb87 430.9  8.79  102.8 1910.8 514.9
Mar87 420.5  8.81  141.2 1922.4 523
Apr87 427.3  8.94  159.3 1945   531.8
May87 427.3  10.02 158.0 1932.4 541.6
Jun87 421.2  10.61 162.9 1952   550.9
Jul87 420.7  10.33 152.4 1945.2 557.3
Aug87 419.4  10.38 143.6 1945.2 566
Sep87 424    10.55 152.0 1958.5 573.5
Oct87 435.1  11.22 139.1 1974.8 577.1
Nov87 430.2  10.9  118.8 1984.6 584.5
Dec87 424.1  10.76 85.4  1999.5 591.9
Jan88 445.2  10.63 78.2  2007.4 597.9
Feb88 456.4  10.17 90.2  2001.8 602.3
Mar88 461.1  9.86  128.8 2007.7 607.9
Apr88 474.6  10.28 153.2 2028.8 615.3
May88 473.5  10.46 140.2 2033.8 623.9
Jun88 478.3  10.84 150.2 2055.1 631.4
Jul88 479    10.65 137.0 2067.4 639.1
Aug88 468.4  10.66 136.8 2066.2 647.4
Sep88 481.5  10.74 131.1 2078.8 654.8
Oct88 488.7  10.58 135.1 2094.5 663.1
Nov88 486.5  10.23 113.0 2111.9 669.7
Dec88 487.6  10.63 94.2  2138.1 675.9
Jan89 497.1  10.81 100.1 2137.3 683.3
Feb89 517.8  10.69 85.8  2130.1 688.9
Mar89 514.4  10.88 117.8 2137.1 695.1
Apr89 509    11.16 129.4 2154.9 704.7
May89 524.2  10.88 131.7 2143.5 712.3
Jun89 515.9  10.55 143.2 2151.8 720.4
Jul89 514    10.08 134.7 2174.2 730.1
Aug89 528.8  9.61  122.4 2175.3 740.8
Sep89 538.2  9.95  109.3 2193.7 750
Oct89 546.8  9.94  130.1 2219.8 756.1
Nov89 552.8  9.73  96.6  2233.9 764.4
Dec89 548.8  9.69  75.0  2263.1 771.3
Jan90 552.4  9.72  99.2  2260.4 775.5
Feb90 553.7  10.01 86.9  2250.4 782.4
Mar90 561.3  10.22 108.5 2255.1 790.2
Apr90 567.3  10.3  119.0 2277.8 798.8
May90 554.9  10.75 121.1 2271.2 809.6
Jun90 573.2  10.23 117.8 2293.8 818.1
Jul90 575.7  10.18 111.2 2312.9 828.1
Aug90 606.1  10.11 102.8 2313.2 833.8
Sep90 599.5  10.28 93.1  2323   839.4
Oct90 593.4  10.24 94.2  2322.5 846.5
Nov90 603.3  10.23 81.4  2329.7 852.5
Dec90 573.1  9.81  57.4  2363.5 857.8
Jan91 528.4  9.66  52.5  2360.6 858.2
Feb91 523.8  9.58  59.1  2374.3 861.5
Mar91 506.5  9.57  73.8  2392.3 864.9
Apr91 497    9.61  99.7  2416.1 869.2
May91 483.6  9.61  97.7  2411.4 873.6
Jun91 486.1  9.62  103.4 2427.9 878.2
Jul91 474.8  9.71  103.5 2433   877.3
Aug91 475.4  9.59  94.7  2442.9 877.3
Sep91 486.7  9.14  86.6  2453.5 877.9
Oct91 494.6  9.06  101.8 2451.8 880.2
Nov91 497.5  8.71  75.6  2478.1 882.2
Dec91 495.2  8.69  65.6  2492.2 882.9
Jan92 511.1  8.1   71.6  2478.5 883.4
Feb92 516.4  8.72  78.8  2473.7 885.6
Mar92 507.8  8.74  111.6 2487   887
Apr92 504.3  8.85  107.6 2497.4 890.3
May92 491.2  8.79  115.2 2475   892.7
Jun92 499.2  8.66  117.8 2481.7 891.2
Jul92 483.2  8.56  106.2 2471.1 891
Aug92 492.7  8.12  109.9 2474.3 893.5
Sep92 502.1  8.08  106.0 2490.3 897.6
Oct92 494.3  8.06  111.8 2495.7 901.6
Nov92 499.3  8.29  84.5  2513.2 904.2
Dec92 497.7  8.54  78.6  2528.3 904.4
Jan93 493.8  8.12  70.5  2497.9 899.7
Feb93 498.1  8.04  74.6  2489.6 897.7
Mar93 498.2  7.55  95.5  2487.1 898.7
Apr93 490.1  7.57  117.8 2499.6 900.9
May93 497.4  7.56  120.9 2506.6 908.1
Jun93 520.4  7.59  128.5 2518.2 914.2
Jul93 524.6  7.52  115.3 2510.7 917.5
Aug93 520.1  7.51  121.8 2507.3 920.5
Sep93 530.8  7.02  118.5 2521.2 924.1
Oct93 526.5  7.03  123.2 2514.7 930.2
Nov93 529.3  7.08  102.3 2542.6 937.1
Dec93 535.1  7.51  98.7  2565.9 945.1
Jan94 543.8  7.52  76.2  2546.7 942.3
Feb94 544    7.05  83.5  2526.5 939.9
Mar94 545.8  7.59  134.3 2513.9 940.2
Apr94 558.8  8.57  137.6 2518.7 945.1
May94 563.6  8.63  148.8 2514.2 950.3
Jun94 577.4  8.63  136.4 2514.4 957.6
Jul94 582.8  9.03  127.8 2513.6 965.5
Aug94 586.3  8.65  139.8 2510.8 973.2
Sep94 592.1  8.66  130.1 2522.8 983
Oct94 594.5  9.1   130.6 2530.2 989.5
Nov94 606.8  9.23  113.4 2544.5 997.6
Dec94 622.4  9.53  98.5  2567.1 1007.3
Jan95 639    9.54  84.5  2555.3 1013.4
Feb95 646.4  9.1   81.6  2544.9 1019
Mar95 646.6  9.05  103.8 2545.3 1023.8
Apr95 667    8.6   116.9 2567.5 1033.4
May95 678.1  8.56  130.5 2569.4 1041.8
Jun95 687.2  8.03  123.4 2593.2 1052.6
Jul95 697.4  8     129.1 2611   1062.9
Aug95 688.9  8.09  135.8 2613.1 1068.4
Sep95 695.8  8.03  122.4 2638.2 1074.7
Oct95 691.5  8.03  126.2 2654.4 1080
Nov95 686.2  7.61  107.2 2668.5 1084.7
Dec95 698.1  7.51  92.8  2700.9 1085.1
Jan96 696.1  7.52  90.7  2703.7 1087.7
Feb96 692.6  7.11  95.9  2681.2 1088.6
Mar96 690.8  7.57  116.0 2696.6 1091.7
Apr96 706.5  8.09  146.6 2723.6 1095.3
May96 715.5  8.52  143.9 2716.8 1099.4
Jun96 720    8.57  138.0 2728.3 1104.4
Jul96 715.5  8.55  137.5 2735.8 1106.5
Aug96 712.2  8.56  144.2 2744.3 1112.9
Sep96 722.4  8.58  128.7 2775   1117.4
Oct96 719.6  8.56  130.8 2817.5 1120.7
Nov96 739.2  8     111.5 2857.8 1129
Dec96 730.1  8.14  93.1  2898.2 1135.9
Jan97 742.9  8.06  82.2  2879.3 1138.7
Feb97 750.1  8.06  94.7  2881.4 1138.3
Mar97 759.5  8.08  120.4 2914.2 1149.5
Apr97 792.4  8.55  142.3 2956.5 1165.8
May97 798    8.56  136.3 2936.8 1177.7
Jun97 776.7  8.05  140.4 2974   1191
Jul97 770.4  8.02  134.6 3001.6 1198.7
Aug97 774.7  7.61  126.5 3025.3 1205.2
Sep97 800.9  8.02  139.2 3051.1 1216.5
Oct97 831.5  7.52  139.0 3071.5 1226.8
Nov97 849.8  7.53  112.4 3129.9 1236.1
Dec97 857.1  7.51  106.0 3153.7 1240.6
Jan98 872.9  7.17  91.2  3126.3 1238.9
Feb98 866.8  7.08  101.1 3145.6 1248.8
Mar98 885.3  7.06  132.6 3192   1260.4
Apr98 907    7.09  144.9 3216.4 1269
May98 905.6  7.37  143.3 3195.5 1267.9
Jun98 910.7  7.07  159.6 3223.8 1269.8
Jul98 906.3  7.07  156.0 3199.1 1274.7
Aug98 897    7.05  147.5 3226.5 1285.7
Sep98 939.8  7.03  141.5 3262.5 1289
Oct98 984.7  6.53  155.5 3289.1 1296.3
Nov98 1021.3 7.07  124.2 3350.2 1319.2
Dec98 1023.6 7.02  119.6 3375.3 1331.4
Jan99 1020.2 7.06  108.0 3368.9 1335.3
Feb99 994.5  7.08  112.2 3355.7 1332.5
;

title1 'Switching Regression Example';

proc model data=switch;
   parms sig1=10 sig2=10 int1 b11 b13 int2 b21 b23 p;
   bounds 0.0001 < sig1 sig2;

   decjanfeb = ( month(date) = 12 | month(date) <= 2 );

   a = p*dif(rate);       /* Upper bound of integral */
   d = probnorm(a);       /* Normal CDF as an approx of switch */

                          /* Regime 1 */
   y1 = int1 + zlag(starts)*b11 + decjanfeb *b13 ;
                          /* Regime 2 */
   y2 = int2 + zlag(starts)*b21 + decjanfeb *b23 ;
                          /* Composite regression equation */
   starts  = (1 - d)*y1 +  d*y2;

                         /* Resulting log-likelihood function */
   logL = (1/2)*( (log(2*3.1415)) +
        log( (sig1**2)*((1-d)**2)+(sig2**2)*(d**2) )
       + (resid.starts*( 1/( (sig1**2)*((1-d)**2)+
        (sig2**2)*(d**2) ) )*resid.starts) ) ;

   errormodel starts ~ general(logL);

   fit starts / method=marquardt converge=1.0e-5;

     /* Test for significant differences in the parms */
   test int1 = int2 ,/ lm;
   test b11 = b21 ,/ lm;
   test b13 = b23 ,/ lm;
   test sig1 = sig2 ,/ lm;

run;