全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
4417 4
2008-07-17
问一下R中如果要直接使用summary得到的结果,该用什么命令。
例如主成份分析中,
Importance of components:
                          Comp.1    Comp.2    Comp.3     Comp.4      Comp.5
Standard deviation     5.7142472 2.5355505 1.9139183 1.49277754 0.428013590
Proportion of Variance 0.7231022 0.1423725 0.0811201 0.04934826 0.004056923
Cumulative Proportion  0.7231022 0.8654747 0.9465948 0.99594308 1.000000000
如何直接得到standard deviation这一向量,赋值给任一变量?


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2008-7-17 15:00:00
查看帮助,   
 'prcomp' returns a list with class '"prcomp"' containing the
     following components:

    sdev: the standard deviations of the principal components (i.e.,
          the square roots of the eigenvalues of the
          covariance/correlation matrix, though the calculation is
          actually done with the singular values of the data matrix).
…………

>pca <- prcomp(~ Murder + Assault + Rape, data = USArrests, scale = TRUE)
>summary(pca)
Importance of components:
                         PC1   PC2    PC3
Standard deviation     1.536 0.677 0.4282
Proportion of Variance 0.786 0.153 0.0611
Cumulative Proportion  0.786 0.939 1.0000
>names(pca)
[1] "sdev"     "rotation" "center"   "scale"    "x"        "call" 

>pca$sdev
[1] 1.5357670 0.6767949 0.4282154
> (x <-  pca$sdev)
[1] 1.5357670 0.6767949 0.4282154

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-17 15:15:00
我是初学,可能是sdev(),就向loading()一样吧
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-17 15:41:00

不太明白你的意思。
> getS3method("prcomp", "default")
function (x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL,
    ...)
{
    x <- as.matrix(x)
    x <- scale(x, center = center, scale = scale.)
    cen <- attr(x, "scaled:center")
    sc <- attr(x, "scaled:scale")
    if (any(sc == 0))
        stop("cannot rescale a constant/zero column to unit variance")
    s <- svd(x, nu = 0)
    s$d <- s$d/sqrt(max(1, nrow(x) - 1))
    if (!is.null(tol)) {
        rank <- sum(s$d > (s$d[1L] * tol))
        if (rank < ncol(x)) {
            s$v <- s$v[, 1L:rank, drop = FALSE]
            s$d <- s$d[1L:rank]
        }
    }
    dimnames(s$v) <- list(colnames(x), paste("PC", seq_len(ncol(s$v)),
        sep = ""))
    r <- list(sdev = s$d, rotation = s$v, center = if (is.null(cen)) FALSE else cen,
        scale = if (is.null(sc)) FALSE else sc)
    if (retx)
        r$x <- x %*% s$v
    class(r) <- "prcomp"
    r
}

list(sdev = s$d, rotation = s$v, center = if (is.null(cen)) FALSE else cen,
        scale = if (is.null(sc)) FALSE else sc)
函数prcomp返回的结果其实是一个列表,既然如此,当然可以利用names函数察看列表中每一项的名称,进而可以用‘$’操作符提取你需要的信息了。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2008-7-17 15:44:00
  谢谢Janus!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群