【发布时间】:2018-10-24 21:16:53
【问题描述】:
我正在尝试从具有 2 个列的简单数据矩阵计算对数返回。我的以下代码全为零,我在这里缺少什么?
dataMatrix<-as.matrix(data[,2:ncol(data)]) #taking data from dataframe
Log_returns<-diff(log(dataMatrix), lag=1)
View(log_returns)
这是数据的前几行
Dates APPL AMZN
1 7-01-2005 4.9464 42.32
2 2005-01-14 5.0143 44.55
3 2005-01-21 5.0350 41.16
4 2005-01-28 5.2843 42.22
5 2005-02-04 5.6314 35.72
6 2005-02-11 5.8007 35.78
7 2005-02-18 6.2007 35.31
8 2005-02-25 6.3564 34.99
9 2005-03-04 6.1157 35.85
10 2005-03-11 5.7529 34.75
【问题讨论】:
-
您能提供您使用
dput(data)使用的数据吗?您也可以edit your question 将行更改为列。 -
我唯一的想法是它不工作,因为矩阵有列名,当我用随机制作的矩阵运行它时它工作......但我不知道为什么会这样
-
确保您的数据是数字形式的?