抑制警告消息并在r中继续循环
更新 ahmohamed的解决方案抑制警告消息并在r中继续循环,r,try-catch,R,Try Catch,更新 ahmohamed的解决方案 lapplyquote_数据为.null,因此您可以像在中按列表_清理qoute_数据一样清理列表。首先,您需要检查下载过程中是否发生错误。您可以在getPrice函数中完成此操作 getPrice<-function(ticker){ ticker<-paste(ticker,'ss',sep='.') price <- try(get.hist.quote(instrument=ticker,start='2013-01-
lapplyquote_数据为.null,因此您可以像在中按列表_清理qoute_数据一样清理列表。首先,您需要检查下载过程中是否发生错误。您可以在getPrice函数中完成此操作
getPrice<-function(ticker){
ticker<-paste(ticker,'ss',sep='.')
price <- try(get.hist.quote(instrument=ticker,start='2013-01-01',end='2015-02-14',quiet=TRUE))
if(class(price) == "try-error") #if error occurred
price <- NULL
return(price)
}
注意:我没有使用get_hist_quote函数的经验,但是如果每次调用它时它都会下载数据,那么您应该为列表中的所有股票代码调用getPrice函数一次,并将数据存储在一个变量中供以后使用。比如说,
screenStock<-function(list_in){
quote_data <- lapply(list_in, getPrice)
filtered_quote_data <- quote_data[-which(lapply(quote_data,is.null))]
for (i in 1:(length(filtered_quote_data)-1)){
s1<-filtered_quote_data[i]
for (j in 2:length(filtered_quote_data)){
s2<-filtered_quote_data[j]
# Continue your code.
谢谢你,这比我的原始代码有了很大的改进。但是它仍然不能避免一些错误。屏幕停止在列表中第一个元素之后的。我已经知道HSlist中的000002将在GetPrice中返回错误。这不是编程问题。出现错误是因为url不存在。即使在浏览器中粘贴也会出现错误。你是对的。该列表包含一些不再存在的代码,即,这是一个“脏”列表。有没有办法确定和清理清单?也就是说,识别“不存在错误类型”,然后从列表中删除相应的ticker。脏ticker在上述代码中给出,其中applyquote_数据为.null,因此您可以像我们在中通过列表清理qoute_数据一样清理列表
getPrice<-function(ticker){
ticker<-paste(ticker,'ss',sep='.')
tryCatch({price<-get.hist.quote(instrument=ticker,start='2013-01-01',end='2015-02-14',quiet=TRUE)})
return(price)
}
tts<-function(s1,s2){
require(fUnitRoots)
a<-getPrice(s1)[,4]
b<-getPrice(s2)[,4]
if (length(a)==length(b)){
fit<-lm(a~b+0)
result<-suppressWarnings(adfTest(fit$residuals,type='nc'))
return(result)
}
else return(NULL)
}
screenStock<-function(list_in){
result<-data.frame()
n<-0
for (i in 1:(length(list_in)-1)){
s1<-getPrice(list_in[i])
for (j in 2:length(list_in)){
s2<-getPrice(list_in[j])
adf_T<-tts(s1,s2)
if (!is.null(adf_T) && adf_T@test$p.value==0.01){
n<-n+1
result[n,1]<-list_in[i]
result[n,2]<-list_in[j]
result[n,3]<-adf_T@test$statistic}
}
}
return(result)
}
> screenStock(HSlist)
download error, retrying ...
download error, retrying ...
download error, retrying ...
download error, retrying ...
Error in get.hist.quote(instrument = ticker, start = "2013-01-01", end = "2015-02-14", :
cannot open URL 'http://chart.yahoo.com/table.csv?s=c("000001", "000002", "000004", "000005", "000006", "000007", "000008", "000009", "000010", "000011", "000012", "000014", "000016", "000017", "000018", "000019", "000020", "000021", "000022", "000023", "000024", "000025", "000026", "000027", "000028", "000029", "000030", "000031", "000032", "000033", "000034", "000035", "000036", "000037", "000039", "000040", "000042", "000043", "000045", "000046", "000048", "000049", "000050", "000055", "000056", "000058", "000059", "000060", "000061", "000062",
"000063", "000065", "000066", "000068", "000655", "000656", "000659", "000661", "000662", "000663", "000665", "000666", "000667", "000668", "000669", "000671", "000673", "000676", "000677", "000678", "000679", "000680", "000682", "000683", "000685", "000686", "000687", "000690", "000691", "000692", "000695", "000697", "000698", "000700", "000701", "000702", "000703", "000705", "000707", "000708", "000709", "000710", "000711", "0007
In addition: Warning messages:
1: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
2: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
3: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
4: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
5: In download.file(url, destfile, method = method, quiet = quiet) :
cannot open: HTTP status was '0 (nil)'
getPrice<-function(ticker){
ticker<-paste(ticker,'ss',sep='.')
price <- try(get.hist.quote(instrument=ticker,start='2013-01-01',end='2015-02-14',quiet=TRUE))
if(class(price) == "try-error") #if error occurred
price <- NULL
return(price)
}
if (!is.null(s1) && !is.null(s2) && (length(a)==length(b))){
# Do your test
}
screenStock<-function(list_in){
quote_data <- lapply(list_in, getPrice)
filtered_quote_data <- quote_data[-which(lapply(quote_data,is.null))]
for (i in 1:(length(filtered_quote_data)-1)){
s1<-filtered_quote_data[i]
for (j in 2:length(filtered_quote_data)){
s2<-filtered_quote_data[j]
# Continue your code.