Announcement

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

  • Options in a programme

    Dear All,

    I am writing a small programme to automatize my analysis. Below I report an example that should highlight my problem (however the tests that I am willing to run are a bit more complex. That is why I need a programme to speed things up). Specifically, I have a situation like this:

    Code:
    Y = a + b1*X1 + b2*X2 + b3*X3 + error
    
    H0 = b1+b2+b3 = 0
    Then I am going to interact those three regressors with other variables, specifically M1, M2 and M1*M2, one by one. Hence the estimated models are:

    Code:
    1)  Y = a + b1*X1 + b2*X2 + b3*X3 + b4*X1*M1 + b5*X2*M1 + b6*X3*M1 + error
    
    H0: b1 + b2 + b3 + b4 + b5 + b6 = 0
    
    or 
    
    2) Y = a + b1*X1 + b2*X2 + b3*X3 + b4*X1*M1 + b5*X2*M1 + b6*X3*M1 + b7*X1*M2 + b8*X2*M2 + b9*X3*M2 + error  
    
    H0: b1 + b2 + b3 + b4 + b5 + b6 +b7 + b8 + b9 = 0
    
    or
    
    3) Y = a + b1*X1 + b2*X2 + b3*X3 + b4*X1*M1 + b5*X2*M1 + b6*X3*M1 + b7*X1*M2 + b8*X2*M2 + b9*X3*M2 + b10*X1*M1*M2 + b11*X2*M1*M2 + b12*X3*M1*M2 + error
    
    H0: b1 + b2 + b3 + b4 + b5 + b6 +b7 + b8 + b9 + b10 + b11 + b12 = 0
    Ideally, I would like to write a command like:

    Code:
    Test on eq. 1
    
    myprogramme X1 X2 X3. interaction(1)
    
    Test on eq. 2
    
    myprogramme X1 X2 X3, interaction (2)
    
    Test on eq. 3
    
    myprogramme X1 X2 X3, interaction (3)
    I am not sure how to specify the three options above. Is it correct to write the first line of my code as follows?

    Code:
    syntax varlist(min=3 max=3 numeric), INTeraction(min=1 max=3 )
    Then in the development of the programme, I can specify a conditional statement to distinguish the three cases. Initially, I thought to write:

    Code:
    syntax varlist(min=3 max=3 numeric), INTeraction(varlist min=1 max=3 numeric)
    However I guess that in this way Stata will look for variables named 1, 2 or 3, without finding them. I hope I was able to express clearly my problem.

    Thanks for your attention.

    Dario


  • #2
    I think you might want to do something like:
    Code:
    syntax varlist(min=3 max=3 numeric), INTeraction(integer) INTVARlist(varlist numeric min=3 max=3)
    and then later in the program, have a check like:
    Code:
    if !inlist(`interaction',1,2,3) {
       dis as error "-interaction- option may only contain 1, 2, or 3."
       exit
    }
    You will be able to access the interaction variables using `intvarlist'
    Last edited by Hemanshu Kumar; 02 Dec 2022, 11:39.

    Comment


    • #3
      Hemanshu Kumar Thanks for your reply. I have two points:

      1) This means that I need to specify under INTVARlist the list of the interactions to be included in the command, right?

      2) In INTVARlist you specify (min=3 max=3). However in models (1) to (3) above I have , 3, 6 and 9 interactions (other than X1, X2 and X3). So if I understood correctly, i should change the min and the max to min=3 max=9). Am I correct?

      Thanks for your patience.

      Dario

      Comment


      • #4
        This is some distance from the kind of programs I ever write, but I am getting the impression that trying to automate this is possibly going to slow you down. It is all too easy to spend hours writing a program when it would be quicker doing things the slow way. The trade-off is delicate, as spending hours to write a program makes sense if that saves you days otherwise.

        In my experience there are two kinds of program. One I write directly, and that's straightforward if and only if I have written loosely similar programs before, so there is a hard chicken and egg question about getting started.

        The other grows out of a do-file, whereby you first start doing things the slow way and then realise (for example) that wiring particular variable names into a do-file can be avoided through greater generality.

        You are taking over from Stata what Stata will do for you, as -- if I understand this correctly -- you need to work out within your program what factor variable notation to use. That could be quite a challenging task in text manipulation, as you need to write code to write code, as it were.
        The same applies if you don't intend to exploit factor variable notation, as you need to write code to write code for variables holding interactions.

        I would tend to exploit the fact that syntax allows you to specify factor variable notation. Your program could then focus on whatever tests you want to follow a model fit.

        Just some thoughts...

        Comment


        • #5
          A slight modification, since you only anticipate having two M variables to interact with:

          Code:
          syntax varlist(min=3 max=3 numeric), INTeraction(integer) INTVARlist(varlist numeric min=2 max=2)
          within your program, you can recover the names of variables using something like
          Code:
          forval i=1/3 {
               local x`i': word `i' of `varlist'
          }
          
          forval i = 1/2 {
             local m`i': word `i' of `intvarlist'
          }
          Your command itself will look something like this:

          Code:
          myprogramme X1 X2 X3, intvar(M1 M2) interaction(#)
          where you would replace # with 1, 2 or 3.

          Comment


          • #6
            Hemanshu Kumar thanks. Muche clearer to me now.

            Nick Cox. You are totally right. My type of programme is the second one you mentioned. Indeed, i need to run those tests for many different models (in total about 40) and i find boring to write again and again the list of variables, the interactions and so on. Therefore, i thought of automating the step. So i started from a do file and then i realized that i could save some time writing a piece of code. It was not really difficult. I was just lost about the point i mentioned in my thread. But i agree that sometimes it is better to evaluate the costs and the benefits of writing something that can be actually done immediately in Stata.

            Comment

            Working...
            X