2012-06-10 54 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

好的,謝謝。我想我會保持原樣。 –