我编写了一个代码,使用20个季度的财务报表为1500家未上市公司提供财务分析。该代码主要使用rollapply函数来计算滚动季度平均值和总和。但是,它的表现一直不稳定。虽然大多数情况下代码工作得很好,但有时却会产生分段错误。如果我在一段时间后重新运行代码它运行没有问题。
经过多次努力,我无法追查错误。我用valgrind来定位错误。分段错误后输出后返回代码。
==24224== Invalid read of size 8
==24224== at 0x4F3C030: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F3ED9B: CONS_NR (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F38146: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F11F89: Rf_applyClosure (in/usr/lib/R/lib/libR.so)
==24224== by 0x4F43617: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F43CA3: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F019FA: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0CF6F: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F1249C: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D065: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F10955: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D38F: ??? (in /usr/lib/R/lib/libR.so)
==24224== Address 0x91635f0 is 4,448 bytes inside a block of size 4,608 free'd
==24224== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24224== by 0x5011332: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x500C208: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x5008710: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F2AEEC: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F012D8: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0CF6F: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F1249C: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D065: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0EFB8: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D466: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F10955: ??? (in /usr/lib/R/lib/libR.so)
==24224==
==24224== Invalid read of size 1
==24224== at 0x4F3C034: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F3ED9B: CONS_NR (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F38146: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F11F89: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F43617: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F43CA3: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F019FA: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0CF6F: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F1249C: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D065: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F10955: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D38F: ??? (in /usr/lib/R/lib/libR.so)
==24224== Address 0x91635e3 is 4,435 bytes inside a block of size 4,608 free'd
==24224== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24224== by 0x5011332: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x500C208: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x5008710: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F2AEEC: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F012D8: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0CF6F: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F1249C: Rf_applyClosure (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D065: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0EFB8: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F0D466: ??? (in /usr/lib/R/lib/libR.so)
==24224== by 0x4F10955: ??? (in /usr/lib/R/lib/libR.so)
这个输出是什么意思?什么是libR.so?
是否有类似rollapply的功能?
任何帮助将不胜感激。提前谢谢。
很抱歉没有提供代码。这是一个800行代码,我不知道问题出在哪里。
我有两个不同的运行的两个traceback()输出。
运行1
*** caught segfault ***
address 0xe1, cause 'memory not mapped'
Traceback:
1: NROW(data)
2: (function (i, offsets, data, ...) { if (is.null(offsets)) return(NULL) posns <- i + offsets ix <- posns >= 1 & posns <= NROW(data) if (partial < 0) { if (all(ix)) FUN(data[posns], ...) } else if (sum(ix) >= partial) { FUN(data[replace(posns, !ix, 0)], ...) }})(dots[[1L]][[20L]], dots[[2L]][[20L]], data = c(52.52, 55.51, 49.83, 47.21, 49.16, 44.55, 50.82, 51.46, 57.29, 48.24, 46.99, 44.23, 45.46, 41.31, 44.75, 51.7, 61.86, 63.34, 61.04, 63.42, 66.04, 69.76, 84.45, 95.53, 96.73, 97.51, 94.57, 84.34, 84.62, 77.97, 78.25, 77.04, 72.13, 86.34, 114.31, 112.92, 120.43, 126.21, 116.4, 118.45))
3: mapply(f, seq_along(time(data)), width, MoreArgs = list(data = coredata(data), ...), SIMPLIFY = FALSE)
4: rollapply.zoo(zoo(data), ...)
5: rollapply(zoo(data), ...)
6: coredata(rollapply(zoo(data), ...))
7: rollapply.default(NetSalesOrrevenues, 4, FUN = sum, fill = NA, align = "right")
8: rollapply(NetSalesOrrevenues, 4, FUN = sum, fill = NA, align = "right")
aborting ...
这是来自同一代码的不同运行
*** caught segfault ***
address (nil), cause 'unknown'
Traceback:
1: ORDER.default(order.by)
2: ORDER(order.by)
3: zoo(do.call("c", dat), index(data), attr(data, "frequency"))
4: rollapply.zoo(zoo(data), ...)
5: rollapply(zoo(data), ...)
6: coredata(rollapply(zoo(data), ...))
7: rollapply.default(temp_NetSalesOrRevenues, 4, FUN = sum, fill = NA, align = "right")
8: rollapply(temp_NetSalesOrRevenues, 4, FUN = sum, fill = NA, align = "right")
aborting ...
我正在使用rollapply函数来计算ltm sales。使用的数据是正常的销售数据。