r - 如何使用 R 中的“应用”为时间序列数据绘制 acf 函数
问题描述
我有时间序列数据。我想应用acf
函数并绘制所有列的结果。我还想让情节自动为每个单变量系列生成标题。
我尝试了以下方法:
library(quantmod)
library(TSclust)
library(ggplot2)
library(tsibble)
library(vcd)
## Prepare the data
symbols = c('AZN', 'BP', 'AAPL')
## Start and end time
start = as.Date("2015-01-05")
until = as.Date("2015-12-31")
stocks.Cl = lapply(symbols, function(symbol) {
Close = getSymbols(symbol,src='yahoo', from = start, to = until, auto.assign = FALSE)[, 4]
names(Close) = symbol
Close
})
stocks.Cl = do.call(merge.xts,stocks.Cl)
acf.dat <- apply(stocks.Cl, MARGIN= 2, FUN = plot(acf))
但我得到以下信息:
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' did not evaluate to an object of length 'n'
解决方案
您需要使用匿名函数将附加函数应用于传递的对象。而不是apply
withMARGIN = 2
我们使用lapply
并遍历names
ofstocks.Cl
以用作情节的标题。
lapply(names(stocks.Cl), function(i) plot(acf(stocks.Cl[, i]), main = i))
推荐阅读
- firebase - 如何在没有 StreamBuilder 的情况下从 Firestore 检索实时日期
- json - Python requests.get 与 Session.get
- qt - QML VirtualKeyboard 即将出现在 Popup 之上
- django - Django Rest - 发布数据在外键引用表中创建新行
- batch-file - 批处理:如果源文件夹上的文件名包含 x,则移动到 x 目标文件夹
- google-cloud-platform - Firestore 云函数 http 请求
- javascript - 承诺不会在循环内返回
- python - 如何使用 Python 从两个绘制的图表中找到 RMSE?
- c++ - 为什么在成员初始化器列表中初始化数组时不需要括号?
- android - 如何在 Android Studio 中为图像按钮添加渐变颜色,中心颜色为最大