【发布时间】:2019-04-10 20:31:22
【问题描述】:
我需要以数值方式计算此积分,然后评估其每次观察的值。我尝试使用集成功能来做到这一点,但每次我收到错误消息。这是我的代码:
`rm(list = ls(all = TRUE)) # Remove all objects in R console
set.seed(123456) # Set the seed for reproducible results.default for the program to fix initial values to start with in each time.
install.packages("MASS")
library (MASS) ## Simulate from a Multivariate Normal Distribution
n=10 # sample size
mu=c(0.3,0.7029) # true values of the mean vector of bivariate normal variables
var.cov<-matrix(c(10,0,0,10),nrow=2,ncol=2,byrow=T) #variance covariance matrix of the bivariate normal random variables
y= mvrnorm(n, mu, var.cov)#generating the bivariate normal random variables
theta.rad=rep(NA,times=n)
for (ii in 1:n){
if (y[ii,1] > 0 & y[ii,2]>=0) {theta.rad[ii]=atan(y[ii,2]/y[ii,1])}
else if (y[ii,1] < 0) {theta.rad[ii]=(atan(y[ii,2]/y[ii,1]))+pi}
else if (y[ii,1] > 0 & y[ii,2]<0) {theta.rad[ii]=atan(y[ii,2]/y[ii,1])+2*pi}
else if (y[ii,1] == 0 & y[ii,2]>0) {theta.rad[ii]=pi/2}
else if (y[ii,1] ==0 & y[ii,2]<0) {theta.rad[ii]=3*pi/2}
}# calculating the angles
u=cbind(cos(theta.rad),sin(theta.rad)) #constructing the main matrix (cos(theta,sin(theat)))
integral=function(uu){
f=function(r){
t=1/sqrt(var.cov[1,1])
tt=t*uu%*%mu
phi=log(1+((tt*pnorm(tt))/dnorm(tt)))
aa=r*(exp((log(r/var.1))-(r^2/(2*var.1))+(r*(uu%*%mu)/var.1)-phi))
return(aa)
}
integrate(f,0,Inf)
}
integral(u)
我收到以下错误消息: f(x, ...) 中的错误: 暗淡 [产品 10] 与对象 [15] 的长度不匹配 另外:警告信息: 在 r * (uu %*% mu) 中: 较长的对象长度不是较短对象长度的倍数
f(x, ...) 中的错误: 暗淡 [产品 10] 与对象 [15] 的长度不匹配 另外:警告信息: 在 r * (uu %*% mu) 中: 较长的对象长度不是较短对象长度的倍数
【问题讨论】:
标签: r