- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有长格式的每日收盘价数据集,我想计算月度返回(算术)。它被计算为
(m1-m0)/m0
其中 m1 和 m0 分别是当前和上个月最后几天的价格。这是两只股票的样本:
dput(q1)
structure(list(Date = structure(c(13027, 13028, 13031, 13032,
13034, 13035, 13038, 13039, 13040, 13041, 13042, 13045, 13046, 13047,
13048, 13049, 13052, 13053, 13054, 13055, 13056, 13059, 13060, 13061,
13062, 13063, 13066, 13067, 13069, 13070, 13073, 13074, 13075, 13076,
13077, 13080, 13081, 13082, 13083, 13084, 13087, 13088, 13089, 13094,
13095, 13096, 13097, 13098, 13101, 13103, 13104, 13105, 13108, 13109,
13110, 13111, 13112, 13113, 13115, 13116, 13117, 13118, 13119, 13122,
13123, 13124, 13125, 13126, 13129, 13130, 13131, 13132, 13133, 13136,
13137, 13138, 13139, 13140, 13143, 13144, 13145, 13146, 13147, 13150,
13151, 13152, 13153, 13154, 13157, 13158, 13160, 13161, 13164, 13165,
13166, 13167, 13168, 13171, 13172, 13173, 13175, 13178, 13179, 13180,
13181, 13182, 13185, 13186, 13187, 13189, 13192, 13193, 13194, 13195,
13196, 13199, 13200, 13201, 13202, 13203, 13206, 13207, 13208, 13209,
13210, 13213, 13214, 13215, 13216, 13217, 13220, 13221, 13223, 13224,
13227, 13228, 13229, 13230, 13231, 13234, 13235, 13236, 13237, 13238,
13241, 13242, 13243, 13245, 13248, 13250, 13251, 13255, 13256, 13257,
13258, 13259, 13262, 13263, 13264, 13265, 13266, 13267, 13270, 13271,
13272, 13273, 13276, 13277, 13278, 13279, 13280, 13283, 13284, 13285,
13286, 13287, 13290, 13291, 13292, 13293, 13294, 13297, 13298, 13299,
13300, 13301, 13304, 13305, 13306, 13307, 13308, 13311, 13312, 13313,
13314, 13315, 13318, 13319, 13320, 13321, 13322, 13324, 13325, 13326,
13327, 13328, 13329, 13332, 13333, 13334, 13335, 13336, 13339, 13340,
13341, 13342, 13343, 13346, 13347, 13348, 13349, 13350, 13353, 13354,
13355, 13356, 13357, 13360, 13361, 13362, 13363, 13364, 13367, 13368,
13369, 13370, 13371, 13374, 13376, 13377, 13378, 13381, 13382, 13383,
13384, 13385, 13388, 13389, 13390, 13391, 13027, 13028, 13031, 13032,
13034, 13035, 13038, 13039, 13040, 13041, 13042, 13045, 13046, 13047,
13048, 13049, 13052, 13053, 13054, 13055, 13056, 13059, 13060, 13061,
13062, 13063, 13066, 13067, 13069, 13070, 13073, 13074, 13075, 13076,
13077, 13080, 13081, 13082, 13083, 13084, 13087, 13088, 13089, 13094,
13095, 13096, 13097, 13098, 13101, 13103, 13104, 13105, 13108, 13109,
13110, 13111, 13112, 13113, 13115, 13116, 13117, 13118, 13119, 13122,
13123, 13124, 13125, 13126, 13129, 13130, 13131, 13132, 13133, 13136,
13137, 13138, 13139, 13140, 13143, 13144, 13145, 13146, 13147, 13150,
13151, 13152, 13153, 13154, 13157, 13158, 13160, 13161, 13164, 13165,
13166, 13167, 13168, 13171, 13172, 13173, 13175, 13178, 13179, 13180,
13181, 13182, 13185, 13186, 13187, 13189, 13192, 13193, 13194, 13195,
13196, 13199, 13200, 13201, 13202, 13203, 13206, 13207, 13208, 13209,
13210, 13213, 13214, 13215, 13216, 13217, 13220, 13221, 13223, 13224,
13227, 13228, 13229, 13230, 13231, 13234, 13235, 13236, 13237, 13238,
13241, 13242, 13243, 13245, 13248, 13250, 13251, 13255, 13256, 13257,
13258, 13259, 13262, 13263, 13264, 13265, 13266, 13267, 13270, 13271,
13272, 13273, 13276, 13277, 13278, 13279, 13280, 13283, 13284, 13285,
13286, 13287, 13290, 13291, 13292, 13293, 13294, 13297, 13298, 13299,
13300, 13301, 13304, 13305, 13306, 13307, 13308, 13311, 13312, 13313,
13314, 13315, 13318, 13319, 13320, 13321, 13322, 13324, 13325, 13326,
13327, 13328, 13329, 13332, 13333, 13334, 13335, 13336, 13339, 13340,
13341, 13342, 13343, 13346, 13347, 13348, 13349, 13350, 13353, 13354,
13355, 13356, 13357, 13360, 13361, 13362, 13363, 13364, 13367, 13368,
13369, 13370, 13371, 13374, 13376, 13377, 13378, 13381, 13382, 13383,
13384, 13385, 13388, 13389, 13390, 13391), class = "Date"), Firm =
c("ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE", "ADE",
"ADE", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP", "AJP",
"AJP"), Price = c(7.05, 6.81, 6.98, 6.82, 6.81, 6.75, 6.73, 6.71,
6.6, 6.62, 6.71, 7.01, 6.82, 6.59, 6.25, 6.29, 6.48, 6.43, 6.45,
6.39, 6.3, 6.39, 6.61, 6.62, 6.52, 6.48, 6.39, 6.31, 6.31, 6.21,
6.15, 6.14, 6.02, 5.88, 5.91, 5.91, 5.91, 5.95, 5.84, 5.73, 5.83,
5.95, 5.94, 6.01, 6.05, 5.95, 5.94, 6.02, 6.06, 6.1, 6.52, 6.42,
6.36, 6.27, 6.29, 6.26, 6.26, 6.28, 6.41, 6.46, 6.34, 6.34, 6.27,
6.38, 6.46, 6.37, 6.34, 6.38, 6.39, 6.33, 6.31, 6.22, 6.23, 6.28,
6.27, 6.18, 6.17, 6.09, 5.99, 6.1, 6.01, 6, 5.9, 5.99, 6.06,
6.1, 6.05, 6.1, 6.11, 6.05, 6.05, 6.03, 6, 6, 6.01, 6.07, 6.14,
6.02, 6.06, 6.01, 6.01, 6.03, 6, 5.98, 5.81, 5.78, 5.95, 5.86,
5.77, 5.78, 5.81, 5.8, 5.8, 6, 5.8, 5.75, 5.79, 5.81, 5.75, 5.75,
5.76, 5.76, 5.79, 5.77, 5.74, 5.71, 5.63, 5.66, 5.69, 5.65, 5.63,
5.61, 5.58, 5.58, 5.63, 5.58, 5.52, 5.53, 5.76, 5.96, 5.85, 5.94,
5.88, 5.99, 6.43, 6.38, 6.37, 6.41, 6.31, 6.11, 5.93, 6.02, 6.11,
6.07, 6.08, 6.02, 5.99, 6, 6.06, 6.02, 5.98, 6.07, 7.29, 8.75,
10.5, 11.55, 12.53, 12.69, 13.96, 15.36, 16.13, 16.94, 17.66,
18.55, 18.97, 18.18, 17.28, 16.42, 15.6, 14.88, 15.62, 16.41,
17.23, 18.09, 19, 18.47, 17.55, 16.68, 15.85, 15.06, 14.3, 13.59,
12.91, 12.27, 11.66, 12.16, 12.77, 13.41, 14.08, 13.56, 13.67,
13.86, 13.17, 12.62, 12.45, 12.47, 13, 12.51, 12.36, 12.55, 12.47,
12.06, 11.77, 11.81, 12.19, 12.32, 12.03, 11.62, 11.43, 11.05,
11.18, 10.72, 10.74, 10.53, 10.84, 11.39, 11.53, 11.51, 11.24,
11.36, 11.62, 11.53, 11.45, 11.73, 11.81, 12.96, 13.36, 13.57,
14.09, 13.41, 13.1, 12.72, 12.52, 12.26, 12, 12.18, 12.52, 12.48,
12.2, 12, 13.54, 13.97, 13.67, 13.32, 13.24, 12.79, 12.98, 12.73,
12.45, 12.69, 12.71, 12.61, 12.34, 11.95, 11.36, 11.11, 11.67,
11.57, 11.61, 11.31, 11.25, 11.24, 11.44, 11.43, 11.21, 11.25,
10.95, 10.63, 10.59, 10.38, 9.86, 9.91, 9.09, 9.16, 9.17, 9.16,
9.14, 9.14, 8.96, 8.66, 8.71, 8.86, 8.84, 9.19, 9.46, 9.31, 9.49,
9.73, 10.71, 11.03, 10.76, 10.73, 10.99, 10.84, 10.81, 10.94,
10.83, 10.76, 10.61, 10.51, 11.13, 10.91, 10.67, 10.59, 10.68,
10.62, 10.81, 10.83, 11.03, 11.06, 10.67, 10.59, 10.46, 10.55,
10.35, 10.41, 10.63, 10.54, 10.23, 10.14, 10.14, 10.2, 10.4,
10.67, 10.79, 10.97, 10.8, 10.96, 11.4, 11.27, 11.17, 11.81,
12.07, 11.3, 11.16, 11.53, 11.31, 11.23, 11.24, 11.06, 11.67,
11.49, 11.23, 10.91, 10.81, 10.81, 10.84, 10.61, 10.68, 10.75,
11.08, 11.07, 10.78, 10.79, 10.58, 10.48, 10.46, 10.33, 10.35,
10.31, 10.3, 10.09, 10.1, 9.91, 9.86, 9.9, 9.76, 9.83, 9.58,
9.61, 9.73, 9.59, 9.48, 9.16, 9.03, 8.95, 9.12, 9.08, 9.02, 9.11,
8.79, 8.67, 9.14, 9.75, 10.3, 10.19, 10.35, 9.93, 9.98, 9.85,
9.61, 9.66, 9.71, 9.71, 9.93, 9.81, 9.99, 9.77, 9.69, 9.81, 10.71,
12.85, 12.76, 12.26, 11.94, 12.51, 12.22, 12.23, 12.32, 11.79,
11.75, 10.94, 11.09, 11.53, 10.7, 10.3, 9.58, 9.96, 9.9, 9.75,
10.01, 10.13, 10.05, 9.61, 9.4, 9.04, 9.03, 8.77, 7.89, 7.35,
8.23, 8.2, 8.05, 8.07, 8.35, 8.7, 9.16, 9.24, 9.37, 9.4, 9.7,
9.66, 9.37, 9.07, 9.35, 9.52, 9.42, 9.64, 9.47, 9.51, 9.36, 9.06,
10.49, 10.24, 9.85, 9.93, 9.7, 9.43, 9.57, 9.33, 9.6, 9.43, 9.63,
9.72, 9.83, 9.69, 9.78, 9.44, 9.39, 9.27, 9.35, 9.21, 9.19, 9.3,
9.45, 9.73, 10.61, 10.82, 10.62, 10.27, 10.41, 10.31, 10.46,
10.21, 10.02, 10.24, 10.13, 10.09, 10.07, 9.94)), .Names = c("Date", "Firm", "Price"), row.names = c(NA, -500L), class = c("tbl_df",
"tbl", "data.frame"))
我不知道如何以长格式执行此操作,所以我将其转换为宽格式并获得了月末价格
q<- spread(q1,Firm,Price) q$Date<-as.Date(q$Date) rownames(q) = q[[1]] q<-as.xts(q) q.m.r<-q[ endpoints(q, on="months", k=1), ]
我还遇到了来自 quantmod 的 monthlyReturn
函数的问题,所以我将以长格式计算返回。我还需要将数据转换成长格式以便进一步处理。因此,使用了 tidyr 中的 gather() 函数,但它不起作用
Error in UseMethod("gather_") : no applicable method for 'gather_' applied to an object of class "c('xts', 'zoo')"
我还找到了this但我不知道在价格后面放什么
xts2df <- function(x) {data.frame(date=index(x), coredata(x))} g<-gather(xts2df(q.m.r), Firm, Price, ?)
我也经历了this question并应用
m <- melt(q.m.r,id="Date",variable_name="Firm") names(m) <- sub("value","Price",names(m))
但它不会堆叠列。感谢您的阅读,请帮助我解决问题。
最佳答案
xts
endpoints
函数可以与 base R 或 dplyr
一起使用来计算每月返回。这两个版本是:
# Use xts and base R
library(xts)
q2 <- within(q1[endpoints(q1$Date, on="months"),],
Return <- ave(Price, Firm, FUN=function(x) c(NA, (diff(x)/head(x,-1)))))[,c("Date", "Firm", "Return")]
q2 <- na.omit(q2)
# Use xts and dplyr
library(dplyr)
library(xts)
q3 <- q1[endpoints(q1$Date, on="months"),] %>%
group_by(Firm) %>%
transmute(Date=Date, Return = c(NA, (diff(Price)/head(Price,-1)))) %>%
na.omit()
关于r - 以长格式计算月返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37878908/
SQL 和一般开发的新手,我有一个表(COUNTRIES),其中包含字段(INDEX、NAME、POPULATION、AREA) 通常我添加一个客户端(Delphi)计算字段(DENSITY)和 On
我想使用 calc(100%-100px),但在我的 demo 中不起作用由于高度只接受像素,因此如何将此百分比值转换为像素。 最佳答案 以下将为您提供高度: $(window).height();
我正在尝试在 MySQL 中添加列并动态填充其他列。 例如我有一张表“数字”并具有第 1 列、第 2 列、第 3 列,这些总数应填充在第 4 列中 最佳答案 除非我误解了你的问题,否则你不只是在寻找:
我想返回简单计算的结果,但我不确定如何执行此操作。我的表格如下: SELECT COUNT(fb.engineer_id) AS `total_feedback`, SUM(fb.ra
我一直在尝试做这个程序,但我被卡住了,我仍然是一个初学者,任何帮助将不胜感激。我需要程序来做 打印一个 10 X 10 的表格,其中表格中的每个条目都是行号和列号的总和 包含一个累加器,用于计算所有表
这个计算背后一定有一些逻辑。但我无法得到它。普通数学不会导致这种行为。谁能帮我解释一下原因 printf ("float %f\n", 2/7 * 100.0); 结果打印 1.000000 为什么会
我想计算从 0 到 (n)^{1/2} - 1 的数字的 AND每个数字从 0 到 (n)^{1/2} - 1 .我想在 O(n) 中执行此操作时间,不能使用 XOR、OR、AND 运算。 具体来说,
如何在 Excel 中将公式放入自定义数字格式?例如(出于说明目的随机示例), 假设我有以下数据: 输入 输出 在不编辑单元格中的实际数据的情况下,我想显示单元格中的值除以 2,并保留两位小数: 有没
每次我在 Flutter 应用程序中调用计算()时,我都会看到内存泄漏,据我所知,这基本上只是一种生成隔离的便捷方法。我的应用程序内存占用增加并且在 GC 之后永远不会减少。 我已将我的代码简化为仅调
我有数字特征观察 V1通过 V12用于目标变量 Wavelength .我想计算 Vx 之间的 RMSE列。数据格式如下。 每个变量“Vx”以 5 分钟的间隔进行测量。我想计算所有 Vx 变量的观测值
我正在寻找一种使用 C 语言计算文件中未知字符数的简单方法。谢谢你的帮助 最佳答案 POSIX 方式(可能是您想要的方式): off_t get_file_length( FILE *file ) {
我正在使用 Postgres,并且我正试图围绕如何在连续日期跨度中得出第一个开始日期的问题进行思考。例如 :- ID | Start Date | End Date =================
我有一个订单表格,我在其中使用 jQuery 计算插件来汇总总数。 此求和工作正常,但生成的“总和”存在问题。总之,我希望用逗号替换任何点。 代码的基础是; function ($this) {
我在使用 double 变量计算简单算术方程时遇到问题。 我有一个具有 double 属性 Value 的组件,我将此属性设置为 100。 然后我做一个简单的减法来检查这个值是否真的是 100: va
我在这里看到了一些关于 CRC 32 计算的其他问题。但没有一个让我满意,因此是这样。 openssl 库是否有任何用于计算 CRC32 的 api 支持?我已经在为 SHA1 使用 openssl,
当我在PHP日期计算中遇到问题时,我感到惊讶。 $add = '- 30 days'; echo date('Y-m-01', strtotime($add)); // result is 2017-
我正在使用 javascript 进行练习,我编写了这个脚本来计算 2 个变量的总和,然后在第三个方程中使用这个总和!关于如何完成这项工作的任何想法都将非常有用! First Number:
我有一个来自EAC的提示单和一个包含完整专辑的FLAC文件。 我正在尝试制作一些python脚本来播放文件,因为我需要能够设置在flac文件中开始的位置。 如何从CueSheet格式MM:SS:FF转
这个问题已经有答案了: Adding two numbers concatenates them instead of calculating the sum (24 个回答) 已关闭去年。 我有一个
4000 我需要上面字段 name="quantity" 和 id="price" 中的值,并使用 javascript 函数进行计算,并将其显示在字段 id= 中仅当我单击计算按钮时才显示“总
我是一名优秀的程序员,十分优秀!