用“R”编程语言显示和报告函数输出
在R编程语言和包pcaPP中,我有以下代码:用“R”编程语言显示和报告函数输出,r,rstudio,R,Rstudio,在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 calculat
# 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的其他输出,如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对象的输出(名称、str和摘要)时非常有用 str是structure的缩写。它输出一个易于阅读的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函数中,它将返回。。。让我们称之为该函数输出的最明显和最有用的摘要
> 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有一些很酷的功能,比如tab auto complete,所以如果你键入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"
通过执行strpc检查pc的结构,您可能会得到一些想法
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
> 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"