Re: [R] sapply function and poisson distribution

2015-01-05 Thread peter dalgaard

On 05 Jan 2015, at 00:21 , Pete Brecknock  wrote:

> n <- c(1,2,3,4,5)
> lambda <- c(0.1,0.8,1.2,2.2,4.2)
> 
> mapply(function(x,y) rpois(x,y), n, lambda)  


Yes. I'd throw in a SIMPLIFY=FALSE to avoid getting results in a different 
format if n is constant (then again, sapply() in the original question is sort 
of asking for that kind of trouble...).

An alternative is to use the fact that rpois() vectorizes on the lambda 
argument:

ll <- rep(lambda, n)
g <- rep(seq_along(lambda), n)
N <- sum(n)
split(rpois(N, ll), g)

which can of course equally well be wrapped in a function as Pete's solution 
can.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] sapply function and poisson distribution

2015-01-04 Thread Pete Brecknock
dimnik wrote
> thank you for your answer.Yes,that sounds right.I thought the same thing
> but the problem is how can i generalize the command for every vector of
> numbers not only for the specific example?not only for c(1,2),c(0.1,0.8).
> 
> 2015-01-04 0:45 GMT+00:00 Pete Brecknock [via R] <

> ml-node+s789695n4701358h57@.nabble

>>:
> 
>>  dimnik wrote
>> i want to find  a function that takes in two vectors of numbers that have
>> the same
>> length.The output should be a list of vectors, where each vector is a
>> sequence of
>> randomly generated Poisson variables where the number of samples in each
>> vector is determined by the entries in the first input vector and the
>> lambdas come
>> from the entries in the second input vector. For example, :If the inputs
>> are c(1,2) and c(0.1,0.8) the output will be a list of twovectors where
>> the
>> first vectorhas a single sample from Poisson(0.1) and the second vector
>> has
>> two samples from Poisson(0.8).How can i do all that kind of stuff using
>> sapply function?
>> thank u in advance
>>
>> How about using mapply, the multivariate version of sapply?
>>
>> Based on your example ...
>>
>> mapply(function(x,y) rpois(x,y), c(1,2),c(0.1,0.8))
>>
>> HTH
>>
>> Pete
>>
>> --
>>  If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701358.html
>>  To unsubscribe from sapply function and poisson distribution, click here
>> ;
>> .
>> NAML
>> ;
>>

Not sure how you intend to specify the input vectors for n and lambda

One way would be as below - you can amend the 2 vectors with the values of
your choice.

n <- c(1,2,3,4,5)
lambda <- c(0.1,0.8,1.2,2.2,4.2)

mapply(function(x,y) rpois(x,y), n, lambda)  

HTH

Pete





--
View this message in context: 
http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701384.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] sapply function and poisson distribution

2015-01-04 Thread dimnik
thank you for your answer.Yes,that sounds right.I thought the same thing
but the problem is how can i generalize the command for every vector of
numbers not only for the specific example?not only for c(1,2),c(0.1,0.8).

2015-01-04 0:45 GMT+00:00 Pete Brecknock [via R] <
ml-node+s789695n4701358...@n4.nabble.com>:

>  dimnik wrote
> i want to find  a function that takes in two vectors of numbers that have
> the same
> length.The output should be a list of vectors, where each vector is a
> sequence of
> randomly generated Poisson variables where the number of samples in each
> vector is determined by the entries in the first input vector and the
> lambdas come
> from the entries in the second input vector. For example, :If the inputs
> are c(1,2) and c(0.1,0.8) the output will be a list of twovectors where the
> first vectorhas a single sample from Poisson(0.1) and the second vector has
> two samples from Poisson(0.8).How can i do all that kind of stuff using
> sapply function?
> thank u in advance
>
> How about using mapply, the multivariate version of sapply?
>
> Based on your example ...
>
> mapply(function(x,y) rpois(x,y), c(1,2),c(0.1,0.8))
>
> HTH
>
> Pete
>
> --
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701358.html
>  To unsubscribe from sapply function and poisson distribution, click here
> 
> .
> NAML
> 
>




--
View this message in context: 
http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701373.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] sapply function and poisson distribution

2015-01-03 Thread Pete Brecknock
dimnik wrote
> i want to find  a functionthattakes   in two vectors of   numbers 
> thathave
> the   same
> length.The output should be a listof vectors, where each  vector 
> is a
> sequence  of  
> randomly  generated   Poisson variableswhere the  number 
> of   samples in  each
> vector is determined by the entries in the first  input   vector and  
> the
> lambdas   come
> from  the entries in the second input vector. For example, :If the 
> inputs
> are c(1,2)and c(0.1,0.8)  the output  will be a list of 
> twovectors where
> the first vectorhas   a   single  sample  fromPoisson(0.1) 
> andthe second
> vector hastwo samples from Poisson(0.8).How can i do all that kind of
> stuff using sapply function?
> thank u in advance

How about using mapply, the multivariate version of sapply?

Based on your example ...

mapply(function(x,y) rpois(x,y), c(1,2),c(0.1,0.8))  

HTH

Pete



--
View this message in context: 
http://r.789695.n4.nabble.com/sapply-function-and-poisson-distribution-tp4701353p4701358.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.