Announcement

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

  • How to generate and fill an empty matrix within 2 loops?

    Hi, I'm very new to Stata and very stuck - would be really grateful for some help!

    Here's my code:




    matrix varsub_trans_matrix = J(5, 18, .)
    matrix var_trans_matrix = J(45, 18, .)

    foreach var of varlist vara varb varc va vb vc varx vary varz {
    preserve
    drop if visit != 1
    keep `var' subject subgroup
    gen subgroup_ = subinstr(subgroup, " ", "_", .)
    reshape wide `var' , i(subject) j(subgroup_) string
    ds sub* , not
    local var_subs `r(varlist)'
    foreach var_sub in `var_subs' {
    ladder `var_sub'
    matrix `var_sub'_matrix = (r(ident), r(P_ident), r(square), r(P_square), r(cube), r(P_cube), r(sqrt), r(P_sqrt), r(inv), r(P_inv), r(invsq), r(P_invsq), r(invcube), r(P_invcube), r(invsqrt), r(P_invsqrt), r(log), r(P_log))
    matrix list `var_sub'_matrix
    matrix `var'_matrix = varsub_trans_matrix \ `var_sub'_matrix
    }
    restore
    matrix combo_matrix = var_trans_matrix \ `var'_matrix
    matrix list combo_matrix
    }
    matrix list combo_matrix




    Basically, I am trying to calculate ladder scalars for 5 different subgroups of 9 different variables and automatically fill a matrix with the R and p values calculated. I thought if I created some empty matrices and then vertically merged them, it might fill the empty ones, but it doesn't seem to have worked.

    I'd be very grateful for your help!

    Many thanks,
    Liz
    Last edited by Liz Broom; 25 May 2023, 05:47.

  • #2
    I don't likely have the knowledge to answer your question as posed, but I can offer some advice on how to pose your question to increase your chances that someone might help you:

    1) You've accidentally posted in the forum designated for questions involving Stata's Mata programming language. However, the code you show only involves Stata's matrix language, which despite how it sounds <grin> is a different thing than Mata. While it is possible that using the Mata language might provide a way to do what you want, you're more likely to get an answer about an "ordinary" Stata matrix question if you posted it in the Stata forum, since the only people who look at the Mata forum are in that small-ish subset of Stata users who use the Mata language.

    2) "It doesn't work" isn't precise enough to help people help you. In what way didn't "it" work? How do you know that? What aren't you getting that you want to get?

    3) It looks to me like the code example you provide is much more complicated than is necessary to show the problem that you're experiencing. Can you provide a different and less complicated example? Also, as described in the FAQ for new StataList users, providing an actual data example (using the -dataex-) command is very helpful, if that is possible in your situation.

    4) I'm not familiar with the term "ladder scalar," and searching on the net using that term didn't reveal much. I suspect there are many people who could help you with your problem in using Stata's matrix language, but don't know about this kind of mathematical object. If you can describe your goal without reference to "ladder scalar," or if you can simply describe what one is, that would increase the number of people who could help, particularly because I'm guessing your problem involves some relatively simple feature of the matrix language in Stata. Related: Your mention of "R and p values" is perhaps something denotative within the context in which the term "ladder scalars" is used, but to those of us outside that area, "R and p" could mean many different things. In short, "Create some empty matrices and then vertically merge them" is something a lot of people here know how to do outside the context of "ladder scalars."

    5) I'd emphasize the desirability of describing your problem in terms of "I have this data or this kind of a matrix, and I want this result." There's enough that's unusual about your code -- such as the repeated use of preserve/drop/restore-- that I would guess that there's a different and easier way to get to your goal.

    Comment


    • #3
      Thanks so much for your help Mike! I have simplified, reworded and moved over to the Stata forum!

      Comment

      Working...
      X