Announcement

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

  • String refering to other variables in logistic regression

    Hello,
    Let's say I have a .dta file with 4 variables. Three of them are dichotomic and the last one contains a string that refers to the other variables' names; something like:
    C1 C2 C3 C4
    1 0 0 C1 C2
    1 1 0 C3 C1
    0 1 1 C3 C2
    0 1 1 C1 C2 C3
    I'm trying to run a set of multiple logistic regressions. Is it possible to use C4 in some way to run the regressions instead of typing each variable separately? I tried as follows:
    (Note that I need C1 to always be the indep. var. and put C4 always afterwards so it "inputs" the other dep. vars.).
    Code:
    forval i=1/4{
    local S C4[`i']
    logistic C1 `"S"'
    Stata doesn't seem to recognize it though. I tried changing C4 [i] and S' syntax and the following errors appear in some cases: "S invalid name or 1 unknown weight type. The rest of the cases, Stata seems to run the logistic regressions, but only "_cons" is shown so I believe it's not doing it properly.

    Thanks in advance!










  • #2
    Here is your code with two changes needed to get you closer to what you want. However, one problem is that you have C1 included in models 1, 2 and 4 twice, since you have it both in C4 and explicitly included as the first argument to the logistic command. I could program my way around this problem, but depending on the size of your real problem, it might be easier to just clean up C4.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(C1 C2 C3) str16 C4
    1 0 0 "C1 C2"   
    1 1 0 "C3 C1"   
    0 1 1 "C3 C2"   
    0 1 1 "C1 C2 C3"
    end
    forval i=1/4{
    local S = C4[`i']
    display "command #`i': logistic C1 `S'"
    }
    
    command #1: logistic C1 C1 C2
    command #2: logistic C1 C3 C1
    command #3: logistic C1 C3 C2
    command #4: logistic C1 C1 C2 C3
    Note the use of the dataex command to present data in a way that is easy for the reader to copy into a sample program. To install dataex, type ssc install dataex and when that is complete type help dataex to read the simple instructions for using it. Using dataex will enable those who want to help you to quickly and easily create a 100% faithful replica of your situation to test their ideas and code on.

    With all this said, though, I'm not convinced that including your various models as a "variable" in your dataset is the best way to approach your objective. But I can only guess at your eventual objective.

    Comment


    • #3
      Dear William,
      Thank you so much for your answer! I corrected it in my code and worked just fine. The reason I was trying with C4 is that in my data there are more than 100 rows, so I believed it would not be practical to just eliminate C4 and run the models one by one.
      On reflection, it kinda was a syntax issue; ergo, sorry for that "silly" question, I'm still getting used to Stata.

      Cheers!
      Ana P. Vargas.

      Comment

      Working...
      X