R user function to run gmm model repeatedly using pgmm function

Viewed 66

I have created a function to run gmm model repeatedly using pgmm function from plm package. here is the code.

```
   run.gmm <- function(data,
              predictor,
              dep,
              controls,
              row.name = predictor,
              add.controls = NULL,
              rm.controls = NULL,
              caption = NULL, model, effect, transformation) {
     gmodel <-
              return.model.gmm(data,
                 predictor,
                 dep,
                 controls,
                 add.controls,
                 rm.controls, model, effect, transformation)
    if (Console == T) {
       print(summary(gmodel, robust = TRUE, time.dummies = TRUE))
     }
      invisible(gmodel)
      }

     return.model.gmm <- function(data, predictor, dep, controls, add.controls = NULL, 
                                  rm.controls = NULL, effect, model, mtransformation) {
                 controls <- controls[!controls %in% rm.controls]
                 controls <- paste("lag(", c(controls, add.controls), ",1)", collapse = " + ")
                 predictors <- paste(predictor, controls, sep = " + ")
                 formula <- paste(dep, predictors, sep = " ~ ")
                 gmmodel <- pgmm(formula, paste("|", controls), data = data, effect = effect, 
                             model = model, transformation = transformation)
                 return(gmmodel)
         } 
     ```

with above, now I plugged the arguments:

my.controls <- c("lnPDENS", "GDPGR", "LFSGR", 'GRRAT')
my.predictor = paste("lag(", c("LaPGrowth", "SRate"), ",1)", collapse = " + ")
my.effect <- c("twoways")
my.model <- c("twosteps")
my.transformation <- c("ld")

run.gmm(data=pdata.lbprt, dep="LaPGrowth", predictor = my.predictor, controls = my.controls, effect=my.effect, 
       model=my.model, transformation=my.transformation)

I get the following error:

Error in match.arg(effect) : 'arg' should be one of “twoways”, “individual”

Any idea, where my code is wrong. I believe the error can be detected even without sample data.

Greatly appreciate your help.

2 Answers

You mixed up the order of model and effect:

run.gmm

return.model.gmm(data,          #1
                 predictor,     #2
                 dep,           #3
                 controls,      #4
                 add.controls,  #5
                 rm.controls,   #6
                 model,         #7
                 effect,        #8
                 transformation #9
)

return.model.gmm

return.model.gmm <- function(
   data,                #1
   predictor,           #2
   dep,                 #3
   controls,            #4
   add.controls = NULL, #5
   rm.controls = NULL,  #6
   effect,              #7
   model,               #8
   mtransformation      #9
)

When you call return.model.gmm you pass effect as the 8th parameter, while when you define return.model.gmm ot os the 7th parameter.

Thus, calling the function return.model.gmm like this should do the trick:

return.model.gmm(data,          #1
                 predictor,     #2
                 dep,           #3
                 controls,      #4
                 add.controls,  #5
                 rm.controls,   #6
                 effect,        #7
                 model,         #8
                 transformation #9
)

Finally, I could solve the problem. It was really worth spending time. Here is the final code:

require(Formula)
require(magrittr)
require(plm)

run.gmm <- function(data,
                  predictor,
                  dep,
                  controls,
                  row.name = predictor,
                  add.controls = NULL,
                  rm.controls = NULL,
                  caption = NULL, effect, model, transformation) {
 gmodel <-
  return.model.gmm(data,
                     predictor,
                     dep,
                     controls,
                     add.controls,
                     rm.controls, effect, model, transformation)
 if (Console == T) {
  print(summary(gmodel, robust = TRUE, time.dummies = TRUE))
 }
 invisible(gmodel)
}

return.model.gmm <- function(data, predictor, dep, controls, add.controls = NULL, 
                         rm.controls = NULL, effect, model, mtransformation) {
  
             controls <- controls[!controls %in% rm.controls]
             controls <- paste("lag(", c(controls, add.controls), ",1)", collapse = " + ")
             predictors <- paste(predictor, paste(controls, sep = " + "), sep='+')
             formula <- paste(dep, predictors, sep = " ~ ")
             multipart <- paste(formula, "|", controls)
             multipartformula <- Formula::Formula(as.formula(multipart))
             gmmodel <- pgmm(multipartformula, data = data, effect = effect, model = 
                         model, transformation = transformation)
    
       return(gmmodel)
}

Notice the changes. I have used Formula function from Formula package to incorporate multipart formula object. Also, note the changes in 'predictors'. The function works perfectly.

Related