在R上创建一个带有字符变量的循环 - 对于双样本t.test

时间:2015-05-19 21:57:32

标签: r loops rstudio

我希望在R中进行多个两个样本t.tests。 我想测试50个有两个级别的指标。所以起初我用过:

t.test(m~f)
   Welch Two Sample t-test
   data:  m by f
   t = 2.5733, df = 174.416, p-value = 0.01091
   alternative hypothesis: true difference in means is not equal to 0
   95 percent confidence interval:
   0.05787966 0.43891600
   sample estimates:
    mean in group FSS mean in group NON-FSS 
        0.8344209             0.5860231

这里m对应于我要测试的第一个指标m = Debt.to.equity.ratio。 以下列出了我需要测试的所有指标:

print (indicators)
 [1] "Debt.to.equity.ratio"                                   "Deposits.to.loans"                                     
 [3] "Deposits.to.total.assets"                               "Gross.loan.portfolio.to.total.assets"                  
 [5] "Number.of.active.borrowers"                             "Percent.of.women.borrowers"                            
 [7] "Number.of.loans.outstanding"                            "Gross.loan.portfolio"                                  
 [9] "Average.loan.balance.per.borrower"                      "Average.loan.balance.per.borrower...GNI.per.capita"    
[11] "Average.outstanding.balance"                            "Average.outstanding.balance...GNI.per.capita"          
[13] "Number.of.depositors"                                   "Number.of.deposit.accounts"                            
[15] "Deposits"                                               "Average.deposit.balance.per.depositor"                 
[17] "Average.deposit.balance.per.depositor...GNI.per.capita" "Average.deposit.account.balance"                       
[19] "Average.deposit.account.balance...GNI.per.capita"       "Return.on.assets"                                      
[21] "Return.on.equity"                                       "Operational.self.sufficiency"                          
[23] "FSS"                                                    "Financial.revenue..assets"                             
[25] "Profit.margin"                                          "Yield.on.gross.portfolio..nominal."                    
[27] "Yield.on.gross.portfolio..real."                        "Total.expense..assets"                                 
[29] "Financial.expense..assets"                              "Provision.for.loan.impairment..assets"                 
[31] "Operating.expense..assets"                              "Personnel.expense..assets"                             
[33] "Administrative.expense..assets"                         "Operating.expense..loan.portfolio"                     
[35] "Personnel.expense..loan.portfolio"                      "Average.salary..GNI.per.capita"                        
[37] "Cost.per.borrower"                                      "Cost.per.loan"                                         
[39] "Borrowers.per.staff.member"                             "Loans.per.staff.member"                                
[41] "Borrowers.per.loan.officer"                             "Loans.per.loan.officer"                                
[43] "Depositors.per.staff.member"                            "Deposit.accounts.per.staff.member"                     
[45] "Personnel.allocation.ratio"                             "Portfolio.at.risk...30.days"                           
[47] "Portfolio.at.risk...90.days"                            "Write.off.ratio"                                       
[49] "Loan.loss.rate"                                         "Risk.coverage"      

我不想每次在t.test中更改指标名称,而是想创建一个自动执行并计算p.value的循环。我已经尝试创建一个循环但由于变量=字符的性质而无法使其工作。

我真的很感激有关如何前进的任何提示! 非常感谢你!

最佳

摩根

1 个答案:

答案 0 :(得分:0)

我假设您正在针对相同的f进行每个指标的回归。

在这种情况下,您可以尝试以下方式:

  p_vals = NULL;
  for(this_indicator in indicators)
    {
    this_formula = paste(c(this_indicator, "f"), collapse="~");
    res = t.test(as.formula(this_formula));
    p_vals = c(p_vals, res$p.value);
    }

然而,有一条评论:您是否对这些p值进行了多重调整?鉴于您正在进行大量测试,您很可能会被误报。