Stefano Lombardi
Stefano Lombardi

Reputation: 1591

plotting histograms of the columns of a matrix (Stata 12)

Working in Stata12, I need to plot 5 histograms, one for each column of the matrix MAT, where:

mata: MAT = uniform(1000, 5)

I understand that one possibility is to use mm_histogram() for obtaining the center, the width and the density of each interval of the histogram. For the first column we have:

mata: HIST_DAT = mm_histogram(MAT[.,1])

But then I do not know how to proceed for plotting the data (either in Stata or in Mata).

Thank you very much for any suggestion.

EDIT: The question is also present at Statalist archive

Upvotes: 2

Views: 2280

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

There is one simple answer: copy your matrix column to a Stata variable and use histogram. Anything else will just be roundabout or approximate.

It's difficult to see what's central to this question, but if the interest is plotting histograms of random numbers, it is a lot easier to create them as variables in Stata:

 . set obs 500 
 . gen y = runiform()
 . histogram y 

Upvotes: 3

Stefano Lombardi
Stefano Lombardi

Reputation: 1591

For the sake of completeness, the code for plotting the histograms relative to MAT columns is:

clear all
set obs 1000

mata:

  // Mata matrix of results 
  MAT = uniform(500, 5)

  // generates Stata variables from within Mata
  Stata_vars = st_addvar("float", ("V1", "V2", "V3", "V4", "V5"))

  // stores MAT columns in the first 500 obs. of the Stata variables
  st_store((1::rows(MAT)), Stata_vars, MAT)  

end

Then we just need to type:

hist(V1)

for V1 or any other V2-V5 newly-created Stata variable.

Upvotes: 2

Related Questions