fraa<-function(missD,Ep,L=2,iter=5,verbose=T) {#Estimate missing values in matrix missD #Ep =initial solution #value: estimate # or, uncomment last lines at end of this file to get #list: estimate = matrix of estimate # U = matrix from final iteration of svd(A) # sing.values=singular values of Ep, final iteration # #6 November 2003/17 August 2004 ##############set-up M<-dim(missD)[2] N<-dim(missD)[1] if (L > M) stop("L must be less than or equal to the number of columns of E") #Check to see if any rows are all missing x<-(1:N)[apply(missD,1,function(A){all(is.na(A))})] if (length(x) > 0) { cat("Rows ", x, " are all NA's\n") stop("Stop and remove these rows\n") } missing<-which(is.na(missD)) m<-length(missing) m2<-m*m ###############begin algorithm Xp1<-matrix(0,N,M) track<-iter while (iter > 0) { A<-t(Ep)%*%Ep #find singular value decomposition svd.decomp<-svd(A) U<-svd.decomp$u S2<-svd.decomp$d #Singular values of Ep singular <- sqrt(S2) partial.sum<-sum(S2[L:M]) total.sum<-sum(S2) fraction<-partial.sum/total.sum if (verbose) cat("\n Iteration ", track-iter+1, "\n", fraction, "\n") ################construct Bp B<-matrix(0,m,m) i <-row(missD)[which(is.na(missD))] j <-col(missD)[which(is.na(missD))] for (s in 1:m) { for (t in s:m) { if (i[s]==i[t]) { B[s,t]<- U[j[s],L:M]%*%U[j[t],L:M] B[t,s]<-B[s,t] } #end if } #end for t } #end for s ##########construct W W<-numeric(m) for (t in 1:m) { K<-matrix(0,N,M) K[missing[t]]<-1 W[t]<-sum(diag(t(U[,L:M])%*%t(Ep)%*%K%*%U[,L:M])) } # QR decomposition qrB <-qr(B) xp1<- - qr.coef(qrB,W) Xp1[missing]<- xp1 Ep<- Ep+Xp1 #set counter iter <- iter - 1 } #End while ############################################# if (verbose) { cat(" Singular values (final iteration):\n", singular) cat("\n") } #If we need the eigenvectors and singular values, uncomment #the next 2 lines #temp<-list(estimate=Ep,U=U,singular=singular) # return(temp) return(Ep) }