2014-03-05 152 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 = '') 
} 

基本上我想分析模型1与模型2,以改变每个不同的基因模型2。不过,我正在通过一个循环发送模型2,该循环只是简单地将model_2_formula_str改变为最终基因,然后进行分析。

我想知道的是,我将如何得到它来执行分析存储结果?然后移动到下一个基因也是这样,等等?

感谢您的帮助!

回答

2

应努力使模型列表与lapply

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") }) 

然后,只需遍历您的机型列表,让你想攀比

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