2012-06-10 47 views
1

我使用lm()函数在R中运行回归,并且无法管理以简单格式显示结果。我需要打印的p值向量:回归结果:默认显示格式

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

我怎么可以重写科学记数法,并得到合理的精度?我试过选项(scipen = 1000),但它显示无穷的数字行,不知何故options(digits = 7)在这里不起作用。

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

+1

你能给出预期的输出吗?你的意思是把'lun'的coef打印为'0.000000'? –

+0

是的,除非明确指定其他精度,我想有0.000000。 –

回答

0

我不认为这将成为可能,在整个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。

+0

好的,谢谢。我想我会保持原样。 –