###前提・実現したいこと
銘柄コード一覧ファイル(StockCode.txt)を順に読み込み,価格時系列,価格変化率時系列,価格変化率の確率密度関数の図を作成し保存するスクリプトを作成したいです。
###発生している問題・エラーメッセージ
価格時系列,価格変化率時系列は描けましたが、価格変化率の確率密度関数はエラーが出てしまい描くことができません。
エラーメッセージ
R
1以下にエラー plot.window(...) : need finite 'xlim' values 24 plot.window(...) 33 localWindow(xlim, ylim, log, asp, ...) 42 plot.default(data_x$date_s, data_x$closing_adjusted, main = paste("Stock price(", 5 as.character(x[1, 1]), ")", sep = ""), xlab = "Time [date]", 6 ylab = "ClosingPrice(Adjusted)", type = "l") 71 plot(data_x$date_s, data_x$closing_adjusted, main = paste("Stock price(", 8 as.character(x[1, 1]), ")", sep = ""), xlab = "Time [date]", 9 ylab = "ClosingPrice(Adjusted)", type = "l") 10 追加情報: 警告メッセージ: 111: In min(x) : min の引数に有限な値がありません: Inf を返します 122: In max(x) : max の引数に有限な値がありません: -Inf を返します
###該当のソースコード
R
1setwd("/home/user/bootcamp") 2for (i in 1:5){ 3 x <- read.table("StockCode.txt", skip = i-1, nrows = 1) #テキストファイルから銘柄コードを順次読み込む 4 file_title <- paste("data", as.character(x[1,1]), ".csv", sep = "") 5 data_x <- read.csv(file_title) #読み込んだ銘柄コードの株価データが入ったcsvファイルを読み込む 6 data_x$closing_adjusted<-round(data_x$closing_adjusted,digits=3) #丸め込み 7 data_x <- data_x[order(data_x$date_s),] #日付(date_s)によるソート(昇順) 8 data_x$date_s <- as.Date(as.character(data_x$date_s), "%Y%m%d") #型変換( integer → string → date(double) ) 9 #価格時系列 10 plot(data_x$date_s, data_x$closing_adjusted, main=paste("Stock price(", as.character(x[1,1]), ")", sep = ""), xlab="Time [date]", ylab="ClosingPrice(Adjusted)", type="l") #図の作成 11 dev.copy2eps(file=paste("Price", as.character(x[1,1]), ".eps",sep = ""), width=6, height=4) #図の保存 12 #価格変化率時系列 13 RDP <- diff(data_x$closing_adjusted)/data_x$closing_adjusted[1:length(data_x$closing_adjusted)-1] 14 plot(data_x$date_s[1:length(data_x$closing_adjusted)-1], RDP, xlab="Time [date]", 15 ylab="RDP(Adjusted)", type="l", ylim=c(-0.4, 0.4)) #図の作成 16 dev.copy2eps(file=paste("RDP", as.character(x[1,1]), ".eps",sep = ""), width=6, height=4) #図の保存 17 #価格変化率確率密度関数 18 data_x <- data_x[order(data_x$date_s),] #日付(date_s)によるソート(昇順) 19 head(data_x) 20 data_x$date_s <- as.Date(as.character(data_x$date_s), "%Y%m%d") #型変換( integer → string → date(double) ) 21 plot(data_x$date_s, data_x$closing_adjusted, main=paste("Stock price(", as.character(x[1,1]), ")", sep = ""), xlab="Time [date]", ylab="ClosingPrice(Adjusted)", type="l") #グラフの描画 22 DP <- diff(data_x$closing_adjusted) #ベクトルの作成(価格差) 23 hist_temp <- hist(DP, plot=FALSE) #ヒストグラム,確率密度関数の作成 24 plot(hist_temp$mids, hist_temp$density, xlim=c(-100, 100), ylim=c(0.00001, 0.1), log="y", xlab="DP", ylab="PDF") #確率密度関数の図示 25 x <- -50:50 #正規分布による近似線を追加 26 lines(x, dnorm(x, mean(DP), sd(DP))) 27} 28
###試したこと
価格変化率の確率密度関数のソースコードのうち、それ以前の行で既出の部分を止めると以下のようになりました。
<エラー>
R
1警告メッセージ: 21: In xy.coords(x, y, xlabel, ylabel, log) : 3 2 y values <= 0 omitted from logarithmic plot 42: In xy.coords(x, y, xlabel, ylabel, log) : 5 1 y value <= 0 omitted from logarithmic plot 63: In xy.coords(x, y, xlabel, ylabel, log) : 7 4 y values <= 0 omitted from logarithmic plot
<ソースコード>
R
1setwd("/home/user/bootcamp") 2for (i in 1:5){ 3 x <- read.table("StockCode.txt", skip = i-1, nrows = 1) 4 file_title <- paste("data", as.character(x[1,1]), ".csv", sep = "") 5 data_x <- read.csv(file_title) 6 data_x$closing_adjusted<-round(data_x$closing_adjusted,digits=3) 7 data_x <- data_x[order(data_x$date_s),] 8 data_x$date_s <- as.Date(as.character(data_x$date_s), "%Y%m%d") 9 #価格時系列 10 plot(data_x$date_s, data_x$closing_adjusted, main=paste("Stock price(", as.character(x[1,1]), ")", sep = ""), xlab="Time [date]", ylab="ClosingPrice(Adjusted)", type="l") #図の作成 11 dev.copy2eps(file=paste("Price", as.character(x[1,1]), ".eps",sep = ""), width=6, height=4) #図の保存 12 #価格変化率時系列 13 RDP <- diff(data_x$closing_adjusted)/data_x$closing_adjusted[1:length(data_x$closing_adjusted)-1] 14 plot(data_x$date_s[1:length(data_x$closing_adjusted)-1], RDP, xlab="Time [date]", 15 ylab="RDP(Adjusted)", type="l", ylim=c(-0.4, 0.4)) #図の作成 16 dev.copy2eps(file=paste("RDP", as.character(x[1,1]), ".eps",sep = ""), width=6, height=4) #図の保存 17 #価格変化率の確率密度関数 18 #data_x <- data_x[order(data_x$date_s),] 19 head(data_x) 20 #data_x$date_s <- as.Date(as.character(data_x$date_s), "%Y%m%d") 21 #plot(data_x$date_s, data_x$closing_adjusted, main=paste("Stock price(", as.character(x[1,1]), ")", sep = ""), xlab="Time [date]", ylab="ClosingPrice(Adjusted)", type="l") 22 DP <- diff(data_x$closing_adjusted) 23 hist_temp <- hist(DP, plot=FALSE) 24 plot(hist_temp$mids, hist_temp$density, xlim=c(-100, 100), ylim=c(0.00001, 0.1), log="y", xlab="DP", ylab="PDF") 25 x <- -50:50 26 lines(x, dnorm(x, mean(DP), sd(DP))) 27}
###補足情報(言語/FW/ツール等のバージョンなど)
銘柄コード一覧ファイル(StockCode.txt)には5つの銘柄コード(9007,9008,9020,9021,9042)が入っています。それぞれの銘柄コードの株価データはcsvファイル(data9007.csv,data9008.csv,data9020.csv,data9021.csv,data9042.csv)に保存されています。
あなたの回答
tips
プレビュー