Announcement

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

  • For Each loops - invalid name

    Hello,

    I'm not sure if I am doing this correctly but I am basically trying to regress different age groups, as defined by "dumm_`i' to`j' ", with different levels of education, represented in the dummy variables for education. The reason I am having issues is because the regressions do not appear! Stata just states "end of do file"


    local education dumm_primary dumm_secondary dumm_bachelors dumm_postbachelors
    foreach x of local education {
    forvalues i = 15(5)49 {
    local j = `i'+ 4
    regress FP2A alone1 dumm_`i' to`j' "`x'", robust
    }
    }
    Last edited by Kavita Singh; 20 May 2018, 21:13.

  • #2
    Code:
    dumm_`i' to`j' "`x'"
    looks quite wrong. My guess is

    Code:
    dumm_`i'-dumm_`j'     `x'
    If that is not an answer, then you need to tell us more about your variables.

    Please note FAQ Advice #12. Using CODE delimiters makes your code easier to read.

    It seems that you are guessing that there is syntax to to indicate groups of variables. Not so.

    Comment


    • #3
      Thanks for the follow up.

      I don't necessarily think dumm_`i' to`j' "`x'" is incorrect because I ran the following code on it and I got the regressions to work:

      forvalues i = 15(5)49 {
      local j = `i'+ 4
      regress FP2A alone1 dumm_`i'to`j' , robust
      }

      Perhaps I can describe my data.

      the local education variables represent dummy variables as to whether or not an individual attended primary/secondary/bachelors/post-bachelors respectively. I want to regress these variables on the dummy variable of different age ranges, the dummy of whether of not the individual is using contraceptives, and a PCA established "alone1" index. Does this help?

      Also something I realized is when I run the code all together "dumm_primary" is an invalid name; but when I run the code separately, "invalid name" does not appear. The only issue with the latter is that the regressions do not run!
      Attached Files
      Last edited by Kavita Singh; 21 May 2018, 08:09.

      Comment


      • #4
        From post #3
        Code:
        dumm_`i'to`j'
        is not incorrect - as you show, you generate a series of variables named
        Code:
        dumm_15to19
        dumm_20to24
        ...
        But that is not the code in post #1, as
        Code:
        dumm_`i' to`j'
        incorrectly includes a space in the middle of the variable name.

        As to your question about the error message regarding dumm_primary, consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD30770.000000"
        
        . local message Hello, world.
        
        . display "`message'"
        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//SD30770.000000"
        
        . local message Hello, world.
        
        .
        end of do-file
        
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD30770.000000"
        
        . display "`message'"
        
        
        .
        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.

        In your case, since when you run the commands separately, the local macro education is undefined, the loop has no values to loop across, so the commands within it are never run.

        Comment


        • #5
          William is right on all points so far as I can see.

          In addition, indicators that are 1 or missing are useless. Observations with missing values drop out of the regression and the remaining values are constant in the model and can explain nothing.

          We ask (FAQ Advice #12) that you don't use images that can't be copied and pasted. Your generic line for creating *to* indicators would be better as

          Code:
          generate dumm_`i'to`j' = inrange(EW6, `i', `j')

          Comment


          • #6
            As to your first point, I respectfully think that I did not include a space in generating dumm_`i'to`j' and regressing it!

            Thank you for teaching me about the local macros, I did not realize I was making this error

            I found that the correct code is:

            local education dumm_primary dumm_secondary dumm_bachelors dumm_postbachelors
            foreach x of local education {
            forvalues i = 15(5)49 {
            local j = `i'+ 4
            regress FP2A alone1 dumm_`i'to`j' `x', robust
            }
            }

            Nick correctly pointed that the code should be `x'. Thanks!

            Comment


            • #7
              There is a space in #1. What you did on your machine and didn't show us is beyond our ken.

              Also, as you will appreciate, this all would have been resolved more quickly and more smoothly if you had given a data example.

              As for whether #6 lays out the correct code, that is in doubt if you use the dummy definitions in #3.
              Last edited by Nick Cox; 21 May 2018, 10:24.

              Comment


              • #8
                As to your first point, I respectfully think that I did not include a space in generating dumm_`i'to`j' and regressing it!
                It appears that the code you presented in post #1 is not the code that worked when you ran it. In post #3 you did not acknowledge the difference, which led me to conclude in post #4 that you remained unaware of your mistake in post #1.

                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.

                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!
                as is Section 12.3 on the use of code delimiters [CODE] and [/CODE] to present code and output copied and pasted from the Do-file Editor or the Results window.

                The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

                I agree with Nick's concerns about the dummies you have defined, as shown in post #3: since you have not addressed Nick's suggestion we again are left to conclude that you have not understood the problem.
                Last edited by William Lisowski; 21 May 2018, 11:03.

                Comment


                • #9
                  My apologies for putting excess strain on the situation; it is hard for me to see what error I am making as I am just becoming familiar with this system! Thank you for your patience.

                  I will try my best to address all points within my knowledge.

                  1. I do see that there is a difference between spacing. Perhaps when I was typing this I did not realize I had made that error.

                  Code:
                   dumm_`i' to`j'
                  and

                  Code:
                   dumm_`i'  to`j'
                  I will change the convention per Nick's suggestion to the following below since "to" is not an appropriate to indicate a group of variables.

                  Code:
                   dumm_`i'-dumm_`j' `x'
                  2. I see that I have unwittingly made a second error in that for the following code I am forgetting to replace variables that aren't 1 with 0. Can I get a bit of guidance on how to do this with observations that are not within the range of i and j?

                  Code:
                   forvalues i = 15(5)49 {
                      local j=`i'+4
                      generate dumm_`i'to`j'=1 if inrange(EW6, `i', `j')
                  }

                  Here is an example of the data where I now see my error. EW6 is a variable that represents age.

                  input int EW6 float (dumm_15to19 dumm_20to24 dumm_40to44 dumm_45to49)
                  49 . . . 1
                  26 . . . .
                  33 . . . .
                  43 . . 1 .
                  47 . . . 1
                  38 . . . .
                  25 . . . .
                  29 . . . .
                  42 . . 1 .

                  3. If I resolve the issue with these district dummy variables involving the naming covention, and replacing nonrange values with 0 while omitting missing values, will the following code then work?:

                  Code:
                   local education dumm_primary dumm_secondary dumm_bachelors dumm_postbachelors
                  foreach x of local education {
                  forvalues i = 15(5)49 {
                  local j = `i'+ 4
                  regress FP2A alone1 dumm_`i'_`j'  `x', robust
                  }
                  }

                  Comment


                  • #10
                    You didn't get an answer to your last question. It is very hard to know if something "will work" without running it. Run it and see what you get.

                    Comment


                    • #11
                      Code:
                       forvalues i = 15(5)49 {
                        2.         local j=`i'+4
                        3.         generate dumm_`i'-dumm_`j'=1 if inrange(EW6, `i', `j')
                        4. }
                      the response is "too many variables specified"; as previously shown, EW6 represents age.

                      Comment


                      • #12
                        1. It is easy to overlook a typing error made when creating a post, which is why we ask for actual code to be copied from Stata and pasted into the post within CODE blocks.

                        2. Nick showed how to do this in post #5. I have included his recommendation in the code below, and have changed the name of the dummy variable to match the name used in the regression model. The suggestion works because help inrange() tells us that it will take the value 1 (if `i'<=EW6<=`j') or 0 (otherwise).

                        ADDED IN EDIT after reading post #11: This is not a variable name
                        Code:
                        dumm_`i'-dumm_`j'
                        It is a variable list of all variables between dumm_`i' and dumm_`j'.

                        Nick suggested it in post #2 because your presentation in post #1 did not describe your data and Nick thought the "to" was meant to indicate a list of variables, not be part of the name of a single variable.

                        3. I think this code will work, no guarantees since I cannot test it. t
                        Code:
                        forvalues i = 15(5)49 {
                            local j=`i'+4
                            generate dumm_`i'_`j' = inrange(EW6, `i', `j')
                        }
                        
                        local education dumm_primary dumm_secondary dumm_bachelors dumm_postbachelors
                        foreach x of local education {
                            forvalues i = 15(5)49 {
                                local j = `i'+ 4
                                regress FP2A alone1 dumm_`i'_`j'  `x', robust
                            }
                        }
                        Last edited by William Lisowski; 21 May 2018, 12:05.

                        Comment


                        • #13
                          This code works, thank you SO MUCH and I will make improvements to my posts in the future to be more clear and aligned with the reqs!

                          Comment


                          • #14
                            William's answers cover all the essential points. Some decoration can be provided in the form of documentation of true and false in Stata. https://www.stata.com/support/faqs/d...rue-and-false/

                            Comment

                            Working...
                            X