[R] Multinomial sampling WAS: Re: (no subject)

Charles C. Berry cberry at tajo.ucsd.edu
Sat Nov 18 20:09:38 CET 2006


Celine,

The posting guide directs you to "Use an informative subject line (not 
something like `question')". Doing so will increase the probability of 
getting an informative response.

You need to spend a more time with the help pages and with "An 
Introduction to R".

--

Specifically for the help page

 	?Logic

you needed to know that "The longer form evaluates left to right examining 
only the first element of each vector" (referring to the behavior of 
"&&"), which I suspect is much different than you assumed.

As for this help page:

 	?rmultinom

Using rmultinom( 1 , 1, p) %% 2 suggests you didn't understand the 
returned value.

--

For "An Introduction to R" see section 5.1 where it explains what it means 
to order the elements of a matrix in "column major order"

For example, using your construction of the matrix 'p':

> as.vector( p )
[1] 0.07692308 0.23076923 0.07692308 0.15384615 0.15384615 0.30769231

is probably not what you expected.

--

Ordinarily, you might benefit from following the advice of the posting 
guide, which you are asked to consult before posting. However, I see that 
the suggestion there to

     * Do help.search("keyword") "

Fails miserably when I tried

 	help.search("sample")

to direct me to the function named 'sample', which I think solves your 
problem, which I guess is to return the row and column location of the '1' 
in a multinomial sample from a two-way table of probabilities with N = 1.

If my guess was correct, then you want something like this:

> which(array(seq(along=p),dim(p))==sample(length(p),1,prob=p), arr.ind=TRUE)
      row col
[1,]   2   1

see

 	?which
 	?sample
 	?seq

for further details.

HTH,

Chuck


On Sat, 18 Nov 2006, David Barron wrote:

> I'm not sure what the last line is trying to achieve, but this might
> be what you want:
>
> i <- ifelse(m==0, l/ky, l/ky + 1)
> j <- ifelse(m==0,ky, l %% ky)
>
>
> On 18/11/06, Céline Henzelin <celine_appui at hotmail.com> wrote:
>> Hello,
>>
>> I need help to understand my error in this code... I would like to make a
>> direct sampler...
>>
>> thanks
>>
>> Celine
>>
>>
>> p<-matrix(c(1,2,3,2,1,4),3,2,byrow=T)
>> p<-p/sum(p)
>> ky<-ncol(p)
>> kx<-nrow(p)
>> p.vec<-as.vector(t(p))
>> l<-rmultinom(1,1,p)
>> l<-(t(l))
>> m<-(l%%ky)
>> ifelse (m==0, i<-l/ky && (j<-ky), i<-((l/ky)+1) &&  (j<-(l %% ky)
>>
>> _________________________________________________________________
>> Faites de MSN Search votre page d'accueil: Toutes les réponses en un clic!
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
> -- 
> =================================
> David Barron
> Said Business School
> University of Oxford
> Park End Street
> Oxford OX1 1HP
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0717


More information about the R-help mailing list