1
votes

I want to generate Poisson distributed random vectors of length n. For testing purposes I have defined an intensity vector lambda as lambda <- c(1:12). Originally I thought that generating the random vectors is an easy process with the lapply function, as we can simply write

vec <- as.matrix(lapply(lambda, function(s) rpois(1, s)))

However with any matrix of proper size, e.g. B <- matrix(data = rep(1, 84), nrow = 7, ncol = 12) we get in matrix multiplication that

B%*%vec

gives

Error in B %*% vec : requires numeric/complex matrix/vector arguments

I thought that I might have done something wrong with the lapply, but creating an empty 12x1 vector and inserting the Poisson distributed values to its components yielded the same error.

So firstly, what is causing this error? Secondly, is there a convenient way to create random vectors of length n from an arbitrary p.r. distribution?

1
See what str(vec) returns. VTC as simple typo.Rui Barradas

1 Answers

2
votes

The vec that you created is a column of lists (because of your lapply call). You can see this by inspecting the first element:

vec[1, ]

[[1]]
[1] 1

The alternative would be to feed your lambda vector directly to rpois:

vec <- rpois(12, lambda = 1:12)
B <- matrix(1, nrow = 7, ncol = 12)
B %*% vec

     [,1]
[1,]   87
[2,]   87
[3,]   87
[4,]   87
[5,]   87
[6,]   87
[7,]   87