[英]Error in as.vector(data) : no method for coercing this S4 class to a vector
I am trying to run a O-garch model, the code seem to be right and on mac it works, but when it is run on windows it doesn not work giving me the following error message:我正在尝试运行 O-garch model,代码似乎是正确的并且在 mac 上它可以工作,但是当它在 windows 上运行时它不起作用给我以下错误消息:
Error in as.vector(data): no method for coercing this S4 class to a vector as.vector(data) 中的错误:没有将此 S4 class 强制为向量的方法
seems that there is a problem with the loop.似乎循环有问题。 Thanks in advance.提前致谢。
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) ```
I had the exact same problem with the volatility line.我对波动线有完全相同的问题。 Apparently the fGarch library doesn't get along with the quantmod library.显然 fGarch 库与 quantmod 库不兼容。 Maybe try reseting RStidio and install all but quantmod library也许尝试重置 RStidio 并安装除 quantmod 库之外的所有库
That was the only way I got it to work这是我让它工作的唯一方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.