如何计算r中的二重积分?
我想计算以下密度函数的积分: 使用R中的包“rmutil”和“psych”,我尝试:如何计算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
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.的宝贵评论和帮助!