0

I have a database that has several factors. I want to iterate a regression over each factor for each variable, but for the whole database. Something like this:

for (i in unique(db$Product)) {
 for (j in unique(db$Super)) {
   for (c in unique(db$Category)) {
     db$prueba <- lm(prueba ~ 0 + as.factor(Super)[j] * 
            (as.factor(Time) + as.factor(Product)[i]) 
          +  as.factor(Category)[c] * as.factor(Time)
          , db)$residuals
   }
 }
} 

Three of the previous variables are to be treated like factors: Category, Super, and Product. I want to pick just one of them in each iteration. That is why I try to call each time Super[j] in the previous example. If I use just as.factor(Category), then R will create one dummy variable for each value in Category, I want to pick just one per iteration.

When I run the previous code I have the following error:

Error in model.frame.default(formula = prueba ~ 0 + as.factor(Super)[j] * : variable lengths differ (found for 'as.factor(Super)[j]')

Any help will be appreciated.

1 Answers1

0

I think a solution could be the following:

for (i in unique(db$Product)) {
  for (j in unique(db$Super)) {
    for (c in unique(db$Category)) {
      db$prueba <- lm(prueba ~ 0 + I(Super == j) * 
            (Time + I(Product == i))
          +  I(Category == c) * Time
          , db)$residuals
   }
 }
}

In this way, the structure of the database is not changed. Hope it is useful.