新浪博客

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

2012-11-27 09:49阅读:
.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);
}
}

我的更多文章

下载客户端阅读体验更佳

APP专享