我有这样的文件。许多公司的日志回归计算r
head(Historical_Stock_Prices_R)
Date1 MSFT AAPL GOOGL
1 25-01-05 21.02985 4.873362 88.56
2 26-01-05 21.02177 4.886890 94.62
3 27-01-05 21.10259 4.913269 94.04
想用这个公式LN(现价/以前的价格)来计算对数收益和我的预期输出是这样
Date1 MSFT AAPL GOOGL
26-01-05 -0.04% 0.28% 6.62%
27-01-05 0.38% 0.54% -0.61%
试图从先前的堆栈溢出回答这个代码解决,但失败
logs=data.frame(cbind.data.frame(newdates[-1],
diff(as.matrix(log(Historical_Stock_Prices_R[,-1])))))
日回报给这个代码cbind.data.frame(日期= Historical_Stock_Prices_R $日期1 [2后: 3],应用(Historical_Stock_Prices_R [,2:4],2,函数(x)log(x [-1]/x [-length(x)])* 100)) 它显示data.frame ...,check.names = FALSE): 参数意味着不同的行数:2,245 > –
我的b广告,这是因为我用你的例子(2行),你的矩阵可能更大。我替换了'df $ Date1 [2:nrow(df)]'这应该可以做到。 (介意df = Historical_Stock_Prices_R) – timfaber
抱歉,我明白 –