Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算r中的二重积分?_R - Fatal编程技术网

如何计算r中的二重积分?

如何计算r中的二重积分?,r,R,我想计算以下密度函数的积分: 使用R中的包“rmutil”和“psych”,我尝试: X=c(8,1,2,3) Y=c(5,2,4,6) correlation=cov(X,Y)/(SD(X)*SD(Y)) bvtnorm <- function(x, y, mu_x = mean(X), mu_y = mean(Y), sigma_x = SD(X), sigma_y = SD(Y), rho = correlation) { force(x) force(y) fu

我想计算以下密度函数的积分:

使用R中的包“rmutil”和“psych”,我尝试:

X=c(8,1,2,3)

Y=c(5,2,4,6)

correlation=cov(X,Y)/(SD(X)*SD(Y))

bvtnorm <- function(x, y, mu_x = mean(X), mu_y = mean(Y), sigma_x = SD(X), sigma_y = SD(Y), rho = correlation) {
  force(x)
  force(y)
  function(x, y) 
    1 / (2 * pi * sigma_x * sigma_y * sqrt(1 - rho ^ 2)) * 
    exp(- 1 / (2 * (1 - rho ^ 2)) * ((x - mu_x) / sigma_x) ^ 2 + 
          ((y - mu_y) / sigma_y) ^ 2 - 2 * rho * (x - mu_x) * (y - mu_y) / 
          (sigma_x * sigma_y))
}

f2 <- bvtnorm(x, y)

print("sum_double_integral :")

integral_1=int2(f2, a=c(-Inf,-Inf), b=c(Inf,Inf)) # should normaly give 1 

print(integral_1)  # gives Nan
X=c(8,1,2,3)
Y=c(5,2,4,6)
相关性=cov(X,Y)/(标准偏差(X)*标准偏差(Y))

bvtnorm缺少一对括号。更正后的代码如下所示:

library(rmutil)
X=c(8,1,2,3)

Y=c(5,2,4,6)

correlation=cor(X,Y)

bvtnorm <- function(x, y, mu_x = mean(X), mu_y = mean(Y), sigma_x = sd(X), sigma_y = sd(Y), rho = correlation) {

  function(x, y) 
    1 / (2 * pi * sigma_x * sigma_y * sqrt(1 - rho ^ 2)) * 
    exp(- 1 / (2 * (1 - rho ^ 2)) * (((x - mu_x) / sigma_x) ^ 2 + 
          ((y - mu_y) / sigma_y) ^ 2 - 2 * rho * (x - mu_x) * (y - mu_y) / 
          (sigma_x * sigma_y)))
}

f2 <- bvtnorm(x, y)

print("sum_double_integral :")

integral_1=int2(f2, a=c(-Inf,-Inf), b=c(Inf,Inf)) # should normaly give 1 

print(integral_1)  # prints 1.000047
库(rmutil)
X=c(8,1,2,3)
Y=c(5,2,4,6)
相关性=cor(X,Y)

bvtnorm我知道
sd
,但什么是
sd
?R是区分大小写的。@John Coleman,“psych”包中的标准偏差是用大写字母(SD)写的!非常感谢M.的宝贵评论和帮助!