R安装ggbiplot在windows下的调试过程以及使用方法

有文章说需要安装上devtools包,发现直接用install.packages("devtools")装上即可

但是在library(devtools)的时候提示:

WARNING: Rtools is required to build R packages, but is not currently installed.


Please download and install Rtools 3.1 from http://cran.r-project.org/bin/windows/Rtools/ and then run find_rtools().

这说明需要装上Rtools,所以直接去网页上去下载即可,这里选择的是Rtools32

http://cran.r-project.org/bin/windows/Rtools/

装上之后,发现提示:

WARNING: Rtools is required to build R packages, but no version of Rtools compatible with R 3.1.3 was found. (Only the following incompatible version(s) of Rtools were found:3.2)


Please download and install Rtools 3.1 from http://cran.r-project.org/bin/windows/Rtools/ and then run find_rtools().
于是乎,下载一下31版本的,再次运行find_rtools()就会得到结果TRUE

library(devtools)就会通过

######################################################################################

上面说了安装的问题,下面转入正题,说一下biplot,下面贴一段维基百科的解释:

A biplot is constructed by using the singular value decomposition (SVD) to obtain a low-rank approximation to a transformed version

biplot利用奇异值分解得到一个低阶近似转化为一个矩阵X,

 of the data matrix X, whose n rows are the samples (also called the cases, or objects), and whose p columns are the variables. The transformed data matrix Y is obtained from the original matrix X by centering and optionally standardizing the columns (the variables). 

n行分别是各个样品(也叫case,或者对象),p列是变量,通过中心对齐和选择列标准化可有原始矩阵得到Y

Using the SVD, we can write Y = ∑k=1,...pdkukvkT;, where the uk are n-dimensional column vectors, the vk are p-dimensional column vectors, and the dk are a non-increasing sequence of non-negative scalars. The biplot is formed from two scatterplots that share a common set of axes and have a between-set scalar product interpretation. 

通过SVD,我们可以把Y写作Y = ∑k=1,...pdkukvkT  ,这里uk  是n维列向量,vk 是p维列向量,dk 是一个非增加的非负序列标量。biplot有两个散点图组成,他们共用同一个轴,其可以由两组间的内积解释。

The first scatterplot is formed from the points (d1αu1i,  d2αu2i), for i = 1,...,n. The second plot is formed from the points (d11-αv1jd21-αv2j), for j = 1,...,p

第一组散点图由点d1αu1i,  d2αu2i), for i = 1,...,n.构成,第二组散点图由点(d11-αv1jd21-αv2j), for j = 1,...,p. 构成

This is the biplot formed by the dominant two terms of the SVD, which can then be represented in a two-dimensional display.

以上即是构成biplot的两个线性的SVD,这样就可以在一个二维的图中展示了。

 Typical choices of α are 1 (to give a distance interpretation to the row display) and 0 (to give a distance interpretation to the column display), and in some rare cases α=1/2 to obtain a symmetrically scaled biplot (which gives no distance interpretation to the rows or the columns, but only the scalar product interpretation).

典型的选择是α为1(这样是行展示)和0(列展示),在甚少的情况下选择1/2得到对称扩展的biplot图(由内积决定行和列展示)

 The set of points depicting the variables can be drawn as arrows from the origin to reinforce the idea that they represent biplot axes onto which the samples can be projected to approximate the original data.

描绘表的变量可以画成从原始到增强数据之间的箭头来表示biplot轴在原始样本上的投影。可以参考3网页上面的图

############################################################################################

下面说一下R脚本的实现问题

#主成分分析
student.pr <- princomp(student, cor = TRUE)
#显示结果
summary(student.pr, loadings=TRUE)
#预测,显示各样本主成分的值
pre<-predict(student.pr)
#显示碎石图
screeplot(student.pr,type="lines")
# 主成分分析散点图
biplot(student.pr)  [3]

数据来自于参考3,人大经济论坛上的一个例子

其实参考4上的第一个例子也有一份数据,大家可以看看

library(plyr)
 library(scales)
 library(grid)
library(ggbiplot)

载入所需要的包

p <- ggbiplot(
    prcomp(dat, scale. = FALSE)
  , obs.scale = 1
  , var.scale = 1
  , labels =  rownames(dat)
  , pc.biplot = FALSE
  )
print(p)
[4]
具体的dat的数据参考4中所给的值


下面还有一种实现方法:


PCbiplot <- function(PC, x="PC1", y="PC2") {
    # PC being a prcomp object
    data <- data.frame(obsnames=row.names(PC$x), PC$x)
    plot <- ggplot(data, aes_string(x=x, y=y)) + geom_text(alpha=.4, size=3, aes(label=obsnames))
    plot <- plot + geom_hline(aes(0), size=.2) + geom_vline(aes(0), size=.2)
    datapc <- data.frame(varnames=rownames(PC$rotation), PC$rotation)
    mult <- min(
        (max(data[,y]) - min(data[,y])/(max(datapc[,y])-min(datapc[,y]))),
        (max(data[,x]) - min(data[,x])/(max(datapc[,x])-min(datapc[,x])))
        )
    datapc <- transform(datapc,
            v1 = .7 * mult * (get(x)),
            v2 = .7 * mult * (get(y))
            )
    plot <- plot + coord_equal() + geom_text(data=datapc, aes(x=v1, y=v2, label=varnames), size = 5, vjust=1, color="red")
    plot <- plot + geom_segment(data=datapc, aes(x=0, y=0, xend=v1, yend=v2), arrow=arrow(length=unit(0.2,"cm")), alpha=0.75, color="red")
    plot
}

fit <- prcomp(USArrests, scale=T)
PCbiplot(fit)

关于图中箭头的解释可以参考一下连接[6]

Rotation: 
          PC1     PC2     PC3       PC4 
Murder   -0.5358995 0.4181809 -0.3412327 0.64922780 
Assault -0.5831836 0.1879856 -0.2681484 -0.74340748 
UrbanPop -0.2781909 -0.8728062 -0.3780158 0.13387773 
Rape   -0.5434321 -0.1673186 0.8177779 0.08902432 
这是主成分负荷系数。(principal component loadings)他们表示的是各个成分和原来变量之间的相关程度。值得注意的是,在分析求解的过程中限定每个成分的各个负荷的平方之和为1(0.5358995^2+0.5831836^2+0.2781909^2+0.5434321^2=1),因此如果一个成分中是接近0的负荷较多,那么其系数就比较大,因此不同成分之间的负荷不能直接比较。如果要比较,要使用个主成分的标准差加权。(因为主成分的各个标准差可以衡量主成分含有的原数据中信息的多少。) 
上面提到的这种限制可以通过如下命令看到: 
> t(fit.pca$ro)%*%fit.pca$ro 
        PC1       PC2       PC3       PC4 
PC1 1.000000e-00 9.400710e-17 2.978845e-17 -2.497223e-16 
PC2 9.400710e-17 1.000000e-00 -1.856696e-18 -2.355853e-17 
PC3 2.978845e-17 -1.856696e-18 1.000000e-00 1.376937e-17 
PC4 -2.497223e-16 -2.355853e-17 1.376937e-17 1.000000e-00 
如果觉得这个东西太难看,可以对于对小数点后面的东西四舍五入,则: 
> round(t(fit.pca$ro)%*%fit.pca$ro) 
  PC1 PC2 PC3 PC4 
PC1   1   0   0   0 
PC2   0   1   0   0 
PC3   0   0   1   0 
PC4   0   0   0   1 


在biplot图中两个坐标对应各自的成分。红色的箭头的长度表示负荷的长度,方向表示符合的符号是正还是负,而各个点是各个个案对应的成分得分。(详细的说明,可以到splus的主页现在说明文档。这个还没有去看,等看完再更新) 






ref:

1:http://yife.im/install-ggbiplot-in-r-of-ubuntu/

2:http://en.wikipedia.org/wiki/Biplot

3:http://bbs.pinggu.org/thread-1384773-1-1.html

4:http://stackoverflow.com/questions/25087428/ggbiplot-and-biplot-in-base-r-producing-two-different-biplots

5:http://stackoverflow.com/questions/6578355/plotting-pca-biplot-with-ggplot2

6:http://www.360doc.com/content/14/1103/09/17553313_422099670.shtml



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值