Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Originally posted by William Lisowski View Post
    The foreach command you have used loops over a list of variable names, not the values of a single variable. In that it is a step backward from the code you presented in post #1 of https://www.statalist.org/forums/for...ustry-and-year .

    Code:
    egen group = group(nic2digit year)
    gen residual = .
    levelsof group, local(grouplist)
    foreach i of local grouplist {
    regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == `i'
    predict temp, residuals if group == `i'
    replace residual=temp if group == `i'
    drop temp
    }
    Subsequent to my post in #10 I realized that
    Code:
    statsby _b, by(group nic2digit year) clear verbose nodots: regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO
    would give you stored coefficients with the group identifier and the nic2digit and year identifiers.
    Thanks you very much for replying.
    But unfortunately the code for residuals didn't generate residuals.
    It shows:
    gen residual = .
    (73474 missing values generated)

    grouplist has been generated but no values under residuals

    no observations r(2000) is also seen at the end.



    Comment


    • #17
      For Statalist to tell you what you did wrong, you have to tell Statalist what you did. Not just the pieces you think are important.

      For example in post #9 you posted that the residuals you were getting were different from those computed by hand, but it wasn't until post #14 that you posted that only a single regression had been run.

      Now you post a few bits and pieces of what you've done and seen, leaving out major pieces.

      Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

      Section 12.1 is particularly pertinent

      12.1 What to say about your commands and your problem

      Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
      So in particular, tell us how many times did your code go through the loop? Many times, one time, no times? It makes a difference; it sounds as if your code never went through the loop.

      If the answer is one time or no times, then you need to copy from the Stata Results window everything from the egen command through the end of the loops - all commands and output - and paste it into a post using CODE delimiters. CODE delimiters are produced with the "#" icon on the Statlist Forum Advanced Editor Toolbar, not with the "<>" icon. Your post should have [code] and [/code] surrounding your code. For example, the following:

      [code]
      // sample code
      sysuse auto, clear
      describe
      [/code]

      will be presented in the post as the following:
      Code:
      // sample code
      sysuse auto, clear
      describe

      Comment


      • #18
        Sorry for the mistake from my end. When I tried the loop for residuals it displayed the following:


        Code:
        . egen group = group(nic2digit year)
        
        . gen residual = .
        (73474 missing values generated)
        
        . levelsof group, local(grouplist)
        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 4
        > 9 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 16
        > 2 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 22
        > 9 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 29
        > 6 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 36
        > 3 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 43
        > 0 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 49
        > 7 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 56
        > 4 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 63
        > 1 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 69
        > 8 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 76
        > 5 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
        
        . foreach i of local grouplist {
          2. regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == `i'
          3. predict temp, residuals if group == `i'
          4. replace residual=temp if group == `i'
          5. drop temp
          6. }
        no observations
        r(2000);
        
        end of do-file
        
        r(2000);
        Hope it clarifies.

        Comment


        • #19
          The lack of any regression output is telling you that the regression for group==1 has not been run successfully.

          The error message you show must therefore come from the regression for group==1 and it is telling you that the regression is failing because there are no observations suitable to regress.

          You need to look at the data for group==1 and see what the problem is.
          Code:
          list ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == 1

          Comment


          • #20
            Actually,
            Code:
            list group nic2digit year ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == 1
            would provide additional information to identify the source of the observations.

            Comment


            • #21
              Hi
              Appreciate your effort to help me out.I have executed the code you recommended. There are some missing values for some observations in the group==1. But it is not the case with other
              groups. There are many cases where all the variables have values (non-missing). But why it is still not producing the residuals is my concern.



              The error message you show must therefore come from the regression for group==1 and it is telling you that the regression is failing because there are no observations suitable to regress
              Code:
               list ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == 1
              
                     +-----------------------------------------------------------------+
                     |     ACC_w       CFO_w      PPE_w      Rev_w   LaCFO       LeCFO |
                     |-----------------------------------------------------------------|
               1106. |  .0235294   -.0149733   .2652406   .7935829       .   -.1978453 |
               2058. |  .0878763   -.0390561   .6753458    .810415       .   -.4009615 |
               2636. | -.0425968    .1212369   .6352652   1.395454       .    .1336564 |
               4931. |  .1254571   -.0365682   .2745429   2.460759       .    .2380591 |
               5798. | -.0098067    .1096048   .7041823   .8498991       .    .1194207 |
                     |-----------------------------------------------------------------|
               6495. |  -.083301    .1506472   .3045261   1.037837       .    .0007339 |
               6937. | -.0221261    .0663784   .4766022   1.001017       .    .0439309 |
               7634. | -.0400601    .0345518   .9344016   .9994993       .    .0387722 |
               8059. |  .0532095   -.0379012   .4914485   2.615498       .    .0100379 |
               8314. | -.0556381    .1782878   .3709206   1.224858       .    .0644387 |
                     |-----------------------------------------------------------------|
               8688. | -.0163573    .0408932   .4696642   .4811223       .    .0015792 |
               9555. |  .0712187    .0219673   .4737652   1.938716       .    .0998586 |
              10320. |  .1436985   -.0930507   .7714959    .327444       .   -.0253165 |
              11680. |  .0382928   -.0151576   .0139165          .       .   -.0389076 |
              12173. | -.0985692   -.0445151   .6868045   .0604134       .    .0938511 |
                     |-----------------------------------------------------------------|
              12428. | -.0487305    .1346736   .7156911   .7569765       .     .076002 |
              12462. | -.0759697    .1401248   .9519694   .6454103       .    .1561787 |
              13091. | -.0203839    .2051674   .8741358   .8452862       .    .1001158 |
              15658. |         .           .          .          .       .   -.0305206 |
              18752. | -.0441045    .1398629   .6293831   1.074203       .    .1198547 |
                     |-----------------------------------------------------------------|
              20129. | -.1007371    .1063805   .7314189   1.204622       .    .0837406 |
              21132. | -.0524774    .0740082   .8375967   .3843292       .    .0949425 |
              23274. |  .0279144    .0347786   .7944171   .5624071       .    .0912265 |
              23359. | -.0153309    .0878821   .4409462   .7879776       .   -.0157623 |
              23716. | -.0124874    .0784173    .895316   .5962394       .    .1155282 |
                     |-----------------------------------------------------------------|
              24022. | -.0150907    .0795603   .5702956   .5111982       .    .0767655 |
              24617. |  .1003742    .0660357   .3226942          .       .   -.2309455 |
              25076. | -.0327138   -.0669145   .8460966    .129368       .    .0292299 |
              25314. |  .0229063   -.0054221   .3267391   .6183525       .    .0322688 |
              26470. | -.0241241    .1010913   .6433085   .6590082       .    .0824463 |
                     |-----------------------------------------------------------------|
              27337. |  -.010449    .0648627    .325398   1.266006       .    .0314507 |
              27864. | -.0184576    .1179735   .2110358   4.307583       .    .0713931 |
              28068. |  .0484299    .0144521   .7389784   .3823654       .    .1099462 |
              29190. | -.0821831     .162589   1.059137   .6891283       .    .1383779 |
              29275. | -.0706188    .0938396   .9031553   1.632837       .   -.0048719 |
                     |-----------------------------------------------------------------|
              30176. |  .0258193    .1710881   .8740247   .3658675       .   -.1161317 |
              32233. | -.1790638    .2422511   .2640444   3.354241       .   -.1137101 |
              32879. | -.0152878    .0276222   .6329476   .3153768       .   -.0152169 |
              34001. |         .           .          .          .       .           . |
              35820. |  .0375845    .0371622   .6891892   4.229308       .    .1382431 |
                     |-----------------------------------------------------------------|
              36143. | -.4384615    .0531306   .4354204   .7812164       .           . |
              36194. |  -.083648     .047262   .9758089   .9732435       .    .1148835 |
              37010. |  .0181656    .0992585   .7101852   .5086355       .    .0813409 |
              37639. | -.0462849    .1026477   .7917545   1.321081       .    .0732664 |
              37962. |         .           .          .          .       .           . |
                     |-----------------------------------------------------------------|
              38404. |  .0911941    .0645414   .1714136   1.525999       .    .1119904 |
              39186. | -.0817218    .0515522   .8569711   .8526322       .    .0371335 |
              39203. |   .006181     .020677   .5683591   .5479764       .     .020677 |
              39322. | -.1406288    .1679866    .400288   1.172066       .    .1147939 |
              41498. | -.0594924     .163264   .2472365   .6169052       .    .0768393 |
                     |-----------------------------------------------------------------|
              42620. | -.3687824    .0406333   .5893232   1.373126       .    .0085232 |
              43317. |   .096299   -.0582106   .5008983   .9112468       .    .0266283 |
              43402. | -.0093277    .0815241   .7692093   .5244969       .    .1027846 |
              46751. | -.0030303    .0257576   .0560606   .3848485       .    -.013104 |
              47210. |         .           .          .          .       .           . |
                     |-----------------------------------------------------------------|
              47822. | -.0707605    .2057986   .9158413   .6411474       .    .2150426 |
              48417. |  .1212052   -.0273093   .5589972   .5998638       .    .0061348 |
              49522. |  .0166146     .066347   .6018064   .7414139       .    .1413901 |
              50338. |         .           .          .          .       .           . |
              50627. | -.1898256   -.0266817   .4744836      .0307       .    .0062909 |
                     |-----------------------------------------------------------------|
              53075. |  .2141764   -.1382554   .2598634   .7383823       .   -.1849502 |
              53670. |         .           .          .          .       .    .0451128 |
              54554. | -.0248927    .1613734   .4263233   1.881259       .    .1233417 |
              54945. |  .0717351   -.0067984   .0557139   .7721807       .    .0328986 |
              56169. |         .           .          .          .       .    .0504151 |
                     |-----------------------------------------------------------------|
              57835. |  .1331729    .0191826   .2483337   1.373216       .    .1547095 |
              58039. |         .           .          .          .       .           . |
              58362. | -.0076665     .048874    1.06277   1.034978       .    .0675255 |
              58685. | -.0248633    .0145201   .4103431   .3544505       .    .0044288 |
              59552. | -.0348187    .0664451   .6060356   1.070806       .    .0559388 |
                     |-----------------------------------------------------------------|
              60827. | -.0116959    -.085213   .8187134   .0116959       .   -.1926606 |
              60878. |  -.034626    .0706371   .6703601   .8144044       .           . |
              61320. |         .           .          .          .       .           . |
              61609. | -.0049857    .1526355   .5670521   1.325184       .    .0993982 |
              61626. | -.0155063    .0712367    .868811   .4065403       .    .0369705 |
                     |-----------------------------------------------------------------|
              62221. |  .1558107   -.0613956   .1841868   1.674449       .    -.039046 |
              62595. |         .           .          .          .       .    .0564558 |
              62816. |  .0457341   -.0803228   .7498078   .4000769       .   -.0803228 |
              64890. |         .           .          .          .       .           . |
              64907. |         .           .          .          .       .           . |
                     |-----------------------------------------------------------------|
              65281. |         .           .          .          .       .           . |
              67406. |         .           .          .          .       .           . |
              68443. |         .           .          .          .       .           . |
              68919. |         .           .          .          .       .           . |
              72693. |         .           .          .          .       .           . |
                     +-----------------------------------------------------------------+

              Comment


              • #22
                But why it is still not producing the residuals is my concern.
                Because you are running a do-file and when Stata encounters an error, unless special steps are taken, Stata stops running the do-file. That is why there is no output from any command - because the very first command it tried to execute failed, and Stata told you a command failed by displaying an error message and a return code.

                The statsby command takes extra steps to accommodate the failure of the command it is running and provide missing values for the statistics being collected for the group being run. I expect if you look at your statsby output from the regressions you will find that you have many cases where you have missing values for the coefficients, including the one that corresponds to this group.

                I note that at one earlier point you sought and were given advice on how to limit your regressions to those groups where there were more than 20 observations. But you apparently had not considered missing values, and since Stata omits any observation for which a dependent or independent variables is missing, you have no way of knowing from your statsby output how few observations were actually used in any of your regressions. You might rerun your statsby regressions with
                Code:
                statsby _b n=(e(N)), by(nic2digit year)clear verbose nodots: regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO
                to return not only the coefficients but also the number of observations after those with missing values are eliminated, so you can tell which regressions have fewer observations than you want to use for your purposes.

                The following code should continue even if the regression command fails, and produced residuals for those groups where the regression command does not fail.
                Code:
                egen group = group(nic2digit year)
                gen residual = .
                levelsof group, local(grouplist)
                foreach i of local grouplist {
                    display "group `i'"
                    capture noisily regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == `i'
                    if _rc=0 {
                        predict temp, residuals if group == `i'
                        replace residual=temp if group == `i'
                        drop temp
                    }
                }
                Now let me take a moment that I think I have earned to give you some advice about using Stata. The question in post #21 is basic and is covered in the introductory Stata documentation, so I think you have not taken advantage of that documentation.

                When I began using Stata in a serious way, I started as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual. So if you had followed that path, you would have previously read Chapter 18 of the Stata User's Guide, and been able to interpret what you are seeing, or had some idea where to look for help.

                Stata supplies exceptionally good documentation that amply repays the time spent studying it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

                Comment


                • #23
                  Thanks. Now I understand that lot of issues would have been solved if I had gone through the stata maual and User's guide before.
                  As you said the statsby command works effectively in my case. But it becomes difficult for me to use those coefficients and find residuals for each companies in the sample by way of manually fitting the coefficients .

                  The following code should continue even if the regression command fails, and produced residuals for those groups where the regression command does not fail.
                  But it displayed =exp not allowed (r101), so I put it as
                  HTML Code:
                  if _rc==0
                  , in the loop then it worked for group 1 and continued but stopped after group 2 . I assume it still stuck at somewhere. How would I get away with it?? Help is highly appreciated.

                  Code:
                  gen group = group(nic2digit year)
                  
                  . gen residual = .
                  (73474 missing values generated)
                  
                  . levelsof group, local(grouplist)
                  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 4
                  > 9 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 16
                  > 2 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 22
                  > 9 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 29
                  > 6 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 36
                  > 3 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 43
                  > 0 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 49
                  > 7 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 56
                  > 4 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 63
                  > 1 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 69
                  > 8 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 76
                  > 5 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
                  
                  . foreach i of local grouplist {
                    2.     display "group `i'"
                    3.     capture noisily regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == `i'
                    4.     if _rc==0 {
                    5.         predict temp, residuals if group == `i'
                    6.         replace residual=temp if group == `i'
                    7.         drop temp
                    8.     }
                    9. }
                  group 1
                  no observations
                  group 2
                  
                        Source |       SS       df       MS              Number of obs =      61
                  -------------+------------------------------           F(  5,    55) =   41.39
                         Model |  .805929502     5    .1611859           Prob > F      =  0.0000
                      Residual |  .214164997    55  .003893909           R-squared     =  0.7901
                  -------------+------------------------------           Adj R-squared =  0.7710
                         Total |   1.0200945    60  .017001575           Root MSE      =   .0624
                  
                  ------------------------------------------------------------------------------
                         ACC_w |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                         CFO_w |  -.9628239   .0857503   -11.23   0.000    -1.134671   -.7909764
                         PPE_w |  -.0752325   .0292598    -2.57   0.013    -.1338704   -.0165947
                         Rev_w |   .0133543   .0132077     1.01   0.316    -.0131145    .0398232
                         LaCFO |   .1401917   .1103844     1.27   0.209    -.0810235     .361407
                         LeCFO |   .5960513    .104256     5.72   0.000     .3871176     .804985
                         _cons |   .0241132   .0255512     0.94   0.349    -.0270926     .075319
                  ------------------------------------------------------------------------------
                  option if not allowed
                  r(198);
                  
                  end of do-file
                  
                  r(198);
                  
                  .

                  Comment


                  • #24
                    Replace
                    Code:
                    predict temp, residuals if group == `i'
                    with
                    Code:
                    predict temp if group == `i', residuals

                    Comment


                    • #25
                      Hi Lisowski,

                      Finally it worked. I am extremely thankful for your timely and effective replies. It generated residuals as I wanted.
                      But one more thing I was concerned about is the number of observation used for each regression. The following statsby command allow me to identify how many observations were used in regression.
                      HTML Code:
                        statsby _b n=(e(N)), by(nic2digit year)clear verbose nodots: regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO
                      But is there any other way I can add a code to find the number of observations used to find residuals ?? Can I add any such code inside the following loop code you advised ?? Otherwise I will not be able to follow my requirement that take residuals only if it is generated by using at least 20 observations. I would prefer having a column on number of observations next to the column on residuals. Kindly advise (Hopefully my last request on this topic!!)

                      HTML Code:
                      egen group = group(nic2digit year)
                      gen residual = .
                      levelsof group, local(grouplist)
                      foreach i of local grouplist {
                          display "group `i'"
                          capture noisily regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == `i'
                          if _rc==0 {
                              predict temp if group == `i', residuals
                              replace residual=temp if group == `i'
                              drop temp
                          }
                      }
                      Thanks

                      Comment


                      • #26
                        The way this code was set up, the number of residuals will be the same as the number of observations. Your regression is carried out on those observations having a single value of group, the value of `i' at each iteration of the loop. The regression will be carried out only on those, and also excluding any observations where any of ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO is a missing value. When you then call -predict-, if left to its own devices, it would calculate a residual for every observation in the data set for which there are no missing values among ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO. But, you have not left it to its own devices: you have restricted it to group == `i'. So you will be calculating residuals on precisely those observations for which group == `i' and ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO are all non-missing. Well, that is exactly the same set of observations that participated in the regression. So there is no need to separately track the number of residuals; it equals the number of observations here.

                        Comment


                        • #27
                          But I want to know how many firms involved in each regression run by nic2digit and year. Because I need to take residuals of those groups where at least 20 observations were there.

                          The way this code was set up, the number of residuals will be the same as the number of observations

                          Comment


                          • #28
                            Let me restate Clyde's first sentence and build from there.

                            The way this code was set up, for each group the number of observations for which residuals are calculated will be the same as the number of observations on which the regression was run.

                            Code:
                            egen group = group(nic2digit year)
                            gen residual = .
                            gen n = 0
                            levelsof group, local(grouplist)
                            foreach i of local grouplist {
                                display "group `i'"
                                capture noisily regress ACC_w CFO_w PPE_w Rev_w LaCFO LeCFO if group == `i'
                                if _rc==0 {
                                    replace n = e(N) if group == `i'
                                    predict temp if group == `i', residuals
                                    replace residual=temp if group == `i'
                                    drop temp
                                }
                            }
                            Last edited by William Lisowski; 08 Jul 2017, 13:43.

                            Comment


                            • #29
                              Great ,
                              This is the one I was looking for.
                              Thank you very much Lisowski..
                              Never knew stata forum is such a helpful platform

                              Comment


                              • #30
                                Hi,

                                I was running the following command to generate coefficients for group wise regression.
                                Code:
                                statsby n=e(N) _b , by(nicyear) clear : regress Ac_w b_w rev_w recc_w PPE_w roa_w
                                I have panel data around 20 years. I wanted to run the regression in each "nicyear" combination and get the coefficients, which I require for further analysis. But the above code generates coefficients by replacing the current data. I want to get the coefficients displayed in separate columns in my existing data set.
                                Each nicyear combination will have many companies. But the coefficients generated will be same for firms under same nicyear combination
                                A snapshot of my data is attached below:

                                Can someone help me: I need coefficients of statsby output in separate columns

                                Thanks






                                Click image for larger version

Name:	Capture2.JPG
Views:	2
Size:	110.6 KB
ID:	1425130


                                Comment

                                Working...
                                X