Announcement

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

  • Trailing zeros in matrix

    I want to create a 3x3 crosstab and output it to latex. I've tried this with a combination of esttab, estout and it is not possible, as I need counts, statistics, test statistics, asterix and various other calculations involving rowsums and colsums in the table.

    On advice from the forum here, I'm iterating through a number of row and column categories, calling the `table' command and populating a matrix (and an annotation matrix). I'm ending up with something like this:

    matrix res = J(2,2,.)
    matrix res[1,1] = round(0.29 * 100,.01)
    matrix res[2,1] = 10514
    matrix res[1,2] = round(0.5566 * 100,.01)
    matrix res[2,2] = 18895
    matrix rownames res = Percentage Observations
    frmttable, tex plain fragment statmat(res) replace ///
    sdec(2)

    I need (i) the first row to have two trailing zeros and (ii) the second row to be round to the nearest integer. I've refactored the underlying code in frmttable so that if I omit sdec(2), no formatting with be applied to the matrix when it's output to latex. However, the issue with trailing zeros is not resolved.

    I've tried to change the cell value to a string, format it and return it with the `real' command, but it still strips off the trailing zeros. The sdec command can accept an array of decimal places for each column, but not an array of decimal places for each row.

    Any idea how I can achieve this?

  • #2
    I don't understand much of this, but round(, .01) won't round to the nearest integer. You need round(, 1) or indeed just round(). (I omit the first argument.)

    If you have to write customised Stata code for each table, you might as well as write the LaTeX directly. I am probably misunderstanding, however.

    Comment


    • #3
      The round(, .01) command above is meant to round to two decimal places. There's code in the question to produce latex output from a 2x2 matrix. I just want to apply a different format to each row of the matrix when it is output, but I don't see that this is possible with Stata. As always, writing the LaTeX directly is not an option!

      Comment


      • #4
        Sorry; I misread the text about the second row being rounded to the nearest integer. Your code does that any way; I was wrongly inferring that there was a problem there.

        If you want to ensure two decimal places always, you must export numbers as strings. round(, .01) is not guaranteed to do what you want as most multiples of 0.01 cannot be held exactly in binary. That is a constant refrain of discussions here and elsewhere about precision in Stata. For example, see sources listed by help precision or search this forum for related threads.

        Thus Stata matrices need to be replaced by Mata matrices, or something else. Alternatively this is what frmttable does for you, but I have never used it, so cannot advise. This will be programmable directly, but whether any canned command will do it for you again I don't know.

        I don't understand the "As always" unless it means just that you prefer to write Stata code regardless.
        Last edited by Nick Cox; 23 Nov 2015, 10:55.

        Comment


        • #5
          I was under the impression that the option sdec(format) in frmttable accepts a maximum number of decimal place specifications corresponding to the number of columns.

          In fact, that's not the case. In frmttable, the sdec() option is passed to a call to strofreal() in mata, which can include a number of decimal place specifications up to a maximum of rows x columns. Anything beyond that has no effect. It can be specified like this to solve the problem above:

          Code:
          frmttable, tex plain fragment statmat(res) replace ///
          sdec(2,2\0,0)
          It's not very elegant in a large matrix, but it works.
          Last edited by tobriain; 03 Dec 2015, 10:49.

          Comment

          Working...
          X