2016-11-14 1 views
1

다중 레벨 모델에서 데이터 프레임의 각 열 (한 번에 하나씩)을 사용하는 간단한 방법이 있습니까? 내가 this 게시물을 찾았지만, 다른 열을 IV로 사용하여 DV를 제공합니다. 나는 상보을하려고하고있는 알 수없는 DV 및 열을 테스트하기 위해 알려진 IV (1) 의미 :DV로 모든 열을 개별적으로 사용하는 모델

library(nlme) 
exampledf<- data.frame(matrix(ceiling(runif(16,0,50)), ncol=4)) 
colnames(exampledf)<-c("a","b","id_num","d") 
my.control<-list(opt='optim',method='ML') 

lme(.~1,random=~1|id_num,na.action=na.fail,data=exampledf,control=my.control) #fails 

내 목표 출력 (이들의 값을 특정 열 이름을 지정하지 않고) :

lme(a~1,random=~1|id_num,na.action=na.fail,data=exampledf,control=my.control) 
lme(b~1,random=~1|id_num,na.action=na.fail,data=exampledf,control=my.control) 
lme(d~1,random=~1|id_num,na.action=na.fail,data=exampledf,control=my.control) 
+0

A-드문 일이 아니다 움직임은 당신의 목록을 끝낼 수 있도록 수식 문자열로를 paste'하고, 실제 공식에 그것을 강요','이름 (exampledf)'에서 lapply''에있다 모델. 모두 함께, lapply (이름 (exampledf) [- 3], 함수 (x) {lme (붙여 넣기 (x, '~ 1')), 임의 = ~ 1 | id_num, na.action = na. data = exampledf, control = my.control)})' – alistaire

답변

2
abc <- function(i){ 
    form <- as.formula(paste0(colnames(exampledf)[i], "~1")) 
    lme(form,random=~1|id_num,na.action=na.fail,data=exampledf,control=my.control) 
} 

lapply(seq_along(colnames(exampledf)), abc) 
+0

함수'abc'에 대한 데이터 프레임을 어떻게 전달할 것인가? – Rilcon42

+0

@ Rilcon42 데이터 프레임이 똑같습니다! 그래서 당신의 질문을 얻지 못했습니다 –

+0

내가 의미하는 것을 유감스럽게 생각해서 : 함수에서 다른 데이터 프레임을 사용하고 싶다면 그냥'abc (new_df, i)' – Rilcon42

관련 문제