【问题标题】:how to use the dmvnorm function and mapply together如何一起使用dmvnorm函数和mapply
【发布时间】:2016-11-03 20:49:13
【问题描述】:
set.seed(1)
### i would like to do this
dmvnorm(c(.5,.5), mean= c(2,15), matrix(c(3, 0, 0, 9), 2))
dmvnorm(c(.6,.6), mean= c(5,18), matrix(c(6, 0, 0, 15), 2))

##### BUT using mapply instead... how can that be done?
u1 = c(2,15)
sigma1 = matrix(c(3, 0, 0, 9), 2)
u2 = c( 5, 18)
sigma2 = matrix(c(6, 0, 0, 15), 2)
parameters = list(mu = list(u1, u2), sigma = list(sigma1, sigma2))
mapply( c(c(.5,.5),c(.6,.6)),   dmvnorm, 
        mean = c(parameters$mu[[1]], parameters$mu[[2]] ) ,
        sigma= c(parameters$sigma[[1]],parameters$sigma[[2]] 

) )

【问题讨论】:

  • mapply 的第一个参数应该是一个函数。

标签: r pca normal-distribution mle multivariate-testing


【解决方案1】:

将每个参数放到一个列表中的同一个参数中:

library(mvtnorm)
u <- list(u1 = c(2,15), u2 = c( 5, 18))
sigma <- list(sigma1 = matrix(c(3, 0, 0, 9), 2),
              sigma2 = matrix(c(6, 0, 0, 15), 2))
x <- list(c(0.5, 0.5), c(0.6, 0.6))
result <- mapply(dmvnorm, x, u, sigma)
# [1] 1.780234e-07 1.384004e-07

这相当于:

result <- numeric(length(x))
for (i in 1:length(x))
  result[i] <- dmvnorm(x[[i]], u[[i]], sigma[[i]])

【讨论】:

    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多