Question

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. 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'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.

Any help is much appreciated!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top