1. 首页
  2. 问答社区

问答社区

Partial Correlation in R 用R做偏相关分析

xsmile 发布于 3个月前 分类:R

from https://www.statology.org/partial-correlation-r/

Suppose we have the following data frame that displays the current grade, total hours studied, and final exam score for 10 students:

#create data frame
df <- data.frame(currentGrade = c(82, 88, 75, 74, 93, 97, 83, 90, 90, 80),
                 hours = c(4, 3, 6, 5, 4, 5, 8, 7, 4, 6),
                 examScore = c(88, 85, 76, 70, 92, 94, 89, 85, 90, 93))

#view data frame
df

   currentGrade hours examScore
1            82     4        88
2            88     3        85
3            75     6        76
4            74     5        70
5            93     4        92
6            97     5        94
7            83     8        89
8            90     7        85
9            90     4        90
10           80     6        93

To calculate the partial correlation between each pairwise combination of variables in the dataframe, we can use the pcor() function from the ppcor library:

#calculate partial correlations
pcor(df)

$estimate
             currentGrade      hours examScore
currentGrade    1.0000000 -0.3112341 0.7355673
hours          -0.3112341  1.0000000 0.1906258
examScore       0.7355673  0.1906258 1.0000000

$p.value
             currentGrade     hours  examScore
currentGrade   0.00000000 0.4149353 0.02389896
hours          0.41493532 0.0000000 0.62322848
examScore      0.02389896 0.6232285 0.00000000

$statistic
             currentGrade      hours examScore
currentGrade    0.0000000 -0.8664833 2.8727185
hours          -0.8664833  0.0000000 0.5137696
examScore       2.8727185  0.5137696 0.0000000

$n
[1] 10

$gp
[1] 1

$method
[1] "pearson"

0个回复

  • 暂无回复

联系我们

在线咨询:点击这里给我发消息

邮件:23683716@qq.com

跳至工具栏