Announcement

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

  • Nested regression with string variables

    Dear all,
    I hope you can help me. I've been trying and trying on my own to find a solution to this problem but I gave up.
    Here is the problem: I have a local varlist in which are listed all the variables I need to regress one by one on a set of variables list in another local varlist. To do this, I set up a loop, in which I need to nest two subconditions, namely if the X (set of dependant) belongs to certain variables and if the v belongs to certain other variables (set of regressors), do something.
    The code is the following
    Code:
    local indep "Business_Climat    Consumer_Confid    ECB_Deposit_Rat    ECB_Refinancing    Economic_Sentim    GDP_Flash_Estim    GDP_Flash_Preli    GDP_Revised_QQ    GDP_Revised_YY    HICPX_FEAT_Fin_    HICPX_FEAT_Fina    HICPX_FE_Flash_    HICP_Final_MM    HICP_Final_YY    HICP_Flash_YY    Ind_Prod_MM    Ind_Prod_YY    Industrial_Sent    Markit_Comp_Fin    Markit_Comp_Fla    Markit_Mfg_Fina    Markit_Mfg_Flas    Markit_Serv_Fin    Markit_Serv_Fla    MoneyM3_Annual_    PPI_MM    PPI_YY    Retail_Sales_YY    Sentix_Index    Services_Sentim    Unemployment_Ra"
    local country "CNY KOR IND TUR ZAF RUS BRA MEX ARG CHL US CAN UK EU DEU FR ITA JPN AUS HKG"
    local row=1
    
    foreach X of local country   {
        foreach v of local indep {
            if inlist(`"`X'"',"CNY" "KOR" "IND" "HKG") {
            sort Date
            gen ret`X'_f= ret`X'[_n+1]
            replace ret`X'_f=ret`X'[_n+3] if dow==5
            if `v'==Consumer_Confid | `v'==ECB_Deposit_Rat | `v'==ECB_Refinancing {
            quietly regress ret`X'_f surpref`v'
            }
            }
            drop ret`X'_f
            }
            else if inlist(`"`X'"',"JPN" "AUS") {
            sort Date
            gen ret`X'_f= ret`X'[_n+1]
            replace ret`X'_f=ret`X'[_n+3] if dow==5
            quietly regress ret`X'_f surpref`v'
            drop ret`X'_f
            }
            else {
            sort Date
            quietly regress ret`X' surpref`v'
            }
            }
            }
    The incriminated part is if `v'==Consumer_Confid | `v'==ECB_Deposit_Rat | `v'==ECB_Refinancin { bla bla}. It seems to make the code unable to read the macro since I got as error message Business_Climat not found. If I comment it, the code works properly. I've already tried to do regress ret`X'_f surpref`v' if `v'==Consumer_Confid | `v'==ECB_Deposit_Rat | `v'==ECB_Refinancing, to add the quotation marks, to list the variable one by one with own quotation marks in the definition of the local, to write if inlist(`"`v'"', "Consumer_Confid" "ECB_Deposit_Rat " "ECB_Refinancing"), to do levelsof() and I do not remember how many other attempts.
    Your help will be really much appreciated.
    Many thanks in advance

    Federica

  • #2
    In this line
    Code:
    if `v'==Consumer_Confid | `v'==ECB_Deposit_Rat | `v'==ECB_Refinancing {
    after substitution for the local macro v the code becomes
    Code:
    if Business_Climat==Consumer_Confid | Business_Climat==ECB_Deposit_Rat | Business_Climat==ECB_Refinancing {
    which interprets Business_Climat, Consumer_Confid, ECB_Deposit_Rat and ECB_Refinancing as a variable names.

    But you are not trying to compare variables, you are trying to compare strings. String constants need to be surrounded by quotation marks. Your code should read
    Code:
    if "`v'"=="Consumer_Confid" | "`v'"=="ECB_Deposit_Rat" | "`v'"=="ECB_Refinancing" {
    which after substitution for the local macro v becomes
    Code:
    if "Business_Climat"=="Consumer_Confid" | "Business_Climat"=="ECB_Deposit_Rat" | "Business_Climat"=="ECB_Refinancing" {
    Added in edit: In this line
    Code:
    if inlist(`"`X'"',"CNY" "KOR" "IND" "HKG") {
    you need to follow the guidance in the output of help inlist() which tells us that the items in the list needs to be separated by commas, thus
    Code:
    if inlist(`"`X'"',"CNY","KOR","IND","HKG") {
    For example:
    Code:
    . local indep "Business_Climat    Consumer_Confid    ECB_Deposit_Rat    ECB_Refinancing    Economic_Sentim"
    
    . local country "CNY KOR IND"
    
    . local row=1
    
    .
    . foreach X of local country   {
      2.     foreach v of local indep {
      3.         if inlist(`"`X'"',"CNY","KOR","IND","HKG") {
      4.             if "`v'"=="Consumer_Confid" | "`v'"=="ECB_Deposit_Rat" | "`v'"=="ECB_Refinancing" {
      5.                 display `"quietly regress ret`X'_f surpref`v'"'
      6.             }
      7.         }
      8.     }
      9. }
    quietly regress retCNY_f surprefConsumer_Confid
    quietly regress retCNY_f surprefECB_Deposit_Rat
    quietly regress retCNY_f surprefECB_Refinancing
    quietly regress retKOR_f surprefConsumer_Confid
    quietly regress retKOR_f surprefECB_Deposit_Rat
    quietly regress retKOR_f surprefECB_Refinancing
    quietly regress retIND_f surprefConsumer_Confid
    quietly regress retIND_f surprefECB_Deposit_Rat
    quietly regress retIND_f surprefECB_Refinancing
    You could change the second if command to use inlist() as well.
    Last edited by William Lisowski; 04 Nov 2021, 06:42.

    Comment


    • #3
      Dear William Lisowski,
      thank you very much for your reply. I'm sorry to tell you that I do not get the error message, but the code still doesn't work. It basically does anything.
      I modified the code as you suggested, first with
      if "`v'"=="Consumer_Confid" | "`v'"=="ECB_Deposit_Rat" | "`v'"=="ECB_Refinancing" { then with the correct specification of inlist as reported below
      Code:
      foreach X of local country   {
          foreach v of local regressors {
              if inlist(`"`X'"',"CNY","KOR","IND","HKG") {
              sort Date
              gen ret`X'_f= ret`X'[_n+1]
              replace ret`X'_f=ret`X'[_n+3] if dow==5
                if inlist(`"`v'"', "Consumer_Confid", "ECB_Deposit_Rat" , "ECB_Refinancing") {
                display `"quietly regress ret`X'_f surpref`v'"'
                }
              drop ret`X'_f
              }
              else if inlist(`"`X'"',"JPN", "AUS") {
              sort Date
              gen ret`X'_f= ret`X'[_n+1]
              replace ret`X'_f=ret`X'[_n+3] if dow==5
              quietly regress ret`X'_f surpref`v'
              drop ret`X'_f
              }
              else {
              sort Date
              quietly regress ret`X' surpref`v'
              }
              }
              }

      Stata goes at the end of the do file but doesn't produce any output.
      Thank you again

      Comment


      • #4
        First of all, your code should not have
        Code:
                  display `"quietly regress ret`X'_f surpref`v'"'
        because I did that as a deomonstration since there was no data to run the regression on.

        But the more general issue of no regression output is because by placing quietly before each of the regress commands you told Stata to run the commands without displaying the output.

        Code:
        foreach X of local country   {
            foreach v of local regressors {
                if inlist(`"`X'"',"CNY","KOR","IND","HKG") {
                sort Date
                gen ret`X'_f= ret`X'[_n+1]
                replace ret`X'_f=ret`X'[_n+3] if dow==5
                  if inlist(`"`v'"', "Consumer_Confid", "ECB_Deposit_Rat" , "ECB_Refinancing") {
                  regress ret`X'_f surpref`v'
                  }
                drop ret`X'_f
                }
                else if inlist(`"`X'"',"JPN", "AUS") {
                sort Date
                gen ret`X'_f= ret`X'[_n+1]
                replace ret`X'_f=ret`X'[_n+3] if dow==5
                regress ret`X'_f surpref`v'
                drop ret`X'_f
                }
                else {
                sort Date
                regress ret`X' surpref`v'
                }
                }
                }

        Comment


        • #5
          Dear William Lisowski, I know that quietly regress doesn't show any output, but here the issue is different: the code doesn't do anything. I put quietely because I have to regress 38 variables over 13 one by one, so it goes without saying that it becomes a bit redudant to see the output. I am just interested in making the code working with the two if conditions, which appereantly they're not. I also did remove the
          display `"quietly regress ret`X'_f surpref`v'"' of course. Probably there is no solution to this problem and the only way is to split the code in two subcodes. Thank you anyway

          Comment


          • #6
            My guess is that you were viewing your code that you show in post #1 in Stata's do-file editor, and first selected and ran the three local commands, then you selected and ran the commands for the nested loops. That is an error best explained with an example.

            In the do-file editor window, I have a two-line program that I run in its entirety.
            Code:
            . do "/Users/lisowskiw/Downloads/example.do"
            
            . local message Hello, world.
            
            . display "The message is `message'"
            The message is Hello, world.
            
            . 
            end of do-file
            
            .
            Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
            Code:
            . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD04017.000000"
            
            . local message Hello, world.
            
            . 
            end of do-file
            
            . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD04017.000000"
            
            . display "The message is `message'"
            The message is 
            
            . 
            end of do-file
            
            .
            The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

            Comment


            • #7
              Dear William Lisowski , thank you very much for your support. I finally manage to run the code by adopting a diffrent way of reasoning for posing conditions. It's less elegant, but it works.
              The code is the following
              Code:
              foreach X of local country {
                  if "`X'"=="JPN" | "`X'"=="AUS" {
                          foreach v of local total_idep {
                          reg f.ret`X' surpref`v'
                          }
                  }
                  else if "`X'"=="CNY" | "`X'"=="HKG" | "`X'"=="IND" | "`X'"=="KOR" {
                          foreach k of local indep2 {
                          reg ret`X' surpref`k'
                          scalar r2a1  = e(r2_a)
                          }
                          foreach j of local indep3 {
                          reg f.ret`X' surpref`j'
                          }
                  }
                  else {
                       foreach l of local total_indep {
                          reg ret`X' surpref`l'
                          scalar r2a3  = e(r2_a)
                      }
                  }        
              }

              Comment

              Working...
              X