Announcement

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

  • Single and Quadratic function in one loop

    Dear Experts,

    I need you advice on the possibility to loop 2 codes into one. One is quadratic function and another linear. The main problem is with local beta2`i'_`select' = _b[`select'2], which generates a macro for quadratic variable. It does nor allow the code to finish if used in a single function regression.
    One has to somehow disable it once a single function regression runs.

    The code below works fine, but i have to try to combine the loops.

    Code:
    //quadratic equation
    foreach j in "Volume Volume2 `varlist_`i''" "TransferPrice TransferPrice2" {
        capture noisily reg GlobalPrice `j' [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', nocons baselevels
            
        tokenize "`j'"
        local select "`1'"    
            
        local beta1`i'_`select' = _b[`select']
        local beta2`i'_`select' = _b[`select'2]
            
        di "`PumpName'"
        di "quadratic equation is `beta1`i'_`select''*x + `beta2`i'_`select''*x^2"
        local g`i'_`select_quadratic' = "function y = `beta1`i'_`select''*x + `beta2`i'_`select''*x^2, range(`m_min`i'' `m_max`i'')"
        di "`g`i'_`select_quadratic''"
    }
    
    //linear equation
    foreach j in "Volume `varlist_`i''" "TransferPrice" {
        capture noisily reg GlobalPrice `j' [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', nocons baselevels
            
        tokenize "`j'"
        local select "`1'"    
            
        local beta1`i'_`select' = _b[`select']
                
        di "`PumpName'"
        di "linear equation is `beta1`i'_`select''*x"
        local g`i'_`select_linear' = "function y = `beta1`i'_`select''*x, range(`m_min`i'' `m_max`i'')"
        di "`g`i'_`select_linear''"
    }
    }
    Thank you

  • #2
    Give a data example please. As well as, be more specific about what you want, please. The most I understand is the you want to square a variable.... I think.

    Comment


    • #3
      What about something like this:

      Code:
      // quadratic and linear combined
      foreach j in "Volume `varlist_`i''" "TransferPrice" {
          
          tokenize "`j'"
          local select "`1'"   
          
          capture noisily reg GlobalPrice `j' `select'2 [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', nocons baselevels
          
          local beta1`i'_`select' = _b[`select']
          local beta2`i'_`select' = _b[`select'2]
          
          di "`PumpName'"
          di "quadratic equation is `beta1`i'_`select''*x + `beta2`i'_`select''*x^2"
          local g`i'_`select_quadratic' = "function y = `beta1`i'_`select''*x + `beta2`i'_`select''*x^2, range(`m_min`i'' `m_max`i'')"
          di "`g`i'_`select_quadratic''"
          
          capture noisily reg GlobalPrice `j' [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', nocons baselevels
          
          local beta1`i'_`select' = _b[`select']
       
          di "`PumpName'"
          di "linear equation is `beta1`i'_`select''*x"
          local g`i'_`select_linear' = "function y = `beta1`i'_`select''*x, range(`m_min`i'' `m_max`i'')"
          di "`g`i'_`select_linear''"
      }
      Above, I use your tokenize, then `select'2 syntax to add what I assume is the quadratic term to the model dynamically.

      Comment


      • #4
        Done, I have used a slightly different approach that the one that Daniel proposed (did not solve the problem of local beta2`i'_`select' = _b[`select'2]
        Now it only defines local, if select2 is present.

        Code:
        //quadratic equation
        foreach j in "Volume" "Volume Volume2" "TransferPrice" "TransferPrice TransferPrice2" {
            
            tokenize "`j'"
            local select1 "`1'"
            local select2 "`2'"
            
            capture unab ``j'' : ``var''
            local variable_count_equation = wordcount("`j'")
            
            capture noisily reg GlobalPrice `j' [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', robust baselevels
        
        forvalues m = 1/`variable_count_equation' {
            
            local beta_`i'_`select`m'' = _b[`select`m'']
        }    
        
            di "`PumpName'"
            di "quadratic equation is `cons`i'_`select'' + `beta1`i'_`select''*x + `beta2`i'_`select''*x^2"
            local g`i'_`select_quadratic' = "function y = `cons`i'_`select'' + `beta1`i'_`select''*x + `beta2`i'_`select''*x^2, range(`m_min`i'' `m_max`i'')"
            di "`g`i'_`select_quadratic''"
        }
        Thanks everyone for your time

        Comment

        Working...
        X