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