测试数据:
PopulationIncomeIlliteracyLife ExpMurderHS Grad
361536242.169.0515.141.3
36563151.569.3111.366.7
221245301.870.557.858.1
211033781.970.6610.139.9
2119851141.171.7110.362.6
254148840.772.066.863.9
310053481.172.483.156
57948090.970.066.254.6
827748151.370.6610.752.6
49314091268.5413.940.6
86849631.973.66.261.9
81341190.671.875.359.5
1119751070.970.1410.352.6
531344580.770.887.152.9
286146280.572.562.359
228046690.672.584.559.9
338737121.670.110.638.5
380635452.868.7613.242.2
105836940.770.392.754.7
412252990.970.228.552.3
581447551.171.833.358.5
911147510.970.6311.152.8
392146750.672.962.357.6
234130982.468.0912.541
476742540.870.699.348.8
74643470.670.56559.2
154445080.672.62.959.3
59051490.569.0311.565.2
81242810.771.233.357.6
733352371.170.935.252.5
114436012.270.329.755.2
1807649031.470.5510.952.7
544138751.869.2111.138.5
63750870.872.781.450.3
1073545610.870.827.453.2
271539831.171.426.451.6
228446600.672.134.260
118604449170.436.150.2
93145581.371.92.446.4
281636352.367.9611.637.8
68141670.572.081.753.3
417338211.770.111141.8
1223741882.270.912.247.4
120340220.672.94.567.3
47239070.671.645.557.1
498147011.470.089.547.8
355948640.671.724.363.5
179936171.469.486.741.6
458944680.772.48354.5
37645660.670.296.962.9
#######偏相关分析
#更新时间2015.7.3
#依赖包:ggm和psych
#输入参数:输入数据pcordata
#输出结果:输出序号1(输出标签:变量的协方差)对应数据集s
# 输出序号2(输出标签:偏相关系数)对应数据集r
# 输出序号3(输出标签:偏相关系数显著性检验)对应数据集pcor_test
#读取数据
pcordata=read.csv("C:\\Users\\Administrator\\Desktop\\pcor_test_data.csv")
#加载ggm包
install.packages("ggm")
library(ggm)
##偏相关系数的计算
#函数调用格式:pcor(u,s)
#其中u是一个数值向量,前两个数值表示要计算相关系数的变量下标,其余数值为条件变量的下标
jsbl <- c(1,5) #要计算的相关系数的变量下标
tjbl <- c(2,3,6) #条件(控制)变量的下标,即要排除影响的变量的下标
u <- c(jsbl,tjbl)
s <- cov(pcordata) #变量的协方差(输出序号1)
r <- pcor(u,s) #偏相关系数(输出序号2)
#加载psych包
install.packages("psych")
library(psych)
##偏相关系数显著性检验
#函数调用格式:pcor.test(r,q,n)
#其中r是由pcor()函数计算得到的偏相关系数,q为要控制的变量数(以数值表示位置),n为样本大小
q <- length(tjbl) #计算要控制的变量数
n <- dim(pcordata)[1] #计算样本量
pcor_test <- pcor.test(r,q,n) #偏相关系数显著性检验结果(输出序号3)
R语言交流QQ群:99598210,欢迎加入!
点击链接加入:http://jq.qq.com/?_wv=1027&k=2GdJIAQ
小雄R语言学习笔记,转载请注明出处,谢谢!官网:http://www.zhangzhengxiong.com
发表评论
已有1位网友发表了看法: