Announcement

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

  • Creating variables from loops

    Hi, I'm doing a monte carlo experiment to create some panel data.
    To do this i need a colum vector for the ID and time_ID of the observations (together with the y and x values which should have been correctly calculated - not included)

    I made the following loop:
    Code:
    // Loop for the time period
        for(t=1; t<=T;t++) {
                
            // Loop for sample size 
            for(i=1; i<=N;i++) {
    
                id[(t-1)*N+i,1] = i
                time_id[(t-1)*N+i,1] = t
    
      }
    }
    id and time_id have both been created before the loop begins with the dimensions [N*T, 1]
    T is 50 and sample size N differs, but let's say it's 100 this time.

    The time_id column vector looks exactly like I want, with the first 100 observations = 1, the next 100 = 2 and etc.

    The column vector id doesn't, however, and I can't seem to grasp why.
    In this, the first 50 observations are listed as id = 1, the next 50 id = 2 and etc until obs. 4951-5000 where id = 100.

    I would like the first 100 observations, corresponding to the 100 people in the sample at T = 1, to have the id from 1-100 and then repeat until end.

    Any help is appreciated

  • #2
    Edit: The solve, stable seemed to do the trick - I think I worked it out. You can delete the post if you want


    I couldn't seem to find an edit button, but I found that the problem isn't with the loop itself, but rather when I converted the data into Stata and xtset it, so I don't know whether this question is still fitting for this subforum.

    I changed the code a bit since I wrote, because I found out you could extract information from a matrix more easily, but it's the same principle - id and time_id are now incorporated in the pan_data matrix:

    Code:
        stata("getmata (id time_id y x)=pan_data, replace")
        stata("xtset id time_id")
    So the problem is really that when i xtset the id and time_id, stata rearranged the id, without the corresponding time_id (or y / x) following along.

    Would it help if I sorted the columns like xtset, with the 50 identical ID's following eachother and their respective time_id, x and y along with it?

    Or can I somehow use my existing generated data and stop the xtset function from fucking it up?
    Last edited by Tor Madsen; 11 Dec 2016, 08:03.

    Comment

    Working...
    X