使用R中plm包pgmm函数时遇terms.default报错的求助
Hey there! Let's work through your Arellano-Bond/GMM implementation issues with plm::pgmm—I’ve hit similar roadblocks before, so let’s break this down step by step.
1. Fixing the Error in terms.default(formula)
This error almost always boils down to formula syntax issues specific to pgmm or incorrect panel data formatting. Here’s what to check first:
- You must use
pgmm’s formula structure: Unlike standard linear models,pgmmrequires you to separate endogenous/predetermined variables from their instruments using a|character. For example, a valid dynamic panel formula looks like this:
If you’re skipping the# Basic Arellano-Bond setup: lagged DV as endogenous, lagged further back as instruments model <- pgmm( y ~ lag(y, 1) + x1 + lag(x2, 1) | lag(y, 2:5) + lag(x1, 1:2), data = your_panel_data, model = "twostep" )|separator or misstructuring lagged variables,pgmmcan’t parse the formula correctly, triggering the terms error. - Ensure your data is a
pdata.frame:plmfunctions rely on properly formatted panel data with explicit individual and time indices. If you haven’t converted your data yet, do this first:
Without this, thelibrary(plm) # Replace "firm_id" and "year" with your actual individual/time columns your_panel_data <- pdata.frame(your_raw_data, index = c("firm_id", "year"))lag()function won’t calculate panel-specific lags, which breaks formula parsing. - Rule out variable type issues: Double-check that all variables in your formula are numeric. If you accidentally included a character/factor variable, or have hidden missing values (even if you think they’re 0s), this can confuse the formula parser. Use
str(your_panel_data)to confirm variable types, andsummary(your_panel_data)to spot anomalies.
As for your question about 0 values: They rarely cause this specific formula error on their own. But if those 0s are placeholders for missing data (instead of true 0 observations), that could introduce hidden NAs that break the model.
2. Why lag(y, 0:1) instead of lag(y, 1)?
The 0:1 argument in lag() tells plm to include both the current period (lag 0) and lagged 1-period values of the variable. Here’s the context for this in Arellano-Bond models:
- For predetermined variables (like your macroprudential/monetary policy variables), you might want to include both their immediate (current period) effect and their lagged effect on the outcome.
- In
pgmm, when you uselag(x, 0:1)as a regressor, you’ll typically pair it with earlier lags (e.g.,lag(x, 2:...)) as instruments—since current-period predetermined variables might be correlated with the error term, but their lags further back are exogenous.
For example, if you’re modeling credit growth (y) against a macroprudential tool (mp), writing lag(mp, 0:1) lets you test both the immediate impact of this period’s policy and the carryover effect from last period’s policy. Using lag(mp, 1) would only test the lagged effect.
3. Troubleshooting Steps to Test
Let’s narrow down the issue with incremental tests:
- Start with a minimal working model: Strip your formula down to the basics to confirm
pgmmworks with your data:
If this runs, add variables one by one (e.g., first a single lagged regressor, then current-period variables) to find which part triggers the error.minimal_model <- pgmm( y ~ lag(y, 1) | lag(y, 2:5), data = your_panel_data, model = "twostep" ) - Update
plm: Older versions of the package have had formula parsing bugs. Runinstall.packages("plm")to get the latest version and re-test. - Check for duplicate indices: If your panel has duplicate (individual, time) pairs,
pdata.framecan’t properly calculate lags. Useis.pbalanced(your_panel_data)to verify balance, and fix duplicates if needed.
内容的提问来源于stack exchange,提问作者J.Martin

