【问题标题】:Calculate correlation between two variables on two different scales by class [closed]按类别计算两个不同尺度上两个变量之间的相关性[关闭]
【发布时间】:2021-12-26 21:31:11
【问题描述】:

我将两个数据集与三个变量组合在一起:class、total1、total2。 total1 是每个学生的个人平均分,total2 是老师对每个班级的评估的平均值。两种不同量表的平均分。

df
    class   total1   total2
    A       4.9      6.7
    A       3.8      6.7
    A       4.2      6.7
    B       4.5      7.2
    B       3.9      7.2 
    B       4.1      7.2
    C       3.5      6.5
    C       4.4      6.5
    C       3.6      6.5

我想用 r 和 p 值计算 total1 和 total2 之间的相关性。我使用了这段代码,但无法按班级获得 total1 的平均分:

library("ggpubr")
ggscatter(df, x = "total2", y = "total1", 
          add = "reg.line", conf.int = TRUE, 
          cor.coef = TRUE, cor.method = "spearman",
          xlab = "Teacher score", ylab = "Student score")

【问题讨论】:

  • 我是否正确,有三个不同的班级,每个班级有三个不同的学生,每个班级有一个单独的老师?
  • 你能发帖dput(df)吗?

标签: r correlation


【解决方案1】:

您可以使用 dplyr 库。

library(dplyr)

class <- c("A", "A", "A", "B", "B", "B", "C", "C", "C")
total1 <- c(4.9, 3.8, 4.2, 4.5, 3.9, 4.1, 3.5, 4.4, 3.6)
total2 <- c(6.7, 6.7, 6.7, 7.2, 7.2, 7.2, 6.5, 6.5, 6.5)

df <- data.frame(class,total1,total2)
sum_data <- df %>% 
  group_by(class) %>% 
  summarise(total1_mean=mean(total1),total2_mean=mean(total2))

sum_data

# A tibble: 3 x 3
#  class total1_mean total2_mean
#  <chr>       <dbl>       <dbl>
#1 A            4.3          6.7
#2 B            4.17         7.2
#3 C            3.83         6.5



res <- cor.test(sum_data$total1_mean,sum_data$total2_mean) 
res

#Pearson's product-moment correlation

#data:  sum_data$total1_mean and sum_data$total2_mean
#t = 0.57735, df = 1, p-value = 0.6667
#alternative hypothesis: true correlation is not equal to 0
#sample estimates:
#cor 
#0.5

res$p.value #[1] 0.6666667 
res$estimate #cor 0.5

【讨论】:

    【解决方案2】:
    s <- "class   total1   total2
        A       4.9      6.7
        A       3.8      6.7
        A       4.2      6.7
        B       4.5      7.2
        B       3.9      7.2 
        B       4.1      7.2
        C       3.5      6.5
        C       4.4      6.5
        C       3.6      6.5
    "
    
    df <- read.table(text=s, header=TRUE)
    dfs <- split(df, df$class)
    
    avg_m <- t(sapply(dfs, function(df) colMeans(df[, -1])))
    res <- cor.test(x=avg_m[,"total1"], y=avg_m[, "total2"])
    res$estimate ## 0.5
    res$p.value  ## [1] 0.6666667
    

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2022-11-22
      • 2017-08-22
      相关资源
      最近更新 更多