如何在R语言中DCC-GARCH估计后计算年度平均相关系数?
Hey there! Let's work through how to compute annual average correlation coefficients from your fitted DCC-GARCH model. I'll break this down into clear, reproducible steps:
First, let's finish the model specification and fitting code (your original snippet was missing the full distribution parameter and model fitting call):
library(rugarch) library(rmgarch) library(lubridate) # For date handling library(dplyr) # For data grouping/summarizing # Load and prep data data(dji30retw) Dat = dji30retw[, 1:8, drop = FALSE] # Define univariate GARCH spec uspec = ugarchspec(mean.model = list(armaOrder = c(0,0)), variance.model = list(garchOrder = c(1,1), model = "eGARCH"), distribution.model = "norm") # Define and fit DCC-GARCH spec spec1 = dccspec(uspec = multispec(replicate(8, uspec)), dccOrder = c(1,1), distribution = "mvnorm") # Common multivariate normal distribution fit1 = dccfit(spec1, data = Dat) # Fit the model to your data
Use the rcor() function to pull the time-series of dynamic correlation matrices from your fitted model:
# Extract dynamic correlations (3D array: [time point, asset i, asset j]) dyn_corrs = rcor(fit1)
We'll pair the correlation data with the corresponding dates, then group by year to compute averages. There are two common ways to structure the output:
Option 1: Tidy Format (Pairwise Annual Averages)
This gives you a clean data frame showing the average correlation for every asset pair, per year:
# Convert 3D correlation array to a long-format data frame corr_df = as.data.frame.table(dyn_corrs) %>% rename(Date = Var1, Asset1 = Var2, Asset2 = Var3, Correlation = Freq) %>% mutate(Year = year(ymd(as.character(Date)))) # Extract year from date # Compute annual average correlations for each asset pair annual_avg_corrs = corr_df %>% group_by(Year, Asset1, Asset2) %>% summarise(Average_Correlation = mean(Correlation, na.rm = TRUE), .groups = "drop") # Preview the result head(annual_avg_corrs)
Option 2: Annual Average Correlation Matrices
If you want a full correlation matrix for each year (matching the structure of your dynamic correlations), use this:
# Extract year labels from your data's row names dates = rownames(Dat) years = unique(year(ymd(dates))) # Generate a list of annual average correlation matrices annual_corr_matrices = lapply(years, function(y) { # Filter time points for the current year time_idx = which(year(ymd(dates)) == y) # Compute the mean across all correlation matrices in the year apply(dyn_corrs[time_idx, , ], 2:3, mean, na.rm = TRUE) }) # Name the list elements with their corresponding years names(annual_corr_matrices) = years # Example: View the 2000 annual average correlation matrix print(annual_corr_matrices[["2000"]])
- If your date format isn't standard, adjust the
ymd()function todmy()ormdy()to correctly parse dates. - Use
na.rm = TRUEto ignore any missing values that might arise from model fitting or date parsing. - This logic works regardless of the multivariate distribution you use (e.g.,
mvstdfor Student's t-distribution)—just update thedistributionparameter indccspec()as needed.
内容的提问来源于stack exchange,提问作者FrankUnderwood268

