我一直在开发一项雄心勃勃的功能,希望完成后其他人也能使用。当我只是自己使用该函数时,我可以接受输出有些单调乏味,但如果我想要一些漂亮的输出怎么办?我期望的是:
- 一种在控制台上打印可读内容的方法
- 能够访问已打印的内容
stat
、dfree
和pval
。目前,我是这样做的:result <- list(statistic = stat, degrees = dfree, p.value = pval)
return(result)
这样我就可以通过运行函数(名为whites.htest
)来访问这些值,例如:
whites.htest$p.value
它可以工作,但输出看起来有点丑陋。
> whites.htest(var.modell)
$statistic
[1] 36.47768
$degrees
[1] 30
$p.value
[1] 0.1928523
如果我们运行一个简单的VAR模型,像这样:
> 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
输出结果非常好。我已经查看了它的基础代码(通过简单运行VAR
),但我找不到是什么让它看起来这么好。
所以我的问题是,如何在仍然能够访问函数中的个别对象(即结果)的同时打印出漂亮易读的内容到控制台?