2012-11-09 3 views
5

96 회분을 실행하고 결과를 96 개의 다른 객체로 저장하려고합니다. 사물을 복잡하게 만들려면 모델의 공변량 중 하나의 첨자도 96 번 변경해야합니다. 나는 거의 문제를 해결했지만 불행히도 벽에 부딪혔다. 이것은 객체 생성 측에서 작동 코드까지이며,회귀 분석에서 공변량을 사용하여 루프하기 R

for(i in 1:96){ 

    assign(paste("z.out", i,sep=""), lm(rMonExp_EGM~ TE_i + Month2+Month3+Month4+Month5+Month6+Month7+Month8+Month9+ 
    Month10+Month11+Month12+Yrs_minus_2004 + 
    as.factor(LGA),data=Pokies)) 

} 

(예를 들어 나는 z.out1이 - z.out96을)하지만 나뿐만 아니라 변경하기 위해 공변량에 첨자를 얻을 수없는 것.

데이터 세트에 TE_1, TE_2 ... TE_96이라는 96 개의 변수가 있습니다. 이와 같이, TE_의 아래 첨자 인 "i"는 내가 만드는 각각의 객체에 해당하도록 변경해야합니다.

z.out1 <- lm(rMonExp_EGM~ TE_1 + Month2+Month3+Month4+Month5+Month6+Month7+Month8+Month9+ 
    Month10+Month11+Month12+Yrs_minus_2004 + as.factor(LGA),data=Pokies) 

그리고 z.out96은 다음과 같아야합니다 :

z.out96 <- lm(rMonExp_EGM~ TE_96+ Month2+Month3+Month4+Month5+Month6+Month7+Month8+Month9+ 
    Month10+Month11+Month12+Yrs_minus_2004 + as.factor(LGA),data=Pokies) 

는 희망이 말이 즉 z.out1이 모델에서 결과를 보유해야한다. 어떤 조언이나 조언에 감사드립니다.

+0

하지만 프로 그 밖에서 문제가 발생할 때 CrossValidate에 대해 개별적인 요소로서 시계열에 관해 다시 생각해 보거나 질문해야합니다. 그 결과는 오해의 소지가 있거나 단순히 잘못되었습니다. 덕분에 –

답변

7

나는 목록에서 결과를 놓고 당신은 당신의 수식에

orig_formula <- MonExp_EGM~ Month2+Month3+Month4+Month5+Month6+Month7+Month8+Month9+ 
Month10+Month11+Month12+Yrs_minus_2004 + as.factor(LGA) 


te_variables <- paste0('TE_', 1:96) 
# Or if you don't have a current version of R 
# te_variables <- paste('TE', 1:96, sep = '_') 

new_formula <- lapply(te_variables, function(x,orig = orig_formula) { 
    new <- reformulate(c(x,'.')) 
    update(orig, new)}) 
## it works!  
new_formula[[1]] 
## MonExp_EGM ~ TE_1 + Month2 + Month3 + Month4 + Month5 + Month6 + 
## Month7 + Month8 + Month9 + Month10 + Month11 + Month12 + 
## Yrs_minus_2004 + as.factor(LGA) 
new_formula[[2]] 
## MonExp_EGM ~ TE_2 + Month2 + Month3 + Month4 + Month5 + Month6 + 
## Month7 + Month8 + Month9 + Month10 + Month11 + Month12 + 
## Yrs_minus_2004 + as.factor(LGA) 


models <- lapply(new_formula, lm, data = pokies) 

지금이 있어야한다 (96 개) 요소를 만들 reformulateupdate의 조합을 사용할 수 있습니다

for loopassign 문을 피할 것 목록 models

원래 계획 한 이름을 반영하도록 이름을 지정할 수 있습니다.

names(models) <- paste0('z.out', 1:96) 
# or if you don't have a current version of R 
# names(models) <-paste('z.out', 1:96 ,sep = '') 

다음

models$z.out5 

하여 단일 모델에 액세스하거나 모든 모델의 요약을 만들

summaries <- lapply(models, summary) 

등 ....

# just the coefficients 
coefficients <- lapply(models, coef) 

# the table with coefficient estimates and standard.errors 

coef_tables <- apply(summaries, '[[', 'coefficients') 
+0

! 이것은 트릭을 수행합니다. 모델에서 계수를 추출하려면 for 루프를 사용하는 것이 현명합니까? 예 - bumpkis <- matrix (nrow = 96, ncol = 1) for (i96가 1:96) { bumpkis [i,] <- c (models $ z.out (i) $ coefficients [2]) } 내가 일하게 할 수 없어서 불행히도 작동하지 않습니다. 루프에 대한 근본적인 것이 빠져 있다고 생각합니다. – kpeyton

+0

아니요. 편집 된 솔루션의 예제를 참조하십시오. 이런 종류의 활동을 위해서는 루프가 작동하지만 관용적 인 R 솔루션은 'lapply'를 사용하는 것입니다. 'models $ z.out (i)'에서'i'를 사용하면 올바르지 않습니다. 'models [[paste ('z.out', i, sep = '')]]'을 시도하십시오. '$'를 사용할 수 없습니다. – mnel

관련 문제