r - How to get dpois results for multiple x and multiple lambda? -


i'm trying probability matrix poisson distribution using vector of lambda. want get:

    x<-seq(1,3,1)     lambda<-seq(1,2,0.5)     dpois(x,lambda[1])     [1] 0.36787944 0.18393972 0.06131324     dpois(x,lambda[2])     [1] 0.3346952 0.2510214 0.1255107     dpois(x,lambda[3])     [1] 0.2706706 0.2706706 0.1804470 

when this:

     dpois(x,lambda)      [1] 0.3678794 0.2510214 0.1804470 

i probs of x[i] lambda[i] , not each lambda probs of x

i want know how without using loop...

in other words insert dpois() 2 vectors x , lambda, , possible probability combination.

for creating pairs of combinations of vectors need use expand.grid function.

x <- seq(1, 3, 1) lambda <- seq(1, 2, 0.5)  grid <- expand.grid(x=x, lambda=lambda) dpois(grid$x, grid$lambda) 

when r in vectorized way, aligns , repeats shorter vectors longest vector in example below:

> cbind(1:5, 1:8, 1:2, 1:3)      [,1] [,2] [,3] [,4] [1,]    1    1    1    1 [2,]    2    2    2    2 [3,]    3    3    1    3 [4,]    4    4    2    1 [5,]    5    5    1    2 [6,]    1    6    2    3 [7,]    2    7    1    1 [8,]    3    8    2    2 

so other way around make longest vector long enough shorter 1 able repeat enough times create combinations:

> x <- 1:2 > y <- 1:3 > cbind(x,y)      x y [1,] 1 1 [2,] 2 2 [3,] 1 3 warning message: in cbind(x, y) :   number of rows of result not multiple of vector length (arg 1) > cbind(rep(x, each=length(y)), y)        y [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 2 1 [5,] 2 2 [6,] 2 3 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -