Announcement

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

  • Type mismatch when using string rowname to subscript matrix

    Hi there,
    I have written some code to take the matrices saved from tabstat and calculate the standardised difference (calculated as the difference in means divided by pooled standard deviation) between a treatment and control group. I'm getting a type mismatch when I subscript the row containing the pooled standard deviation by name. tabstat outputs three matrices when the by() option is used with a variable with two groups: the matrices for the two groups ('Stat1' and 'Stat2') and the overall matrix ('StatTotal').

    This first example results in a type mismatch on the line within the loop, because of using "sd" in the row subscript of matrix A:

    Code:
    tabstat var1 var2 var3 var4 var5, stat(n mean sd min max q)  by(group) longstub save
    matrix A = r(StatTotal)
    matrix B = r(Stat2)
    matrix C = r(Stat1)
    local c = colsof(A)
    matrix Standardised_Difference = J(1,`c',0)
    forvalues j = 1/`c' {
       matrix Standardised_Difference[1,`j'] = (B["mean",`j'] - C["mean",`j']) / A["sd",`j']
    }
    local rsubs : colnames A
    matrix colnames Standardised_Difference =  `rsubs'
    matrix rownames Standardised_Difference = StDiff
    matrix list Standardised_Difference
    This second example works as intended, and first saves the "sd" row of matrix A in matrix D, which is then used within the loop:

    Code:
    tabstat var1 var2 var3 var4 var5, stat(n mean sd min max q)  by(group) longstub save
    matrix A = r(StatTotal)
    matrix B = r(Stat2)
    matrix C = r(Stat1)
    matrix D = A["sd",1...]
    local c = colsof(A)
    matrix Standardised_Difference = J(1,`c',0)
    forvalues j = 1/`c' {
       matrix Standardised_Difference[1,`j'] = (B["mean",`j'] - C["mean",`j']) / D[1,`j']
    }
    local rsubs : colnames A
    matrix colnames Standardised_Difference =  `rsubs'
    matrix rownames Standardised_Difference = StDiff
    matrix list Standardised_Difference
    I would just like to understand why this is. Any pointers?

    All the best,
    Will
    Last edited by Will Parry; 29 Sep 2016, 09:54.

  • #2
    Here is a guess, and it's quite possibly incorrect.

    From help matrix subscripting

    Subscripting by row/column name may only be used in a matrix context.
    From help matrix operators

    The matrix dyadic operators are
    ...
    B / z division by scalar
    Perhaps because the denominator is expected to be scalar, the expression A["sd",`j'] in
    Code:
    matrix Standardised_Difference[1,`j'] = (B["mean",`j'] - C["mean",`j']) / A["sd",`j']
    is not being evaluated in a matrix context, and thus the string for the first subscript is generating the type mismatch.

    If that is indeed the problem, then the following should work:
    Code:
    matrix Standardised_Difference[1,`j'] = (B["mean",`j'] - C["mean",`j']) / A[rownumb(A,"sd"),`j']

    Comment


    • #3
      I think you must be correct. Certainly, the solution you provide works. I was assuming that subscripting a matrix to provide one value would treat it as scalar, but looks like that is not the case.
      Many thanks!

      Will

      Comment

      Working...
      X