我想从估计的var模型的输出中提取系数和方差 - 协方差矩阵(用vars包估算)。有没有办法将系数保存到数组中,并将var-cov矩阵保存到矩阵中,以便以后可以从中提取某些数字并用作后续函数的输入(这是我的最终目标)。
这是一个模拟估计和结果。
sale1_ts <- ts(sale1, frequency=12, start=c(2012,1))
sale2_ts <- ts(sale2, frequency=12, start=c(2012,1))
#VARMODEL
varendo<-data.frame(sale1_ts,sale2_ts)
names(varendo)<-c("sale1_ts","sale2_ts")
attach(varendo)
library(vars)
fitvar<- VAR(varendo, type = c("both"), season = NULL, lag.max = 12,ic = c("AIC"))
summary(fitvar)
这导致以下输出
Endogenous variables: sale1_ts, sale2_ts
Deterministic variables: both
Sample size: 21
Log Likelihood: -43.463
Roots of the characteristic polynomial:
1.19 1.06 1.06 0.961 0.961 0.96 0.96 0.914 0.914 0.883 0.883 0.84 0.84 0.75
Call:
VAR(y = varendo, type = c("both"), lag.max = 12, ic = c("AIC"))
Estimation results for equation sale1_ts:
=========================================
sale1_ts = sale1_ts.l1 + sale2_ts.l1 + sale1_ts.l2 + sale2_ts.l2 + sale1_ts.l3 + sale2_ts.l3 + sale1_ts.l4 + sale2_ts.l4 + sale1_ts.l5 + sale2_ts.l5 + sale1_ts.l6 + sale2_ts.l6 + sale1_ts.l7 + sale2_ts.l7 + const + trend
Estimate Std. Error t value Pr(>|t|)
sale1_ts.l1 -0.0576 0.3603 -0.16 0.88
sale2_ts.l1 -0.0384 0.3538 -0.11 0.92
sale1_ts.l2 -0.4768 0.3933 -1.21 0.28
sale2_ts.l2 0.1963 0.3137 0.63 0.56
sale1_ts.l3 -0.0837 0.3245 -0.26 0.81
sale2_ts.l3 -0.2097 0.6108 -0.34 0.75
sale1_ts.l4 -0.5665 0.3653 -1.55 0.18
sale2_ts.l4 -0.3511 0.5174 -0.68 0.53
sale1_ts.l5 -0.1479 0.4199 -0.35 0.74
sale2_ts.l5 0.1263 0.4640 0.27 0.80
sale1_ts.l6 -0.3143 0.3684 -0.85 0.43
sale2_ts.l6 0.1174 0.2700 0.43 0.68
sale1_ts.l7 -0.4727 0.4810 -0.98 0.37
sale2_ts.l7 0.3255 0.3071 1.06 0.34
const 12.5453 13.4200 0.93 0.39
trend 0.1062 0.0759 1.40 0.22
Residual standard error: 1.5 on 5 degrees of freedom
Multiple R-Squared: 0.741, Adjusted R-squared: -0.0376
F-statistic: 0.952 on 15 and 5 DF, p-value: 0.575
Estimation results for equation sale2_ts:
=========================================
sale2_ts = sale1_ts.l1 + sale2_ts.l1 + sale1_ts.l2 + sale2_ts.l2 + sale1_ts.l3 + sale2_ts.l3 + sale1_ts.l4 + sale2_ts.l4 + sale1_ts.l5 + sale2_ts.l5 + sale1_ts.l6 + sale2_ts.l6 + sale1_ts.l7 + sale2_ts.l7 + const + trend
Estimate Std. Error t value Pr(>|t|)
sale1_ts.l1 -0.7135 0.3899 -1.83 0.127
sale2_ts.l1 -0.0598 0.3829 -0.16 0.882
sale1_ts.l2 -0.4054 0.4257 -0.95 0.385
sale2_ts.l2 -0.0300 0.3395 -0.09 0.933
sale1_ts.l3 -0.7129 0.3512 -2.03 0.098 .
sale2_ts.l3 0.6945 0.6611 1.05 0.342
sale1_ts.l4 -0.7165 0.3954 -1.81 0.130
sale2_ts.l4 0.4709 0.5600 0.84 0.439
sale1_ts.l5 0.1324 0.4545 0.29 0.783
sale2_ts.l5 0.7127 0.5022 1.42 0.215
sale1_ts.l6 0.2318 0.3987 0.58 0.586
sale2_ts.l6 -0.0859 0.2922 -0.29 0.781
sale1_ts.l7 0.4826 0.5206 0.93 0.397
sale2_ts.l7 0.5011 0.3324 1.51 0.192
const -0.6180 14.5252 -0.04 0.968
trend 0.1744 0.0822 2.12 0.087 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.6 on 5 degrees of freedom
Multiple R-Squared: 0.819, Adjusted R-squared: 0.276
F-statistic: 1.51 on 15 and 5 DF, p-value: 0.343
Covariance matrix of residuals:
sale1_ts sale2_ts
sale1_ts 2.14 -1.25
sale2_ts -1.25 2.51
Correlation matrix of residuals:
sale1_ts sale2_ts
sale1_ts 1.000 -0.541
sale2_ts -0.541 1.000
我尝试过print()和coefficient(),但这会让我产生奇怪的结果。例如。
test<-array(coefficients(fitvar), c(16,4,2))
> test
, , 1
[,1] [,2] [,3] [,4]
[1,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[2,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[3,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[4,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[5,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[6,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[7,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[8,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[9,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[10,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[11,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[12,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[13,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[14,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[15,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[16,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
, , 2
[,1] [,2] [,3] [,4]
[1,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[2,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[3,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[4,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[5,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[6,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[7,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[8,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[9,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[10,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[11,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[12,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[13,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[14,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[15,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
[16,] Numeric,64 Numeric,64 Numeric,64 Numeric,64
答案 0 :(得分:1)
要访问系数,您需要运行
fitvar$varresult$sale1_ts$coefficients
给出了命名向量,例如
> fitvar$varresult$sale1_ts$coefficients
sale1_ts.l1 sale2_ts.l1 const trend
-0.1799802 -0.1048722 -0.3722163 1.2865624
访问协方差/关联使用
summary(fitvar)$covres
summary(fitvar)$corres
给予matrix
对象:
> summary(fitvar)$covres
sale1_ts sale2_ts
sale1_ts 0.7955669 -0.1140167
sale2_ts -0.1140167 0.7013895
> summary(fitvar)$corres
sale1_ts sale2_ts
sale1_ts 1.0000000 -0.1526337
sale2_ts -0.1526337 1.0000000