2013-03-22 4 views
0

루프 내에서 aov()을 실행하고있었습니다. 각 반복의 출력은 목록 항목으로 저장되었습니다. 각 aov 개체의 데이터 프레임을 확인하기 위해 model.frame()을 사용했을 때, 모두 동일했습니다. 사실 모든 개체는 기본적으로 마지막 반복의 데이터 프레임이었습니다. 좀더 흥미롭게도 이것은 오류 기간이 포함 된 aov 모델에만 발생하는 것으로 보입니다.는이를 설명하기 위해, 나는 오류 용어가없는 경우이상한 현상 : 루프 내에서 생성 된 aov() 객체에서 다른 aov 객체의 데이터 프레임은 동일합니다.

대조적으로
results<-list() 
length(results2)<-4 
counter<-1 
for(i in unique(ChickWeight$Diet)){ 
results[[counter]] <- aov(weight~factor(Time) + Error(Chick),  
           ChickWeight[ChickWeight$Diet==i,]) 
counter<-counter+1 
} 
head(model.frame(results[[1]]), 2) 
# weight factor(Time) Chick 
#461  42   0 41 
#462  51   2 41 
head(model.frame(results[[2]]), 2) 
# weight factor(Time) Chick 
#461  42   0 41 
#462  51   2 41 
head(model.frame(results[[3]]), 2) 
# weight factor(Time) Chick 
#461  42   0 41 
#462  51   2 41 
head(model.frame(results[[4]]), 2) 
# weight factor(Time) Chick 
#461  42   0 41 
#462  51   2 41 

이,이 발생하지 않습니다 (아래 참조) R에 ChickWeight 데이터를 사용 : 궁금

results2<-list() 
length(results2)<-4 
counter<-1 
for(i in unique(ChickWeight$Diet)){ 
results2[[counter]] <- aov(weight~factor(Time),  
           ChickWeight[ChickWeight$Diet==i,]) 
counter<-counter+1 
} 
head(model.frame(results2[[1]]), 2) 
# weight factor(Time) 
#1  42   0 
#2  51   2 
head(model.frame(results2[[2]]), 2) 
# weight factor(Time) 
#221  40   0 
#222  50   2 
head(model.frame(results2[[3]]), 2) 
# weight factor(Time) 
#341  42   0 
#342  53   2 
head(model.frame(results2[[4]]), 2) 
# weight factor(Time) 
#461  42   0 
#462  51   2 

사람이 알고 있다면 무엇 진행되고있다.

답변

2

오차 범위가 모델의 일부인 경우 model.frame.aovlist은 적합성의 호출 특성을 평가하여 데이터를 가져옵니다 (- attr(*, "call")= language aov(formula = weight ~ factor(Time) + Error(Chick), data = ChickWeight[ChickWeight$Diet == i, ])). 보시다시피 호출은 i 값에 따라 다릅니다. 또한 aovlist 개체는 환경 이름을 저장하여 데이터를 조회합니다. 이 경우는 R_GlobalEnv입니다. 이것

봐 : 당신이 접근 방식을-결합하여 적용 분할 - 더를 사용하는 경우

results<-list() 
counter<-1 
for(i in unique(ChickWeight$Diet)){ 
    results[[counter]] <- aov(weight~factor(Time) + Error(Chick),  
          data=ChickWeight[ChickWeight$Diet==i,]) 
    counter<-counter+1 
} 

head(model.frame(results[[1]]), 2) 
#  weight factor(Time) Chick 
# 461  42   0 41 
# 462  51   2 41 

i <- 1 
head(model.frame(results[[1]]), 2) 
# weight factor(Time) Chick 
# 1  42   0  1 
# 2  51   2  1 

당신은이 문제를 방지 할 수 있습니다.

res <- by(ChickWeight,ChickWeight$Diet,FUN=function(DF) { 
    aov(weight~factor(Time)+ Error(Chick) ,  
     data=DF) 
}) 

head(model.frame(res[[1]]),2) 
# weight factor(Time) Chick 
#1  42   0  1 
#2  51   2  1 

이 접근법은 데이터를 검색하기 위해 적절한 환경을 사용하여 저장되며, by에 전달 익명 함수 호출의 환경, 즉 : 여기 by를 사용하는 예이다.

+0

매우 재미 있습니다. 그렇다면 루프 내에서 사용한 문자 인덱스를 사용하여 각 반복 결과를 호출하는 것 외에 다른 방법으로 쉽게 돌아갈 수있는 방법이 없다는 뜻입니까? 또한 숫자 값을 사용하여 데이터 프레임을 추출 할 때 "잘못된"데이터의 출처는 어디입니까? – Alex

+1

@Alex 내 편집을 참조하십시오. – Roland