【问题标题】:Regression results: default display format回归结果:默认显示格式
【发布时间】:2012-06-13 14:32:19
【问题描述】:

我正在使用 lm() 函数在 R 中运行回归,但我无法以简单格式显示结果。我需要打印一个 p 值向量:

> summary(lm)$coef[,4]
  (Intercept)           lun          d1un 
 1.433706e-01 4.673723e-158  6.629044e-04 

如何覆盖科学记数法并获得合理的精度?我尝试了 options(scipen=1000),但它显​​示无穷无尽的数字行,不知何故 options(digits=7) 在这里不起作用。

我知道我可以使用格式和冲刺之类的东西,但我想为所有数字输出设置默认显示规则,例如不超过 6 位小数。

【问题讨论】:

  • 你能给出预期的输出吗?您的意思是将lun 的coef 打印为0.000000
  • 是的,除非明确指定其他精度,否则我想要 0.000000。

标签: r options lm


【解决方案1】:

我认为这是不可能的,因为整个 R 中的一般显示。?options 这么说关于数字:

 ‘digits’: controls the number of digits to print when printing
      numeric values.  It is a suggestion only.  Valid values are
      1...22 with default 7.  See the note in ‘print.default’ about
      values greater than 15.

关键词是“这只是一个建议”。

接下来,请注意打印的内容首先由应用的print() 方法控制(这在交互使用期间是隐藏的,因为R auto-print()s)。有关详细信息,请参阅?print?print.default 了解基本方法。从?print.default我们注意到

digits: a non-null value for ‘digits’ specifies the minimum number of
        significant digits to be printed in values.  The default,
        ‘NULL’, uses ‘getOption(digits)’.  (For the interpretation
        for complex numbers see ‘signif’.)  Non-integer values will
        be rounded down, and only values greater than or equal to 1
        and no greater than 22 are accepted.

详细信息部分我们有:

   The same number of decimal places is used throughout a vector.
   This means that ‘digits’ specifies the minimum number of
   significant digits to be used, and that at least one entry will be
   encoded with that minimum number.  However, if all the encoded
   elements then have trailing zeroes, the number of decimal places
   is reduced until at least one element has a non-zero final digit.
   Decimal points are only included if at least one decimal place is
   selected.

digits 的默认值是 NULL,表示使用了 getOption("digits"),但正如我们已经指出的,这只是一个指南。

似乎没有一种方法可以将 R 配置为执行您想要的操作,也无法在全局范围内执行此操作。您需要重写 print.default() 或您需要使用的所有 print() 方法,并使用这些版本而不是标准版本 --- 现在使用 NAMESPACES 并不容易。

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2019-08-19
    • 2017-10-20
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多