Announcement

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

  • How do I save scalars in a loop to a variable?

    Hello everyone!

    I'd like to save two scalars generated in a loop to a variable. My current code reads as follows:
    -------------------------
    forvalues i = 1(1)158 {
    gen z = _b[_cons] + _b[republican]*republican + _b[ideology]*ideology + _b[vulnerability]*vulnerability + _b[seniority]*seniority if vote_id == 'i'
    gen Phi_z = normal(z)
    gen newvote = 1 if vote_id == 'i'
    replace newvote = 0 if Phi_z < .5 if vote_id == 'i'
    sum vote_behavior if vote_id == 'i'
    scalar vote_old = r(mean)
    sum newvote
    scalar vote_new = r(mean)
    }
    -------------------------

    I'd like to save the scalars vote_old and vote_new to new variables. I'd appreciate any suggestions on how to do that.

    G.

  • #2
    Hi Gautam, I cannot observe clear purpose from your code.
    If you want to save the scalars to new variables, you should use -gen- to create a new variable (all observations are the same constant).
    Otherwise, you may also use local to store it in a macro.
    You can post your data using -dataex- and tell the statalisters more about your intention so that others know how to help you.
    2B or not 2B, that's a question!

    Comment


    • #3
      I don't think the loop makes sense unless there is a model fitting command at the start, possibly probit? And, given that, you seem to be reinventing what is possible with predict after a model fit.

      The commands

      Code:
      sum vote_behavior if vote_id == 'i'
      scalar vote_old = r(mean)
      can be taken out of the loop and replaced with

      Code:
      egen vote_old = mean(vote_behavior), by(vote_id)
      so that is one of the variables you want.

      Comment


      • #4
        Thank you both!

        Nick, you're right. I'm running the probit command outside the loop that predicts the probability of a legislator voting "No" on up/down votes, and two of the regressors are campaign contributions the legislator received. In the loop, which loops over all the issues voted on, I'm trying to predict how the vote would have come out without the campaign contributions - hence predict does not generate the predicted probabilities I need.

        Thanks for your suggestion about egen. I'm a Matlab programmer, and some of the Stata syntax works differently from what I'm used to. This suggestion of yours worked for vote_old.

        Code:
        egen vote_old = mean(vote_behavior), by(vote_id) Any suggestions on how I can save each of the scalar values of newvote in the loop to create a vector called vote_new that captures the mean of new vote for each vote_id?

        Comment


        • #5
          On second thoughts, I think I can do this without a loop by simply creating a vector of zeros. Let me try that.

          Comment


          • #6
            Thank you both for your help. I was able to accomplish everything I needed with no loop. Your suggestions nudged my thinking in the right direction.

            Comment

            Working...
            X