【问题标题】:Calculate correlation for all country pairs over multiple years in R计算 R 中多年所有国家对的相关性
【发布时间】:2021-09-26 08:58:23
【问题描述】:

我目前面临以下问题:根据我之前的question我需要计算多个国家/地区之间多年的相关性(Value.x 和 Value.y) . 实现这一目标的最佳方法是什么?

预期输出应该是这样的(所有国家对和所有年份)。因此,所有相关结果都存储在一个表/数据框中。


correlation country_x     country_y       Time

0.787812    Germany     United States   2000

我的数据集的前 100 行如下所示:



structure(list(LOCATION.x = c("AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS"), Country.x = c("Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia", "Australia", 
"Australia", "Australia", "Australia", "Australia"), IPC = c("A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", "A01B", 
"A01B", "A01B", "A01B"), Time = c(2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L), Value.x = c(3.3333, 
3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 
3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 
3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 
3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 
3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 3.3333, 
3.3333, 3.3333, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 
4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 
4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 
4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 
4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 
4.2143, 4.2143, 4.2143, 4.2143, 4.2143, 1.1944, 1.1944, 1.1944, 
1.1944, 1.1944, 1.1944, 1.1944, 1.1944, 1.1944, 1.1944, 1.1944, 
1.1944, 1.1944, 1.1944), LOCATION.y = c("AUS", "AUT", "BEL", 
"CAN", "CZE", "DNK", "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", 
"IRL", "ITA", "JPN", "KOR", "LUX", "MEX", "NLD", "NZL", "NOR", 
"POL", "PRT", "SVK", "ESP", "SWE", "CHE", "TUR", "GBR", "USA", 
"BRA", "CHL", "CHN", "COL", "CRI", "EST", "IND", "IDN", "ISR", 
"LVA", "LTU", "SVN", "ZAF", "AUS", "AUT", "BEL", "CAN", "CZE", 
"DNK", "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ITA", 
"JPN", "KOR", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT", 
"SVK", "ESP", "SWE", "CHE", "TUR", "GBR", "USA", "BRA", "CHL", 
"CHN", "COL", "CRI", "EST", "IND", "IDN", "ISR", "LVA", "LTU", 
"SVN", "ZAF", "AUS", "AUT", "BEL", "CAN", "CZE", "DNK", "FIN", 
"FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ITA"), Country.y = c("Australia", 
"Austria", "Belgium", "Canada", "Czech Republic", "Denmark", 
"Finland", "France", "Germany", "Greece", "Hungary", "Iceland", 
"Ireland", "Italy", "Japan", "Korea", "Luxembourg", "Mexico", 
"Netherlands", "New Zealand", "Norway", "Poland", "Portugal", 
"Slovak Republic", "Spain", "Sweden", "Switzerland", "Turkey", 
"United Kingdom", "United States", "Brazil", "Chile", "China", 
"Colombia", "Costa Rica", "Estonia", "India", "Indonesia", "Israel", 
"Latvia", "Lithuania", "Slovenia", "South Africa", "Australia", 
"Austria", "Belgium", "Canada", "Czech Republic", "Denmark", 
"Finland", "France", "Germany", "Greece", "Hungary", "Iceland", 
"Ireland", "Italy", "Japan", "Korea", "Luxembourg", "Mexico", 
"Netherlands", "New Zealand", "Norway", "Poland", "Portugal", 
"Slovak Republic", "Spain", "Sweden", "Switzerland", "Turkey", 
"United Kingdom", "United States", "Brazil", "Chile", "China", 
"Colombia", "Costa Rica", "Estonia", "India", "Indonesia", "Israel", 
"Latvia", "Lithuania", "Slovenia", "South Africa", "Australia", 
"Austria", "Belgium", "Canada", "Czech Republic", "Denmark", 
"Finland", "France", "Germany", "Greece", "Hungary", "Iceland", 
"Ireland", "Italy"), Value.y = c(3.3333, 0, 0, 0.6667, 0, 1.1429, 
0.7333, 7.0667, 3.29, 0, 0, 0, 0, 1, 0.2, 1, 0, 0, 0.4762, 0, 
3, 0, 0, 0, 0, 3, 0, 0, 2.5, 13.2, 0, 0, 0, 0, 0, 0, 0.5, 0, 
0, 0, 0, 0, 1, 4.2143, 0.3333, 0, 0, 0, 3, 1, 2.5, 5.0833, 0, 
0, 0, 0, 1.4167, 0.5667, 0, 0, 0.5, 1.6278, 0.5, 1, 0, 0, 0, 
0, 0, 0, 0, 2.45, 9.1742, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0.1, 1.1944, 0.5, 0.5, 2.3409, 0.0909, 0, 0.6, 8, 6.0286, 0, 
0, 0, 0, 1.5)), row.names = c(NA, -100L), groups = structure(list(
    IPC = "A01B", .rows = structure(list(1:100), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

为了响应 cmets,我添加了这个示例数据集以用于教学目的。它应该与@r2evans 提供的代码一起使用。 (假设所列国家在 2000 年和 2001 年只有两个子维度 A01B 和 A01C。)


structure(list(LOCATION.x = c("AUS", "AUS", "USA", "USA", "AUS", 
"AUS", "USA", "USA"), Country.x = c("Australia", "Australia", 
"United States", "United States", "Australia", "Australia", "United States", 
"United States"), IPC = c("A01B", "A01C", "A01B", "A01C", "A01B", 
"A01C", "A01B", "A01C"), Time = c(2000, 2000, 2000, 2000, 2001, 
2001, 2001, 2001), Value.x = c(7, 42, 92, 1, 70, 87, 88, 3), 
    LOCATION.y = c("DEU", "DEU", "DEU", "DEU", "DEU", "DEU", 
    "DEU", "DEU"), Country.y = c("Germany", "Germany", "Germany", 
    "Germany", "Germany", "Germany", "Germany", "Germany"), Value.y = c(97, 
    85, 25, 14, 15, 97, 97, 52)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))


非常感谢!

【问题讨论】:

  • 据我所知,Country.xCountry.y 的每个组合每年只有一个唯一的 x 和 y 值。您希望如何从这些单个数据点计算相关性?还是我误解了你的问题?
  • @Rory S:我相信你的解释是正确的。由于空间限制,我只上传了有限的数据集。然后我会修改它以使事情更清楚。感谢您的意见!

标签: r loops dplyr correlation


【解决方案1】:

我认为您可以为此做一个简单的summarize

dat %>%
  group_by(Country.x, Country.y) %>%
  summarize(corr = cor(Value.x, Value.y)) %>%
  filter(complete.cases(.))
# Warning in cor(Value.x, Value.y) : the standard deviation is zero
##### repeated 21 times
# # A tibble: 21 x 3
# # Groups:   Country.x [1]
#    Country.x Country.y        corr
#    <chr>     <chr>           <dbl>
#  1 Australia Australia       1    
#  2 Australia Austria        -0.539
#  3 Australia Belgium        -0.959
#  4 Australia Canada         -1.00 
#  5 Australia Czech Republic -0.959
#  6 Australia Denmark         0.931
#  7 Australia Finland         0.911
#  8 Australia France         -0.825
#  9 Australia Germany        -0.550
# 10 Australia India          -1    
# # ... with 11 more rows

有更多数据并希望将Time 添加到您的分组中,然后使用

dat %>%
  group_by(Time, Country.x, Country.y) %>%
  summarize(corr = cor(Value.x, Value.y))

由于样本量小,这在这里不起作用,但应该适用于更大的数据。

【讨论】:

  • 您建议的代码运行良好!非常感谢。明天我将添加第二个示例数据集,以便更好地理解我的问题和您的解决方案,因为根据您和@Rory S 的反馈,前 100 行不足以理解。
  • 按照承诺,我添加了另一个数据集用于教学目的。它适用于您的代码并确认 时间 维度。
猜你喜欢
  • 2021-09-26
  • 1970-01-01
  • 2017-08-09
  • 2023-03-27
  • 1970-01-01
  • 2011-10-09
  • 2018-06-11
  • 1970-01-01
  • 2015-04-07
相关资源
最近更新 更多