我一直在研究一个相当雄心勃勃的功能,我希望一旦完成,我希望可以被其他人使用。当它只是我使用的功能,我可以忍受的输出是一种跛脚,但如果我想要一些漂亮的输出?我正在寻找的本质是这样的:创建漂亮的输出
- 打印的东西可读控制台
- 能够访问什么印刷
更具体地说,假设我有三个标对象的方法我想要打印: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
),但我无法找到使它看起来像这样的好东西。
所以我的问题是,如何在控制台上打印出漂亮可读的内容,同时仍然可以从函数中访问单个对象(即结果)?
我看不出你的输出有什么不好。你正在比较两个完全不同的东西,如果你举例说明你真正需要输出的东西,包括样本数据,你可能会得到更好的答案。 – N8TRO 2013-02-23 23:05:21