简体   繁体   中英

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:

Error in as.vector(data): no method for coercing this S4 class to a vector

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. Maybe try reseting RStidio and install all but quantmod library

That was the only way I got it to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM