r - 连续时间数据的分位数

标签 r plot continuous quantile

我有 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,其中 lowmedhigh 列包含 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/

相关文章:

r - 如何在 R 中的多面板图中为每个面板指定不同的颜色

php - 如何在没有用户访问页面的情况下运行 php 代码?

r - ggplot : arranging boxplots of multiple y-variables for each group of a continuous x

r - 创建下三角遗传距离矩阵

java - 通过 Rserve 将多维数组从 Java 分配给 R

r - 在 R 警官中,如何为 PowerPoint 设置文本和项目符号级别的格式

c++ - 实时 QCustomPlot 的设置

R - 表情符号 unicode 到字符

plot - 在 .octaverc 中设置默认绘图线宽

c# - 使用线程连续检查值的更改