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]

[no subject]



In that case, the random shuffling of a previous prepared list appears
to be what you want. The code might be like

set obs 25
egen varA = seq(), block(5)
egen varB = seq(), to(5)
set seed 2505
gen rnd = runiform()
sort varA rnd
l

Nick
[email protected]


On 25 May 2013 16:15, Michael McCulloch <[email protected]> wrote:
> Thanks for the suggestions on learning Mata.
>
> On my sampling exercise, I articulated my request for guidance incorrectly. What I seek to do is, for each level of varA (n=5), generate an observation in which varB is a randomly chosen non-repeating value between 1-5.
>
>
> Best wishes,
> Michael McCulloch
>
> --
> Pine Street Foundation, since 1989
> 124 Pine Street | San Anselmo | California | 94960-2674
> P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org
>
>
> On May 25, 2013, at 12:40 AM, Nick Cox <[email protected]> wrote:
>
>> The preceding specifications I take to mean
>>
>> "I wish to generate random numbers between 1 and 5, without replacement."
>>
>> I don't understand what this means as you don't say from what
>> distribution such numbers are drawn (discrete integers 1,2,3,4,5 or
>> continuous on the interval; uniform or stated alternative) or about
>> dependence or independence structure.  I could speculate, but I would
>> rather you gave a precise specification.
>>
>> The only complete explanation of Mata is the manual. It's in flavour
>> an in-house manual for StataCorp's developers that happens to be
>> public to Stata users. (That's a bit tongue-in-cheek.) There's perhaps
>> a presumption that you are at least broadly familiar with programming
>> in a C-like language. Excessive familiarity with Stata is in some ways
>> a disadvantage as Stata is full of Stataish idiosyncrasies, starting
>> with heavy use of local and global macros.
>>
>> But cheer up... Quite a few people have written programs using Mata
>> without ever using more than a small fraction of the language. And
>> there is a very nice introduction in Kit Baum's book
>>
>> http://www.stata.com/bookstore/stata-programming-introduction/
>>
>> Nick
>> [email protected]
>>
>>
>> On 25 May 2013 06:23, Michael McCulloch <[email protected]> wrote:
>>> Thank you kindly Nick for the translation!
>>> How would I rewrite the ultimate line (a, meaning "now show me a), to instead generate the variable a according to the preceding specifications?
>>>
>>> And, can you recommend a book or tutorial that explains Mata as you so clearly do here?
>>>
>>> Best wishes,
>>> Michael McCulloch, LAc MPH PhD
>>>
>>> --
>>> Pine Street Foundation, since 1989
>>> 124 Pine Street | San Anselmo | California | 94960-2674
>>> P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org
>>>
>>> On May 24, 2013, at 10:05 PM, Nick Cox wrote:
>>>
>>>> Note that the Mata code misses a trick
>>>>
>>>> a = J(100,1,.)
>>>> for(i = 1; i<=100; i++) a[i,1] = i
>>>>
>>>> is a long-winded alternative to
>>>>
>>>> a = 1::100
>>>>
>>>> In fact you could shorten the code further, but I will stop there.
>>>>
>>>> One Stata translation is
>>>>
>>>> set obs 100
>>>> gen a = _n
>>>> gen rnd = runiform()
>>>> sort rnd
>>>> l a rnd
>>>>
>>>> I would usually preface that by -set seed 2803- or some such.
>>>>
>>>> Here is a line-by-line translation
>>>>
>>>> mata
>>>> // open Mata
>>>> a=J(100,1,.)
>>>> // a is a vector with 100 rows, 1 column, all missing values
>>>> for(i=1;i<=100;i++) a[i,1]=i
>>>> // loop from i = 1 to i = 100 incrementing i by 1 at each step:
>>>> // set the i th row and 1st column of a to i
>>>> a=a,runiform(100,1)
>>>> // join a row-by-row with a vector with 100 rows, 1 column of uniform
>>>> random numbers
>>>> a=sort(a,2)
>>>> // sort a according to the values in the second column:
>>>> // wanted consequence is a random shuffle of 1::100 in first column
>>>> a
>>>> // now show me a
>>>>
>>>> I wrote -swor- a while back, but I can't remember much about it. More
>>>> important is -sample-.
>>>>
>>>> SJ-5-1  dm86_1  . . . . . . . . . . . . . . . . . . . Software update for swor
>>>>      (help swor if installed)  . . . . . . . . . . . . . . . . .  N. J. Cox
>>>>      Q1/05   SJ 5(1):139
>>>>      swor rewritten to use sort, stable for reproducibility and
>>>>      sortpreserve for compatibility; help file also modernized
>>>>
>>>> STB-59  dm86  Sampling without replacement: abs. sample sizes & keeping all obs
>>>>      (help swor if installed)  . . . . . . . . . . . . . . . . .  N. J. Cox
>>>>      1/01    pp.8--9; STB Reprints Vol 10, pp.38--39
>>>>      samples without replacement a specified sample size;
>>>>      alternative to sample
>>>>
>>>> Nick
>>>> [email protected]
>>>>
>>>> On 24 May 2013 22:30, Michael McCulloch <[email protected]> wrote:
>>>>
>>>>> I wish to generate random numbers between 1 and 5, without replacement. I have located the following Mata code, but have not yet learned Mata. Is it possible to rewrite this to create the variable a?
>>>>>
>>>>> mata
>>>>> a=J(100,1,.)
>>>>> for(i=1;i<=100;i++) a[i,1]=i
>>>>> a=a,runiform(100,1)
>>>>> a=sort(a,2)
>>>>> a
>>>>> end
>>>> *
>>>> *   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