曲线下面积
Area under the curve
我有这样的长格式数据,有 20 个不同的变量(但它们都有相同的时间点):
Time variable value
1 0 P1 0.07
2 1 P1 0.02
3 2 P1 0.12
4 3 P1 0.17
5 4 P1 0.10
6 5 P1 0.17
66 0 P12 0.02
67 1 P12 0.11
68 2 P12 0.20
69 3 P12 0.19
70 4 P12 0.07
71 5 P12 0.20
72 6 P12 0.19
73 7 P12 0.19
74 8 P12 0.12
75 10 P12 0.13
76 12 P12 0.08
77 14 P12 NA
78 24 P12 0.07
79 0 P13 0.14
80 1 P13 0.17
81 2 P13 0.24
82 3 P13 0.24
83 4 P13 0.26
84 5 P13 0.25
85 6 P13 0.21
86 7 P13 0.21
87 8 P13 NA
88 10 P13 0.19
89 12 P13 0.14
90 14 P13 NA
91 24 P13 0.12
我想计算 time=0
和 time=24
之间每个变量的曲线下面积。理想情况下,我还想计算 y>0.1
.
曲线下的面积
我已经尝试过 pracma 包,但它只与 NA 一起出现。
trapz(x=P2ROKIlong$Time, y=P2ROKIlong$value)
我是否必须将我的数据拆分成许多不同的向量,然后手动进行,或者是否有办法从长格式数据中提取出来?
以下代码对我来说运行良好:
require(pracma)
df = data.frame(Time =c(0,1,2,3,4,5),value=c(0.07,0.02,0.12,0.17,0.10,0.17))
AUC = trapz(df$Time,df$value)
你的数据框的其余部分有什么奇怪的(NA 吗?)?
EDIT: New code based on comments
可能不是最有效的,但您的数据量似乎有限。这个 returns 一个向量 AUC_result 具有每个变量的 AUC。这是否解决了您的问题?
require(pracma)
df = data.frame(Time =c(0,1,2,3,4,5),value=c(0.07,0.02,0.12,0.17,NA,0.17),variable = c("P1","P1","P1","P2","P2","P2"))
df=df[!is.na(df$value),]
unique_groups = as.character(unique(df$variable))
AUC_result = c()
for(i in 1:length(unique_groups))
{
df_subset = df[df$variable %in% unique_groups[i],]
AUC = trapz(df_subset$Time,df_subset$value)
AUC_result[i] = AUC
names(AUC_result)[i] = unique_groups[i]
}
我有这样的长格式数据,有 20 个不同的变量(但它们都有相同的时间点):
Time variable value
1 0 P1 0.07
2 1 P1 0.02
3 2 P1 0.12
4 3 P1 0.17
5 4 P1 0.10
6 5 P1 0.17
66 0 P12 0.02
67 1 P12 0.11
68 2 P12 0.20
69 3 P12 0.19
70 4 P12 0.07
71 5 P12 0.20
72 6 P12 0.19
73 7 P12 0.19
74 8 P12 0.12
75 10 P12 0.13
76 12 P12 0.08
77 14 P12 NA
78 24 P12 0.07
79 0 P13 0.14
80 1 P13 0.17
81 2 P13 0.24
82 3 P13 0.24
83 4 P13 0.26
84 5 P13 0.25
85 6 P13 0.21
86 7 P13 0.21
87 8 P13 NA
88 10 P13 0.19
89 12 P13 0.14
90 14 P13 NA
91 24 P13 0.12
我想计算 time=0
和 time=24
之间每个变量的曲线下面积。理想情况下,我还想计算 y>0.1
.
我已经尝试过 pracma 包,但它只与 NA 一起出现。
trapz(x=P2ROKIlong$Time, y=P2ROKIlong$value)
我是否必须将我的数据拆分成许多不同的向量,然后手动进行,或者是否有办法从长格式数据中提取出来?
以下代码对我来说运行良好:
require(pracma)
df = data.frame(Time =c(0,1,2,3,4,5),value=c(0.07,0.02,0.12,0.17,0.10,0.17))
AUC = trapz(df$Time,df$value)
你的数据框的其余部分有什么奇怪的(NA 吗?)?
EDIT: New code based on comments
可能不是最有效的,但您的数据量似乎有限。这个 returns 一个向量 AUC_result 具有每个变量的 AUC。这是否解决了您的问题?
require(pracma)
df = data.frame(Time =c(0,1,2,3,4,5),value=c(0.07,0.02,0.12,0.17,NA,0.17),variable = c("P1","P1","P1","P2","P2","P2"))
df=df[!is.na(df$value),]
unique_groups = as.character(unique(df$variable))
AUC_result = c()
for(i in 1:length(unique_groups))
{
df_subset = df[df$variable %in% unique_groups[i],]
AUC = trapz(df_subset$Time,df_subset$value)
AUC_result[i] = AUC
names(AUC_result)[i] = unique_groups[i]
}