1

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 Answers2

0

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
)
thothal
  • 16,690
  • 3
  • 36
  • 71
  • Thanks for spotting this. I didn't realize order of parameters do have effect. I have corrected this but now new error is showing. this is ``` Error in Formula(formula) : inherits(object, "formula") is not TRUE ``` Checking error line shows it is in this line of codes: ``` gmmodel <- pgmm(formula, paste("|", controls), data = data, effect = effect, model = model, transformation = transformation) ``` – Abdullah Mamun Jun 14 '22 at 12:17
  • Got you. Now new error is showing Error in x$terms %||% attr(x, "terms") %||% stop("no terms component nor attribute") : no terms component nor attribute I was concerned about how to introduce multi-part formula like the following: pgmm(LaPGrowth ~ lag(LaPGrowth,1)+ lag(SRate, 1) +lag(lnPDENS,1)+lag(GDPGR,1)+lag(LFSGR,1)+lag(GRRAT,1) | lag(lnPDENS, 1) + lag(GDPGR,1)+lag(LFSGR,1)+lag(GRRAT,1), data = pdata.lbprt, effect = "twoways", model = "twosteps", transformation = "ld") The above is if you run gmm without creating user function. – Abdullah Mamun Jun 14 '22 at 12:28
  • Please provide a proper [reprex] of your problem and also consider [this article on what to do if somebody answers your question](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – thothal Jun 14 '22 at 14:27
  • I think the problem now boils down to construct a multi-part formula object, something using Formula. Here is an useful article https://cran.r-project.org/web/packages/Formula/vignettes/Formula.pdf – Abdullah Mamun Jun 14 '22 at 14:39
0

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.