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   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Assigning values to a number of rows of a matrix
Date   Tue, 25 Feb 2014 00:25:48 +0000

See -help matrix operators-.

Nick
[email protected]


On 24 February 2014 21:42, Eilya Torshizian <[email protected]> wrote:
> Hi Nick,
>
> Thanks for this. I am sorry for the inappropriate use of macros (I should use global rather than local and also the foreach loop needs a correction).
>
> I would really appreciate if you let me know about assigning values to specific rows and columns of a matrix. For example, in the code you had written "mat b = b, e(b)", how can I simply add the e(b) values to the row 2 columns 3 to 5 of the 'b' matrix (let's assume e(b) includes 3 values)?
>
> Kind regards,
> Eilya.
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: Tuesday, 25 February 2014 9:37 a.m.
> To: [email protected]
> Subject: Re: st: Assigning values to a number of rows of a matrix
>
> Sorry, but I get lost in this, although some of the details are clearly wrong or ill-advised and I can say something.
>
> Someone else may have a better guess at what you want; if so, fine. In any case, the difference of time zones is against us.
>
> You don't seem to be registering my point about SHOUTING or my points
> #2 and #5.
>
> Your basic structure
>
> local VARS ...
> ...
> foreach var of varlist $VARS {
> ...
> }
>
> confuses locals and globals. You could go
>
> local VARS ...
>
> foreach v in `VARS' {
>
> }
>
> or
>
> local VARS ...
>
> foreach v of local VARS {
>
> }
>
> or
>
> global VARS ....
>
> foreach v in $VARS {
>
> ...
>
> }
>
> (and yet other variants) but locals and globals are quite different.
>
> I'd advise getting this to work for _one_ variable and populating a matrix. Then think about looping over several variables and joining the matrices. As long as they have the same number of columns, you do not need to know how many rows they have.
>
> Nick
> [email protected]
>
>
> On 24 February 2014 20:12, Eilya Torshizian <[email protected]> wrote:
>> 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/
>
> *
> *   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