2017-05-04 1 views
0

일련의 서수 변수에서 누락 값 대체를 수행하고 있습니다.쥐를 사용할 때 dim (X)는 양수 길이 여야 함

내가 먼저 데이터 프레임에 읽고 몇 가지 청소 할

: 그럼 얻을

# 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) 

: 함수를, 그리고

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,] 

내가없는 값 전가하는 기능을 만들 수를 데이터 집합을 부분 집합 및 실행 오류 메시지 :

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) 

도와주세요! 고맙습니다!

답변

0

나는 또한이 오류 메시지를 몇 번 받았습니다. 몇 가지 코드 실험을 한 후에 나는 왜 그런 메시지를 얻었는지 알아 냈다 : (a) 변수에서 누락 된 사례가 매우 적고 (해당 변수에서만 한 사례의 누락 값), (b) 해당 변수에 대해 '잘못된'대체 방법을 지정합니다 (예 : polr 메소드를 사용하여 2 진 변수를 대체 함). 바이너리 변수에 대해 imputation 메소드를 'logreg'로 설정하면이 오류 메시지가 표시되지 않게되었습니다.

본인이 아닌지 확실하지 않습니다. 나는 각각의 경우에 누락 된 값의 수를 확인하고 각 변수에 '올바른'대체 방법을 할당하는 데이터 검사를 권장합니다 (pmm-pmm을 사용하지 않는 경우를 위해 여러 변수 유형에 대해 잘 작동합니다. Van Buuren 's 여기에 의견 : https://statisticalhorizons.com/predictive-mean-matching). 예를 들어

, 당신은 V1 (진), V2 (주문), V3 (연속), V4 (multinom), 및 V5를 (주문), 당신은 방법을 설정할 수있는 경우로 :

method=c('logreg', 'polr', 'pmm', 'polyreg', 'polr') 

희망 이것은 도움이됩니다.

관련 문제