Announcement

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

  • Error type mismatch

    Dear everyone,

    Code:
    qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
    replace `x' = `x'_pval in 1
    replace `x' = `x'_pval2 in 2
    replace `x' = `x'_OLSa in 3
    replace `x' = `x'_OLSt in 4
    replace `x' = 0 in 5 
    replace `x' = 1 in 5 if `x'_pval<0.1 & `x'_OLSa>0
    replace `x' = 0 in 6
    replace `x' = 1 in 6 if `x'_pval<0.1 & `x'_OLSa<0
    replace `x' = 0 in 7 
    replace `x' = 1 in 7 if `x'_pval2<0.1 & `x'_OLSa>0
    replace `x' = 0 in 8
    replace `x' = 1 in 8 if `x'_pval2<0.1 & `x'_OLSa<0
    }
    gen Variable=.
    order Variable
    tostring Variable, replace
    replace Variable = "" if Variable == "."
    replace Variable = "Alpha_pval" in 1
    replace Variable = "Tstat_pval" in 2
    replace Variable = "OLS_Alpha" in 3
    replace Variable = "OLS_Tstat" in 4
    replace Variable = "skill_from_alpha" in 5
    replace Variable = "neg_skill_from_alpha" in 6
    replace Variable = "skill_from_tstat" in 7
    replace Variable = "neg_skill_from_tstat" in 8
    *Converting each variable to strings:
    foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS  {
    gen str7 `x'2 = string(`x')
    }
    foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
    label variable `x'2 "`: var label `x''"
    }
    drop ADDVALUEFUND- ROBECOHOLLANDS
    foreach var of varlist ADDVALUEFUND2-ROBECOHOLLANDS2  {
    local newvar2 = subinstr("`var'", "2", "",.)
    quietly rename `var' `newvar2'
    }
    foreach var of varlist * {
    qui replace `var' = "`var'" in 9
    }
    yields the error
    Code:
    type mismatch
    r(109);
    Could someone please explain why Stata gives me the error code?

    Best regards,
    Rens Eggink


  • #2
    for some particular variable, you are coding it as though it is numeric when it is actually string (or vice versa); you should be able to find the problem just by describing all the variables in your code (however, I note that the code you pasted is clearly not complete, as you start with a -replace-, so be careful)

    actually, I can see one place where this happens: you -gen- the variable named "Variable" as a numeric variable but then treat it as string in your -replace- commands

    Comment


    • #3
      Dear Rich,

      Thanks a lot for your help!

      I browsed through the data indeed Stata thinks the variables ADDVALUEFUND - ROBECOHOLLANDS are string even though they look numeric.

      Initially, I did not share my entire code because it might spook forum users to respond. However, the following is my entire code. I am a beginner in Stata. Do you know how to fix the syntax?

      :
      Code:
      use "C:\Users\Rens\OneDrive\Master scriptie Rens\STATA, DATA and MT Rens Eggink\Output\Bootstrap simulations.dta"
      
      **********Inference based on alpha--
      *See CNS 2008 footnote 5 for justification of one-sided tests.
      *Note: For funds with a positive estimated OLS alpha, the p-value is equal to the number of times the estimated OLS alpha is less than the simulated alpha divided by total no of simulations. For negative alpha funds, it is the reverse.
      *drop realindexaussmallcompanie realindexwholesaleaustral                // Martineer
      
      *Calculating p-values for each fund with positive OLS alphas (H0: No skill/Any abnormal returns due to luck-- the OLS alpha is just a random draw of the simulated alpha distribution imposing a true alpha of 0, H1: Skill).
      *`x'_test is given a value of 0 for fund x if the OLS alpha exceeds simulated alpha b (b=1,...,999)
      *Hence, the average `x'_test is the p-value for the test (since otherwise, it is given a value of 1).
      qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {                    
      if `x'_alpOLS > 0 {
      gen `x'_test=.
      replace `x'_test=0 if `x'_alpOLS > `x'_alphas
      replace `x'_test=1 if `x'_alpOLS <= `x'_alphas
      sum `x'_test
      scalar `x'_pval = r(mean)
      }
      }
      
      *Performing the same test for funds with negative OLS alpha (now, H1 is 'negative' skill i.e. actual alpha is significantly less than what would be expected under randomisation): 
      qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
      if `x'_alpOLS < 0 {
      gen `x'_test=.
      replace `x'_test=0 if `x'_alpOLS < `x'_alphas
      replace `x'_test=1 if `x'_alpOLS >= `x'_alphas
      sum `x'_test
      scalar `x'_pval = r(mean)
      }
      }
      
      
      **********Inference based on t-stat of alpha--
      
      *Intuition same as alpha, but better statistical properties
      
      *For positive alpha funds:
      qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
      if `x'_t_OLS > 0 {
      gen `x'_test2=.
      replace `x'_test2=0 if `x'_t_OLS > `x'_tstats
      replace `x'_test2=1 if `x'_t_OLS <= `x'_tstats
      sum `x'_test2
      scalar `x'_pval2 = r(mean)
      }
      }
      
      *For negative alpha funds:
      qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
      if `x'_t_OLS < 0 {
      gen `x'_test2=.
      replace `x'_test2=0 if `x'_t_OLS < `x'_tstats
      replace `x'_test2=1 if `x'_t_OLS >= `x'_tstats
      sum `x'_test2
      scalar `x'_pval2 = r(mean)
      }
      }
      
      
      **********Finally, presenting the main results:
      
      *Converting some 'variables' back into scalars:
      qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
      scalar `x'_OLSa = `x'_alpOLS[1]
      scalar `x'_OLSt = `x'_t_OLS[1]
      }
      drop Date-MOM                     //Pay attention! See next line. 
      *drop date-MOM allords-zurichinvausvalueretailsh_test2 // This was Martineer's code on line 1532. 
      drop in 1/999
      set obs 9
      *The results:
      qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
      replace `x' = `x'_pval in 1
      replace `x' = `x'_pval2 in 2
      replace `x' = `x'_OLSa in 3
      replace `x' = `x'_OLSt in 4
      replace `x' = 0 in 5 
      replace `x' = 1 in 5 if `x'_pval<0.1 & `x'_OLSa>0
      replace `x' = 0 in 6
      replace `x' = 1 in 6 if `x'_pval<0.1 & `x'_OLSa<0
      replace `x' = 0 in 7 
      replace `x' = 1 in 7 if `x'_pval2<0.1 & `x'_OLSa>0
      replace `x' = 0 in 8
      replace `x' = 1 in 8 if `x'_pval2<0.1 & `x'_OLSa<0
      }
      gen Variable=.
      order Variable
      tostring Variable, replace
      replace Variable = "" if Variable == "."
      replace Variable = "Alpha_pval" in 1
      replace Variable = "Tstat_pval" in 2
      replace Variable = "OLS_Alpha" in 3
      replace Variable = "OLS_Tstat" in 4
      replace Variable = "skill_from_alpha" in 5
      replace Variable = "neg_skill_from_alpha" in 6
      replace Variable = "skill_from_tstat" in 7
      replace Variable = "neg_skill_from_tstat" in 8
      *Converting each variable to strings:
      foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS  {
      gen str7 `x'2 = string(`x')
      }
      foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
      label variable `x'2 "`: var label `x''"
      }
      drop ADDVALUEFUND- ROBECOHOLLANDS
      foreach var of varlist ADDVALUEFUND2-ROBECOHOLLANDS2  {
      local newvar2 = subinstr("`var'", "2", "",.)
      quietly rename `var' `newvar2'
      }
      foreach var of varlist * {
      qui replace `var' = "`var'" in 9
      }
      Best regards,
      Rens Eggink

      Comment


      • #4
        first, if your variables look numeric and might be used as though they are numeric - but they are actually string at present - you should probably use the -destring- command; see
        Code:
        help destring
        second, I don't understand the overall purpose of your code so I don't offer any other comments at this time

        Comment


        • #5
          I don't understand this either, but the code can be shortened quite a bit. The creation of indicator variables can be trimmed and there is no point in creating a variable that is numeric missing, converting it to string and replacing it with empty: just start out with a string variable with empty strings. See also the comment flagged NJC (my initials).

          I may have broken this by accident, as I can't test it. I recommend more use of indenting.


          Code:
          use "C:\Users\Rens\OneDrive\Master scriptie Rens\STATA, DATA and MT Rens Eggink\Output\Bootstrap simulations.dta"
          
          **********Inference based on alpha--
          *See CNS 2008 footnote 5 for justification of one-sided tests.
          *Note: For funds with a positive estimated OLS alpha, the p-value is equal to the number of times the estimated OLS alpha is less than the simulated alpha divided by total no of simulations. For negative alpha funds, it is the reverse.
          *drop realindexaussmallcompanie realindexwholesaleaustral                // Martineer
          
          *Calculating p-values for each fund with positive OLS alphas (H0: No skill/Any abnormal returns due to luck-- the OLS alpha is just a random draw of the simulated alpha distribution imposing a true alpha of 0, H1: Skill).
          *`x'_test is given a value of 0 for fund x if the OLS alpha exceeds simulated alpha b (b=1,...,999)
          *Hence, the average `x'_test is the p-value for the test (since otherwise, it is given a value of 1).
          
          qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {                    
              if `x'_alpOLS > 0 {
                  gen `x'_test = `x'_alpOLS <= `x'_alphas
                  sum `x'_test
                  scalar `x'_pval = r(mean)
              }
          }
          
          *Performing the same test for funds with negative OLS alpha (now, H1 is 'negative' skill i.e. actual alpha is significantly less than what would be expected under randomisation): 
          qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
              if `x'_alpOLS < 0 {
                  gen `x'_test= `x'_alpOLS >= `x'_alphas
                  sum `x'_test
                  scalar `x'_pval = r(mean)
              }
          }
          
          
          **********Inference based on t-stat of alpha--
          
          *Intuition same as alpha, but better statistical properties
          
          *For positive alpha funds:
          qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
              if `x'_t_OLS > 0 {
                  gen `x'_test2= `x'_t_OLS <= `x'_tstats
                  sum `x'_test2
                  scalar `x'_pval2 = r(mean)
              }
          }
          
          *For negative alpha funds:
          qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
              if `x'_t_OLS < 0 {
                  gen `x'_test2= `x'_t_OLS >= `x'_tstats
                  sum `x'_test2
                  scalar `x'_pval2 = r(mean)
              }
          }
          
          
          **********Finally, presenting the main results:
          
          *Converting some 'variables' back into scalars:
          qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
              scalar `x'_OLSa = `x'_alpOLS[1]
              scalar `x'_OLSt = `x'_t_OLS[1]
          }
          drop Date-MOM                     //Pay attention! See next line. 
          *drop date-MOM allords-zurichinvausvalueretailsh_test2 // This was Martineer's code on line 1532. 
          drop in 1/999
          set obs 9
          *The results:
          qui foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS {
              replace `x' = `x'_pval in 1
              replace `x' = `x'_pval2 in 2
              replace `x' = `x'_OLSa in 3
              replace `x' = `x'_OLSt in 4
              replace `x' = (`x'_pval<0.1 & `x'_OLSa>0) in 5 
              replace `x' = (`x'_pval<0.1 & `x'_OLSa<0) in 6 
              replace `x' = (`x'_pval2<0.1 & `x'_OLSa>0) in 7 
              replace `x' = (`x'_pval2<0.1 & `x'_OLSa<0) in 8 
          }
          
          gen Variable = "" 
          order Variable
          replace Variable = "Alpha_pval" in 1
          replace Variable = "Tstat_pval" in 2
          replace Variable = "OLS_Alpha" in 3
          replace Variable = "OLS_Tstat" in 4
          replace Variable = "skill_from_alpha" in 5
          replace Variable = "neg_skill_from_alpha" in 6
          replace Variable = "skill_from_tstat" in 7
          replace Variable = "neg_skill_from_tstat" in 8
          
          *Converting each variable to strings:
          * NJC wouldn't tostring ADDVALUEFUND- ROBECOHOLLANDS, replace be enough here? 
          foreach x of varlist ADDVALUEFUND- ROBECOHOLLANDS  {
              gen str7 `x'2 = string(`x')
              label variable `x'2 "`: var label `x''"
              drop `x'
              rename `x'2 `x'  
          }
          
          foreach var of varlist * {
              qui replace `var' = "`var'" in 9
          }

          Comment

          Working...
          X