一种简单的方法是计算总分或平均值。另一种方法不会假设所有变量都同等重要,我们可以计算加权平均值。
假设我们有以下 10 名患者和变量v1
。v5
> set.seed(1)
> df <- data.frame(v1 = sample(1:5, 10, replace = TRUE),
+ v2 = sample(1:5, 10, replace = TRUE),
+ v3 = sample(1:5, 10, replace = TRUE),
+ v4 = sample(1:5, 10, replace = TRUE),
+ v5 = sample(1:5, 10, replace = TRUE))
>
> df
v1 v2 v3 v4 v5
1 2 2 5 3 5
2 2 1 2 3 4
3 3 4 4 3 4
4 5 2 1 1 3
5 2 4 2 5 3
6 5 3 2 4 4
7 5 4 1 4 1
8 4 5 2 1 3
9 4 2 5 4 4
10 1 4 2 3 4
1.总分和排名
> df$sum <- rowSums(df)
> df$ranks <- abs(rank(df$sum) - (dim(df)[1] + 1))
> df
v1 v2 v3 v4 v5 sum ranks
1 2 2 5 3 5 17 4.0
2 2 1 2 3 4 12 9.5
3 3 4 4 3 4 18 2.5
4 5 2 1 1 3 12 9.5
5 2 4 2 5 3 16 5.0
6 5 3 2 4 4 18 2.5
7 5 4 1 4 1 15 6.5
8 4 5 2 1 3 15 6.5
9 4 2 5 4 4 19 1.0
10 1 4 2 3 4 14 8.0
2. 平均分和排名(注:ranks
和ranks2
是相等的)
> df$means <- apply(df[, 1:5], 1, mean)
> df$ranks2 <- abs(rank(df$mean) - (dim(df)[1] + 1))
> df
v1 v2 v3 v4 v5 sum ranks means ranks2
1 2 2 5 3 5 17 4.0 3.4 4.0
2 2 1 2 3 4 12 9.5 2.4 9.5
3 3 4 4 3 4 18 2.5 3.6 2.5
4 5 2 1 1 3 12 9.5 2.4 9.5
5 2 4 2 5 3 16 5.0 3.2 5.0
6 5 3 2 4 4 18 2.5 3.6 2.5
7 5 4 1 4 1 15 6.5 3.0 6.5
8 4 5 2 1 3 15 6.5 3.0 6.5
9 4 2 5 4 4 19 1.0 3.8 1.0
10 1 4 2 3 4 14 8.0 2.8 8.0
3.加权平均分(即我假设V3和V4比v1、v2或v5更重要)
> weights <- c(0.5, 0.5, 1, 1, 0.5)
> wmean <- function(x, w = weights){weighted.mean(x, w = w)}
> df$wmeans <- sapply(split(df[, 1:5], 1:10), wmean)
> df$ranks3 <- abs(rank(df$wmeans) - (dim(df)[1] + 1))
> df
v1 v2 v3 v4 v5 sum ranks means ranks2 wmeans ranks3
1 2 2 5 3 5 17 4.0 3.4 4.0 3.571429 2.5
2 2 1 2 3 4 12 9.5 2.4 9.5 2.428571 9.0
3 3 4 4 3 4 18 2.5 3.6 2.5 3.571429 2.5
4 5 2 1 1 3 12 9.5 2.4 9.5 2.000000 10.0
5 2 4 2 5 3 16 5.0 3.2 5.0 3.285714 5.0
6 5 3 2 4 4 18 2.5 3.6 2.5 3.428571 4.0
7 5 4 1 4 1 15 6.5 3.0 6.5 2.857143 6.0
8 4 5 2 1 3 15 6.5 3.0 6.5 2.571429 8.0
9 4 2 5 4 4 19 1.0 3.8 1.0 4.000000 1.0
10 1 4 2 3 4 14 8.0 2.8 8.0 2.714286 7.0