【发布时间】:2014-09-24 03:40:19
【问题描述】:
我有一个名为 pcaObj 的“prcomp”对象。
当我上课(pcaObj)时,我得到 -
[1] "prcomp"
当我执行 str(pcaObj) 时,我得到 -
List of 5
$ sdev : num [1:10] 1.834 1.333 1.079 0.919 0.843 ...
$ rotation: num [1:10, 1:10] -0.279 0.447 0.271 0.375 0.279 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:10] "Climate" "Diversions" "Economic" "Education" ...
.. ..$ : chr [1:10] "PC1" "PC2" "PC3" "PC4" ...
$ center : Named num [1:10] 63.7 41.9 35.1 38.6 47.6 ...
..- attr(*, "names")= chr [1:10] "Climate" "Diversions" "Economic" "Education" ...
$ scale : Named num [1:10] 9.93 13.36 8.44 14.09 11.92 ...
..- attr(*, "names")= chr [1:10] "Climate" "Diversions" "Economic" "Education" ...
$ x : num [1:193, 1:10] -2.77 -1.08 -3.17 -2.13 -3.15 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:193] "1.Albertville.AL" "2.Auburn-Opelika.AL" "3.Cullman.AL" "4.Selma.AL" ...
.. ..$ : chr [1:10] "PC1" "PC2" "PC3" "PC4" ...
- attr(*, "class")= chr "prcomp"
然后我做总结(pcaObj),并得到类似 -
summary(pcaObj)
Importance of components:
PC1 PC2 PC3 PC4 PC5 PC6 PC7
Standard deviation 1.8336 1.3328 1.0788 0.91905 0.84344 0.80628 0.75001
Proportion of Variance 0.3362 0.1776 0.1164 0.08447 0.07114 0.06501 0.05625
Cumulative Proportion 0.3362 0.5138 0.6302 0.71469 0.78583 0.85084 0.90709
但是,我希望汇总函数中列出的值在句点后每个都恰好是 3 位小数。我尝试了 sprintf("%.3f", summary(unlist(pcaObj))) -
[1] "-3.842" "-0.526" "0.012" "0.307" "0.516" "65.800"
如果可以的话,请帮忙!很抱歉,我不确定在这种情况下如何重现此对象!
编辑 -
我尝试了@MrFlick 的建议。首先,我重写了函数,然后在我的对象上调用它,但我得到了错误:
getSum <- function (object, ...)
{
vars <- object$sdev^2
vars <- vars/sum(vars)
importance <- rbind(`Standard deviation` = sprintf("%.3f", summary(object$sdev)), `Proportion of Variance` = sprintf("%.3f", summary(vars)), `Cumulative Proportion` = sprintf("%.3f", summary(cumsum)))
colnames(importance) <- colnames(object$rotation)
object$importance <- importance
class(object) <- "summary.prcomp"
object
}
getSum(pcaObj)
对象[[i]] 中的错误:“内置”类型的对象不是可子集的
我也尝试过使用四舍五入和小数,但仍然只更改了一些列!:
getSum <- function (object, ...)
{
vars <- object$sdev^2
vars <- vars/sum(vars)
importance <- rbind(`Standard deviation` = object$sdev, `Proportion of Variance` = round(vars,
digits = 3), `Cumulative Proportion` = round(cumsum(vars), digits = 3))
colnames(importance) <- colnames(object$rotation)
object$importance <- importance
class(object) <- "summary.prcomp"
object
}
getSum(pcaObj)
Importance of components:
PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8 PC9 PC10
Standard deviation 1.834 1.333 1.079 0.9191 0.8434 0.8063 0.750 0.6346 0.5623 0.4585
Proportion of Variance 0.336 0.178 0.116 0.0840 0.0710 0.0650 0.056 0.0400 0.0320 0.0210
Cumulative Proportion 0.336 0.514 0.630 0.7150 0.7860 0.8510 0.907 0.9470 0.9790 1.0000
如您所见,有些列是 3 其他列是 4 位小数。
【问题讨论】:
-
您可以尝试
options(digits = 4)设置全局选项。或者print(summary(towns.pca), digits = 4)可能会起作用。但是sprintf会将所有值转换为您可能不想要的字符 -
四舍五入被硬编码到
summary.prcomp函数中(参见getAnywhere(summary.prcomp))。summary()只是为了在屏幕上看起来很漂亮。如果您将它用于其他目的,最好提取和格式化您想要的值。如果您愿意,您可以根据您的特定需求调整该代码(大约 8 行)并创建自己的摘要函数。 -
它似乎可以更改为某些值。我在
print(summary(prcomp(mtcars)), digits = 2)中有几列要更改 -
谢谢,理查德斯克里文。我也试过了,但只改变了几列!
-
@MrFlick:谢谢。我试过了,并添加了一个编辑来显示我做了什么,但这似乎仍然不能解决问题!