2014-03-05 5 views
1

안녕하세요 저는 기본 함수를 만들었습니다 (저는 R에 익숙합니다).함수에서 결과 저장

analyse <- function(gene_set,probe_ids) { 
    Xprobe_ids <- mapply(function(k) paste('X',k,sep=''), probe_ids) 
    model_1_formula_str = 'Status ~ Age + Sex + Source'; 
    model_1 <- glm(as.formula(model_1_formula_str), data = fcA, family = "binomial"); 
    model_2_formula_str = model_1_formula_str; 
    for (next_id in probe_ids) { 
     model_2_formula_str = paste(model_2_formula_str, ' + X',next_id,sep = '') 
    } 
    model_2 <- glm(as.formula(model_2_formula_str), data = fcA, family = "binomial"); 
    gene_pval = anova(model_2,model_1, test="Chisq")[2,5]; 
    probe_id_str <- paste(probe_ids,collapse=','); 
    probe_num <- length(probe_ids); 
    c(gene_set,gene_pval,probe_num,probe_id_str,); 
} 

하고 문제가 발생은

for (next_id in probe_ids) { 
    model_2_formula_str = paste(model_2_formula_str, ' + X',next_id,sep = '') 
} 

본질적 I는 각각 상이한 유전자 변화 모델 2 모델 2 대 1 모델을 분석 할. 그러나 나는 model_2_formula_str을 반복적으로 변경하여 최종 유전자를 얻은 후 분석을 수행하는 루프를 통해 모델 2를 보냅니다.

궁금한 점은 분석 결과를 저장하는 데 어떻게 결과를 얻을 수 있을까요? 그런 다음 다음 유전자로 이동하는 것은 동일합니까?

도움 주셔서 감사합니다.

답변

2

strings <- c() 
for (next_id in probe_ids) { 

    strings = c(strings, paste(model_2_formula_str, ' + X',next_id,sep = '')) 

} 

mods <- lapply(strings, FUN = function(x) { 
    glm(as.formula(x), data = fcA, family = "binomial") }) 

그런 다음 바로 비교가 원하는 수 있도록 모델의 목록을 반복 lapply와 모델의 목록을 만드는 작업을해야

gene_pvals = lapply(mods, FUN = function(x) { 
    anova(x, model_1, test="Chisq")[2,5] 
})