错误提示:
Error in X[[floor(a * n) + 1]] : recursive indexing failed at level 2
In addition: Warning messages:
1: In (floor(a * n) + 1):(n - floor(a * n)) :
numerical expression has 10000 elements: only the first used
2: In (floor(a * n) + 1):(n - floor(a * n)) :
numerical expression has 10000 elements: only the first used
代码:函数略微复杂,
n <- 100
X <- list()
#生成X(1),...,X(n)
samples <- sample(1:(10*n), n)
order <- order(samples, decreasing=F)
for(i in 1:n){
orderX[
] <- samples[[order[]]]
}
#s^2(a)
s2 <- function(a){
#m(α)
sumX <- 0
for(i in (floor(a*n)+1):(n-floor(a*n))){
sumX <- sumX+X[]
}
m <- 1/(n-2*floor(a*n))*sumX
#Var(a)
VarX <- 0
for(i in (floor(a*n)+1):(n-floor(a*n))){
VarX <- VarX+(X[]-m)^2
}
VarX <- VarX/n
result <- 1/(1-2*a)^2*(VarX+a*(X[[floor(a*n)+1]]-m)^2+a*(X[[n-floor(a*n)]]-m)^2)
}
# 利用curve函数进行绘制
curve(s2,from=0.1,to=0.4)