美文网首页
R语言做PCA分析及可视化代码

R语言做PCA分析及可视化代码

作者: bio_橡树 | 来源:发表于2017-12-11 16:24 被阅读0次

    setwd("/Users/apple/Desktop/R/")

    data<-read.table("pcadata",header=T,row.names=1)

    PCAdata.pr<-princomp(data,cor=TRUE)

    summary(PCAdata.pr,loadings=TRUE)

    predict(PCAdata.pr)

    screeplot(PCAdata.pr,type="lines")

    biplot(PCAdata.pr,choices=1:2,scale=1,pc.biplot=FALSE)

    scoresdata=PCAdata.pr$scores

    scoredatasz<-read.table("scoredatasz.txt",header=T,row.names=1)

    library(scatterplot3d)

    > with(scoredatasz, {

    s3d <- scatterplot3d(Comp.1, Comp.2, Comp.3,        # x y and z axis

    pch=19,        # circle color indicates no. of cylinders

    scale.y=.75,                # scale y axis (reduce by 25%)

    main="PCA analysis",

    xlab="PCA1(47.12%)",

    ylab="PCA2(16.65%)",

    zlab="PCA3(16.03%)")

    s3d.coords <- s3d$xyz.convert(Comp.1, Comp.2, Comp.3)

    text(s3d.coords$x, s3d.coords$y,    # x and y coordinates

    labels=row.names(scoredatasz),      # text to plot

    pos=4, cex=.5)                  # shrink text 50% and place to right of points)

    })

    loaddatasz<-read.table("loadsz.txt",header=T,row.names=1)

    with(loaddatasz, {

    s3d <- scatterplot3d(Comp.1, Comp.2, Comp.3,        # x y and z axis

    pch=19,        # circle color indicates no. of cylinders

    scale.y=.75,                # scale y axis (reduce by 25%)

    main="PCA analysis",

    xlab="PCA1(32.44%)",

    ylab="PCA2(22.94%)",

    zlab="PCA3(12.83%)")

    s3d.coords <- s3d$xyz.convert(Comp.1, Comp.2, Comp.3)

    text(s3d.coords$x, s3d.coords$y,    # x and y coordinates

    labels=row.names(loaddatasz),      # text to plot

    pos=4, cex=.5)                  # shrink text 50% and place to right of points)

    })

    相关文章

      网友评论

          本文标题:R语言做PCA分析及可视化代码

          本文链接:https://www.haomeiwen.com/subject/jguiixtx.html