Announcement

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

  • Editing values in one matrix according to values in another, with missingness

    Hi all,

    I have, on the face of it, a very simple question that has been causing me a great deal of trouble.

    I’m working with two real matrices, A and B, that necessarily have the same dimensions. These matrices will not usually be square, although they will tend to have a similar number of rows and columns (typically between 1000 and 5000).

    I’d like to replace the values in A with the corresponding value in B whenever A equals 0, leaving the other elements of A unchanged. The problem arises when B is missing, as I only want this missingness to propagate through to the resulting matrix if A is initially equal to 0 (or missing).

    For instance, if we define:

    Code:
    A = (., 0 \ 0, 1)
    B = (0, . \ 1, .)
    Then the resulting matrix should be:

    Code:
    WANT = (., . \ 1, 1)
    ... which I am having difficulty arriving at it. My initial attempt had been to run:

    Code:
    A = (A :!= 0) :* A :+ (A :== 0) :* B
    But then elements in A, irrespective of their previous value, are replaced with missing whenever the corresponding element in B is missing.

    Short of running loops across rows and columns, I am not sure how best to address this. Given the size of the matrices, I am keen to avoid any undue computational cost.

    Any suggestions would be greatly appreciated.

    Best,
    Dylan

  • #2
    Originally posted by Dylan James-Taylor View Post
    I’d like to replace the values in A with the corresponding value in B whenever A equals 0, leaving the other elements of A unchanged. . . . when B is missing, . . . I only want this missingness to propagate through to the resulting matrix if A is initially equal to 0 (or missing).

    For instance, if we define:

    Code:
    A = (., 0 \ 0, 1)
    B = (0, . \ 1, .)
    Then the resulting matrix should be:

    Code:
    WANT = (., . \ 1, 1)
    Maybe something like the following.
    Code:
    version 19
    
    clear *
    
    mata:
    
    A = ., 0 \ 0, 1
    B = 0, . \ 1, .
    
    // Step 1
    WANT = A + (A :== 0) :* editmissing(B, 0)
    
    // Step 2
    WANT = WANT + editvalue( ((A :== 0) :+ (A :>= .)) :* (B :>= .), 1, .  )
    WANT
    
    end
    
    exit
    Is there a reason why you want to propagate missing values rather than, say, to set them to zero?
    Last edited by Joseph Coveney; 29 Oct 2025, 02:37.

    Comment


    • #3
      Thank you so much Joseph, that is exactly what I was looking for.

      Originally posted by Joseph Coveney View Post
      Is there a reason why you want to propagate missing values rather than, say, to set them to zero?
      And yes. In this context, the matrices represent the set of pairwise comparisons in a win ratio analysis: A is an updating, overall win-loss matrix, while B contains the results from comparisons on a single outcome. Each element of A and B then represents an outcome status – won (1), lost (-1), tied (0), or terminated (missing) – for a given patient pair.

      If a pair's comparison is terminated, I want their status to remain unchanged in subsequent updates, but tied pairs should be updated whenever new information allows. It was important that terminated values did not contribute to the column or row sums, and ultimately declaring them missing was the simplest solution.

      Again, many thanks for taking the time to answer my question, it is hugely appreciated.

      Comment


      • #4
        Thank you so much Joseph, that is exactly what I was looking for.

        Originally posted by Joseph Coveney View Post
        Is there a reason why you want to propagate missing values rather than, say, to set them to zero?
        And yes. In this context, the matrices represent the set of pairwise comparisons in a win ratio analysis: A is an updating, overall win-loss matrix, while B contains the results from comparisons on a single outcome. Each element of A and B then represents an outcome status – won (1), lost (-1), tied (0), or terminated (missing) – for a given patient pair.

        If a pair's comparison is terminated, I want their status to remain unchanged in subsequent updates, but tied pairs should be updated whenever new information allows. It was important that terminated values did not contribute to the column or row sums, and ultimately declaring them missing was the simplest solution.

        Again, many thanks for taking the time to answer my question, it is hugely appreciated.

        Comment


        • #5
          Originally posted by Dylan James-Taylor View Post
          And yes. . . . It was important that terminated values did not contribute to the column or row sums, and ultimately declaring them missing was the simplest solution.
          OK, thank you for responding to my question.

          As an aside, I wrote the code above so that you could easily follow the logic, but you might be able to save a little time by refactoring to compute the A :== 0 matrix just once each updating. (You can also put the two WANT evaluations on a single continuation line.) Something along these lines.
          Code:
          A0 = A :== 0
          WANT = 
              A + A0 :* editmissing(B, 0) + 
              editvalue( (A0 :+ (A :>= .)) :* (B :>= .), 1, .  )

          Comment

          Working...
          X