How do I get the stationarity
test from the fractal package
in R to not print any output to the screen.
For example, with the shapiro.wilk
test when setting the result as a variable it does not give any output as follows
lg.day.ret.vec <- rnorm(100, mean = 5, sd = 3)
shap.p <- shapiro.test(lg.day.ret.vec)$p.value
This is the case for most tests but when I do it for the stationarity
test I get some output in the r console.
library(fractal)
stat.p <- attr(stationarity(lg.day.ret.vec),"pvals")[1]
1
2
3
4
5
6
N = 2609, nblock = 11, n_block_max = 238, dt = 1.0000
7
8
9
10
11
12
13
14
15
16
17
18
In fact, you can suppress the output to R console by rerouting it. Two methods are available in R utils, sink
, and capture.output
. Both methods are intended to send output to a file.
Since you want to suppress the output of a single expression, you can use capture.output
, with file=NULL
(default). This will return your output as a string. To prevent showing this returned string in the R console, you can use invisible
.
The final code can be:
library(fractal)
lg.day.ret.vec <- rnorm(100, mean = 5, sd = 3)
shap.p <- shapiro.test(lg.day.ret.vec)$p.value
invisible(capture.output(
stat.p <- attr(stationarity(lg.day.ret.vec),"pvals")[1]
))
Hope this helps. Let me know if not.
来源:https://stackoverflow.com/questions/28665099/suppress-output-of-stationarity-test-that-is-printed-to-screen