[英]Loop through column names in Fixed Effects Regression
I am trying to code a fixed effects regression, but I have MANY dummy variables. 我正在尝试编写固定效果回归的代码,但是我有很多虚拟变量。 Basically, I have 184 variables on the RHS of my equation.
基本上,我的方程式的RHS有184个变量。 Instead of writing this out, I am trying to create a loop that will pass through each column (I have named each column with a number).
而不是写出来,我试图创建一个循环通过每个列(我用数字命名了每个列)。
This is the code i have so far, but the paste is not working. 这是我到目前为止的代码,但是粘贴不起作用。 I may be totally off base using paste, but I wasn't sure how else to approach this.
使用粘贴可能会使我完全不满意,但是我不确定该如何处理。 However, I am getting an error (see below).
但是,我收到一个错误(请参阅下文)。
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)
As an example for the column names, when i=41 the names should be "hour.dummy41" "dummy.CDH41", etc. 以列名称为例,当i = 41时,名称应为“ hour.dummy41”,“ dummy.CDH41”,等等。
I'm getting the following error: 我收到以下错误:
Error in paste("hour.dummy", i, sep = "") + paste("dummy.CDH", i, sep = "") : non-numeric argument to binary operator
So I'm not sure if it's the paste function that is not appropriate here, or if it's the loop. 所以我不确定粘贴功能是否不合适,或者是循环。 I can't seem to find a way to loop through column names easily in R.
我似乎找不到一种在R中轻松遍历列名的方法。
Any help is much appreciated! 任何帮助深表感谢!
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 我仅在第二行中放入了两个“虚拟”,但您应该明白
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.