1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* dpanel.c: implementation of dunamic panel data models of the sort
developed by Arellano, Bond and Blundell
*/
#include "libgretl.h"
#include "version.h"
#include "matrix_extra.h"
#include "uservar.h"
#define ADEBUG 0
#define WRITE_MATRICES 0
#define DPDEBUG 0
#define IVDEBUG 0
enum {
DPD_TWOSTEP = 1 << 0,
DPD_TIMEDUM = 1 << 1,
DPD_WINCORR = 1 << 2,
DPD_SYSTEM = 1 << 3,
DPD_DPDSTYLE = 1 << 4,
DPD_REDO = 1 << 5,
DPD_COLLAPSE = 1 << 6
};
#define gmm_sys(d) (d->flags & DPD_SYSTEM)
#define dpd_style(d) (d->flags & DPD_DPDSTYLE)
#define collapse(d) (d->flags & DPD_COLLAPSE)
#define LEVEL_ONLY 2
typedef struct dpmod_ dpmod;
typedef struct unit_info_ unit_info;
typedef struct diag_info_ diag_info;
struct unit_info_ {
int t1; /* first usable obs in differences for unit */
int t2; /* last usable obs */
int nobs; /* number of usable observations (in the system case
this is the sum of the differenced and level
observations) */
int nlev; /* number of obs in levels (0 in non-system case) */
};
struct diag_info_ {
int v; /* ID number of variable */
int depvar; /* is the target variable the dependent variable (1/0) */
int minlag; /* minimum lag order */
int maxlag; /* maximum lag order */
int level; /* instrument spec is for levels */
int rows; /* max rows occupied in Zi */
int tbase; /* first obs with potentially available instruments */
int collapse; /* "collapse" the instruments? */
};
struct dpmod_ {
int flags; /* option flags */
int step; /* what step are we on? (1 or 2) */
int yno; /* ID number of dependent var */
int p; /* lag order for dependent variable */
int nx; /* number of independent variables */
int ifc; /* model includes a constant */
int nzr; /* number of regular instruments */
int nzb; /* number of block-diagonal instruments (other than y) */
int nz; /* total columns in instrument matrix */
int pc0; /* column in Z where predet vars start */
int xc0; /* column in Z where exog vars start */
int N; /* total number of units in sample */
int effN; /* number of units with usable observations */
int T; /* total number of observations per unit */
int minTi; /* minimum equations (> 0) for any given unit */
int maxTi; /* maximum diff. equations for any given unit */
int max_ni; /* max number of (possibly stacked) obs per unit
(equals maxTi except in system case) */
int k; /* number of parameters estimated */
int nobs; /* total observations actually used (diffs or levels) */
int totobs; /* total observations (diffs + levels) */
int t1; /* initial offset into dataset */
int t1min; /* first usable observation, any unit */
int t2max; /* last usable observation, any unit */
int ntdum; /* number of time dummies to use */
double SSR; /* sum of squared residuals */
double s2; /* residual variance */
double AR1; /* z statistic for AR(1) errors */
double AR2; /* z statistic for AR(2) errors */
double sargan; /* overidentification test statistic (1-step) */
double hansen; /* overidentification test statistic (FEGMM) */
double wald[2]; /* Wald test statistic(s) */
int wdf[2]; /* degrees of freedom for Wald test(s) */
int *xlist; /* list of independent variables */
int *ilist; /* list of regular instruments */
gretl_matrix_block *B1; /* matrix holder */
gretl_matrix_block *B2; /* matrix holder */
gretl_matrix *beta; /* parameter estimates */
gretl_matrix *vbeta; /* parameter variance matrix */
gretl_matrix *uhat; /* residuals, differenced version */
gretl_matrix *H; /* step 1 error covariance matrix */
gretl_matrix *A; /* \sum Z'_i H Z_i */
gretl_matrix *Acpy; /* back-up of A matrix */
gretl_matrix *V; /* covariance matrix */
gretl_matrix *ZT; /* transpose of full instrument matrix */
gretl_matrix *Zi; /* per-unit instrument matrix */
gretl_matrix *Y; /* transformed dependent var */
gretl_matrix *X; /* lagged differences of y, indep vars, etc. */
gretl_matrix *kmtmp; /* workspace */
gretl_matrix *kktmp;
gretl_matrix *M;
gretl_matrix *L1;
gretl_matrix *XZA;
gretl_matrix *ZY;
gretl_matrix *XZ;
diag_info *d; /* info on block-diagonal instruments */
unit_info *ui; /* info on panel units */
char *used; /* global record of observations used */
int ndiff; /* total differenced observations */
int nlev; /* total levels observations */
int nzb2; /* number of block-diagonal specs, levels eqns */
int nzdiff; /* number of insts specific to eqns in differences */
int nzlev; /* number of insts specific to eqns in levels */
int *laglist; /* (possibly discontinuous) list of lags */
diag_info *d2; /* info on block-diagonal instruments, levels eqns
(note: not independently allocated) */
int dcols; /* number of columns, differenced data */
int dcolskip; /* initial number of skipped obs, differences */
int lcol0; /* column adjustment for data in levels */
int lcolskip; /* initial number of skipped obs, levels */
};
#define data_index(dpd,i) (i * dpd->T + dpd->t1)
static void dpanel_residuals (dpmod *dpd);
static int dpd_process_list (dpmod *dpd, int *list, const int *ylags);
static void dpmod_free (dpmod *dpd)
{
if (dpd == NULL) {
return;
}
gretl_matrix_block_destroy(dpd->B1);
gretl_matrix_block_destroy(dpd->B2);
gretl_matrix_free(dpd->V);
free(dpd->xlist);
free(dpd->ilist);
free(dpd->laglist);
free(dpd->d);
free(dpd->used);
free(dpd->ui);
free(dpd);
}
static int dpd_allocate_matrices (dpmod *dpd)
{
int T = dpd->max_ni;
dpd->B1 = gretl_matrix_block_new(&dpd->beta, dpd->k, 1,
&dpd->vbeta, dpd->k, dpd->k,
&dpd->uhat, dpd->totobs, 1,
&dpd->ZT, dpd->nz, dpd->totobs,
&dpd->H, T, T,
&dpd->A, dpd->nz, dpd->nz,
&dpd->Acpy, dpd->nz, dpd->nz,
&dpd->Zi, T, dpd->nz,
&dpd->Y, dpd->totobs, 1,
&dpd->X, dpd->totobs, dpd->k,
NULL);
if (dpd->B1 == NULL) {
return E_ALLOC;
}
dpd->B2 = gretl_matrix_block_new(&dpd->kmtmp, dpd->k, dpd->nz,
&dpd->kktmp, dpd->k, dpd->k,
&dpd->M, dpd->k, dpd->k,
&dpd->L1, 1, dpd->nz,
&dpd->XZA, dpd->k, dpd->nz,
&dpd->ZY, dpd->nz, 1,
&dpd->XZ, dpd->k, dpd->nz,
NULL);
if (dpd->B2 == NULL) {
return E_ALLOC;
}
return 0;
}
static int dpd_add_unit_info (dpmod *dpd)
{
int i, err = 0;
dpd->ui = malloc(dpd->N * sizeof *dpd->ui);
if (dpd->ui == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<dpd->N; i++) {
dpd->ui[i].t1 = 0;
dpd->ui[i].t2 = 0;
dpd->ui[i].nobs = 0;
dpd->ui[i].nlev = 0;
}
}
return err;
}
static int dpd_flags_from_opt (gretlopt opt)
{
/* apply Windmeijer correction unless OPT_A is given */
int f = (opt & OPT_A)? 0 : DPD_WINCORR;
if (opt & OPT_D) {
/* include time dummies */
f |= DPD_TIMEDUM;
}
if (opt & OPT_T) {
/* two-step estimation */
f |= DPD_TWOSTEP;
}
if (opt & OPT_L) {
/* system GMM: include levels equations */
f |= DPD_SYSTEM;
}
if (opt & OPT_X) {
/* compute H as per Ox/DPD */
f |= DPD_DPDSTYLE;
}
if (opt & OPT_C) {
/* "collapse" all block-diagonal instruments as per Roodman */
f |= DPD_COLLAPSE;
}
return f;
}
static dpmod *dpmod_new (int *list, const int *ylags,
const DATASET *dset, gretlopt opt,
diag_info *d, int nzb, int *err)
{
dpmod *dpd = NULL;
int NT;
if (list[0] < 3) {
*err = E_PARSE;
return NULL;
}
dpd = malloc(sizeof *dpd);
if (dpd == NULL) {
*err = E_ALLOC;
return NULL;
}
/* set pointer members to NULL just in case */
dpd->B1 = dpd->B2 = NULL;
dpd->V = NULL;
dpd->ui = NULL;
dpd->used = NULL;
dpd->xlist = NULL;
dpd->ilist = NULL;
dpd->laglist = NULL;
dpd->flags = dpd_flags_from_opt(opt);
dpd->d = d;
dpd->nzb = nzb;
dpd->step = 1;
dpd->nx = dpd->nzr = 0;
dpd->nzdiff = 0;
dpd->nzlev = 0;
dpd->t1min = dpd->t2max = 0;
dpd->ntdum = 0;
dpd->ifc = 0;
/* "system"-specific */
dpd->d2 = NULL;
dpd->nzb2 = 0;
*err = dpd_process_list(dpd, list, ylags);
if (*err) {
goto bailout;
}
NT = sample_size(dset);
dpd->t1 = dset->t1; /* start of sample range */
dpd->T = dset->pd; /* max obs. per individual */
dpd->effN = dpd->N = NT / dpd->T; /* included individuals */
dpd->minTi = dpd->maxTi = 0;
dpd->max_ni = 0;
dpd->nz = 0;
dpd->pc0 = 0;
dpd->xc0 = 0;
dpd->nobs = dpd->totobs = 0;
dpd->ndiff = dpd->nlev = 0;
dpd->SSR = NADBL;
dpd->s2 = NADBL;
dpd->AR1 = NADBL;
dpd->AR2 = NADBL;
dpd->sargan = NADBL;
dpd->hansen = NADBL;
dpd->wald[0] = dpd->wald[1] = NADBL;
dpd->wdf[0] = dpd->wdf[1] = 0;
/* # of coeffs on lagged dep var, indep vars */
if (dpd->laglist != NULL) {
dpd->k = dpd->laglist[0] + dpd->nx;
} else {
dpd->k = dpd->p + dpd->nx;
}
#if ADEBUG
fprintf(stderr, "yno = %d, p = %d, nx = %d, k = %d\n",
dpd->yno, dpd->p, dpd->nx, dpd->k);
fprintf(stderr, "t1 = %d, T = %d, N = %d\n", dpd->t1, dpd->T, dpd->N);
#endif
*err = dpd_add_unit_info(dpd);
if (!*err) {
dpd->used = calloc(dset->n, 1);
if (dpd->used == NULL) {
*err = E_ALLOC;
}
}
bailout:
if (*err) {
dpmod_free(dpd);
dpd = NULL;
}
return dpd;
}
/* if the const has been included among the regressors but not
the instruments, add it to the instruments */
static int maybe_add_const_to_ilist (dpmod *dpd)
{
int i, addc = dpd->ifc;
int err = 0;
if (dpd->xlist == NULL || (dpd->nzr == 0 && dpd->nzb == 0)) {
/* no x's, or all x's treated as exogenous already */
return 0;
}
if (addc && dpd->ilist != NULL) {
for (i=1; i<=dpd->ilist[0]; i++) {
if (dpd->ilist[i] == 0) {
addc = 0;
break;
}
}
}
if (addc) {
dpd->ilist = gretl_list_append_term(&dpd->ilist, 0);
if (dpd->ilist == NULL) {
err = E_ALLOC;
} else {
dpd->nzr += 1;
}
}
return err;
}
static int dpd_make_lists (dpmod *dpd, const int *list, int xpos)
{
int i, nz = 0, spos = 0;
int err = 0;
/* do we have a separator, between exog vars and instruments? */
for (i=xpos; i<=list[0]; i++) {
if (list[i] == LISTSEP) {
spos = i;
break;
}
}
#if ADEBUG
printlist(list, "incoming list in dpd_make_lists");
fprintf(stderr, "separator pos = %d\n", spos);
#endif
if (spos > 0) {
dpd->nx = spos - xpos;
nz = list[0] - spos;
} else {
dpd->nx = list[0] - (xpos - 1);
}
#if ADEBUG
fprintf(stderr, "got intial dpd->nx = %d, nz = %d\n", dpd->nx, nz);
#endif
if (dpd->nx > 0) {
/* compose indep vars list */
dpd->xlist = gretl_list_new(dpd->nx);
if (dpd->xlist == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<dpd->nx; i++) {
dpd->xlist[i+1] = list[xpos + i];
if (dpd->xlist[i+1] == 0) {
dpd->ifc = 1;
}
}
#if ADEBUG
printlist(dpd->xlist, "dpd->xlist");
#endif
if (nz == 0 && dpd->nzb == 0) {
/* implicitly all x vars are exogenous */
dpd->ilist = gretl_list_copy(dpd->xlist);
if (dpd->ilist == NULL) {
err = E_ALLOC;
} else {
dpd->nzr = dpd->ilist[0];
}
}
}
}
if (!err && nz > 0) {
/* compose regular instruments list */
dpd->ilist = gretl_list_new(nz);
if (dpd->ilist == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<nz; i++) {
dpd->ilist[i+1] = list[spos + i + 1];
}
dpd->nzr = nz;
}
}
if (!err) {
err = maybe_add_const_to_ilist(dpd);
}
#if ADEBUG
printlist(dpd->ilist, "dpd->ilist");
#endif
return err;
}
static int dpanel_make_laglist (dpmod *dpd, const int *list,
int seppos, const int *ylags)
{
int nlags = seppos - 1;
int i, err = 0;
if (nlags != 1) {
err = E_INVARG;
} else if (ylags != NULL) {
dpd->laglist = gretl_list_copy(ylags);
if (dpd->laglist == NULL) {
err = E_ALLOC;
} else {
nlags = dpd->laglist[0];
}
} else {
/* only got p; make "fake" list 1, ..., p */
dpd->p = list[1];
if (dpd->p < 1) {
err = E_INVARG;
} else {
dpd->laglist = gretl_consecutive_list_new(1, dpd->p);
if (dpd->laglist == NULL) {
err = E_ALLOC;
}
}
}
if (ylags != NULL && !err) {
/* sort lags and check for duplicates */
gretl_list_sort(dpd->laglist);
dpd->p = dpd->laglist[nlags];
for (i=1; i<=nlags; i++) {
if (dpd->laglist[i] <= 0) {
err = E_INVARG;
} else if (i > 1 && dpd->laglist[i] == dpd->laglist[i-1]) {
err = E_INVARG;
}
}
}
#if ADEBUG
printlist(dpd->laglist, "dpd->laglist");
#endif
return err;
}
/* Process the incoming list and figure out various parameters
including the maximum lag of the dependent variable, dpd->p,
and the number of exogenous regressors, dpd->nx.
The first sublist (before the first LISTSEP) contains either
a single integer value for p or a set of specific lags of y to
use as regressors (given in the form of integers in braces or
as the name of a matrix). In the former case the auxiliary
@ylags list will be NULL; in the latter @ylags records the
specific lags requested. At the user level, therefore,
dpanel 2 ; y ... means use lags 1 and 2
dpanel {2} ; y ... means use lag 2 only
Note that placing a limit on the max lag of y _as instrument_ is
done via "GMM(y,min,max)".
*/
static int dpd_process_list (dpmod *dpd, int *list,
const int *ylags)
{
int seppos = gretl_list_separator_position(list);
int xpos, err = 0;
if (seppos < 2 || list[0] == seppos) {
/* there must be at least one element before (p) and
one element after (y) the first list separator
*/
return E_PARSE;
}
dpd->yno = list[seppos + 1];
xpos = seppos + 2;
if (!dpd_style(dpd) && !gmm_sys(dpd)) {
/* maybe drop the constant (differenced out) */
int i;
for (i=list[0]; i>=xpos; i--) {
if (list[i] == 0) {
gretl_list_delete_at_pos(list, i);
}
}
}
/* the auxiliary 'ylags' list may contain specific y lags,
otherwise there should be just one field */
err = dpanel_make_laglist(dpd, list, seppos, ylags);
if (!err) {
if (dpd->p < 1) {
fprintf(stderr, "dpanel lag order = %d < 1\n", dpd->p);
err = E_INVARG;
}
}
if (!err && list[0] >= xpos) {
err = dpd_make_lists(dpd, list, xpos);
}
return err;
}
/* Figure the 0-based index of the position of the constant
in the coefficient vector (or return -1 if the constant is
not included).
*/
static int dpd_const_pos (dpmod *dpd)
{
int cpos = -1;
if (dpd->xlist != NULL) {
int i;
for (i=1; i<=dpd->xlist[0]; i++) {
if (dpd->xlist[i] == 0) {
cpos = i - 1;
break;
}
}
if (cpos >= 0) {
/* we have the position in the array of coeffs on
exogenous vars, but these follow the lagged
values of the dependent variable.
*/
if (dpd->laglist != NULL) {
cpos += dpd->laglist[0];
} else {
cpos += dpd->p;
}
}
}
return cpos;
}
/* We do either one or two tests here: first we test for the joint
significance of all regular regressors (lags of y plus any
exogenous variables, except for the constant, if present).
Then, if time dummies are present, we test for their joint
significance.
*/
static int dpd_wald_test (dpmod *dpd)
{
gretl_matrix *b, *V;
double x = 0.0;
int cpos = dpd_const_pos(dpd);
int k1, knd;
int i, j, ri, rj;
int err = 0;
/* total number of coefficients excluding any time dummies */
knd = dpd->k - dpd->ntdum;
/* the number of coefficients to be tested at the first step
(we exclude the constant)
*/
k1 = (cpos > 0)? (knd - 1) : knd;
b = gretl_matrix_reuse(dpd->kmtmp, k1, 1);
V = gretl_matrix_reuse(dpd->kktmp, k1, k1);
ri = 0;
for (i=0; i<knd; i++) {
if (i != cpos) {
b->val[ri++] = dpd->beta->val[i];
}
}
ri = 0;
for (i=0; i<knd; i++) {
if (i != cpos) {
rj = 0;
for (j=0; j<knd; j++) {
if (j != cpos) {
x = gretl_matrix_get(dpd->vbeta, i, j);
gretl_matrix_set(V, ri, rj++, x);
}
}
ri++;
}
}
err = gretl_invert_symmetric_matrix(V);
if (!err) {
x = gretl_scalar_qform(b, V, &err);
}
if (!err) {
dpd->wald[0] = x;
dpd->wdf[0] = k1;
}
if (!err && dpd->ntdum > 0) {
/* time dummies: these are always at the end of the
coeff vector
*/
int kt = dpd->ntdum;
b = gretl_matrix_reuse(dpd->kmtmp, kt, 1);
V = gretl_matrix_reuse(dpd->kktmp, kt, kt);
gretl_matrix_extract_matrix(b, dpd->beta, knd, 0,
GRETL_MOD_NONE);
gretl_matrix_extract_matrix(V, dpd->vbeta, knd, knd,
GRETL_MOD_NONE);
err = gretl_invert_symmetric_matrix(V);
if (!err) {
x = gretl_scalar_qform(b, V, &err);
}
if (!err) {
dpd->wald[1] = x;
dpd->wdf[1] = kt;
}
}
gretl_matrix_reuse(dpd->kmtmp, dpd->k, dpd->nz);
gretl_matrix_reuse(dpd->kktmp, dpd->k, dpd->k);
if (err) {
fprintf(stderr, "dpd_wald_test failed: %s\n",
errmsg_get_with_default(err));
}
return err;
}
static int dpd_overid_test (dpmod *dpd)
{
int L1rows = dpd->L1->rows;
int L1cols = dpd->L1->cols;
gretl_matrix *ZTE;
double test;
int err = 0;
ZTE = gretl_matrix_reuse(dpd->L1, dpd->nz, 1);
gretl_matrix_multiply(dpd->ZT, dpd->uhat, ZTE);
gretl_matrix_divide_by_scalar(dpd->A, dpd->effN);
test = gretl_scalar_qform(ZTE, dpd->A, &err);
gretl_matrix_reuse(dpd->L1, L1rows, L1cols);
if (!err && test < 0) {
test = NADBL;
err = E_NOTPD;
}
if (!err && dpd->step == 1) {
/* allow for scale factor in H matrix */
double adj = dpd_style(dpd)? 2.0 : 1.0;
test *= adj / dpd->s2;
}
if (err) {
fprintf(stderr, "dpd_overid_test failed: %s\n",
errmsg_get_with_default(err));
} else if (dpd->step == 1 || dpd_style(dpd)) {
dpd->sargan = test;
} else {
dpd->hansen = test;
}
return err;
}
/* \sigma^2 H_1, sliced and diced for unit i */
static void make_asy_Hi (dpmod *dpd, int i, gretl_matrix *H,
char *mask)
{
int T = dpd->T;
int t, s = data_index(dpd, i);
for (t=0; t<T; t++) {
mask[t] = (dpd->used[t+s] == 1)? 0 : 1;
}
gretl_matrix_reuse(H, T, T);
gretl_matrix_zero(H);
gretl_matrix_set(H, 0, 0, 1);
for (t=1; t<T; t++) {
gretl_matrix_set(H, t, t, 1);
gretl_matrix_set(H, t-1, t, -0.5);
gretl_matrix_set(H, t, t-1, -0.5);
}
gretl_matrix_multiply_by_scalar(H, dpd->s2);
gretl_matrix_cut_rows_cols(H, mask);
}
/*
AR(1) and AR(2) tests for residuals, see
Doornik, Arellano and Bond, DPD manual (2006), pp. 28-29.
AR(m) = \frac{d_0}{\sqrt{d_1 + d_2 + d_3}}
d_0 = \sum_i w_i' u_i
d_1 = \sum_i w_i' H_i w_i
d_2 = -2(\sum_i w_i' X_i) M^{-1}
(\sum_i X_i' Z_i) A_N (\sum Z_i' H_i w_i)
d_3 = (\sum_i w_i' X_i) var(\hat{\beta}) (\sum_i X_i' w_i)
The matrices with subscript i in the above equations are all
in differences. In the "system" case the last term in d2 is
modified as
(\sum Z_i^f' u_i^f u_i' w_i)
where the f superscript indicates that all observations are
used, differences and levels.
one step: H_i = \sigma^2 H_{1,i}
one step, robust: H_i = H_{2,i}; M = M_1
two step: H_i = H_{2,i}; M = M_2
H_{2,i} = v^*_{1,i} v^*_{1,i}'
*/
static int dpd_ar_test (dpmod *dpd)
{
double x, d0, d1, d2, d3;
gretl_matrix_block *B = NULL;
gretl_matrix *ui, *wi;
gretl_matrix *Xi, *Zi;
gretl_matrix *Hi, *ZU;
gretl_matrix *wX, *ZHw;
gretl_matrix *Tmp;
char *hmask = NULL;
int HT, T = dpd->maxTi;
int save_rows, save_cols;
int nz = dpd->nz;
int asy, ZU_cols;
int i, j, k, s, t;
int nlags, m = 1;
int err = 0;
save_rows = gretl_matrix_rows(dpd->Zi);
save_cols = gretl_matrix_cols(dpd->Zi);
/* if non-robust and on first step, Hi is computed differently */
if ((dpd->flags & DPD_TWOSTEP) || (dpd->flags & DPD_WINCORR)) {
asy = 0;
HT = T;
} else {
asy = 1;
HT = dpd->T;
hmask = malloc(HT);
if (hmask == NULL) {
err = E_ALLOC;
goto finish;
}
}
/* The required size of the workspace matrix ZU depends on
whether or not we're in the system case */
ZU_cols = (gmm_sys(dpd))? 1 : T;
B = gretl_matrix_block_new(&ui, T, 1,
&wi, T, 1,
&Xi, T, dpd->k,
&Hi, HT, HT,
&ZU, nz, ZU_cols,
&wX, 1, dpd->k,
&ZHw, nz, 1,
&Tmp, dpd->k, nz,
NULL);
if (B == NULL) {
err = E_ALLOC;
goto finish;
}
Zi = gretl_matrix_reuse(dpd->Zi, T, nz);
restart:
/* initialize cumulators */
d0 = d1 = 0.0;
gretl_matrix_zero(wX);
gretl_matrix_zero(ZHw);
nlags = 0;
s = 0;
for (i=0; i<dpd->N; i++) {
unit_info *unit = &dpd->ui[i];
int s_, t0, ni = unit->nobs;
int Ti = ni - unit->nlev;
int nlags_i = 0;
double uw;
if (Ti == 0) {
continue;
}
gretl_matrix_reuse(ui, Ti, -1);
gretl_matrix_reuse(wi, Ti, -1);
gretl_matrix_reuse(Xi, Ti, -1);
gretl_matrix_reuse(Zi, Ti, -1);
if (!asy) {
gretl_matrix_reuse(Hi, Ti, Ti);
}
if (!gmm_sys(dpd)) {
gretl_matrix_reuse(ZU, -1, Ti);
}
gretl_matrix_zero(wi);
t0 = data_index(dpd, i);
/* Construct the per-unit matrices ui, wi, Xi and Zi. We have
to be careful with the dpd->used values for the lags here:
1 indicates an observation in (both levels and) differences
while observations that are levels-only have a "used" value
of LEVEL_ONLY.
*/
k = 0;
for (t=unit->t1; t<=unit->t2; t++) {
if (dpd->used[t0+t]) {
ui->val[k] = dpd->uhat->val[s];
if (m == 1) {
if (dpd->used[t0+t-1] == 1) {
wi->val[k] = dpd->uhat->val[s-1];
nlags_i++;
}
} else if (dpd->used[t0+t-2] == 1) {
/* m = 2: due to missing values the lag-2
residual might be at slot s-1, not s-2
*/
s_ = (dpd->used[t0+t-1] == 1)? (s-2) : (s-1);
wi->val[k] = dpd->uhat->val[s_];
nlags_i++;
}
for (j=0; j<dpd->k; j++) {
x = gretl_matrix_get(dpd->X, s, j);
gretl_matrix_set(Xi, k, j, x);
}
for (j=0; j<nz; j++) {
x = gretl_matrix_get(dpd->ZT, j, s);
gretl_matrix_set(Zi, k, j, x);
}
k++;
s++;
}
}
if (nlags_i == 0) {
/* no lagged residuals available for this unit */
s += unit->nlev;
continue;
}
nlags += nlags_i;
/* d0 += w_i' * u_i */
uw = gretl_matrix_dot_product(wi, GRETL_MOD_TRANSPOSE,
ui, GRETL_MOD_NONE, &err);
d0 += uw;
if ((dpd->flags & DPD_TWOSTEP) || (dpd->flags & DPD_WINCORR)) {
/* form H_i (robust or step 2 variant) */
gretl_matrix_multiply_mod(ui, GRETL_MOD_NONE,
ui, GRETL_MOD_TRANSPOSE,
Hi, GRETL_MOD_NONE);
} else {
make_asy_Hi(dpd, i, Hi, hmask);
}
/* d1 += w_i' H_i w_i */
d1 += gretl_scalar_qform(wi, Hi, &err);
/* wX += w_i' X_i */
gretl_matrix_multiply_mod(wi, GRETL_MOD_TRANSPOSE,
Xi, GRETL_MOD_NONE,
wX, GRETL_MOD_CUMULATE);
if (gmm_sys(dpd)) {
/* Here we need (Z_i^f' u_i^f) * (u_i' w_i), which
mixes full series and differences.
*/
gretl_matrix_multiply_mod(Zi, GRETL_MOD_TRANSPOSE,
ui, GRETL_MOD_NONE,
ZU, GRETL_MOD_NONE);
for (t=0; t<unit->nlev; t++) {
/* catch the levels terms */
for (j=0; j<nz; j++) {
x = gretl_matrix_get(dpd->ZT, j, s);
ZU->val[j] += x * dpd->uhat->val[s];
}
s++;
}
gretl_matrix_multiply_by_scalar(ZU, uw);
gretl_matrix_add_to(ZHw, ZU);
} else {
/* differences only: ZHw += Z_i' H_i w_i */
gretl_matrix_multiply_mod(Zi, GRETL_MOD_TRANSPOSE,
Hi, GRETL_MOD_NONE,
ZU, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(ZU, GRETL_MOD_NONE,
wi, GRETL_MOD_NONE,
ZHw, GRETL_MOD_CUMULATE);
}
}
if (m == 1) {
/* form M^{-1} * X'Z * A -- this doesn't have to
be repeated for m = 2 */
gretl_matrix_multiply(dpd->M, dpd->XZ, dpd->kmtmp);
gretl_matrix_multiply(dpd->kmtmp, dpd->A, Tmp);
}
if (nlags == 0) {
/* no data for AR(m) test at current m */
if (m == 1) {
m = 2;
goto restart;
} else {
goto finish;
}
}
/* d2 = -2 * w'X * (M^{-1} * XZ * A) * Z'Hw */
gretl_matrix_multiply(wX, Tmp, dpd->L1);
d2 = gretl_matrix_dot_product(dpd->L1, GRETL_MOD_NONE,
ZHw, GRETL_MOD_NONE, &err);
if (!err) {
d2 *= -2.0;
/* form w'X * var(\hat{\beta}) * X'w */
d3 = gretl_scalar_qform(wX, dpd->vbeta, &err);
}
if (!err) {
double z, den = d1 + d2 + d3;
#if ADEBUG
fprintf(stderr, "ar_test: m=%d, d0=%g, d1=%g, d2=%g, d3=%g, den=%g\n",
m, d0, d1, d2, d3, den);
#endif
if (den <= 0) {
err = E_NAN;
} else {
z = d0 / sqrt(den);
if (m == 1) {
dpd->AR1 = z;
} else {
dpd->AR2 = z;
}
}
}
if (!err && m == 1) {
m = 2;
goto restart;
}
finish:
gretl_matrix_block_destroy(B);
free(hmask);
/* restore original dimensions */
gretl_matrix_reuse(dpd->Zi, save_rows, save_cols);
if (err) {
fprintf(stderr, "dpd_ar_test failed: %s\n",
errmsg_get_with_default(err));
} else if (na(dpd->AR1) && na(dpd->AR1)) {
fprintf(stderr, "dpd_ar_test: no data\n");
}
return err;
}
/* Windmeijer, Journal of Econometrics, 126 (2005), page 33:
finite-sample correction for step-2 variance matrix. Note: from a
computational point of view this calculation is expressed more
clearly in the working paper by Bond and Windmeijer, "Finite Sample
Inference for GMM Estimators in Linear Panel Data Models" (2002),
in particular the fact that the matrix named "D" below must be
built column by column, taking the derivative of the "W" (or "A")
matrix with respect to the successive independent variables.
*/
static int windmeijer_correct (dpmod *dpd, const gretl_matrix *uhat1,
const gretl_matrix *varb1)
{
gretl_matrix_block *B;
gretl_matrix *aV; /* standard asymptotic variance */
gretl_matrix *D; /* finite-sample factor */
gretl_matrix *dWj; /* one component of the above */
gretl_matrix *ui; /* per-unit residuals */
gretl_matrix *xij; /* per-unit X_j values */
gretl_matrix *mT; /* workspace follows */
gretl_matrix *km;
gretl_matrix *k1;
gretl_matrix *R1;
gretl_matrix *Zui;
gretl_matrix *Zxi;
int i, j, t;
int err = 0;
aV = gretl_matrix_copy(dpd->vbeta);
if (aV == NULL) {
return E_ALLOC;
}
B = gretl_matrix_block_new(&D, dpd->k, dpd->k,
&dWj, dpd->nz, dpd->nz,
&ui, dpd->max_ni, 1,
&xij, dpd->max_ni, 1,
&mT, dpd->nz, dpd->totobs,
&km, dpd->k, dpd->nz,
&k1, dpd->k, 1,
&Zui, dpd->nz, 1,
&Zxi, 1, dpd->nz,
NULL);
if (B == NULL) {
err = E_ALLOC;
goto bailout;
}
R1 = dpd->ZY; /* borrowed */
/* form -(1/N) * asyV * XZW^{-1} */
gretl_matrix_multiply(aV, dpd->XZA, dpd->kmtmp);
gretl_matrix_multiply_by_scalar(dpd->kmtmp, -1.0 / dpd->effN);
/* form W^{-1}Z'v_2 */
gretl_matrix_multiply(dpd->A, dpd->ZT, mT);
gretl_matrix_multiply(mT, dpd->uhat, R1);
for (j=0; j<dpd->k; j++) { /* loop across the X's */
int s = 0;
gretl_matrix_zero(dWj);
/* form dWj = -(1/N) \sum Z_i'(X_j u' + u X_j')Z_i */
for (i=0; i<dpd->N; i++) {
int ni = dpd->ui[i].nobs;
if (ni == 0) {
continue;
}
gretl_matrix_reuse(ui, ni, 1);
gretl_matrix_reuse(xij, ni, 1);
gretl_matrix_reuse(dpd->Zi, ni, dpd->nz);
/* extract ui (first-step residuals) */
for (t=0; t<ni; t++) {
ui->val[t] = uhat1->val[s++];
}
/* extract xij */
gretl_matrix_extract_matrix(xij, dpd->X, s - ni, j,
GRETL_MOD_NONE);
/* extract Zi */
gretl_matrix_extract_matrix(dpd->Zi, dpd->ZT, 0, s - ni,
GRETL_MOD_TRANSPOSE);
gretl_matrix_multiply_mod(dpd->Zi, GRETL_MOD_TRANSPOSE,
ui, GRETL_MOD_NONE,
Zui, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(xij, GRETL_MOD_TRANSPOSE,
dpd->Zi, GRETL_MOD_NONE,
Zxi, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(Zui, GRETL_MOD_NONE,
Zxi, GRETL_MOD_NONE,
dWj, GRETL_MOD_CUMULATE);
}
gretl_matrix_add_self_transpose(dWj);
gretl_matrix_multiply_by_scalar(dWj, -1.0 / dpd->effN);
/* D[.,j] = -aV * XZW^{-1} * dWj * W^{-1}Z'v_2 */
gretl_matrix_multiply(dpd->kmtmp, dWj, km);
gretl_matrix_multiply(km, R1, k1);
/* write into appropriate column of D (k x k) */
for (i=0; i<dpd->k; i++) {
gretl_matrix_set(D, i, j, k1->val[i]);
}
}
/* add to AsyV: D * AsyV */
gretl_matrix_multiply_mod(D, GRETL_MOD_NONE,
aV, GRETL_MOD_NONE,
dpd->vbeta, GRETL_MOD_CUMULATE);
/* add to AsyV: AsyV * D' */
gretl_matrix_multiply_mod(aV, GRETL_MOD_NONE,
D, GRETL_MOD_TRANSPOSE,
dpd->vbeta, GRETL_MOD_CUMULATE);
/* add to AsyV: D * var(\hat{\beta}_1) * D' */
gretl_matrix_qform(D, GRETL_MOD_NONE, varb1,
dpd->vbeta, GRETL_MOD_CUMULATE);
bailout:
gretl_matrix_block_destroy(B);
gretl_matrix_free(aV);
return err;
}
/* second-step (asymptotic) variance */
static int dpd_variance_2 (dpmod *dpd,
gretl_matrix *u1,
gretl_matrix *V1)
{
int err;
err = gretl_matrix_qform(dpd->XZ, GRETL_MOD_NONE, dpd->V,
dpd->vbeta, GRETL_MOD_NONE);
if (!err) {
err = gretl_invert_symmetric_matrix(dpd->vbeta);
}
if (!err) {
gretl_matrix_multiply_by_scalar(dpd->vbeta, dpd->effN);
}
if (!err && u1 != NULL && V1 != NULL) {
err = windmeijer_correct(dpd, u1, V1);
}
return err;
}
/*
Compute the robust step-1 robust variance matrix:
N * M^{-1} * (X'*Z*A_N*\hat{V}_N*A_N*Z'*X) * M^{-1}
where
M = X'*Z*A_N*Z'*X
and
\hat{V}_N = N^{-1} \sum Z_i'*v_i*v_i'*Z_i,
(v_i being the step-1 residuals).
(But if we're doing 1-step and get the asymptotic
flag, just do the simple thing, \sigma^2 M^{-1})
*/
static int dpd_variance_1 (dpmod *dpd)
{
gretl_matrix *V, *ui;
int i, t, k, c;
int err = 0;
if (!(dpd->flags & DPD_TWOSTEP) && !(dpd->flags & DPD_WINCORR)) {
/* one step asymptotic variance */
err = gretl_matrix_copy_values(dpd->vbeta, dpd->M);
gretl_matrix_multiply_by_scalar(dpd->vbeta, dpd->s2 * dpd->effN/2.0);
return 0;
}
V = gretl_zero_matrix_new(dpd->nz, dpd->nz);
ui = gretl_column_vector_alloc(dpd->max_ni);
if (V == NULL || ui == NULL) {
gretl_matrix_free(V);
gretl_matrix_free(ui);
return E_ALLOC;
}
c = k = 0;
for (i=0; i<dpd->N; i++) {
int ni = dpd->ui[i].nobs;
if (ni == 0) {
continue;
}
/* get per-unit instruments matrix, Zi */
gretl_matrix_reuse(dpd->Zi, ni, dpd->nz);
gretl_matrix_reuse(ui, ni, 1);
gretl_matrix_extract_matrix(dpd->Zi, dpd->ZT, 0, c,
GRETL_MOD_TRANSPOSE);
c += ni;
/* load residuals into the ui vector */
for (t=0; t<ni; t++) {
ui->val[t] = dpd->uhat->val[k++];
}
gretl_matrix_multiply_mod(ui, GRETL_MOD_TRANSPOSE,
dpd->Zi, GRETL_MOD_NONE,
dpd->L1, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(dpd->L1, GRETL_MOD_TRANSPOSE,
dpd->L1, GRETL_MOD_NONE,
V, GRETL_MOD_CUMULATE);
}
gretl_matrix_divide_by_scalar(V, dpd->effN);
/* form X'Z A_N Z'X */
gretl_matrix_multiply(dpd->XZ, dpd->A, dpd->kmtmp);
gretl_matrix_qform(dpd->kmtmp, GRETL_MOD_NONE, V,
dpd->kktmp, GRETL_MOD_NONE);
if (!err) {
/* pre- and post-multiply by M^{-1} */
gretl_matrix_qform(dpd->M, GRETL_MOD_NONE, dpd->kktmp,
dpd->vbeta, GRETL_MOD_NONE);
gretl_matrix_multiply_by_scalar(dpd->vbeta, dpd->effN);
}
if (!err && (dpd->flags & DPD_TWOSTEP)) {
/* preserve V for the second stage */
dpd->V = V;
} else {
gretl_matrix_free(V);
}
gretl_matrix_free(ui);
return err;
}
/* In the "system" case dpd->uhat contains both differenced
residuals and levels residuals (stacked per unit). In that case
trim the vector down so that it contains only one set of
residuals prior to transcribing in series form. Which set
we preserve is governed by @save_levels.
*/
static int dpanel_adjust_uhat (dpmod *dpd,
const DATASET *dset,
int save_levels)
{
double *tmp;
int ntmp, nd;
int i, k, s, t;
ntmp = (save_levels)? dpd->nlev : dpd->ndiff;
tmp = malloc(ntmp * sizeof *tmp);
if (tmp == NULL) {
return E_ALLOC;
}
k = s = 0;
for (i=0; i<dpd->N; i++) {
nd = dpd->ui[i].nobs - dpd->ui[i].nlev;
if (save_levels) {
/* skip residuals in differences */
k += nd;
for (t=0; t<dpd->ui[i].nlev; t++) {
tmp[s++] = dpd->uhat->val[k++];
}
} else {
/* use only residuals in differences */
for (t=0; t<nd; t++) {
tmp[s++] = dpd->uhat->val[k++];
}
k += dpd->ui[i].nlev;
}
}
for (t=0; t<ntmp; t++) {
dpd->uhat->val[t] = tmp[t];
}
gretl_matrix_reuse(dpd->uhat, ntmp, 1);
free(tmp);
if (!save_levels) {
for (t=0; t<dset->n; t++) {
if (dpd->used[t] == LEVEL_ONLY) {
dpd->used[t] = 0;
}
}
}
return 0;
}
static void dpd_add_param_names (MODEL *pmod, dpmod *dpd,
const DATASET *dset,
int full)
{
char tmp[32];
int i, j = 0;
if (dpd->laglist != NULL) {
for (i=1; i<=dpd->laglist[0]; i++) {
sprintf(tmp, "%.10s(-%d)", dset->varname[dpd->yno],
dpd->laglist[i]);
gretl_model_set_param_name(pmod, j++, tmp);
}
} else {
for (i=0; i<dpd->p; i++) {
sprintf(tmp, "%.10s(-%d)", dset->varname[dpd->yno], i+1);
gretl_model_set_param_name(pmod, j++, tmp);
}
}
for (i=0; i<dpd->nx; i++) {
gretl_model_set_param_name(pmod, j++, dset->varname[dpd->xlist[i+1]]);
}
if (full) {
for (i=0; i<dpd->ntdum; i++) {
if (dpd->ifc) {
sprintf(tmp, "T%d", dpd->t1min + i + 2);
gretl_model_set_param_name(pmod, j++, tmp);
} else {
sprintf(tmp, "T%d", dpd->t1min + i + 1);
gretl_model_set_param_name(pmod, j++, tmp);
}
}
}
}
static int dpd_finalize_model (MODEL *pmod,
dpmod *dpd,
int *list,
const int *ylags,
const char *istr,
const DATASET *dset,
gretlopt opt)
{
const double *y = dset->Z[dpd->yno];
int keep_extra = opt & OPT_K;
int i, err = 0;
if (dpd->flags & DPD_REDO) {
goto restart;
}
pmod->ci = DPANEL;
pmod->t1 = dset->t1;
pmod->t2 = dset->t2;
pmod->dfn = dpd->k;
pmod->dfd = dpd->nobs - dpd->k;
pmod->list = list; /* donated, don't free */
gretl_model_set_int(pmod, "yno", dpd->yno);
gretl_model_set_int(pmod, "n_included_units", dpd->effN);
gretl_model_set_int(pmod, "t1min", dpd->t1min + 1);
gretl_model_set_int(pmod, "t2max", dpd->t2max + 1);
gretl_model_set_int(pmod, "ntdum", dpd->ntdum);
gretl_model_set_int(pmod, "ifc", dpd->ifc);
pmod->ncoeff = dpd->k;
pmod->nobs = dpd->nobs;
pmod->full_n = dset->n;
pmod->rsq = pmod->adjrsq = NADBL;
pmod->fstt = pmod->chisq = NADBL;
pmod->lnL = NADBL;
if (pmod->params == NULL) {
/* this step not already done */
gretl_model_allocate_param_names(pmod, dpd->k);
if (pmod->errcode) {
return pmod->errcode;
}
dpd_add_param_names(pmod, dpd, dset, 1);
}
err = gretl_model_allocate_storage(pmod);
restart:
if (!err) {
pmod->ess = dpd->SSR;
if (dpd->s2 >= 0) {
pmod->sigma = sqrt(dpd->s2);
}
for (i=0; i<dpd->k; i++) {
pmod->coeff[i] = dpd->beta->val[i];
}
err = gretl_model_write_vcv(pmod, dpd->vbeta);
}
/* add uhat, yhat */
if (!err && dpd->nlev > 0) {
err = dpanel_adjust_uhat(dpd, dset, 1); /* or 0 */
}
if (!err) {
int t, s = 0;
for (t=0; t<dset->n; t++) {
if (dpd->used[t]) {
pmod->uhat[t] = dpd->uhat->val[s++];
pmod->yhat[t] = y[t] - pmod->uhat[t];
}
}
}
/* additional dpd-specific data */
if (!err) {
gretl_model_set_int(pmod, "step", dpd->step);
if (dpd->step == 2) {
pmod->opt |= OPT_T;
}
if (!na(dpd->AR1)) {
gretl_model_set_double(pmod, "AR1", dpd->AR1);
}
if (!na(dpd->AR2)) {
gretl_model_set_double(pmod, "AR2", dpd->AR2);
}
if (!na(dpd->sargan)) {
gretl_model_set_int(pmod, "sargan_df", dpd->nz - dpd->k);
gretl_model_set_double(pmod, "sargan", dpd->sargan);
}
if (!na(dpd->hansen)) {
gretl_model_set_int(pmod, "hansen_df", dpd->nz - dpd->k);
gretl_model_set_double(pmod, "hansen", dpd->hansen);
}
gretl_model_set_int(pmod, "wald_df", dpd->wdf[0]);
if (!na(dpd->wald[0])) {
gretl_model_set_double(pmod, "wald", dpd->wald[0]);
}
if (!na(dpd->wald[1])) {
gretl_model_set_int(pmod, "wald_time_df", dpd->wdf[1]);
gretl_model_set_double(pmod, "wald_time", dpd->wald[1]);
}
if (!(dpd->flags & DPD_WINCORR)) {
gretl_model_set_int(pmod, "asy", 1);
pmod->opt |= OPT_A;
}
if (dpd->flags & DPD_COLLAPSE) {
gretl_model_set_int(pmod, "collapse", 1);
pmod->opt |= OPT_C;
}
if (dpd->A != NULL) {
gretl_model_set_int(pmod, "ninst", dpd->A->rows);
if (keep_extra) {
gretl_matrix *A = gretl_matrix_copy(dpd->A);
gretl_model_set_matrix_as_data(pmod, "wgtmat", A);
}
}
if (keep_extra && dpd->ZT != NULL) {
gretl_matrix *Z = gretl_matrix_copy(dpd->ZT);
gretl_model_set_matrix_as_data(pmod, "GMMinst", Z);
}
if (opt & OPT_D) {
maybe_suppress_time_dummies(pmod, dpd->ntdum);
}
}
if (!err && !(dpd->flags & DPD_REDO)) {
/* things that only need to be done once */
if (istr != NULL && *istr != '\0') {
gretl_model_set_string_as_data(pmod, "istr", gretl_strdup(istr));
}
if (ylags != NULL) {
gretl_model_set_list_as_data(pmod, "ylags", gretl_list_copy(ylags));
}
if (dpd->flags & DPD_TIMEDUM) {
pmod->opt |= OPT_D;
}
if (dpd->flags & DPD_SYSTEM) {
pmod->opt |= OPT_L;
}
if (dpd_style(dpd)) {
pmod->opt |= OPT_X;
}
if (dpd->maxTi > 0 && dpd->minTi > 0 && dpd->maxTi > dpd->minTi) {
gretl_model_set_int(pmod, "Tmin", dpd->minTi);
gretl_model_set_int(pmod, "Tmax", dpd->maxTi);
}
}
return err;
}
/* Based on reduction of the A matrix, trim ZT to match and
adjust the sizes of all workspace matrices that have a
dimension involving dpd->nz. Note that this is called
on the first step only, and it is called before computing
XZ' and Z'Y. The only matrices we need to actually "cut"
are A (already done when we get here) and ZT; XZ' and Z'Y
should just have their nz dimension changed.
*/
static void dpd_shrink_matrices (dpmod *dpd, const char *mask)
{
#if IVDEBUG
fprintf(stderr, "dpanel: dpd_shrink_matrices: cut nz from %d to %d\n",
dpd->nz, dpd->A->rows);
#endif
gretl_matrix_cut_rows(dpd->ZT, mask);
dpd->nz = dpd->A->rows;
gretl_matrix_reuse(dpd->Acpy, dpd->nz, dpd->nz);
gretl_matrix_reuse(dpd->kmtmp, -1, dpd->nz);
gretl_matrix_reuse(dpd->L1, -1, dpd->nz);
gretl_matrix_reuse(dpd->XZA, -1, dpd->nz);
gretl_matrix_reuse(dpd->XZ, -1, dpd->nz);
gretl_matrix_reuse(dpd->ZY, dpd->nz, -1);
}
static int dpd_step_2_A (dpmod *dpd)
{
int err = 0;
#if ADEBUG
gretl_matrix_print(dpd->V, "V, in dpd_step_2_A");
#endif
if (gretl_matrix_rows(dpd->V) > dpd->effN) {
/* we know this case requires special treatment */
err = gretl_SVD_invert_matrix(dpd->V);
if (!err) {
gretl_matrix_xtr_symmetric(dpd->V);
}
} else {
gretl_matrix_copy_values(dpd->Acpy, dpd->V);
err = gretl_invert_symmetric_matrix(dpd->V);
if (err) {
/* revert the data in dpd->V and try again */
gretl_matrix_copy_values(dpd->V, dpd->Acpy);
err = gretl_SVD_invert_matrix(dpd->V);
if (!err) {
gretl_matrix_xtr_symmetric(dpd->V);
}
}
}
if (!err) {
/* A <- V^{-1} */
gretl_matrix_copy_values(dpd->A, dpd->V);
dpd->step = 2;
}
return err;
}
/* compute \hat{\beta} from the moment matrices */
static int dpd_beta_hat (dpmod *dpd)
{
int err = 0;
if (dpd->step == 2) {
/* form the new A matrix */
err = dpd_step_2_A(dpd);
}
if (!err) {
/* M <- XZ * A * XZ' */
err = gretl_matrix_qform(dpd->XZ, GRETL_MOD_NONE,
dpd->A, dpd->M, GRETL_MOD_NONE);
#if 0
gretl_matrix_print(dpd->XZ, "XZ");
gretl_matrix_print(dpd->A, "A");
gretl_matrix_print(dpd->M, "M");
#endif
}
if (!err) {
/* dpd->XZA <- XZ * A */
gretl_matrix_multiply(dpd->XZ, dpd->A, dpd->XZA);
/* using beta as workspace */
gretl_matrix_multiply(dpd->XZA, dpd->ZY, dpd->beta);
gretl_matrix_copy_values(dpd->kktmp, dpd->M);
err = gretl_cholesky_decomp_solve(dpd->kktmp, dpd->beta);
}
if (!err) {
/* prepare M^{-1} for variance calculation */
err = gretl_inverse_from_cholesky_decomp(dpd->M, dpd->kktmp);
}
#if ADEBUG > 1
gretl_matrix_print(dpd->M, "M");
#endif
return err;
}
/* recompute \hat{\beta} and its variance matrix */
static int dpd_step_2 (dpmod *dpd)
{
gretl_matrix *u1 = NULL;
gretl_matrix *V1 = NULL;
int err;
dpd->step = 2;
err = dpd_beta_hat(dpd);
if (!err && (dpd->flags & DPD_WINCORR)) {
/* we'll need a copy of the step-1 residuals, and also
the step-1 var(\hat{\beta})
*/
u1 = gretl_matrix_copy(dpd->uhat);
V1 = gretl_matrix_copy(dpd->vbeta);
if (u1 == NULL || V1 == NULL) {
err = E_ALLOC;
}
}
if (!err) {
dpanel_residuals(dpd);
err = dpd_variance_2(dpd, u1, V1);
}
gretl_matrix_free(u1);
gretl_matrix_free(V1);
if (!err) {
dpd_ar_test(dpd);
dpd_overid_test(dpd);
dpd_wald_test(dpd);
}
return err;
}
/* This function is used on the first step only */
static int dpd_invert_A_N (dpmod *dpd)
{
int err = 0;
/* just in case */
gretl_matrix_xtr_symmetric(dpd->A);
/* make a backup in case the first attempt fails */
gretl_matrix_copy_values(dpd->Acpy, dpd->A);
/* first try straight inversion */
err = gretl_invert_symmetric_matrix(dpd->A);
if (err) {
/* try again, first reducing A based on its rank */
char *mask = NULL;
fprintf(stderr, "inverting dpd->A failed on first pass\n");
gretl_matrix_copy_values(dpd->A, dpd->Acpy);
mask = gretl_matrix_rank_mask(dpd->A, &err);
if (!err) {
err = gretl_matrix_cut_rows_cols(dpd->A, mask);
}
if (!err) {
err = gretl_invert_symmetric_matrix(dpd->A);
if (!err) {
/* OK, now register effects of reducing nz */
dpd_shrink_matrices(dpd, mask);
} else {
fprintf(stderr, "inverting dpd->A failed on second pass\n");
}
}
free(mask);
}
#if ADEBUG
gretl_matrix_print(dpd->A, "A_N");
#endif
return err;
}
static int dpd_step_1 (dpmod *dpd, gretlopt opt)
{
int err;
/* invert A_N: we allow two attempts */
err = dpd_invert_A_N(dpd);
if (!err) {
/* construct additional moment matrices: we waited
until we knew what size these should really be
*/
gretl_matrix_multiply(dpd->ZT, dpd->Y, dpd->ZY);
gretl_matrix_multiply_mod(dpd->X, GRETL_MOD_TRANSPOSE,
dpd->ZT, GRETL_MOD_TRANSPOSE,
dpd->XZ, GRETL_MOD_NONE);
}
#if ADEBUG > 1
gretl_matrix_print(dpd->XZ, "XZ'");
gretl_matrix_print(dpd->ZY, "Z'Y");
#endif
if (!err) {
err = dpd_beta_hat(dpd);
}
if (!err) {
dpanel_residuals(dpd);
err = dpd_variance_1(dpd);
}
if (!err) {
if (!(dpd->flags & DPD_TWOSTEP)) {
/* step 1 is the final destination */
if (dpd->nzb2 > 0 && (opt & OPT_A)) {
/* GMMlev + asymptotic + 1step: as per Ox/DPD,
skip the AR tests (which won't work)
*/
;
} else {
dpd_ar_test(dpd);
}
dpd_overid_test(dpd);
dpd_wald_test(dpd);
} else if (!dpd_style(dpd) || (opt & OPT_V)) {
/* we're going on to step 2, but record the
result of the first-step Sargan test
*/
dpd_overid_test(dpd);
}
}
return err;
}
static int diag_try_list (const char *vname, int *vp, int *nd,
diag_info **dp, int **listp)
{
int *list = get_list_by_name(vname);
if (list == NULL) {
return E_UNKVAR;
} else {
int nv = list[0];
if (nv == 1) {
*vp = list[1];
} else if (nv < 1) {
return E_DATA;
} else {
/* nv > 1 */
int newlen = *nd + nv - 1;
diag_info *dnew;
dnew = realloc(*dp, newlen * sizeof *dnew);
if (dnew == NULL) {
return E_ALLOC;
} else {
*listp = list;
*dp = dnew;
*nd = newlen;
}
}
}
return 0;
}
/* Parse a particular entry in the (optional) incoming array of
"GMM(foo,m1,m2[,collapse])" specifications. We check that foo
exists and that m1 and m2 have sane values.
*/
static int parse_diag_info (const char *s, diag_info **dp,
int *ip, int *nd, const DATASET *dset)
{
char vname[VNAMELEN];
char copt[9];
char fmt[32];
int level = 0;
int i, m1, m2, c;
int err = 0;
if (!strncmp(s, "GMM(", 4)) {
s += 4;
} else if (!strncmp(s, "GMMlevel(", 9)) {
level = 1;
s += 9;
}
s += strspn(s, " ");
/* count the comma separators */
c = 0;
for (i=0; s[i] != '\0'; i++) {
if (s[i] == ',') c++;
}
if (c == 3) {
/* three commas; we need four fields */
c = 0;
sprintf(fmt, "%%%d[^, ] , %%d , %%d , %%%d[^ )])", VNAMELEN-1, 8);
if (sscanf(s, fmt, vname, &m1, &m2, copt) != 4) {
err = E_PARSE;
} else if (!strcmp(copt, "collapse")) {
c = 1;
} else {
err = E_PARSE;
}
} else if (c == 2) {
/* we must have three fields */
c = 0;
sprintf(fmt, "%%%d[^, ] , %%d , %%d)", VNAMELEN-1);
if (sscanf(s, fmt, vname, &m1, &m2) != 3) {
err = E_PARSE;
}
} else {
err = E_PARSE;
}
if (!err) {
int v = current_series_index(dset, vname);
int *vlist = NULL;
if (m1 < 0 || m2 < m1) {
err = E_DATA;
} else if (v < 0) {
err = diag_try_list(vname, &v, nd, dp, &vlist);
}
if (!err) {
diag_info *d, *darray = *dp;
int nv = (vlist == NULL)? 1 : vlist[0];
int j, i = *ip;
for (j=0; j<nv; j++) {
if (vlist != NULL) {
v = vlist[j+1];
}
d = &darray[i+j];
d->depvar = 0;
d->level = level;
d->v = v;
d->minlag = m1;
d->maxlag = m2;
d->rows = 0;
d->collapse = c;
}
*ip = i + nv;
}
}
return err;
}
/* parse requests of the form
GMM(xvar, minlag, maxlag[, collapse])
which call for inclusion of lags of xvar in block-diagonal
(or perhaps collapsed) fashion
*/
static int
parse_GMM_instrument_spec (const char *spec,
const DATASET *dset,
diag_info **pd,
int *pnspec,
int *pnlevel,
gretlopt opt)
{
diag_info *d = NULL;
const char *s;
int nspec = 0;
int err = 0;
/* first rough check: count closing parentheses */
s = spec;
while (*s) {
if (*s == ')') {
nspec++;
}
s++;
}
if (nspec == 0) {
/* spec is junk */
err = E_PARSE;
} else {
/* allocate info structs */
d = calloc(nspec, sizeof *d);
if (d == NULL) {
err = E_ALLOC;
}
}
if (!err) {
/* parse and record individual GMM instrument specs */
char test[48];
const char *p;
int len, i = 0;
s = spec;
while (*s && !err) {
while (*s == ' ') s++;
p = s;
while (*p && *p != ')') p++;
len = p - s + 1;
if (len > 47) {
err = E_PARSE;
} else {
*test = '\0';
strncat(test, s, len);
err = parse_diag_info(test, &d, &i, &nspec, dset);
s = p + 1;
}
}
}
if (!err) {
int i, j;
for (i=0; i<nspec && !err; i++) {
if (pnlevel != NULL && d[i].level) {
*pnlevel += 1;
}
if (opt & OPT_C) {
/* we got the "global" --collapse option */
d[i].collapse = 1;
}
for (j=0; j<i && !err; j++) {
if (d[i].v == d[j].v && d[i].level == d[j].level) {
gretl_errmsg_sprintf(_("variable %d duplicated "
"in the command list."),
d[i].v);
err = E_DATA;
}
}
}
}
if (err) {
free(d);
*pnspec = 0;
} else {
*pd = d;
*pnspec = nspec;
}
return err;
}
/* Populate the residual vector, dpd->uhat. In the system case
we stack the residuals in levels under the residuals in
differences, per unit. Calculate SSR and \sigma^2 while
we're at it.
*/
static void dpanel_residuals (dpmod *dpd)
{
const double *b = dpd->beta->val;
double SSRd = 0.0, SSRl = 0.0;
double x, ut;
int i, j, k, t;
k = 0;
for (i=0; i<dpd->N; i++) {
unit_info *unit = &dpd->ui[i];
int ndiff = unit->nobs - unit->nlev;
for (t=0; t<ndiff; t++) {
/* differences */
ut = dpd->Y->val[k];
for (j=0; j<dpd->k; j++) {
x = gretl_matrix_get(dpd->X, k, j);
ut -= b[j] * x;
}
SSRd += ut * ut;
dpd->uhat->val[k++] = ut;
}
for (t=0; t<unit->nlev; t++) {
/* levels, if applicable */
ut = dpd->Y->val[k];
for (j=0; j<dpd->k; j++) {
x = gretl_matrix_get(dpd->X, k, j);
ut -= b[j] * x;
}
SSRl += ut * ut;
dpd->uhat->val[k++] = ut;
}
}
if (gmm_sys(dpd)) {
dpd->nobs = dpd->nlev;
dpd->SSR = SSRl;
} else {
dpd->nobs = dpd->ndiff;
dpd->SSR = SSRd;
}
if (dpd_style(dpd)) {
dpd->s2 = dpd->SSR / (dpd->nobs - dpd->k);
} else {
/* The following produces agreement with xtabond2's Sargan
test, though xtabond's reported 'sig2' value is half of
the dpd->s2 we calculate here.
*/
dpd->SSR = SSRd;
dpd->s2 = dpd->SSR / (2 * dpd->nobs);
}
}
/*
This is the first thing we do for each panel unit: construct a list
holding the usable observations. A usable observation is defined
as one for which we have all the data for the equation _in levels_.
The list of good observations takes this form: the first element
gives the number of good observations and the following elements
give their positions, i.e. the 0-based places relative to the start
of the time-series for the unit. While we're at it we record the
positions of the good observations relative to the full dataset,
in the big array dpd->used, so that we can place the residuals
correctly later on.
(Note that @t0 gives the offset in the full dataset of the start of
the unit's data.)
We return the number of "good observations" minus one, to allow
for differencing; the return value (if positive) will be the
max number of observations that are actually usable.
*/
static int check_unit_obs (dpmod *dpd, int *goodobs,
const DATASET *dset,
int t0)
{
const double *y = dset->Z[dpd->yno];
int i, s, t, ok;
goodobs[0] = 0;
for (t=0; t<dpd->T; t++) {
int big_t = t + t0;
/* do we have the dependent variable? */
ok = !na(y[big_t]);
/* and lags of dependent variable? */
for (i=1; i<=dpd->laglist[0] && ok; i++) {
s = t - dpd->laglist[i];
if (s < 0) {
ok = 0;
} else {
ok &= !na(y[s+t0]);
}
}
if (ok && dpd->xlist != NULL) {
/* and regressors? */
for (i=1; i<=dpd->xlist[0] && ok; i++) {
ok &= !na(dset->Z[dpd->xlist[i]][big_t]);
}
}
if (ok) {
goodobs[0] += 1;
goodobs[goodobs[0]] = t;
if (goodobs[0] > 1) {
dpd->used[big_t] = 1;
} else if (gmm_sys(dpd)) {
dpd->used[big_t] = LEVEL_ONLY;
}
}
}
ok = goodobs[0];
/* allow for differencing (but don't set ok < 0) */
if (ok > 0) ok--;
return ok;
}
static void copy_diag_info (diag_info *targ, diag_info *src)
{
*targ = *src;
}
/* diff_iv_accounts:
On input tmin should be the first available obs in levels;
tmax should be the second-last available obs in levels
(in each case, for any unit). These indices are based at 0
for the first period in the unit's data, and they
represent the subtractive terms in the first and last
feasible observations in differences, respectively.
minlag and maxlag represent the minimum and maximum
lags that have been specified for the given instrument.
These lags are relative to the "base" of the differenced
observation, that is, the x_t from which x_{t-k} is
subtracted to form a difference. With non-gappy data
k = 1 (the differences are x_t - x_{t-1}) but with
gappy data we may have k > 1 for some observations;
nonetheless, we "impute" a difference-base index of
(the subtractive term's index + 1). This ensures
that we don't use level instruments that are entangled
in the difference they are supposed to be instrumenting.
*/
int diff_iv_accounts (dpmod *dpd, int tmin, int tmax)
{
int t, tbot, ttop;
int k, i, nrows = 0;
tbot = tmin + 1;
ttop = tmax + 1;
#if IVDEBUG
fprintf(stderr, "*** diff_iv_accounts: tbot = %d, ttop = %d\n", tbot, ttop);
#endif
for (i=0; i<dpd->nzb; i++) {
int minlag = dpd->d[i].minlag;
int maxlag = dpd->d[i].maxlag;
int usable_maxlag = 0;
int tbase = tmax + 2;
int ii, imax = 0;
dpd->d[i].rows = 0;
#if IVDEBUG
fprintf(stderr, "GMM spec %d, incoming: minlag = %d, maxlag = %d\n",
i, minlag, maxlag);
#endif
/* find tbase = the 'base' of the first differenced observation
for which there can be any usable instruments */
for (t=tbot; t<=ttop; t++) {
if (t - minlag >= 0) {
tbase = t;
break;
}
}
if (tbase > ttop) {
fprintf(stderr, " no usable instruments for this spec\n");
dpd->nzb -= 1;
for (k=i; k<dpd->nzb; k++) {
copy_diag_info(&dpd->d[k], &dpd->d[k+1]);
}
i--;
continue;
}
#if IVDEBUG
fprintf(stderr, " tbase = %d\n", tbase);
#endif
/* step forward, cumulating in-principle usable instruments */
for (t=tbase; t<=ttop; t++) {
ii = 0;
for (k=minlag; k<=maxlag && t-k >= 0; k++) {
ii++;
if (k > usable_maxlag) {
usable_maxlag = k;
}
}
#if IVDEBUG
fprintf(stderr, " max insts at t=%d = %d\n", t, ii);
#endif
if (dpd->d[i].collapse) {
if (ii > imax) imax = ii;
} else {
imax += ii;
}
}
#if IVDEBUG
fprintf(stderr, " total insts = %d\n", imax);
fprintf(stderr, " usable maxlag = %d\n", usable_maxlag);
#endif
dpd->d[i].tbase = tbase;
dpd->d[i].rows = imax;
dpd->d[i].maxlag = usable_maxlag;
nrows += imax;
}
return nrows;
}
/* lev_iv_accounts:
On input tbot should be the first available obs in levels
and ttop should be the last available obs in levels
(in each case, for any unit). These indices are based at 0
for the first period in the unit's data.
minlag and maxlag represent the minimum and maximum
lags that have been specified for the given instrument.
*/
int lev_iv_accounts (dpmod *dpd, int tbot, int ttop)
{
int i, t, k, nrows = 0;
#if IVDEBUG
fprintf(stderr, "*** lev_iv_accounts: tbot = %d, ttop = %d\n", tbot, ttop);
#endif
for (i=0; i<dpd->nzb2; i++) {
int minlag = dpd->d2[i].minlag;
int maxlag = dpd->d2[i].maxlag;
int usable_maxlag = 0;
int tbase = ttop + 1;
int ii, imax = 0;
dpd->d2[i].rows = 0;
#if IVDEBUG
fprintf(stderr, "spec %d: minlag = %d, maxlag = %d\n",
i, minlag, maxlag);
#endif
/* find tbase = the first obs in levels for which there can
be any usable instruments; since these instruments are
differences we need to go back one step beyond minlag
*/
for (t=tbot; t<=ttop; t++) {
if (t - minlag - 1 >= 0) {
tbase = t;
break;
}
}
if (tbase > ttop) {
fprintf(stderr, " no usable instruments for this spec\n");
dpd->nzb2 -= 1;
for (k=i; k<dpd->nzb2; k++) {
copy_diag_info(&dpd->d2[k], &dpd->d2[k+1]);
}
i--;
continue;
}
#if IVDEBUG
fprintf(stderr, " tbase = %d\n", tbase);
#endif
/* step forward, cumulating in-principle usable instruments */
for (t=tbase; t<=ttop; t++) {
ii = 0;
for (k=minlag; k<=maxlag && t-k-1 >= 0; k++) {
ii++;
if (k > usable_maxlag) {
usable_maxlag = k;
}
}
#if IVDEBUG
fprintf(stderr, " max insts at t=%d = %d\n", t, ii);
#endif
if (dpd->d[i].collapse) {
if (ii > imax) imax = ii;
} else {
imax += ii;
}
}
#if IVDEBUG
fprintf(stderr, " total insts = %d\n", imax);
fprintf(stderr, " usable maxlag = %d\n", usable_maxlag);
#endif
dpd->d2[i].tbase = tbase;
dpd->d2[i].rows = imax;
dpd->d2[i].maxlag = usable_maxlag;
nrows += imax;
}
return nrows;
}
/* Work through the array of block-diagonal instrument
specifications. Discard any useless ones, trim the
maxlag values to what is supported on the data,
and for each spec, count and record the implied number
of instrument rows that will appear in the Z matrix.
*/
static int block_instrument_count (dpmod *dpd, int t1lev, int t2pen)
{
int nrows;
nrows = diff_iv_accounts(dpd, t1lev, t2pen);
dpd->nzdiff = nrows;
dpd->nz += dpd->nzdiff;
#if IVDEBUG
fprintf(stderr, "block_instrument_count, diffs: got %d rows (total = %d)\n",
dpd->nzdiff, dpd->nz);
#endif
if (gmm_sys(dpd)) {
nrows = lev_iv_accounts(dpd, dpd->t1min, dpd->t2max);
dpd->nzlev = nrows;
dpd->nz += dpd->nzlev;
} else {
nrows = 0;
}
#if IVDEBUG
fprintf(stderr, "block_instrument_count, levels: got %d rows (total = %d)\n",
dpd->nzlev, dpd->nz);
#endif
return 0;
}
/* We do this accounting first so that we know the sizes of the
various (and numerous) matrices that we'll need for the whole
analysis at the outset; we can then allocate memory en bloc.
*/
static void do_unit_accounting (dpmod *dpd, const DATASET *dset,
int **Goodobs)
{
/* t1lev = index of first good obs in levels,
t1dif = index of first good obs in differences,
t2pen = index of penultimate good obs in levels
*/
int t1lev = dpd->T, t1dif = dpd->T, t2pen = 0;
int i, t;
/* just make sure these are zeroed */
dpd->nzdiff = dpd->nzlev = 0;
/* total instruments, so far */
dpd->nz = dpd->nzr;
/* initialize observation counts */
dpd->effN = dpd->ndiff = dpd->nlev = 0;
dpd->minTi = dpd->T;
/* initialize other accounts */
dpd->t2max = 0;
for (i=0, t=dpd->t1; i<dpd->N; i++, t+=dpd->T) {
int *goodobs = Goodobs[i];
int Ti = check_unit_obs(dpd, goodobs, dset, t);
int gmax = goodobs[0];
#if DPDEBUG > 1
fprintf(stderr, "unit %d: Ti = %d\n", i+1, Ti);
#endif
if (Ti > 0) {
dpd->effN += 1;
dpd->ndiff += Ti;
if (gmm_sys(dpd)) {
dpd->nlev += Ti + 1;
}
if (Ti > dpd->maxTi) {
dpd->maxTi = Ti;
}
if (Ti < dpd->minTi) {
dpd->minTi = Ti;
}
if (goodobs[1] < t1lev) {
t1lev = goodobs[1];
}
if (goodobs[2] < t1dif) {
t1dif = goodobs[2];
}
if (goodobs[gmax] > dpd->t2max) {
dpd->t2max = goodobs[gmax];
}
if (goodobs[gmax-1] > t2pen) {
t2pen = goodobs[gmax-1];
}
}
}
dpd->t1min = (gmm_sys(dpd))? t1lev : t1dif;
/* figure number of time dummies, if wanted */
if (dpd->flags & DPD_TIMEDUM) {
dpd->ntdum = dpd->t2max - dpd->t1min;
if (dpd->ifc == 0) {
dpd->ntdum += 1;
}
dpd->k += dpd->ntdum;
dpd->nz += dpd->ntdum;
}
if (dpd->nzb > 0) {
/* figure number of extra block-diagonal instruments */
block_instrument_count(dpd, t1lev, t2pen);
}
/* figure the required number of columns for the Yi and Xi data
matrices: this must be great enough to span the data range
for all units taken together
*/
dpd->max_ni = dpd->t2max - dpd->t1min + 1;
if (dpd->flags & DPD_SYSTEM) {
dpd->max_ni += dpd->max_ni - 1;
}
dpd->dcols = dpd->t2max - t1dif + 1;
dpd->dcolskip = dpd->p + 1;
if (t1dif > dpd->dcolskip) {
dpd->dcolskip = t1dif;
}
dpd->lcolskip = (t1lev > dpd->p)? t1lev : dpd->p;
dpd->lcol0 = dpd->dcols - dpd->lcolskip;
/* sum the total observations overall */
dpd->totobs = dpd->ndiff + dpd->nlev;
#if DPDEBUG
fprintf(stderr, "*** after dpanel accounting:\n"
" effN=%d, max_ni=%d, k=%d, ntdum=%d, nz=%d\n",
dpd->effN, dpd->max_ni, dpd->k, dpd->ntdum, dpd->nz);
fprintf(stderr, " maxTi=%d, minTi=%d\n", dpd->maxTi, dpd->minTi);
fprintf(stderr, " t1min=%d, t2max=%d\n", dpd->t1min, dpd->t2max);
fprintf(stderr, " t1lev=%d, t1dif=%d\n", t1lev, t1dif);
#endif
}
/* Based on the accounting of good observations for a unit recorded
in the @goodobs list, fill matrix D (which is used to construct
H unless we're doing things "dpdstyle").
*/
static void build_unit_D_matrix (dpmod *dpd, int *goodobs, gretl_matrix *D)
{
int usable = goodobs[0] - 1;
int i, j, i0, i1;
gretl_matrix_zero(D);
/* differences */
for (i=0; i<usable; i++) {
i0 = goodobs[i+1];
i1 = goodobs[i+2];
j = i1 - dpd->dcolskip;
gretl_matrix_set(D, i0, j, -1);
gretl_matrix_set(D, i1, j, 1);
}
/* levels */
if (gmm_sys(dpd)) {
for (i=1; i<=goodobs[0]; i++) {
i1 = goodobs[i];
j = i1 + dpd->lcol0;
gretl_matrix_set(D, i1, j, 1);
}
}
#if DPDEBUG > 2
gretl_matrix_print(D, "D");
#endif
}
static void build_unit_H_matrix (dpmod *dpd, int *goodobs,
gretl_matrix *D)
{
build_unit_D_matrix(dpd, goodobs, D);
gretl_matrix_multiply_mod(D, GRETL_MOD_TRANSPOSE,
D, GRETL_MOD_NONE,
dpd->H, GRETL_MOD_NONE);
}
static void make_dpdstyle_H (gretl_matrix *H, int nd)
{
int i;
gretl_matrix_zero(H);
gretl_matrix_set(H, 0, 0, 2);
for (i=1; i<H->rows; i++) {
if (i < nd) {
/* the differences portion */
gretl_matrix_set(H, i, i, 2);
gretl_matrix_set(H, i-1, i, -1);
gretl_matrix_set(H, i, i-1, -1);
} else {
/* the levels portion */
gretl_matrix_set(H, i, i, 1);
}
}
#if DPDEBUG > 1
gretl_matrix_print(H, "dpdstyle H");
#endif
}
static int timedum_level (dpmod *dpd, int j, int t)
{
if (dpd->ifc) {
return (t == j + 1 + dpd->t1min)? 1 : 0;
} else {
return (t == j + dpd->t1min)? 1 : 0;
}
}
static double timedum_diff (dpmod *dpd, int j, int t)
{
int d0 = timedum_level(dpd, j, t);
int d1 = timedum_level(dpd, j, t-1);
return d0 - d1;
}
/* Build row vector of dependent variable values in @Yi using
differences, followed by levels if wanted.
*/
static int build_Y (dpmod *dpd, int *goodobs,
const DATASET *dset,
int t, gretl_matrix *Yi)
{
const double *y = dset->Z[dpd->yno];
int i, usable = goodobs[0] - 1;
int t0, t1, i0, i1, ycol;
double dy;
gretl_matrix_zero(Yi);
/* differences */
for (i=0; i<usable; i++) {
i0 = goodobs[i+1];
i1 = goodobs[i+2];
t0 = t + i0;
t1 = t + i1;
dy = y[t1] - y[t0];
ycol = i1 - dpd->dcolskip;
if (ycol >= Yi->cols) {
fprintf(stderr, "Bzzt! scribbling off the end of Yi (diffs)\n"
" Yi->cols = %d; i1 - dcolskip = %d - %d = %d\n",
Yi->cols, i1, dpd->dcolskip, ycol);
return E_DATA;
} else {
gretl_vector_set(Yi, ycol, dy);
}
}
if (gmm_sys(dpd)) {
/* levels */
for (i=0; i<=usable; i++) {
i1 = goodobs[i+1];
t1 = t + i1;
ycol = i1 + dpd->lcol0;
if (ycol >= Yi->cols) {
fprintf(stderr, "Bzzt! scribbling off the end of Yi (levels)\n"
" Yi->cols = %d; i1 + lcol0 = %d + %d = %d\n",
Yi->cols, i1, dpd->lcol0, ycol);
fprintf(stderr, " note: dpd->t1min = %d, 1 + dpd->p = %d\n",
dpd->t1min, 1 + dpd->p);
return E_DATA;
} else {
gretl_vector_set(Yi, ycol, y[t1]);
}
}
}
return 0;
}
/* Build matrix of right-hand side variable values in @Xi using
differences, followed by levels if wanted.
*/
static void build_X (dpmod *dpd, int *goodobs,
const DATASET *dset,
int t, gretl_matrix *Xi)
{
const double *y = dset->Z[dpd->yno];
int usable = goodobs[0] - 1;
int nlags = dpd->laglist[0];
const double *xj;
int t0, t1, i0, i1;
int i, j, lj;
int row, col;
double dx;
gretl_matrix_zero(Xi);
/* differences */
for (i=0; i<usable; i++) {
i0 = goodobs[i+1];
i1 = goodobs[i+2];
t0 = t + i0;
t1 = t + i1;
row = 0;
col = i1 - dpd->dcolskip;
for (j=1; j<=nlags; j++) {
lj = dpd->laglist[j];
dx = y[t1-lj] - y[t0-lj];
gretl_matrix_set(Xi, row++, col, dx);
}
for (j=1; j<=dpd->nx; j++) {
/* Note: we don't difference away the constant
here, but if dpdstyle is not in force the
constant will have been removed already.
*/
if (!gmm_sys(dpd) && dpd->xlist[j] == 0) {
dx = 1.0;
} else {
xj = dset->Z[dpd->xlist[j]];
dx = xj[t1] - xj[t0];
}
gretl_matrix_set(Xi, row++, col, dx);
}
if (dpd->ntdum > 0) {
for (j=0; j<dpd->ntdum; j++) {
if (gmm_sys(dpd)) {
dx = timedum_diff(dpd, j, i1);
} else if (dpd_style(dpd)) {
/* as per DPD: leave dummies in levels */
dx = timedum_level(dpd, j, i1);
} else {
/* as per xtabond2: difference the dummies */
dx = timedum_diff(dpd, j, i1);
}
gretl_matrix_set(Xi, row++, col, dx);
}
}
}
if (gmm_sys(dpd)) {
for (i=0; i<=usable; i++) {
i1 = goodobs[i+1];
t1 = t + i1;
row = 0;
col = i1 + dpd->lcol0;
for (j=1; j<=nlags; j++) {
lj = dpd->laglist[j];
gretl_matrix_set(Xi, row++, col, y[t1-lj]);
}
for (j=1; j<=dpd->nx; j++) {
xj = dset->Z[dpd->xlist[j]];
gretl_matrix_set(Xi, row++, col, xj[t1]);
}
for (j=0; j<dpd->ntdum; j++) {
dx = timedum_level(dpd, j, i1);
gretl_matrix_set(Xi, row++, col, dx);
}
}
}
}
/* note: this amounts to t1*(t1-1)/2 in the
straightforward case */
static int row_increment (diag_info *d, int t1)
{
int k1 = d->level ? 1 : 0;
int t, k, r = 0;
for (t=d->tbase; t<t1; t++) {
for (k=d->minlag; k<=d->maxlag && t-k-k1 >= 0; k++) {
r++;
}
}
return r;
}
#if IVDEBUG
/* verify that we're not writing instrument values to rows of
the Z matrix that are incompatible with what was figured
out by block_instrument_count (see above).
*/
static int bad_write_check (dpmod *dpd, int row, int lev)
{
if (!lev && row >= dpd->nzdiff) {
fprintf(stderr, "*** ERROR in gmm_inst_diff: writing to "
"bad row %d (max is %d)\n", row,
dpd->nzdiff - 1);
return 1;
} else if (lev && (row < dpd->nzdiff || row >= dpd->nzdiff + dpd->nzlev)) {
fprintf(stderr, "*** ERROR in gmm_inst_lev: writing to "
"bad row %d (min is %d, max is %d)\n", row,
dpd->nzdiff, dpd->nzdiff + dpd->nzlev - 1);
return 1;
}
return 0;
}
#endif
/* GMM-style instruments in levels for the eqns in differences */
static int gmm_inst_diff (dpmod *dpd, int bnum, const double *x,
int s, int *goodobs, int row0, int col0,
gretl_matrix *Zi)
{
int maxlag = dpd->d[bnum].maxlag;
int minlag = dpd->d[bnum].minlag;
int tmax = goodobs[goodobs[0]];
int i, t, t1, t2;
int col, row;
double xt;
for (i=1; i<goodobs[0]; i++) {
t1 = goodobs[i];
t2 = goodobs[i+1];
col = col0 + t2 - dpd->dcolskip;
if (dpd->d[bnum].collapse) {
row = row0;
} else {
row = row0 + row_increment(&dpd->d[bnum], t1+1);
}
for (t=tmax; t>=0; t--) {
/* 2021-11-16: the iteration was: t=0; t<=tmax; t++ */
if (t1 - t >= minlag - 1 && t1 - t < maxlag) {
/* the criterion here needs care! */
xt = x[s+t];
if (!na(xt)) {
#if IVDEBUG
bad_write_check(dpd, row, 0);
#endif
gretl_matrix_set(Zi, row, col, xt);
}
row++;
}
}
}
return row0 + dpd->d[bnum].rows;
}
/* GMM-style instruments in differences for the eqns in levels */
static int gmm_inst_lev (dpmod *dpd, int bnum, const double *x,
int s, int *goodobs, int row0, int col0,
gretl_matrix *Zi)
{
int maxlag = dpd->d2[bnum].maxlag;
int minlag = dpd->d2[bnum].minlag;
int tmax = goodobs[goodobs[0]];
int i, k, t, t1;
int col, row;
double x0, x1;
for (i=1; i<=goodobs[0]; i++) {
t1 = goodobs[i];
col = col0 + t1 - dpd->lcolskip;
if (dpd->d[bnum].collapse) {
row = row0;
} else {
row = row0 + row_increment(&dpd->d2[bnum], t1);
}
for (t=tmax; t>=1; t--) {
/* 2021-11-16: the iteration was: t=1; t<=tmax; t++ */
k = t1 - t;
if (k <= maxlag && k >= minlag) {
x0 = x[s+t-1];
x1 = x[s+t];
if (!na(x1) && !na(x0)) {
#if IVDEBUG
bad_write_check(dpd, row, 1);
#endif
gretl_matrix_set(Zi, row, col, x1 - x0);
}
row++;
}
}
}
return row0 + dpd->d2[bnum].rows;
}
/* Build the matrix of per-unit instrument values in @Zi, which
has the instruments in rows and the observations in columns.
Note that each unit's Zi is the same size, padded with zero columns
for missing observations as needed. The number of columns in Zi
equals the maximal span of the data for all units taken together,
counting both observations in differences and observations in
levels, if applicable.
We pack the instruments in the following order:
1) G1: GMM-style instruments in levels for equations in
differences
3) G2: GMM-style instruments in differences for equations in
levels, if present
5) I1: "Regular" instruments, differenced exog vars for eqns
in differences
6) I2: "Regular" instruments, levels of exog vars for eqns
in levels, if any
7) D1: Time dummies for eqns in differences, if specified and if
"system" estimation is not being done
8) D2: Time dummies for eqns in levels, if specified
The pattern for the non-system case is
Z' = | G1 : I1 : D1 |
and for the full system case it is
Z' = | G1 : 0 : I1 : 0 |
| 0 : G2 : I2 : D2 |
*/
static void build_Z (dpmod *dpd, int *goodobs,
const DATASET *dset,
int t, gretl_matrix *Zi,
int unit)
{
const int usable = goodobs[0] - 1;
const double *x;
double dx;
/* k2 is the starting row for "regular" instruments */
int k2 = dpd->nzdiff + dpd->nzlev;
/* k3 marks the starting row for time dummies */
int k3 = k2 + dpd->nzr;
int t0, t1, i0, i1;
int i, j, col, row = 0;
gretl_matrix_zero(Zi);
#if IVDEBUG
if (unit == 0) {
fprintf(stderr, "Z is %d x %d\n", Zi->rows, Zi->cols);
fprintf(stderr, " nzb (levels for diffs) = %d\n", dpd->nzb);
fprintf(stderr, " nzb2 (diffs for levels) = %d\n", dpd->nzb2);
fprintf(stderr, " nzr (differenced exog vars) = %d\n", dpd->nzr);
fprintf(stderr, " ntdum (time dummies for diffs) = %d\n",
gmm_sys(dpd) ? 0 : dpd->ntdum);
fprintf(stderr, " nzr (levels of exog vars) = %d\n",
!gmm_sys(dpd) ? 0 : dpd->nzr);
fprintf(stderr, " ntdum (time dummies for levels) = %d\n",
!gmm_sys(dpd) ? 0 : dpd->ntdum);
}
#endif
/* GMM-style instruments in levels for diffs equations */
for (i=0; i<dpd->nzb; i++) {
x = dset->Z[dpd->d[i].v];
row = gmm_inst_diff(dpd, i, x, t, goodobs, row, 0, Zi);
}
col = dpd->t2max - dpd->t1min;
/* GMM-style instruments in diffs for levels equations */
for (i=0; i<dpd->nzb2; i++) {
x = dset->Z[dpd->d2[i].v];
row = gmm_inst_lev(dpd, i, x, t, goodobs, row, col, Zi);
}
/* equations in differences: differenced exog vars */
if (dpd->nzr > 0) {
for (i=0; i<usable; i++) {
i0 = goodobs[i+1];
i1 = goodobs[i+2];
col = i1 - dpd->dcolskip;
t0 = t + i0;
t1 = t + i1;
for (j=0; j<dpd->nzr; j++) {
/* we don't difference the constant, but unless
dpdstyle is in force it will have been
dropped by this point
*/
if (!gmm_sys(dpd) && dpd->ilist[j+1] == 0) {
dx = 1.0;
} else {
x = dset->Z[dpd->ilist[j+1]];
dx = x[t1] - x[t0];
}
gretl_matrix_set(Zi, k2 + j, col, dx);
}
}
}
/* equations in differences: time dummies */
if (dpd->ntdum > 0 && !gmm_sys(dpd)) {
for (i=0; i<usable; i++) {
i1 = goodobs[i+2];
col = i1 - dpd->dcolskip;
for (j=0; j<dpd->ntdum; j++) {
dx = timedum_level(dpd, j, i1);
gretl_matrix_set(Zi, k3 + j, col, dx);
}
}
}
if (gmm_sys(dpd)) {
/* equations in levels: levels of exog vars */
if (dpd->nzr > 0) {
for (i=0; i<=usable; i++) {
i1 = goodobs[i+1];
t1 = t + i1;
col = i1 + dpd->lcol0;
for (j=0; j<dpd->nzr; j++) {
x = dset->Z[dpd->ilist[j+1]];
gretl_matrix_set(Zi, k2 + j, col, x[t1]);
}
}
}
/* equations in levels: time dummies */
if (dpd->ntdum > 0) {
for (i=0; i<=usable; i++) {
i1 = goodobs[i+1];
col = i1 + dpd->lcol0;
for (j=0; j<dpd->ntdum; j++) {
dx = timedum_level(dpd, j, i1);
gretl_matrix_set(Zi, k3 + j, col, dx);
}
}
}
}
}
static int trim_zero_inst (dpmod *dpd, PRN *prn)
{
char *mask;
int err = 0;
#if DPDEBUG
fprintf(stderr, "before trimming, order of A = %d\n", dpd->A->rows);
#endif
#if WRITE_MATRICES
gretl_matrix_write_to_file(dpd->A, "dpd-bigA.bin", 0);
#endif
mask = gretl_matrix_zero_diag_mask(dpd->A, &err);
if (mask != NULL) {
err = gretl_matrix_cut_rows_cols(dpd->A, mask);
if (!err) {
if (prn != NULL) {
pprintf(prn, _("%d redundant instruments dropped, leaving %d\n"),
dpd->nz - dpd->A->rows, dpd->A->rows);
}
dpd_shrink_matrices(dpd, mask);
}
free(mask);
}
#if DPDEBUG
gretl_matrix_print(dpd->A, "dpd->A, after trim_zero_inst");
#endif
if (!err) {
gretl_matrix_divide_by_scalar(dpd->A, dpd->effN);
}
return err;
}
/* allocate temporary storage needed by do_units() */
static int make_units_workspace (dpmod *dpd, gretl_matrix **D,
gretl_matrix **Yi, gretl_matrix **Xi)
{
int err = 0;
if (dpd_style(dpd)) {
/* Ox/DPD-style H matrix: D matrix is not needed */
*D = NULL;
} else {
*D = gretl_matrix_alloc(dpd->T, dpd->max_ni);
if (*D == NULL) {
return E_ALLOC;
}
}
*Yi = gretl_matrix_alloc(1, dpd->max_ni);
*Xi = gretl_matrix_alloc(dpd->k, dpd->max_ni);
if (*Yi == NULL || *Xi == NULL) {
gretl_matrix_free(*D);
gretl_matrix_free(*Yi);
gretl_matrix_free(*Xi);
err = E_ALLOC;
}
return err;
}
/* Stack the per-unit data matrices from unit @unum for future use,
skipping unused observations and recording the numbers of
observations in differences and in levels.
*/
static void stack_unit_data (dpmod *dpd,
const gretl_matrix *Yi,
const gretl_matrix *Xi,
const gretl_matrix *Zi,
int *goodobs, int unum,
int *row)
{
unit_info *unit = &dpd->ui[unum];
double x;
int i, j, k, s = *row;
for (i=2; i<=goodobs[0]; i++) {
k = goodobs[i] - dpd->dcolskip;
gretl_vector_set(dpd->Y, s, Yi->val[k]);
for (j=0; j<Xi->rows; j++) {
x = gretl_matrix_get(Xi, j, k);
gretl_matrix_set(dpd->X, s, j, x);
}
for (j=0; j<dpd->nz; j++) {
x = gretl_matrix_get(Zi, j, k);
gretl_matrix_set(dpd->ZT, j, s, x);
}
s++;
}
/* record the indices of the first and last
differenced observations */
unit->t1 = goodobs[2];
unit->t2 = goodobs[goodobs[0]];
/* record the number of differenced obs */
unit->nobs = (goodobs[0] > 0)? (goodobs[0] - 1) : 0;
if (gmm_sys(dpd)) {
for (i=1; i<=goodobs[0]; i++) {
k = goodobs[i] + dpd->lcol0;
if (k >= Yi->cols) {
fprintf(stderr, "*** stack_unit_data: reading off "
"end of Yi (k=%d, Yi->cols=%d)\n", k, Yi->cols);
fprintf(stderr, " at goodobs[%d] = %d\n", i, goodobs[i]);
continue;
}
gretl_vector_set(dpd->Y, s, Yi->val[k]);
for (j=0; j<Xi->rows; j++) {
x = gretl_matrix_get(Xi, j, k);
gretl_matrix_set(dpd->X, s, j, x);
}
for (j=0; j<dpd->nz; j++) {
x = gretl_matrix_get(Zi, j, k);
gretl_matrix_set(dpd->ZT, j, s, x);
}
s++;
}
/* record the number of levels obs and augment total */
unit->nlev = goodobs[0];
unit->nobs += unit->nlev;
}
#if WRITE_MATRICES
gretl_matrix_write_to_file(dpd->ZT, "dpdZT.bin", 0);
#endif
*row = s;
}
/* Main driver for system GMM: the core is a loop across
the panel units to build the data and instrument matrices
and cumulate A = \sum_i Z_i H_i Z_i'.
At this point we have already done the observations
accounts, which are recorded in the Goodobs lists.
*/
static int do_units (dpmod *dpd, const DATASET *dset,
int **Goodobs)
{
#if DPDEBUG
char istr[32];
#endif
gretl_matrix *D = NULL;
gretl_matrix *Yi = NULL;
gretl_matrix *Xi = NULL;
gretl_matrix *Zi = NULL;
int i, t, Yrow;
int err = 0;
err = make_units_workspace(dpd, &D, &Yi, &Xi);
if (err) {
return err;
}
Zi = dpd->Zi;
gretl_matrix_reuse(Zi, dpd->nz, dpd->max_ni);
if (D == NULL) {
/* the H matrix will not vary by unit */
int tau = dpd->t2max - dpd->t1min + 1;
if (gmm_sys(dpd)) {
/* t1min is actually "levels-only" */
tau--;
}
make_dpdstyle_H(dpd->H, tau);
}
/* initialize cumulators */
gretl_matrix_zero(dpd->XZ);
gretl_matrix_zero(dpd->A);
gretl_matrix_zero(dpd->ZY);
/* initialize data stacker */
Yrow = 0;
#if DPDEBUG
/* this should not be necessary if stack_unit_data() is
working correctly */
gretl_matrix_zero(dpd->Y);
gretl_matrix_zero(dpd->X);
gretl_matrix_zero(dpd->ZT);
#endif
for (i=0; i<dpd->N; i++) {
int *goodobs = Goodobs[i];
int Ti = goodobs[0] - 1;
if (Ti == 0) {
continue;
}
t = data_index(dpd, i);
err = build_Y(dpd, goodobs, dset, t, Yi);
if (err) {
break;
}
build_X(dpd, goodobs, dset, t, Xi);
build_Z(dpd, goodobs, dset, t, Zi, i);
#if DPDEBUG
sprintf(istr, "do_units: Y[%d]", i);
gretl_matrix_print(Yi, istr);
sprintf(istr, "do_units: X[%d]", i);
gretl_matrix_print(Xi, istr);
sprintf(istr, "do_units: Z[%d]", i);
gretl_matrix_print(Zi, istr);
#endif
if (D != NULL) {
build_unit_H_matrix(dpd, goodobs, D);
}
gretl_matrix_qform(Zi, GRETL_MOD_NONE,
dpd->H, dpd->A, GRETL_MOD_CUMULATE);
/* stack the individual data matrices for future use */
stack_unit_data(dpd, Yi, Xi, Zi, goodobs, i, &Yrow);
}
#if DPDEBUG
gretl_matrix_print(dpd->Y, "dpd->Y");
gretl_matrix_print(dpd->X, "dpd->X");
gretl_matrix_print(dpd->H, "dpd->H");
#endif
#if WRITE_MATRICES
gretl_matrix_write_to_file(dpd->Y, "dpdY.bin", 0);
gretl_matrix_write_to_file(dpd->X, "dpdX.bin", 0);
#endif
gretl_matrix_free(D);
gretl_matrix_free(Yi);
gretl_matrix_free(Xi);
return err;
}
/* the user hasn't supplied a block-diagonal spec for
y in the differences equations: here we set up the
default version, with unlimited lags
*/
static int add_default_ydiff_spec (dpmod *dpd)
{
diag_info *d;
d = realloc(dpd->d, (dpd->nzb + 1) * sizeof *d);
if (d == NULL) {
return E_ALLOC;
} else {
/* insert the y spec in first place, moving
any other specs up */
int i;
dpd->d = d;
for (i=dpd->nzb; i>0; i--) {
copy_diag_info(&dpd->d[i], &dpd->d[i-1]);
}
d = &dpd->d[0];
d->v = dpd->yno;
d->depvar = 1;
d->minlag = 2;
d->maxlag = 99;
d->level = 0;
d->rows = 0;
d->collapse = collapse(dpd);
dpd->nzb += 1;
}
return 0;
}
/* the user has specified "system" but hasn't supplied a
block-diagonal spec for y in the levels equations: here
we set up the default version, with 1 lag
*/
static int add_default_ylev_spec (dpmod *dpd)
{
diag_info *d;
d = realloc(dpd->d, (dpd->nzb + 1) * sizeof *d);
if (d == NULL) {
return E_ALLOC;
} else {
dpd->d = d;
d = &dpd->d[dpd->nzb];
d->v = dpd->yno;
d->depvar = 1;
d->minlag = 1;
d->maxlag = 1;
d->level = 1;
d->rows = 0;
d->collapse = collapse(dpd);
dpd->nzb += 1;
dpd->nzb2 += 1;
}
return 0;
}
static int compare_gmm_specs (const void *a, const void *b)
{
const diag_info *da = a;
const diag_info *db = b;
int ret = da->level - db->level;
if (ret == 0) {
ret = db->depvar - da->depvar;
}
return ret;
}
/* Given the info on instrument specification returned by the
parser, make any adjustments that may be needed in the
system case.
*/
static int dpanel_adjust_GMM_spec (dpmod *dpd)
{
int have_ydiff_spec = 0;
int have_ylev_spec = 0;
int i, err = 0;
/* check whether we have:
- a GMM-style spec for the dep var, differenced equations
- any block-diagonal specs for levels eqns
- a GMM-style spec for dep var, levels equations
*/
for (i=0; i<dpd->nzb; i++) {
if (dpd->d[i].level == 0) {
if (dpd->d[i].v == dpd->yno) {
dpd->d[i].depvar = 1;
have_ydiff_spec = 1;
}
} else {
dpd->nzb2 += 1;
if (dpd->d[i].v == dpd->yno) {
dpd->d[i].depvar = 1;
have_ylev_spec = 1;
}
}
}
if (!have_ydiff_spec) {
err = add_default_ydiff_spec(dpd);
if (err) {
return err;
}
}
if (gmm_sys(dpd) && !have_ylev_spec) {
err = add_default_ylev_spec(dpd);
if (err) {
return err;
}
}
if (dpd->nzb2 > 0 && dpd->nzb2 < dpd->nzb) {
/* ensure the levels-equations specs come last */
qsort(dpd->d, dpd->nzb, sizeof *dpd->d, compare_gmm_specs);
}
if (dpd->nzb2 > 0) {
/* henceforth dpd->nzb refers to the number of specs in
differences, not the total number */
dpd->nzb -= dpd->nzb2;
dpd->d2 = dpd->d + dpd->nzb;
dpd->flags |= DPD_SYSTEM; /* in case it's not present */
}
return err;
}
static char *maxlag_string (char *targ, diag_info *d)
{
if (d->maxlag == 99) {
strcpy(targ, "maximum");
} else {
sprintf(targ, "%d", d->maxlag);
}
return targ;
}
static void print_instrument_specs (dpmod *dpd, const char *ispec,
const DATASET *dset, PRN *prn)
{
char lmax[16];
int i;
#if IVDEBUG
if (ispec != NULL) {
pputs(prn, "Incoming instrument specification:\n");
pprintf(prn, " '%s'\n", ispec);
}
#endif
pputc(prn, '\n');
pputs(prn, _("GMM-style instruments, differences equation:\n"));
for (i=0; i<dpd->nzb; i++) {
pprintf(prn, _(" %s: %s %d %s %s\n"), dset->varname[dpd->d[i].v],
_("lags"), dpd->d[i].minlag, _("to"), maxlag_string(lmax, &dpd->d[i]));
}
if (dpd->nzb2 > 0) {
pputs(prn, _("GMM-style instruments, levels equation:\n"));
for (i=0; i<dpd->nzb2; i++) {
pprintf(prn, _(" %s: %s %d %s %s\n"), dset->varname[dpd->d2[i].v],
_("lags"), dpd->d2[i].minlag, _("to"),
maxlag_string(lmax, &dpd->d2[i]));
}
}
}
/* we're doing two-step, but print the one-step results for
reference */
static int print_step_1 (dpmod *dpd, MODEL *pmod,
const DATASET *dset,
PRN *prn)
{
gretl_array *pnames = NULL;
gretl_matrix *cse;
double sei;
int i, err = 0;
cse = gretl_matrix_alloc(dpd->k, 2);
if (cse == NULL) {
return E_ALLOC;
}
gretl_model_allocate_param_names(pmod, dpd->k);
if (pmod->errcode) {
return pmod->errcode;
}
dpd_add_param_names(pmod, dpd, dset, 1);
pnames = gretl_array_from_strings(pmod->params, dpd->k,
0, &err);
if (!err) {
pputc(prn, '\n');
pprintf(prn, _("Step 1 parameter estimates, using %d observations"),
dpd->nobs);
pputc(prn, '\n');
for (i=0; i<dpd->k; i++) {
gretl_matrix_set(cse, i, 0, dpd->beta->val[i]);
sei = sqrt(gretl_matrix_get(dpd->vbeta, i, i));
gretl_matrix_set(cse, i, 1, sei);
}
err = print_model_from_matrices(cse, NULL, pnames, 0,
OPT_NONE, prn);
if (!na(dpd->sargan)) {
int df = dpd->nz - dpd->k;
pputs(prn, " ");
pprintf(prn, _("Sargan test: Chi-square(%d) = %g [%.4f]\n"),
df, dpd->sargan, chisq_cdf_comp(df, dpd->sargan));
}
}
gretl_matrix_free(cse);
if (pnames != NULL) {
gretl_array_nullify_content(pnames);
gretl_array_destroy(pnames);
}
return err;
}
/* Public interface for the dpanel command */
MODEL dpd_estimate (const int *list, const int *laglist,
const char *ispec, const DATASET *dset,
gretlopt opt, PRN *prn)
{
diag_info *d = NULL;
dpmod *dpd = NULL;
PRN *vprn = NULL;
int **Goodobs = NULL;
int *dlist = NULL;
int verbose = 0;
int nlevel = 0;
int nzb = 0;
MODEL mod;
int err = 0;
gretl_model_init(&mod, dset);
if (opt & OPT_V) {
verbose = 1;
vprn = prn;
} else if (opt & OPT_Q) {
prn = NULL;
}
/* parse GMM instrument info, if present */
if (ispec != NULL && *ispec != '\0') {
mod.errcode = parse_GMM_instrument_spec(ispec, dset, &d,
&nzb, &nlevel, opt);
if (mod.errcode) {
return mod;
}
}
if (nlevel > 0 && !(opt & OPT_L)) {
/* make --system (levels) explicit */
opt |= OPT_L;
}
dlist = gretl_list_copy(list);
if (dlist == NULL) {
mod.errcode = E_ALLOC;
return mod;
}
dpd = dpmod_new(dlist, laglist, dset, opt, d, nzb, &mod.errcode);
if (mod.errcode) {
fprintf(stderr, "Error %d in dpd_init\n", mod.errcode);
return mod;
}
dpanel_adjust_GMM_spec(dpd);
Goodobs = gretl_list_array_new(dpd->N, dpd->T);
if (Goodobs == NULL) {
err = E_ALLOC;
}
if (!err) {
do_unit_accounting(dpd, dset, Goodobs);
err = dpd_allocate_matrices(dpd);
#if ADEBUG
if (!err) {
fprintf(stderr, "allocate_matrices: dpd->Zi is %d x %d\n",
dpd->Zi->rows, dpd->Zi->cols);
}
#endif
}
if (!err) {
/* build the moment matrices */
err = do_units(dpd, dset, Goodobs);
}
gretl_list_array_free(Goodobs, dpd->N);
if (!err) {
err = trim_zero_inst(dpd, vprn);
}
if (verbose && (dpd->nzb > 0 || dpd->nzb2 > 0)) {
print_instrument_specs(dpd, ispec, dset, prn);
}
if (!err) {
err = dpd_step_1(dpd, opt);
}
if (!err && (opt & OPT_T)) {
/* second step, if wanted */
if (verbose) {
err = print_step_1(dpd, &mod, dset, prn);
}
if (!err) {
err = dpd_step_2(dpd);
}
}
if (err && !mod.errcode) {
mod.errcode = err;
}
if (!mod.errcode) {
/* write estimation info into model struct */
mod.errcode = dpd_finalize_model(&mod, dpd, dlist, laglist, ispec,
dset, opt);
} else {
free(dlist);
}
dpmod_free(dpd);
return mod;
}
|