0

This should be very easy but I can't find a good answer:

I want to collect a number of scalars, for example the means of several variables (or regression outputs, or test statistics, or p-values), into one object to be used in plotting or as a variable etc.

Consider a dataset like:

clear
input str3 iso3 var1 var2 var3
GBR    10 13 12
USA    9 7 4
FRA    8 8 7
end

Say I get the scalars I want to collect from a loop:

foreach i in var1 var2 var3{
mean `i'
matrix A= r(table)
scalar s_`i'= A[1,1]
}

Now I can display s_var1 for example but how do I get an object which simply gives me [9, 9.3333, 7.6666] to use for plotting or as a variable? Ideally without losing the original dataset? Of course, my actual dataset is not 3x3 but much longer.

Edit: After clarifications in the comments, the most straightforward answer is in Robertos Edit. Ander2ed's answer give intuition towards programming the problem directly.

Jakob
  • 1,325
  • 15
  • 31
  • What exactly do you want to graph? How would that graph look like? What graph commands do you intend to use? – Roberto Ferrer Sep 03 '15 at 20:34
  • How big is your dataset? Does Stata take a "significant" amount of time to load it? – Roberto Ferrer Sep 03 '15 at 20:36
  • Graph commands take variables, so that's probably what you should aim for. How you get there is another matter, but I sense more information is needed for you to receive a specific solution to your problem. – Roberto Ferrer Sep 03 '15 at 20:41
  • I want to do bar diagrams with very few observations (maximum 10) and the dataset has around 200 variables of less than 2000 observations so size should be no problem – Jakob Sep 03 '15 at 23:00

2 Answers2

3

In addition to collapse as suggested by @Roberto's answer, I often use Stata's matrix language, particularly svmat, for similar tasks. I think here it is a matter of preference (although collapse may be more direct) so I thought it worth mentioning in a separate, nonsensical example:

clear *
input str3 iso3 var1 var2 var3
GBR    10 13 12
USA    9 7 4
FRA    8 8 7
end

mean var1 - var3
mat A = (r(table))'

preserve
clear

svmat A, names(matcol)
rename A* *

// Build Plots Here
twoway scatter t pvalue, name(plot1)

restore

graph display plot1
ander2ed
  • 1,318
  • 1
  • 11
  • 19
2

Setting up the data to create a graph ultimately depends on the structure of your dataset, required computations, (maybe) the size of your dataset, and the type of graph.

A nonsensical example:

clear
input str3 iso3 var1 var2 var3
GBR    10 13 12
USA    9 7 4
FRA    8 8 7
end

preserve

collapse var*

gen i = _n
reshape long var, i(i)

graph twoway line var _j

restore

collapse here is an example of some computation, but it can be anything. I use preserve and restore to easily go back to the original data. This may or may not be the best approach, but like I mentioned before, it really depends on the problem at hand.

Edit

In response to your comment, you probably want postfile. An example (pretty much from the manual):

clear
set more off

*----- example data -----

input str3 iso3 var1 var2 var3
GBR    10 13 12
USA    9 7 4
FRA    8 8 7
end

*----- what you want -----

tempfile results

tempname sim
postfile `sim' mean var using `results', replace
quietly {
    foreach v of varlist var? {
        summarize `v'
        post `sim' (r(mean)) (r(Var))
    }
}
postclose `sim'
list // original ok

use `results', clear
list // results to graph

postfile is very flexible. Just read the manual entry and experiment.

Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
  • ok, I think I have to re-think a little from R... Collapse can, of course, not be replaced by other operators very easily in your example. Say I want the correlations instead of the mean, it would not work. My idea is to build a code which does some test/regression/statistic, collects one element of the resulting r() matrix and adds it as an element to a vector before doing the same thing for the next variable. That way, I'd build a vector of these results and I can easily adapt the code to whatever statistic I want. – Jakob Sep 03 '15 at 23:33
  • Try `help statsby` and `help postfile`. See also their respective manual entries (the `help` files have links to those). – Roberto Ferrer Sep 04 '15 at 01:39
  • I think the comment "I have to re-think a little from R" is revealing here. Stata certainly has classes (`help class`) but to do what you want you would need to do all the programming yourself. Saved results are typically saved one by one, not in a bag of any kind. – Nick Cox Sep 04 '15 at 06:07