如何确定R中prcomp对象的小数位数

时间:2014-09-24 03:40:20

标签: r decimal pca

我有一个" prcomp"对象叫做pcaObj。

当我上课(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]]中的错误:类型&#39;内置&#39;的对象不是子集表格

我也尝试使用舍入和小数,但仍然只更改了一些列!:

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位小数。

1 个答案:

答案 0 :(得分:0)

这是您尝试重写的功能的更新版本

getSum <- function (object, strf="%.3f", ...) {
    vars <- object$sdev^2
    vars <- vars/sum(vars)
    strf <- rep_len(strf, 3)
    importance <- rbind(
        `Standard deviation` = sprintf(strf[1], object$sdev), 
        `Proportion of Variance` = sprintf(strf[2], vars), 
        `Cumulative Proportion` = sprintf(strf[3], cumsum(vars)))
    colnames(importance) <- colnames(object$rotation)
    object$importance <- noquote(importance)
    class(object) <- "summary.prcomp"
    object
}

您在复制计算时犯了一些错误,并添加了一些不必要的summary()次来电。我还做过,如果你愿意,可以指定格式作为参数。现在这似乎有效了

px<-prcomp(USArrests, scale = TRUE)
getSum(px)

# Importance of components:
#                        PC1   PC2   PC3   PC4  
# Standard deviation     1.575 0.995 0.597 0.416
# Proportion of Variance 0.620 0.247 0.089 0.043
# Cumulative Proportion  0.620 0.868 0.957 1.000