加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

使用R语言做成对的相关分析和偏相关分析

(2012-11-27 09:49:13)
标签:

r语言相关分析

杂谈

.libPaths();  #add new path
getwd(); #get the directory of working space
setwd("/home/mingyang/R/data"); #set the directory of working space

data = read.table(file="data.txt",header=TRUE); #loading the data set
edit(data);    #viewing the data set  
dim(data);    #get the dimension of data set  
names(data);    #view the names of variable  
nrow(data)
#correlation analysis  
#pairwise mode
for (i in 1:(ncol(data)-1)){
  for (j in (i+1):ncol(data)){
    lab = paste("the correlation analysis between ",names(data)[i]," and ",names(data)[j]);
    print(lab);
    re = cor.test(data[,i],data[,j]);
    print(re);
  }
}
    

#partial correlation analysis
#pairwise mode
library(ppcor);   #load the library of ppcor
for (i in 1:(ncol(data)-1)){
  for (j in (i+1):ncol(data)){
    lab = paste("the partial correlation analysis between ",names(data)[i]," and ",names(data)[j]);
    print(lab);
    re = pcor.test(data[,i],data[,j],data[,-c(i,j)]);
    print(re);
  }
}



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有