为SITAR平均增长曲线增加95%的置信区间

时间:2019-05-09 13:53:29

标签: r ggplot2 plot

我使用sitar软件包将SITAR(用于模拟生长生物学的生长曲线分析形式)拟合为高度数据。我使用该模型制作了显示峰值生长速度下的平均生长曲线,平均速度曲线和平均年龄的图。

library(sitar)

data <- na.omit(berkeley[berkeley$sex == 2 & berkeley$age >= 8 & 
berkeley$age <= 18, c('id', 'age', 'height')])

sitar_model <- sitar(x = age, y = height, id = id, data = data, df = 5)


#PLOT
par(mar = c(4,4,1,1) + 0.1, cex = 0.8)
plot(sitar_model, opt = 'd', las = 1, apv = TRUE)
plot(sitar_model, opt = 'v', las = 1, apv = TRUE, lty = 2)

我希望这些图在平均线周围包括上下95%置信区间的线。

1 个答案:

答案 0 :(得分:0)

library(sitar)

data <- na.omit(berkeley[berkeley$sex == 2 & berkeley$age >= 8 & berkeley$age <= 18, 
                   c('id', 'age', 'height')])
sitar_model <- sitar(x = age, y = height, id = id, data = data, df = 5)

plot(sitar_model, opt = c('d',  'v'), las = 1, apv = F, 
 legend = NULL, ylim = c(100,200),  
 vlab="", ylab="", xlab="",
 y2par=list(lwd=2), vlim = c(0,12), lwd=2,
 main="Mean and 95 CI growth curves")
lines(sitar_model, 
  opt=c('d',  'v'), y2par=list(col='light blue', lwd=1.5, ylim = c(0,12)), 
  apv = F, lwd=1.5, lty=1, col='light blue', abc= 
(sqrt(diag(getVarCov(sitar_model)))*1.96), vlim = c(0,10), ylim = c(100,200))
lines(sitar_model, 
  opt=c('d',  'v'), y2par=list(col='light gray', lwd=1.5, ylim = c(0,12)), apv = F, lwd=1.5, 
  col='light gray', lty=1, abc=(sqrt(diag(getVarCov(sitar_model)))*1.96), vlim = c(0,10), ylim = c(100,200))
abline(v = 11.720, lwd=2, lty=3)
abline(v = 9.214, lwd=1.5, lty=3, col="light blue")
abline(v = 14.110, lwd=1.5, lty=3, col="light gray")
mtext("Age - years", side = 1, line = 3)
mtext("Height - cm", side = 2, line = 3.5)
mtext("Height velocity - cm / year", side = 4, line = 3)
legend("topleft", c( "Mean growth curve", "Lower 95% CI", "Upper 95% CI"),
   col = c("black", "light blue", "light gray"), lty = c(1, 1, 1))

enter image description here