如何获取YieldCurve包估计的Nelson Siegel系数标准误
Great question! The Nelson.Siegel() function in the YieldCurve package outputs estimated beta parameters (β₀, β₁, β₂) and λ, but it doesn’t directly provide their standard errors. To calculate these, you can fit the Nelson-Siegel model using nonlinear least squares (NLS) directly in R—this gives you access to the full model summary, including standard errors.
Step-by-Step Solution
First, let’s start with your existing code to get initial parameter estimates (we’ll use these to help the NLS model converge faster):
library("YieldCurve") library(dplyr) # For the `first()` function data(FedYieldCurve) maturity.Fed <- c(3/12, 0.5, 1, 2, 3, 5, 7, 10) NSParameters <- Nelson.Siegel(rate = first(FedYieldCurve, '10 month'), maturity = maturity.Fed)
Next, define the Nelson-Siegel model as a function—this is what we’ll use in the NLS fit:
# Define the Nelson-Siegel yield curve formula nelson_siegel <- function(maturity, b0, b1, b2, lambda) { b0 + b1 * (1 - exp(-maturity/lambda))/(maturity/lambda) + b2 * ((1 - exp(-maturity/lambda))/(maturity/lambda) - exp(-maturity/lambda)) }
Now, let’s fit the model for a single month (we’ll use the first month of your 10-month sample as an example). We’ll use the parameters from Nelson.Siegel() as starting values for the NLS fit to ensure convergence:
# Extract yields for the first month first_month_yields <- as.numeric(first(FedYieldCurve, '10 month')[1, ]) # Fit the NLS model nls_fit <- nls( formula = first_month_yields ~ nelson_siegel(maturity.Fed, b0, b1, b2, lambda), start = list( b0 = NSParameters[1, "beta0"], b1 = NSParameters[1, "beta1"], b2 = NSParameters[1, "beta2"], lambda = NSParameters[1, "lambda"] ) )
Finally, view the model summary—this will include standard errors for each parameter:
# Print the full summary (look for the "Std. Error" column) summary(nls_fit)
What You’ll See
The summary output will have a Coefficients section that looks like this (example values):
Estimate Std. Error t value Pr(>|t|) b0 2.50123 0.04567 54.77 1.28e-08 *** b1 -0.23456 0.02134 -10.99 0.00012 *** b2 0.11234 0.01567 7.17 0.00087 *** lambda 1.89012 0.23456 8.06 0.00054 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
The Std. Error column gives you the standard error for each parameter (e.g., the standard error for β₀ is 0.04567 in this example).
Batch Processing for All 10 Months
If you want standard errors for every month in your 10-month sample, you can wrap this in a loop or use purrr::map():
# Load purrr for easier iteration library(purrr) # Function to get SEs for a single row of yield data get_ns_se <- function(yield_row, maturity, start_params) { fit <- nls( as.numeric(yield_row) ~ nelson_siegel(maturity, b0, b1, b2, lambda), start = list( b0 = start_params["beta0"], b1 = start_params["beta1"], b2 = start_params["beta2"], lambda = start_params["lambda"] ) ) # Extract standard errors from the summary coef(summary(fit))[, "Std. Error"] } # Apply the function to each month all_se <- map2_df( .x = split(first(FedYieldCurve, '10 month'), seq(nrow(first(FedYieldCurve, '10 month')))), .y = split(NSParameters, seq(nrow(NSParameters))), ~ get_ns_se(.x, maturity.Fed, .y) ) # View the results all_se
This will give you a data frame where each row is a month, and each column is the standard error for a parameter.
内容的提问来源于stack exchange,提问作者Roy

