|
代码如下,运行之后显示
Error in if (x[k] > min && x[k] < max) { :
missing value where TRUE/FALSE needed
应该是说min和max的值是空的。因为我尝试把min和max换成常数后发现这一部分可以运行,所以应该不是x为null.
我把hat_G函数单独拿出来跑了一遍,却运行正常,请问这是为什么啊?
t1 <- Sys.time()
runall <- function(h,n)
{
x1 <- 1
x2 <- 1
eps1 <- 0
eps2 <- 0
y1 <- 0
y2 <- 0
y3 <- 0
y4 <- 0
y5 <- 0
y6 <- 0
y7 <- 0
y8 <- 0
MSE1=0
MSE2=0
MSE3=0
MSE4=0
MSE5=0
MSE6=0
MSE7=0
MSE8=0
x=0
t=0
h1=0
y=0
G0 <- function(x)
{
result <- x*sin(x)
return(result)
}
G1 <- function(x)
{
result <- log(1+1/x)
return(result)
}
hat_G <- function(t,h1,x,y)
{
i <- 0
sumy <- 0
min <- 0
max <- 0
min <- t-h1
max <- t+h1
for(k in 1:length(x))
{
if(x[k]>min && x[k]<max)
{
i <- i+1
sumy <- sumy+y[k]
}
}
return(sumy/i)
}
a0 <- matrix(nrow=5,ncol=1000)
b0 <- matrix(nrow=5,ncol=1000)
for(i in 1:1000)
{
x1 <- (1:n)/(n+1)
for(j in 1:n)
{
x2[j] <- runif(1,0,1)
}
x2=sort(x2)
for(j in 1:n)
{
eps1[j] <- rnorm(1,0,0.01)
eps2[j] <- rt(1,1)
y1[j] <- G0(x1[j])+eps1[j]
y2[j] <- G0(x2[j])+eps1[j]
y3[j] <- G0(x1[j])+eps2[j]
y4[j] <- G0(x2[j])+eps2[j]
y5[j] <- G1(x1[j])+eps1[j]
y6[j] <- G1(x2[j])+eps1[j]
y7[j] <- G1(x1[j])+eps2[j]
y8[j] <- G1(x2[j])+eps2[j]
}
for(k in 1:n-1)
{
MSE1[i] <- MSE1[i]+abs(x1[k+1]-x1[k])*(hat_G(x1[k],h,x1,y1)-G0(x1[k]))^2
MSE2[i] <- MSE2[i]+abs(x2[k+1]-x2[k])*(hat_G(x2[k],h,x2,y2)-G0(x2[k]))^2
MSE3[i] <- MSE3[i]+abs(x1[k+1]-x1[k])*(hat_G(x1[k],h,x1,y3)-G0(x1[k]))^2
MSE4[i] <- MSE4[i]+abs(x2[k+1]-x2[k])*(hat_G(x2[k],h,x2,y4)-G0(x2[k]))^2
MSE5[i] <- MSE5[i]+abs(x1[k+1]-x1[k])*(hat_G(x1[k],h,x1,y1)-G1(x1[k]))^2
MSE6[i] <- MSE6[i]+abs(x2[k+1]-x2[k])*(hat_G(x2[k],h,x2,y2)-G1(x2[k]))^2
MSE7[i] <- MSE7[i]+abs(x1[k+1]-x1[k])*(hat_G(x1[k],h,x1,y3)-G1(x1[k]))^2
MSE8[i] <- MSE8[i]+abs(x2[k+1]-x2[k])*(hat_G(x2[k],h,x2,y4)-G1(x2[k]))^2
}
}
MSE1=mean(MSE1)
MSE2=mean(MSE2)
MSE3=mean(MSE3)
MSE4=mean(MSE4)
MSE5=mean(MSE5)
MSE6=mean(MSE6)
MSE7=mean(MSE7)
MSE8=mean(MSE8)
return(c(MSE1,MSE2,MSE3,MSE4,MSE5,MSE6,MSE7,MSE8))
}
t2 <- Sys.time()
t2-t1
|
|