5

我正在尝试运行 96 个回归并将结果保存为 96 个不同的对象。更复杂的是,我希望模型中协变量之一的下标也改变 96 次。我几乎解决了这个问题,但不幸的是我碰壁了。到目前为止的代码是,

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),但我似乎无法让协变量上的下标也发生变化。

我在数据集中有 96 个变量,称为 TE_1、TE_2 ... TE_96。因此,TE_ 上的下标“i”需要更改以对应于我创建的每个对象。也就是说,z.out1 应该保存这个模型的结果:

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)

希望这是有道理的。我很感激任何提示/建议。

4

1 回答 1

7

我会将结果放在一个列表中并避免使用for loopandassign语句

您可以使用 reformulate和的组合update来创建您的公式

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 个元素models

您可以命名它们以反映您最初计划的 nnames

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

ETC

或创建所有模型的摘要

 summaries <- lapply(models, summary)

ETC....

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

 # the table with coefficient estimates and standard.errors

 coef_tables <- apply(summaries, '[[', 'coefficients')
于 2012-11-09T05:04:48.920 回答