0

使用下面的这个程序,我会得到错误:

Error in solve.default(sig[!pick.miss, !pick.miss]) : 'a' is 0-diml 

我想使用 EM 算法来估算缺失值。如果缺失值很小(从数据集的 5% 到 20%),这个函数可以工作。但是如果缺失值大于数据集的 30%,运行程序我会得到上述错误。我很困惑,迫切需要帮助,非常感谢。

library(e1071)
EMalg <- function(x, tol=.001){
missvals <- is.na(x)
new.impute<-x
old.impute <- x
count.iter <- 1
reach.tol <- 0
sig <- as.matrix(var(na.exclude(x)))
mean.vec <- as.matrix(apply(na.exclude(x),2,mean))

while(reach.tol != 1) {
for(i in 1:nrow(x)) {
  pick.miss <-( c( missvals[i,]) )
  if ( sum(pick.miss) != 0 ) {
    inv.S <- solve(sig[!pick.miss,!pick.miss]) 


    new.impute[i,pick.miss] <- mean.vec[pick.miss] +
      sig[pick.miss,!pick.miss] %*%
      inv.S %*%
      (t(new.impute[i,!pick.miss])- t(t(mean.vec[!pick.miss])))
  }
}

sig <- var((new.impute))
mean.vec <- as.matrix(apply(new.impute,2,mean))

if(count.iter > 1){ 
  for(l in 1:nrow(new.impute)){
    for(m in 1:ncol(new.impute)){
      if( abs((old.impute[l,m]-new.impute[l,m])) > tol ) {
        reach.tol < - 0
      } else {
        reach.tol <- 1
      }
    }
  }
}

count.iter <- count.iter+1 # used for debugging purposes to ensure process it iterating properly
old.impute <- new.impute
}

 return(new.impute)
 }
 modelerData<-read.csv(file.choose(), header=TRUE)
 imputed <- EMalg(modelerData, tol=.0001)
4

0 回答 0