Announcement

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

  • matrix conformability error from survey data

    Hello, everyone. I am trying to create survey data statistics using matrix and export the statistics to tex file by frmttable (many variables in one table). Sometimes, it works. But there are some exceptions. For example, the variable "importance1" has value scaled from 1 to 5 (i.e., the survey participant evaluated the survey question from 1 to 5). The variable "importance2" has also value scaled from 1 to 5. However, the value 4 is missing in this variable "importance2", simply because no one indicated 4 in this survey question. When I append the matrix, it shows the conformability error, because the number of columns does not match anymore.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id importance1 importance2)
     1 1 1
     2 1 1
     3 5 1
     4 5 4
     5 4 5
     6 4 3
     7 3 5
     8 5 4
     9 2 4
    10 2 3
    end
    Code:
    tab importance1
    tab importance2
    
    mata: mata clear
    
    local i = 0
    foreach var1 in importance1 importance2 {
        g var1`i' = `var1'
        estpost tabulate var1`i', missing
          mat percent`i' = e(pct)
          mat list percent`i'
        local ++i
    }
    
    mat define percent_all = (percent0 \ percent1)
    
    mat list percent_all

    What I can do is to create another matrix manually. However, as I have many variables for such a case, it would be super helpful if anyone has a better solution to automate this step. Basically, it should automatically create 0 in that column for any variables, which have missing numbers in the scale 1 - 5. Any ideas?

    Thank you very much.

    Code:
    mata: mata clear
    
    local i = 0
    foreach var1 in importance1 {
        g var1`i' = `var1'
        estpost tabulate var1`i', missing
          mat percent`i' = e(pct)
          mat list percent`i'
        local ++i
    }
    
    tab importance2, missing
    mat define percent1 = (30.00,0.00,20.00,30.00,20.00,100.00)
    
    mat define percent_all = (percent0 \ percent1)
    
    mat list percent_all                    
    
    * Generate output table saved as tex file.
    frmttable using "${tables}/texfiles/table_ex.tex", tex statmat(percent_all) sdec(2)  ///
    ctitles("", "1", "2", "3", "4", "5", "Overall")  ///
    rtitles("Importance 1" \ "Importance 2") ///
    replace
    Click image for larger version

Name:	Screen Shot 2022-07-24 at 3.58.10 PM.png
Views:	1
Size:	18.4 KB
ID:	1674862

    Last edited by mws macekk; 24 Jul 2022, 08:07.

  • #2
    There is no way that Stata can guess what categories are missing. You just have to work in long layout and use column percentages from estpost (part of estout, from SSC, as you are asked to explain in FAQ Advice #12).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id importance1 importance2)
     1 1 1
     2 1 1
     3 5 1
     4 5 4
     5 4 5
     6 4 3
     7 3 5
     8 5 4
     9 2 4
    10 2 3
    end
    
    clear matrix
    local stub "importance"
    reshape long `stub' , i(id) j(which)
    qui levelsof which, local(responses)
    estpost tab importance which, nototal
    foreach num of numlist `responses'{
        mat p=  e(colpct)[1,"`num':" ]
        matrix coleq p = :
        mat rowname p= "`stub' `num'"
        mat pct= nullmat(pct)\p
    }
    mat l pct

    Res.:

    Code:
    . mat l pct
    
    pct[2,5]
                   1   2   3   4   5
    importance 1  20  20  10  20  30
    importance 2  30   0  20  30  20

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      There is no way that Stata can guess what categories are missing. You just have to work in long layout and use column percentages from estpost (part of estout, from SSC, as you are asked to explain in FAQ Advice #12).

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(id importance1 importance2)
      1 1 1
      2 1 1
      3 5 1
      4 5 4
      5 4 5
      6 4 3
      7 3 5
      8 5 4
      9 2 4
      10 2 3
      end
      
      clear matrix
      local stub "importance"
      reshape long `stub' , i(id) j(which)
      qui levelsof which, local(responses)
      estpost tab importance which, nototal
      foreach num of numlist `responses'{
       mat p= e(colpct)[1,"`num':" ]
      matrix coleq p = :
      mat rowname p= "`stub' `num'"
      mat pct= nullmat(pct)\p
      }
      mat l pct

      Res.:

      Code:
      . mat l pct
      
      pct[2,5]
      1 2 3 4 5
      importance 1 20 20 10 20 30
      importance 2 30 0 20 30 20
      Thank you very much.

      Comment

      Working...
      X