0

我正在尝试编写固定效应回归,但我有很多虚拟变量。基本上,我的方程的 RHS 上有 184 个变量。我没有把它写出来,而是试图创建一个将通过每一列的循环(我用一个数字命名了每一列)。

这是我到目前为止的代码,但粘贴不起作用。我可能完全不使用粘贴,但我不确定如何解决这个问题。但是,我收到一个错误(见下文)。

FE.model <- plm(avg.kw ~ 0 + (for (i in 41:87) {
                    paste("hour.dummy",i,sep="") + paste("dummy.CDH",i,sep="")
                   + paste("dummy.MA",i,sep="") + paste("DR.variable",i,sep="")
              }),
              data = data.reg,
              index=c('Site.ID','date.hour'),
              model='within',
              effect='individual') 
summary(FE.model)

作为列名的示例,当 i=41 时,名称应为“hour.dummy41”“dummy.CDH41”等。

我收到以下错误:

Error in paste("hour.dummy", i, sep = "") + paste("dummy.CDH", i, sep = "") : non-numeric argument to binary operator

所以我不确定是粘贴功能在这里不合适,还是循环。我似乎找不到在 R 中轻松遍历列名的方法。

任何帮助深表感谢!

4

1 回答 1

1

Ignoring worries about fitting a model with so many terms for the moment, you probably want to generate a string, and then cast it as a formula:

#create a data.frame where rows are the parts of the variable names, then collapse it
rhs <- do.call(paste, c(as.list(expand.grid(c("hour.dummy","dummy.CDH"), 41:87)), sep=".", collapse=" + "))
fml <- as.formula(sprintf ("avg.kw ~ %s"), rhs))
FE.model <-pml(flm, ...

I've only put in two of the 'dummy's in the second line- but you should get the idea

于 2014-04-15T13:57:03.487 回答