![](/img/trans.png)
[英]Error in as.vector(x) : no method for coercing this S4 class to a vector
[英]Error in as.vector(data) : no method for coercing this S4 class to a vector
我正在嘗試運行 O-garch model,代碼似乎是正確的並且在 mac 上它可以工作,但是當它在 windows 上運行時它不起作用給我以下錯誤消息:
as.vector(data) 中的錯誤:沒有將此 S4 class 強制為向量的方法
似乎循環有問題。 提前致謝。
graphics.off() # clean up graphic window
#install.packages("fGarch")
library(rmgarch)
library(tseries)
library(stats)
library(fGarch)
library(rugarch)
library(quantmod)
getSymbols(Symbols = c('PG','CVX','CSCO'),from="2005-01-01", to="2020-04-17",
env=parent.frame(),
reload.Symbols = FALSE,
verbose = FALSE,
warnings = TRUE,
src="yahoo",
symbol.lookup = TRUE,
auto.assign = getOption('getSymbols.auto.assign', TRUE))
Pt=cbind(PG$PG.Adjusted,CVX$CVX.Adjusted,CSCO$CSCO.Adjusted)
rt = 100 * diff(log(Pt))
rt=na.omit(rt)
rm(CSCO,CVX,PG)
rt_ts=ts(rt)
n=nrow(rt_ts)
N=ncol(rt_ts)
#O-GARCH:
Sigma = cov(rt_ts); # Covariance matrix
P = cor(rt_ts) # correlation matrix
# spectral decomposition
SpectralDec = eigen(Sigma, symmetric=TRUE)
V = SpectralDec$vectors # eigenvector matrix
V
lambda = SpectralDec$values # eigenvalues
lambda
Lambda = diag(lambda) # Eigenvalues on the diagonal
print(Sigma - V %*% Lambda %*% t(V), digits = 3) # Sigma - V Lambda V' = 0
print(V %*% t(V), digits = 3) # V'V = I
print(t(V) %*% V, digits = 3) # VV' = I
f = ts(as.matrix(rt_ts) %*% V);
cov(f) # diagonal matrix with lambda on the diagonal
ht.f = matrix(0, n, N)
for (i in 1:N)
{
fit = garchFit(~ garch(1,1), data =f[, i], trace = FALSE);
summary(fit);
ht = volatility(fit, type = "h");
ht.f[, i] = ht;
}
ht.f=ts(ht.f) ```
我對波動線有完全相同的問題。 顯然 fGarch 庫與 quantmod 庫不兼容。 也許嘗試重置 RStidio 並安裝除 quantmod 庫之外的所有庫
這是我讓它工作的唯一方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.