R语言中MSGARCH包如何像rugarch设定均值方程外部回归变量?
Great question! Moving from rugarch to MSGARCH (for regime-switching or standard GARCH models) means adjusting to a slightly different specification structure, but adding external regressors to the mean equation is straightforward once you know where to look.
Key Difference from rugarch
In rugarch, you specify mean equation regressors inside the mean.model$external.regressors argument of ugarchspec(). In MSGARCH, the equivalent setting lives within the mean.spec list passed to the CreateSpec() function—this is the core spot to target.
Step-by-Step Implementation
Here’s a concrete, reproducible example to walk you through the process:
Load the package and prepare your data
First, loadMSGARCHand create your time series returns + external regressor matrix (each column is a separate regressor, and rows must match the length of your returns data):library(MSGARCH) # Replace with your actual returns data returns <- rnorm(100) # Create external regressors (2 regressors for 100 observations) external_regs <- matrix(rnorm(100 * 2), ncol = 2, dimnames = list(NULL, c("macro_reg", "firm_reg")))Define the MSGARCH specification
UseCreateSpec()to build your model, and plug in the external regressors directly in themean.speclist:spec <- CreateSpec( # Match your sGARCH(1,1) variance setup from rugarch variance.spec = list(model = "sGARCH", garchOrder = c(1, 1)), # Mean model with ARMA(1,1) + external regressors mean.spec = list( model = "ARMA", armaOrder = c(1, 1), include.mean = TRUE, # Keeps the intercept in the mean equation external.regressors = external_regs # Your regressors go here ), # Match your normal distribution setting distribution.spec = list(distribution = "norm"), # Disable regime switching if you don't need it (set to TRUE for multi-regime models) switch.spec = list(do.mix = FALSE) )Fit the model and inspect results
Estimate the model withFitML()and check the summary to confirm your regressor coefficients are included in the mean equation output:fit <- FitML(spec = spec, data = returns) summary(fit)
Critical Details to Keep in Mind
- Regressor Format:
external.regressorsrequires a matrix (not a data frame or vector). Double-check that the number of rows exactly matches your returns data length to avoid errors. - Regime Switching Support: If you’re building a multi-regime model (
switch.spec = list(do.mix = TRUE)), use theswitch.meanparameter inswitch.specto control whether regressor coefficients vary across regimes: setswitch.mean = TRUEfor regime-specific coefficients, orFALSEto share coefficients across regimes. - Mean Equation Flexibility: If you set
include.mean = FALSE, the mean equation will omit the intercept and only include ARMA terms + external regressors.
内容的提问来源于stack exchange,提问作者Markoff Chainz

