2015-05-28 66 views
2

R編程語言和包pcaPP我有這樣的代碼:顯示和報告功能輸出

# multivariate data with outliers 
library(mvtnorm) 
library(pcaPP) 

x <- rbind(rmvnorm(200, rep(0, 6), diag(c(5, rep(1,5)))), 
      rmvnorm( 15, c(0, rep(20, 5)), diag(rep(1, 6)))) 
# Here we calculate the principal components with PCAgrid 
pc <- PCAproj(x) 

下面是PCAproj函數的輸出值的文檔:

The function returns a list of class '"princomp"', i.e. a list 
similar to the output of the function 'princomp'. 

    sdev: the (robust) standard deviations of the principal components. 

loadings: the matrix of variable loadings (i.e., a matrix whose columns 
      contain the eigen- vectors). This is of class "loadings": 
      see loadings for its print method. 

    center: the means that were subtracted. 

    scale: the scalings applied to each variable. 

    n.obs: the number of observations. 

    scores: if 'scores = TRUE', the scores of the supplied data on the 
      principal components. 

    call: the matched call. 

如何調用PCAproj的其他輸出,如loadingssdev並在R-studio中報告?

+2

您可能會通過執行'str(pc)'來檢查'pc'的結構, –

回答

4

在你的例子中,它全部存儲在pc

如果您處於交互模式,只需鍵入pc$sdevpc$loading以查看它們包含的內容。 names()str(),並且summary -

> pc$sdev 
    Comp.1 Comp.2 
2.425413 1.346727 


> pc$loadings 

Loadings: 
    Comp.1 Comp.2 
V1 0.972 0.153 
V2 -0.201 0.447 
V3  -0.130 
V4  -0.211 
V5   0.739 
V6 -0.109 0.412 

       Comp.1 Comp.2 
SS loadings  1.000 1.000 
Proportion Var 0.167 0.167 
Cumulative Var 0.167 0.333 
1

剛補足了一點什麼下裝先生說,更深的挖掘到像你pc對象輸出時,我發現下面的一組超級有用的功能。

# Set Up 
library(mvtnorm) 
library(pcaPP) 
x <- rbind(rmvnorm(200, rep(0, 6), diag(c(5, rep(1,5)))), 
      rmvnorm( 15, c(0, rep(20, 5)), diag(rep(1, 6)))) 
pc <- PCAproj(x) 

名稱()返回數據結構中的每個元件的頂部級別的名稱的矢量。

> names(pc) 
[1] "loadings" "sdev"  "center" "scale" "n.obs" "scores" "call"  

STR()是短期的結構。它輸出一個簡單的R數據結構的描述。我喜歡把它看作一個內容表。你會注意到它匹配你的名單。

str(pc) 
List of 7 
$ loadings: loadings [1:6, 1:2] 0.962 0.1011 0.048 0.2461 0.0152 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:6] "V1" "V2" "V3" "V4" ... 
    .. ..$ : chr [1:2] "Comp.1" "Comp.2" 
$ sdev : Named num [1:2] 2.79 1.39 
    ..- attr(*, "names")= chr [1:2] "Comp.1" "Comp.2" 
$ center : num [1:6] 0.193 0.114 0.093 0.117 0.215 ... 
$ scale : num [1:6(1d)] 1 1 1 1 1 1 
$ n.obs : int 215 
$ scores : num [1:215, 1:2] -0.413 1.707 0.835 2.164 0.495 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:215] "1" "2" "3" "4" ... 
    .. ..$ : chr [1:2] "Comp.1" "Comp.2" 
$ call : language PCAproj(x = x) 
- attr(*, "class")= chr [1:2] "pcaPP" "princomp" 

摘要()最精心設計的功能使您可以將新的對象傳遞到彙總函數,並返回......讓我們把它的「最明顯的和有用的」總結該功能的輸出。

> summary(pc) 
Importance of components: 
          Comp.1 Comp.2 
Standard deviation  2.7873357 1.3855889 
Proportion of Variance 0.8018539 0.1981461 
Cumulative Proportion 0.8018539 1.0000000 

然後RStudio和其他IDE有很酷的功能,如標籤自動完成,因此,如果您鍵入pc$然後打tab鍵,它會列出所有上面列出的名字。然後你可以使用箭頭鍵選擇你想要選擇的元素。

> str(pc$loadings) 
loadings [1:6, 1:2] 0.962 0.1011 0.048 0.2461 0.0152 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : chr [1:6] "V1" "V2" "V3" "V4" ... 
    ..$ : chr [1:2] "Comp.1" "Comp.2"