我有 100 名受试者的血液浓度与时间的数据。我有兴趣绘制 5%、50% 和 95% 分位数浓度与时间的曲线。虽然我可以确定整个浓度范围的分位数,但我无法在 R 中弄清楚如何按时间对浓度分位数进行分层。任何帮助将不胜感激。
a<-quantile(conc~time, 0.05)
不起作用。
最佳答案
假设数据框 df
包含列 df$subject、df$time 和 df$conc
,则
q <- sapply(c(low=0.05,med=0.50,high=0.95),
function(x){by(df$conc,df$time,quantile,x)})
生成一个矩阵 q
,其中 low
、med
和 high
列包含 5、50 ,和 95% 分位数,每次一行。完整代码如下。
# generate some moderately realistic data
# concentration declines exponentially over time
# rate (k) is different for each subject and distributed as N[50,10]
# measurement error is distributed as N[1, 0.2]
time <- 1:1000
df <- data.frame(subject=rep(1:100, each=1000),time=rep(time,100))
k <- rnorm(100,50,10) # rate is different for each subject
df$conc <- 5*exp(-time/k[df$subject])+rnorm(100000,1,0.2)
# generates a matrix with columns low, med, and high
q <- sapply(c(low=0.05,med=0.50,high=0.95),
function(x){by(df$conc,df$time,quantile,x)})
# prepend time and convert to dataframe
q <- data.frame(time,q)
# plot the results
library(reshape2)
library(ggplot2)
gg <- melt(q, id.vars="time", variable.name="quantile", value.name="conc")
ggplot(gg) +
geom_line(aes(x=time, y=conc, color=quantile))+
scale_color_discrete(labels=c("5%","50%","95%"))
关于r - 连续时间数据的分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20846560/