###前提・実現したいこと
Rで確率密度関数をプロットして正規分布による近似線を描画したいです。
###発生している問題・エラーメッセージ
エラーは発生していませんが、何故か正規分布による近似線が描かれません。
###該当のソースコード
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(RDP, plot=FALSE) #ヒストグラム,確率密度関数の作成 24 for (j in 1:length(hist_temp$density)){ 25 if (is.na(hist_temp$density[j]) || hist_temp$density[j] <= 0){ 26 next 27 } else { 28 if (j == 1){ 29 newhist_temp_mids <- hist_temp$mids[j] 30 newhist_temp_density <- hist_temp$density[j] 31 } else { 32 newhist_temp_mids <- c(newhist_temp_mids, hist_temp$mids[j]) 33 newhist_temp_density <- c(newhist_temp_density, hist_temp$density[j]) 34 } 35 } 36 } 37 plot(newhist_temp_mids, newhist_temp_density, xlim=c(-0.4, 0.4), ylim=c(0.001, 1), log="y", xlab="RDP", ylab="PDF") 38 x_ <- -0.1:0.1 #正規分布による近似線を追加 39 lines(x_, dnorm(x_, mean(RDP), sd(RDP))) 40 dev.copy2eps(file=paste("Probability Density", as.character(x[1,1]), ".eps",sep = ""), width=6, height=4) 41}
###試したこと
・株価データを増やす。
・xlim,ylimを調整する。
・正規分布曲線の定義域を調整する。
以上、いずれも正規分布曲線による近似線は描かれませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
あなたの回答
tips
プレビュー