Andrea
Andrea

Reputation: 175

Vapply command and mvtnorm

I am not familiar with function over a vector in R. I would like a vector with the different values of cumulative probability of a bivariate when some parameters change value simultaneously according to different function. For example here:

library(mvtnorm)
m<-2
corr<-diag(2)
corr[2,1]<-0
vapply(2*1:3,function(x) 
pmvnorm(mean=c(2,x),corr,lower=c(-Inf,-Inf), upper=c(1,2)),1)
[1] 7.932763e-02 3.609428e-03 5.024809e-06

I have the different value of cumulative probability when the mean of the second distribution takes value 2,4 and 6. My problem is that I want simultaneously change also the value of the value of the mean of the first distribution. I can't write properly the vapply command with more than one function. What can I do? Thank you very much

Upvotes: 1

Views: 255

Answers (1)

dickoa
dickoa

Reputation: 18437

You will need to use mapply for this task

library(mvtnorm)

corr <- diag(2)
m1 <- c(3, 5, 7)
m2 <- c(2, 4, 6)

mapply(function(x, y)
  pmvnorm(mean = c(x, y), corr, lower = c(-Inf, -Inf), upper = c(1, 2)), 
  m1, m2)
## [1] 1.1375e-02 7.2052e-07 3.1246e-14

Upvotes: 3

Related Questions