R 中多样性的 Blau 指数
Blau index of Diversity in R
我正在尝试在我的数据框上计算 R 中的 Blau 多样性指数 (gini-simpson)。对于一组中的每个人,我有 6 列,值范围为 "Student"、"Faculty"、"Alumni" "Not Applicable"。如果组小于 6,列中也有 NA。
我想计算跨行的 Blau 指数(整个组的多样性),而不是在每列中,na.rm= TRUE。
有谁知道如何在 R 中执行此操作?
非常感谢!
See here for a picture of data frame
我们可以很容易地用手算出基尼-辛普森指数。
首先,我将生成一些示例数据:
# Generate sample data
set.seed(2017);
type <- c("Student", "Faculty", "Alumni");
data <- sample(type, 6 * 20, replace = TRUE);
# Replace 40 entries with NAs
set.seed(2017);
data[sample(6 * 20, 40)] <- NA;
# Reformat as 6 column dataframe
df <- as.data.frame(matrix(data, ncol = 6), stringsAsFactors = FALSE);
names(df) <- paste0("e", seq(1:6), "_affiliation");
head(df);
#e1_affiliation e2_affiliation e3_affiliation e4_affiliation e5_affiliation
#1 <NA> Faculty <NA> Student Student
#2 <NA> <NA> <NA> Faculty Alumni
#3 <NA> Alumni Student Faculty Faculty
#4 Student <NA> <NA> <NA> <NA>
#5 <NA> Student Alumni Alumni Student
#6 Alumni Alumni Faculty Faculty Student
# e6_affiliation
#1 Alumni
#2 Alumni
#3 <NA>
#4 Student
#5 Faculty
#6 Student
Gini-Simpson (= Gibbs-Martin = Blau) index of diversity 由
给出
其中R表示类型总数,是第i类型的比例丰度。
我们定义了一个函数,它接受字符串向量和 returnGS 索引:
# Define function to calculate the Gini-Simpson index
# We ensure the same levels (present or absent) of x
# by factor(x, levels = type)
# Note that NAs will not be considered by default
get.GS.index <- function(x, type) {
x <- factor(x, levels = type);
return(1 - sum(prop.table(table(x))^2));
}
我们现在可以将 get.GS.index
应用于数据框的所有行
apply(df, 1, get.GS.index, type)
#[1] 0.6250000 0.4444444 0.6250000 0.0000000 0.6400000 0.6666667 0.5000000
#[8] 0.6250000 0.6400000 0.5000000 0.4444444 0.6400000 0.3750000 0.3750000
#[15] 0.0000000 0.0000000 0.6111111 0.4444444 0.6666667 0.6400000
更新
如果组中只有一种类型,我们可以将函数 get.GS.index
修改为 return NA
。
get.GS.index <- function(x, type) {
x <- factor(x, levels = type);
t <- table(x);
if (length(t[t>0]) == 1) return(NA) else return(1 - sum(prop.table(t)^2));
}
apply(df, 1, get.GS.index, type);
# [1] 0.6250000 0.4444444 0.6250000 NA 0.6400000 0.6666667 0.5000000
# [8] 0.6250000 0.6400000 0.5000000 0.4444444 0.6400000 0.3750000 0.3750000
#[15] NA NA 0.6111111 0.4444444 0.6666667 0.6400000
我正在尝试在我的数据框上计算 R 中的 Blau 多样性指数 (gini-simpson)。对于一组中的每个人,我有 6 列,值范围为 "Student"、"Faculty"、"Alumni" "Not Applicable"。如果组小于 6,列中也有 NA。
我想计算跨行的 Blau 指数(整个组的多样性),而不是在每列中,na.rm= TRUE。
有谁知道如何在 R 中执行此操作?
非常感谢!
See here for a picture of data frame
我们可以很容易地用手算出基尼-辛普森指数。
首先,我将生成一些示例数据:
# Generate sample data
set.seed(2017);
type <- c("Student", "Faculty", "Alumni");
data <- sample(type, 6 * 20, replace = TRUE);
# Replace 40 entries with NAs
set.seed(2017);
data[sample(6 * 20, 40)] <- NA;
# Reformat as 6 column dataframe
df <- as.data.frame(matrix(data, ncol = 6), stringsAsFactors = FALSE);
names(df) <- paste0("e", seq(1:6), "_affiliation");
head(df);
#e1_affiliation e2_affiliation e3_affiliation e4_affiliation e5_affiliation
#1 <NA> Faculty <NA> Student Student
#2 <NA> <NA> <NA> Faculty Alumni
#3 <NA> Alumni Student Faculty Faculty
#4 Student <NA> <NA> <NA> <NA>
#5 <NA> Student Alumni Alumni Student
#6 Alumni Alumni Faculty Faculty Student
# e6_affiliation
#1 Alumni
#2 Alumni
#3 <NA>
#4 Student
#5 Faculty
#6 Student
Gini-Simpson (= Gibbs-Martin = Blau) index of diversity 由
给出其中R表示类型总数,
我们定义了一个函数,它接受字符串向量和 returnGS 索引:
# Define function to calculate the Gini-Simpson index
# We ensure the same levels (present or absent) of x
# by factor(x, levels = type)
# Note that NAs will not be considered by default
get.GS.index <- function(x, type) {
x <- factor(x, levels = type);
return(1 - sum(prop.table(table(x))^2));
}
我们现在可以将 get.GS.index
应用于数据框的所有行
apply(df, 1, get.GS.index, type)
#[1] 0.6250000 0.4444444 0.6250000 0.0000000 0.6400000 0.6666667 0.5000000
#[8] 0.6250000 0.6400000 0.5000000 0.4444444 0.6400000 0.3750000 0.3750000
#[15] 0.0000000 0.0000000 0.6111111 0.4444444 0.6666667 0.6400000
更新
如果组中只有一种类型,我们可以将函数 get.GS.index
修改为 return NA
。
get.GS.index <- function(x, type) {
x <- factor(x, levels = type);
t <- table(x);
if (length(t[t>0]) == 1) return(NA) else return(1 - sum(prop.table(t)^2));
}
apply(df, 1, get.GS.index, type);
# [1] 0.6250000 0.4444444 0.6250000 NA 0.6400000 0.6666667 0.5000000
# [8] 0.6250000 0.6400000 0.5000000 0.4444444 0.6400000 0.3750000 0.3750000
#[15] NA NA 0.6111111 0.4444444 0.6666667 0.6400000