0
我正在對一系列有序變量進行缺失值插補。dim(X)在使用鼠標函數時必須具有正值長度
我第一次在數據幀中讀出,並做一些清潔:
dietgp1m<-read.csv(file='1 Month data-diet.csv',header=TRUE,na.strings=c(""," ","NA","."))
for (i in 1:ncol(dietgp1m)) {dietgp1m[,i]<-as.factor(dietgp1m[,i])}
dietgp1m<-dietgp1m[!is.na(dietgp1m$Patient.Trial.ID),]
dietgp1m["count"]<-0
for (i in 1:nrow(dietgp1m)) {dietgp1m$count[i]<-0; for (j in 9:298) {if (!is.na(dietgp1m[i,j])) {dietgp1m$count[i]<-dietgp1m$count[i]+1}}}
dietgp1m<-dietgp1m[dietgp1m$count!=0,]
然後,我創建了缺失值插補功能,子集的數據集和運行函數:
# Imputation
imputation<-function(A){
B<-mice(data = A, m = 5, method = "polr", maxit = 50, seed = 500)
C<-complete(B, 'long', include=TRUE) #include=TRUE if include the original dataset with missing values
print(colnames(C))
###pool imputed data
for (i in 4:ncol(C)) {C[,i]<-as.numeric(as.character(C[,i]))}
for (j in 4:ncol(C)) {for (i in 1:159) {if (is.na(C[i,j])) {C[i,j]<-round((C[i+159,j]+C[i+159*2,j]+C[i+159*3,j]+C[i+159*4,j]+C[i+159*5,j])/5)}}}
print(nrow(C)); print(ncol(C))
}
# Quality of life
# Diet group 1 month
seb<-subset(df3, select=c(Patient.Trial.ID, Q32a:Q32j))
missinganalysis(seb)
imputation(seb)
然後我得到錯誤信息:
iter imp variable
1 1 Q32a
Error in apply(draws, 2, sum) : dim(X) must have a positive length
Called from: apply(draws, 2, sum)
請幫忙!謝謝!