以 "R" 编程语言显示和报告函数输出
Show and report function outputs in "R" programming language
在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
的其他输出,如 loadings
和 sdev
并在 R-studio 中报告这些输出?
在你的例子中,它都存储在pc
中。
如果您处于交互模式,只需键入 pc$sdev
和 pc$loading
即可查看它们包含的内容。
> 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
补充一下 Bottoms 先生所说的内容,我发现以下一组函数在深入研究 pc
对象等输出时非常有用 -- names()
,str()
, 和 summary
.
# 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() returns 数据结构中每个元素顶层名称的向量。
> names(pc)
[1] "loadings" "sdev" "center" "scale" "n.obs" "scores" "call"
str() 是结构的缩写。它输出一个易于阅读的 R 数据结构描述。我喜欢将其视为 table 的内容。您会注意到它与您的名单相匹配。
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() 大多数设计良好的函数都允许您将新对象传递到摘要函数中,它 returns ...让我们称它为 "most obvious and useful" 该函数的输出摘要。
> 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
键,它将列出上面列出的所有这些名称。然后您可以使用箭头键 select 您想要 select 的元素。
> 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"
在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
的其他输出,如 loadings
和 sdev
并在 R-studio 中报告这些输出?
在你的例子中,它都存储在pc
中。
如果您处于交互模式,只需键入 pc$sdev
和 pc$loading
即可查看它们包含的内容。
> 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
补充一下 Bottoms 先生所说的内容,我发现以下一组函数在深入研究 pc
对象等输出时非常有用 -- names()
,str()
, 和 summary
.
# 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() returns 数据结构中每个元素顶层名称的向量。
> names(pc)
[1] "loadings" "sdev" "center" "scale" "n.obs" "scores" "call"
str() 是结构的缩写。它输出一个易于阅读的 R 数据结构描述。我喜欢将其视为 table 的内容。您会注意到它与您的名单相匹配。
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() 大多数设计良好的函数都允许您将新对象传递到摘要函数中,它 returns ...让我们称它为 "most obvious and useful" 该函数的输出摘要。
> 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
键,它将列出上面列出的所有这些名称。然后您可以使用箭头键 select 您想要 select 的元素。
> 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"