Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Assigning values to a number of rows of a matrix


From   Eilya Torshizian <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: Assigning values to a number of rows of a matrix
Date   Mon, 24 Feb 2014 20:12:45 +0000

Hi Nick,

Thanks for your comments. The problem that I brought up is a simplified version of my main problem. The loop is actually over more than 80 variables. The reason for using a matrix is to add other outcomes derived from -test- and -regress- in the its other columns. The purpose is to derive a desirable descriptive statistics table. I know that 'xi' is outdated, but as I tried -svy: mean- without 'xi' I got the "factor variables and time-series operators not allowed" error.

I need to know the number of unique values of all variables in the varlist (>50 variables) to assign the number of rows to the matrix.

I like to keep the outcomes in a matrix. Based on which, I am going to export my desired descriptive statistics table as follows:

		MEAN P_value ...OTHER COLUMNS
GENDER	0.4	0.1
		0.6	
AGE		0.1	0.05
		0.15
		0.5
		0.15
		0.1
MARITAL	0.3	0.001
		0.7
*Mean is derived from the -mean- command. P_value is derived from the test of the mean difference between years -test- (e.g. test [GENDER]2008=[GENDER]2009=[GENDER]2010)

I would say my code is not a good one, thus if you have a better idea please ignore my code. In the following code, I don't know how to keep the values derived from different commands in a matrix.

Please see my comments in the following code (it is still a simplified version):

local VARS GENDER MARITAL AGE //...(AND SO ON) local C: //THE NUMBER OF UNIQUE VALUES DERIVED FROM THE r(N_unique) OF THE -INSPECT- COMMAND?!
matrix M = J(`C',2,0)
local i = 0
foreach var of varlist $VARS {
svy: mean i.`var' //TO AVOID CONFUSION I DON'T RUN THIS COMMAND OVER(YEAR) local MEAN = e(b) //THE NUMBER OF VALUES IN `MEAN' DEPENDS ON THE NUMBER OF UNIQUES VALUES OF `var'
qui inspect `var'
local k = `i'
local j = `j'+r(N_unique)-1
local i = `i'  + j
matrix M[`k':`j',1]=MEAN //THIS IS MY PROBLEM WITH ASSIGNING A NUMBER OF ROWS TO A MARIX }


Thanks,
Eilya.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Monday, 24 February 2014 10:22 p.m.
To: [email protected]
Subject: Re: st: Assigning values to a number of rows of a matrix

internet exchanges and so is best avoided.

Comments:

1. A loop over just two variables is somewhat over the top.

2. You cannot easily store an entire vector in a local macro. As it happens, putting a vector into a macro only to take it out again is quite unnecessary.

3. -xi:- is outdated unless you are using an old version of Stata (in which case, as advised in the FAQ, you should be telling us that).

4. Unique values are those occurring precisely once. I suspect that you are interested in the number of distinct values. -search distinct- in Stata to get guidance on that. (StataCorp's use of terminology could be improved here, in my view.)

5. But you don't need to know the number of rows or columns you are adding to a matrix, which makes this kind of technique possible

sysuse auto, clear
xi: mean mpg i.rep78
mat b = e(b)
xi: mean mpg i.foreign
mat b = b , e(b)
mat li b

I don't know anything about .rtf.
Nick
[email protected]


On 24 February 2014 05:14, Eilya Torshizian <[email protected]> wrote:
> Dear Statalisters,
>
> In a loop, I would like to add values derived from a command (such as -mean-) to a matrix. Let's assume GENDER and MARITAL are categorical variables with 2 categories, which brings the total categories to 4 (CAT=4). I need to create a matrix with the size of the number of categories. However, I could not derive the total number of unique values by using -inspect-.
>
> *HOW TO DERIVE THE TOTAL UNIQUE VALUES FOR A VARLIST: global VARS GENDER MARITAL?
> matrix M = J(`CAT',2,0)
> local i = 1
> foreach VAR of varlist GENDER MARITAL {
> xi: mean i.`VAR'
> local MEAN = e(b)
> *HERE I DO NOT KNOW HOW TO SAY: MATRIX M[1:2,1] = MEAN[1:2,1]:
> matrix M[`i',1] = MEAN
> local i = `i' + `THE NUMBER OF CATEGORIES OF `VAR'' - 1 }
>
> In the end, I would like to save the M matrix as an 'rtf' document.
>
> Thanks,
> Eilya.
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index