Announcement

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

  • Creating and filling Matrix, graphing results in STATA

    Hello all,
    I'd like to fill a blank matrix in STATA with 2 columns and 5 rows I found this post from 2011 that suggested (for an 8 by 8 matrix).
    Code:
     matrix mat1 = J(8,8,.)
    so I tried
    Code:
     matrix mat1 = J(5,2)
    , which yielded an "invalid syntax error." Perhaps the command has changed and one should use MATA instead?

    EDIT: I noticed I was missing the ,. so now I've managed to create the empty matrix and just have to do the rest.

    After I have the matrix, I'd like the first column to be a value that I define. For example, in R the code would be:
    Code:
    mat[1, 1] <- log(10)
    mat[2, 1] <- log(20)
    mat[3, 1] <- log(30)
    mat[4, 1] <- log(40)
    mat[5, 1] <- log(50)
    followed by filling the other columns for different observations (in this case, country years) and then plotting the results of the matrix. Again, the code in R is (where 1:50 refers to the number of observations and 1:5 is the matrix defined above).

    Code:
    for (j in 1:50) {
    for (i in 1:5) {
      mat[i, 2] <- log(df[j, 1 + i])
    }
    mat <- as.data.frame(mat)
    plot(mat$V2, mat$V1)
    }
    What is the best way to do this in STATA? Or do I necessarily have to use MATA instead? I'm not familiar with the latter. Basically, the end result should be 50 graphs, where the y-coordinate corresponds to column 1 in the matrix (and stays the same for each graph) and the x-coordinate corresponds to column 2. I just included the code in R in case it's helpful for explaining what I'm trying to do.

    Let me know if this makes sense. Thank you in advance!
    Last edited by John Singer; 02 Nov 2021, 08:31.

  • #2
    Assignment is straightforward in Stata

    mat[1, 1] <- log(10)
    Code:
    mat mat1[1,1] = log(10)
    See

    Code:
    help matrix

    then plotting the results of the matrix.
    In Stata, you normally don't create graphs using matrices. If you give a data example and explain what you want, suggestions may be forthcoming. Otherwise

    Code:
    help svmat
    to convert the matrix to a dataset.
    Last edited by Andrew Musau; 02 Nov 2021, 09:03.

    Comment


    • #3
      I agree with Andrew Musau

      Code:
       
       matrix mat1 = J(8,8,.)
      in Stata creates a matrix with 8 rows and 8 columns and missing as every entry. In contrast
      Code:
      matrix mat1 = J(5,2)
      offers two arguments to the function, but not three as are required. Hence it is illegal. It's certainly not a special case of the first command yielding a matrix with that size but no content, as you must have something in each matrix entry. . On the big picture I note that I wrote the post you cite from 2011 but I would have to do a lot of reading to re-discover the context. But it was true in 2011 and remains true that (with minor exceptions that don't undermine the major idea) that you need variables, not matrices, to draw a graph/

      Comment


      • #4



        hello nick
        congrats on your posts!
        By the way, I would like to ask if you are aware of how to "plot" in stata a graphic matrix like the one presented with the histograms on the diagonal and the correlation coefficients in the upper half and the scatter plots in the lower half?

        Comment


        • #5
          There is no quick way to do that in Stata that I know of.

          Comment


          • #6
            Do you think the support people could help or be interested in developing this type of output?
            Any idea of someone who could 'develop' this command?

            Comment

            Working...
            X