Announcement

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

  • Using putdocx to report matrix cells in putdocx text ("")

    I want to create a report in Stata that includes both the tables and the text descriptions of that table. I know how to use putdocx / esttab to create the summary tables. But I am struggling to understand if it is for example directly possible to "reference" correlations between two variables in putdocx text without having to save them as locals first. I am asking because saving each statistic I would like to report in a local seems inefficient and tedious. I know I could possibly use dyndoc, but I want to know if there is also an option for putdocx.

    The standard approach from Chuck Hubers' presentation seems to be:
    Code:
    webuse auto.dta
    pwcorr *
    matrix correlations = r(C)
    local cor1 = string(correlations["price", "mpg"])
    putdocx begin
    putdocx paragraph
    putdocx text ("The correlation between price and mileage is `cor1'")
    putdocx save Corr1, replace
    Based on the Putdocx manual (p. 88), I can see however that we directly plug in stored results from a command into putdocx text. For example:
    Code:
    summarize price
    putdocx begin
    putdocx paragraph
    putdocx text (" We analyze a dataset with `r(N)' cars,")
    putdocx save Corr2, replace
    So, in an ideal world, I would like to directly plug in values from my pwcorr command without having to save them in a local, but using my approach does not work.
    webuse auto.dta
    Code:
    pwcorr *
    matrix correlations = r(C)
    putdocx begin
    putdocx paragraph
    putdocx text (`correlations["price", "mpg"]'), nformat(%5.2f)
    putdocx save Corr3
    putdocx append Corr1 Corr2 Corr3
    Last edited by Felix Kaysers; 12 Dec 2023, 04:39. Reason: fixed some bugs
    __________________________________________________ __________

    Cheers, Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    You want to force Stata to evaluate the expression:

    Code:
    putdocx text ("`:di %5.2f `=correlations["price", "mpg"]''")
    Last edited by Andrew Musau; 12 Dec 2023, 07:03.

    Comment


    • #3
      Thank you Andrew! Since I am forcing Stata to evaluate the expression, what are the potential disadvantages of this approach?
      __________________________________________________ __________

      Cheers, Felix
      Stata Version: MP 18.0
      OS: Windows 11

      Comment


      • #4
        I don't understand the question. In the first approach, you are getting the result from the matrix and holding it in a local macro. Thereafter, you reference the local in the putdocx command. In #2, you are getting the result directly from the matrix. So in the former, the local must be defined (nonempty) and in latter, the matrix must be accessible. The latter can be seen to be more efficient as it is a single step compared to two steps for the former, if you want to compare the approaches.

        Comment


        • #5
          If you are planning to write a lengthy word document referencing multiple correlations one after the other, consider wrapping the pwcorr and resulting local into a callable format (including the -di- portion) as an -include- file or a program. That way, you will have less clutter between each putdocx statement and can get your referenced macros to look neater in each putdocx chunk. Can share a sample if you want.

          Comment


          • #6
            Andrew Musau: The connotation of force is negative for me and something I thought should be avoided if possible. Thus, I was wondering if there is anything wrong.

            Girish Venkataraman: Sure, I would love to hear more about that! I am indeed struggling with a lot of cluttering. Plus, if there is any way to extract the p-values for each correlation and be able to use it in my report, I would be super grateful.
            Last edited by Felix Kaysers; 13 Dec 2023, 02:58. Reason: How to report p-values.
            __________________________________________________ __________

            Cheers, Felix
            Stata Version: MP 18.0
            OS: Windows 11

            Comment


            • #7
              Felix Kaysers: Nothing wrong with `force-ing' the expression. By the way, Andrew Musau is a legend. You can pretty much copy/paste his code and they will work right off the bat. Have learnt one too many things from him.

              While I don't know your specific use case, see this link below for something I posted on this forum that extracts medians and Mann-Whitney P-values in the format I want for a putdocx statement. The use case is for medical manuscripts results sections' descriptive statistics for retrospective cohorts where we like a clutter of numbers, fractions and percentages one after the other in the word document (don't ask me why , unless you are in the medical field too).

              If your use case is analogous, perhaps you can adapt the code from there and tack in that forced expression into the program. I am going to presume you are conversant with programs and -include- files. It took me a while to get 15 odd programs together for medians/Mann-Whitney, tabulations/Chi-squares, death rates, median survival, overall survival (using parametric and non-parametric when PH violation occurs) and KM plot in panels with panel labels. These programs go through iterative improvements nearly on a daily basis at this time since I learnt about programs only in the past 3 months or so. But I love the time I put in, as bulky and amateurish as the programs might look for advanced programmers. Feel free to send a private message if its a specific question that might not be of broader interest on this forum.

              https://www.statalist.org/forums/for...ed-on-criteria

              PS: The p-value portion of the code in the program is an adaptation of Tim Plante's post on his UVermont blog page. Really useful set of tutorials, many of which I have adapted. https://blog.uvm.edu/tbplante/
              Last edited by Girish Venkataraman; 13 Dec 2023, 07:13. Reason: Had to credit Tim Plante.

              Comment

              Working...
              X