创建肯德尔相关矩阵
creating kendall correlation matrix
我有这样的数据:
总共 38 列。
数据代码示例:
df <- structure(
list(
Christensenellaceae = c(
0.010484508,
0.008641566,
0.010017172,
0.010741488,
0.1,
0.2,
0.3,
0.4,
0.7,
0.8,
0.9,
0.1,
0.3,
0.45,
0.5,
0.55
),
Date=c(27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28),
Treatment = c(
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 2",
"Treatment 2",
"Treatment 2",
"Treatment 2",
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 2",
"Treatment 2",
"Treatment 2",
"Treatment 2"
)
),class = "data.frame",
row.names = c(NA,-9L)
)
我想做的是为每一列(治疗和日期除外)在治疗类型(总共 10 种,但示例中有 2 种)之间创建肯德尔相关矩阵(数据没有线性行为),所以总共 36大小为 1010 的相关矩阵(此处为 22).
这是我的代码:
res2 <- cor(as.matrix(data),method ="kendall")
但我收到错误消息:
Error in cor(data, method = "kendall") : 'x' must be numeric
有什么办法可以解决这个问题吗?谢谢:)
您可以使用 tidyverse
方法来做到这一点,首先进行一些数据整理,然后使用 correlate
计算每个变量组合的成对相关性。
library(corrr)
library(tidyverse)
df |>
# Transform data into wide format
pivot_wider(id_cols = Date,
names_from = Treatment,
values_from = -starts_with(c("Treatment", "Date"))) |>
# Unnest lists inside each column
unnest(cols = starts_with("Treatment")) |>
# Remove Date from the columns
select(-Date) |>
# Correlate all columns using kendall
correlate(method = "kendall")
# A tibble: 2 x 3
# term `Treatment 1` `Treatment 2`
# <chr> <dbl> <dbl>
#1 Treatment 1 NA 0.546
#2 Treatment 2 0.546 NA
我有这样的数据:
总共 38 列。 数据代码示例:
df <- structure(
list(
Christensenellaceae = c(
0.010484508,
0.008641566,
0.010017172,
0.010741488,
0.1,
0.2,
0.3,
0.4,
0.7,
0.8,
0.9,
0.1,
0.3,
0.45,
0.5,
0.55
),
Date=c(27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28),
Treatment = c(
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 2",
"Treatment 2",
"Treatment 2",
"Treatment 2",
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 1",
"Treatment 2",
"Treatment 2",
"Treatment 2",
"Treatment 2"
)
),class = "data.frame",
row.names = c(NA,-9L)
)
我想做的是为每一列(治疗和日期除外)在治疗类型(总共 10 种,但示例中有 2 种)之间创建肯德尔相关矩阵(数据没有线性行为),所以总共 36大小为 1010 的相关矩阵(此处为 22).
这是我的代码:
res2 <- cor(as.matrix(data),method ="kendall")
但我收到错误消息:
Error in cor(data, method = "kendall") : 'x' must be numeric
有什么办法可以解决这个问题吗?谢谢:)
您可以使用 tidyverse
方法来做到这一点,首先进行一些数据整理,然后使用 correlate
计算每个变量组合的成对相关性。
library(corrr)
library(tidyverse)
df |>
# Transform data into wide format
pivot_wider(id_cols = Date,
names_from = Treatment,
values_from = -starts_with(c("Treatment", "Date"))) |>
# Unnest lists inside each column
unnest(cols = starts_with("Treatment")) |>
# Remove Date from the columns
select(-Date) |>
# Correlate all columns using kendall
correlate(method = "kendall")
# A tibble: 2 x 3
# term `Treatment 1` `Treatment 2`
# <chr> <dbl> <dbl>
#1 Treatment 1 NA 0.546
#2 Treatment 2 0.546 NA