You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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, pgmm requires you to separate endogenous/predetermined variables from their instruments using a | character. For example, a valid dynamic panel formula looks like this:
    # 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"
    )
    
    If you’re skipping the | separator or misstructuring lagged variables, pgmm can’t parse the formula correctly, triggering the terms error.
  • Ensure your data is a pdata.frame: plm functions rely on properly formatted panel data with explicit individual and time indices. If you haven’t converted your data yet, do this first:
    library(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"))
    
    Without this, the 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, and summary(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 use lag(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:

  1. Start with a minimal working model: Strip your formula down to the basics to confirm pgmm works with your data:
    minimal_model <- pgmm(
      y ~ lag(y, 1) | lag(y, 2:5),
      data = your_panel_data,
      model = "twostep"
    )
    
    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.
  2. Update plm: Older versions of the package have had formula parsing bugs. Run install.packages("plm") to get the latest version and re-test.
  3. Check for duplicate indices: If your panel has duplicate (individual, time) pairs, pdata.frame can’t properly calculate lags. Use is.pbalanced(your_panel_data) to verify balance, and fix duplicates if needed.

内容的提问来源于stack exchange,提问作者J.Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:16:42