2

我是 R 新手,刚刚开始使用它。我目前正在尝试使用 quantmod 包。

quantmod 包似乎完成了我想做的大部分工作,但是,我不想使用 getSymbols() 函数将数据提取到 R 中。相反,我想使用我自己的数据 - 作为 csv 文件存储在我的本地磁盘上.

我希望能够从我的 CSV 文件中提取数据以用于 quantmod。我来到了这篇文章,它展示了如何读取 CSV 文件以与 quantmod 一起使用,但我不喜欢它至少有两个原因:

  1. 它在加载到 quantmod 之前将一个新的(重新格式化的)CSV 文件写入磁盘。我宁愿在内存中做任何必要的修改,使用 R。

  2. CSV 文件具有列标题。我的数据没有列标题。相反,这些字段位于预定的固定列位置(与 Yahoo Finance 数据表采用的“标准”格式相匹配)。

我还没有设法计算出getSymbols()函数返回的数据类型。我希望它返回一个数据框,但是当我检查它的类时,它被识别为一个字符向量 - 我发现这非常令人惊讶(坦率地说,不相信,因为我能够从包含的数据中绘制一个条形图在变量中):

yhoo <- getSymbols("YHOO",src="google")
class(yhoo)
[1] "character"
> yhoo
[1] "YHOO"

如果有人能展示如何编写一个小的 R 函数(很可能是 read.csv 的包装器),它将从我的 CSV 文件中读取数据并将其作为 R 对象(数据框?)返回以供 quantmod 使用,我将不胜感激。

这是一些解释我想要做的伪代码:

# in case I need some funcs here for creating data type returned by function
library(quantmod) 

loadCSVDataFile <- function(full_pathname){
    csvdata <- read.csv(full_pathname, header=FALSE,sep=",")
    dates <- csvdata[,1]
    op <- csvdata[,2]
    hi <- csvdata[,3]
    lo <- csvdata[,4]
    cl <- csvdata[,5]
    vol <- csvdata[,6]
    oi <- csvdata[,7]

    # Now combine columns into a data type that matches that returned by the
    # getSymbols() ....
    # return(dataset)
}

[[更新]]

使用迄今为止给出的答案,我没有设法让它工作......:

> gbpusd <- as.xts(read.zoo('/path/to/gbpusd.csv', header=FALSE))
> class (gbpusd)
[1] "xts" "zoo"
> barChart(gbpusd)
Error in `[.xts`(x, xsubset) : subscript out of bounds

> gbpusd2 <- getSymbols.csv('gbpusd',,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> gbpusd2 <- getSymbols.csv('gbpusd',.GlobalEnv,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> 
> gbpusd2 <- getSymbols.csv('gbpusd','.GlobalEnv','/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> gbpusd2 <- getSymbols.csv('gbpusd',env,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments

我究竟做错了什么?

4

3 回答 3

4

The common idiom is:

yhoo = as.xts(read.zoo("yhoo.csv",header=T))

If you want to use a quantmod function, then you can tell getSymbols() to use a csv file. See http://www.quantmod.com/documentation/getSymbols.csv.html I.e.

getSymbols('yhoo',src='csv')

(I've followed your lowercase convention, but remember the filenames will be case-sensitive; directory defaults to current directory and can be specified explicitly with the dir parameter, see ?getSymbols.csv)

于 2012-01-23T12:16:42.170 回答
4

I can make it work, but you have to determine which parameters are needed for your setup.

library(quantmod)

# create sample data
getSymbols("SPY")
write.zoo(SPY, file="SPY.csv", sep=",")

# set symbol lookup
setSymbolLookup(SPY=list(src="csv",format="%Y-%m-%d"))
# call getSymbols(.csv) with auto.assign=FALSE
spy <- getSymbols("SPY", auto.assign=FALSE)
barChart(spy)
于 2012-01-23T20:49:49.697 回答
1

如果您在代码中包含 ls(),您会发现您遗漏了一个变量:

yhoo <- getSymbols("YHOO", src = "google")
ls()
# [1] "yhoo" "YHOO"
class(yhoo)
# [1] "character"
class(YHOO)
# [1] "xts" "zoo"

因此 getSymbols() 创建了 2 个变量:字符向量“yhoo”和“YHOO”,这是扩展zoo类的可扩展时间序列 ( xts )类的对象,用于存储不规则的时间序列数据。

要查看文档,请使用:

?xts # or ?zoo

特别是,xts的文档描述了as.xts()函数,用于从矩阵转换为 xts。从那里使用read.csvread.table读取您自己的 CSV 文件并转换为xts对象应该很简单。

于 2012-01-23T12:02:05.737 回答