I've been working on quite an ambitious function, which I hope can be used by people other than me once I am finished. When it's just me using the function I can live with the output being kind of lame, but what if I want some nice looking output? What I'm looking for is essentially this:
- A way of printing something readable to the console
- Being able to access what's printed
More specifically, let's assume I have three scalar objects I want to be printed: stat
, dfree
and pval
. Currently, the way I do it is:
result <- list(statistic = stat, degrees = dfree, p.value = pval)
return(result)
That way I can access these values by running, for example (the function is called whites.htest
):
whites.htest$p.value
It works, but the output is kind of ugly.
> whites.htest(var.modell)
$statistic
[1] 36.47768
$degrees
[1] 30
$p.value
[1] 0.1928523
If we run a simple VAR model like this:
> library(vars)
> data <- matrix(rnorm(200), ncol = 2)
> VAR(data, p = 2, type = "trend")
VAR Estimation Results:
=======================
Estimated coefficients for equation y1:
=======================================
Call:
y1 = y1.l1 + y2.l1 + y1.l2 + y2.l2 + trend
y1.l1 y2.l1 y1.l2 y2.l2 trend
-0.090102007 -0.060138062 0.126250484 0.014423006 0.003138521
Estimated coefficients for equation y2:
=======================================
Call:
y2 = y1.l1 + y2.l1 + y1.l2 + y2.l2 + trend
y1.l1 y2.l1 y1.l2 y2.l2 trend
0.040118527 0.018274399 -0.132943318 -0.031235939 0.003242241
The output looks really good. I've had a look at the underlying code for it (by simply running VAR
), but I cannot find what makes it look good like this.
So my question is, how do I print something nice and readable to the console while still being able to access individual objects (i.e. results) from the function?