#########################################計算簡單收益率#install.packages('xts')library(xts)da=read.csv('luowen.csv')#讀取文件class(da)#查看文件類型head(da)#查看數據da=da[,c(2,9)]#截取時間和收盤價c=da[,2]#獲取收盤價head(c)df=xts(c,order.by=as.Date(da[,1]))#時間序列df=df[-1,]df=df[!is.na(df)]df1=lag(df,1)#滯后一天df1=df1[-1,]df1=df1[!is.na(df1)]data=merge(df,df1)head(data)simplerate=(df-df1)/dfhead(simplerate)############使用R包計算簡單收益率install.packages('quantmod')library(quantmod)rate=periodReturn(df,period="daily",type="arithmetic")head(rate)head(simplerate)使用的數據是螺紋鋼的日線數據。
新聞熱點
疑難解答