我想将funcStdErrMle
和funcStdErrMome
返回的值写入矩阵,以显示n
和theta
的不同值的结果。当我尝试这样做时,它会显示以下错误:
**Error in `[<-`(`*tmp*`, i, j, value = 0.419321467165103) :
subscript out of bounds**
这是我的代码:
n <- c(5,10,30,100)
theta <- c(1,2,4)
funcPop_Mean<-function(theta)
{
pop_mean<-mean(runif(5000,0,theta))
return(pop_mean)
}
funcMLE<-function(n,theta)
{
run1 <- runif(n,0,theta)
mle_est <- max(run1)
return (mle_est)
}
funcMOME<-function(n,theta)
{
run2 <- runif(n,0,theta)
avg<-mean(run2)
mome_est <- 2*avg
return (mome_est)
}
funcStdErrMome<-function(n,theta)
{
diff1 <- funcPop_Mean(theta)-funcMOME(n,theta)
se_mome <- mean(diff1**2)
return (se_mome)
}
funcStdErrMle<-function(n,theta)
{
diff2 <- funcPop_Mean(theta)-funcMLE(n,theta)
se_mle <- mean(diff2**2)
return (se_mle)
}
MOME_res <- matrix(nrow=3,ncol=4)
MLE_res <- matrix(nrow=3,ncol=4)
for(i in theta)
{
for(j in n)
{
MOME_res[i,j] <- funcStdErrMome(i,j)
}
}
for(i in theta)
{
for(j in n)
{
MLE_res[i,j] <- funcStdErrMle(i,j)
}
}
答案 0 :(得分:0)
将for
循环更改为以下内容是否符合您的要求?
for(i in 1:length(theta)) {
for(j in 1:length(n)) {
MOME_res[i, j] <- funcStdErrMome(theta[i], n[j])
}
}
for(i in 1:length(theta)) {
for(j in 1:length(n)) {
MLE_res[i, j] <- funcStdErrMle(theta[i], n[j])
}
}