Percentiles from VGAM

痞子三分冷 提交于 2019-12-06 15:11:14

How about something like this:

# required packages
library(VGAM)
require(reshape2)
require(ggplot2)
# fitted values from vgam 
fit4 <- vgam(BMI ~ s(age, df = c(4, 2)), lms.bcn(zero = 1), data = bmi.nz, trace = TRUE)
fitted.values <- data.frame(qtplot.lmscreg(fit4, percentiles = c(5,50,90,99))$fitted.values)
fitted.values[, 'age'] <- bmi.nz[, 'age']
# melt data.frame
dmelt <- melt(fitted.values, id.vars='age')
# ploting
ggplot(dmelt, aes(age, value, group=variable)) + 
  geom_line(color='blue') + 
  annotate(geom='text', 
           x = max(bmi.nz[, 'age']) + 3, 
           y = unlist(fitted.values[which.max(fitted.values[, 'age']), -ncol(fitted.values)]),
           label=c(' 5%', '50%', '90%', '99%')) +
  lapply(2:8*10, function(i) {
    annotate(geom='text', 
           x = i, 
           y = 1+unlist(fitted.values[which.min(abs(fitted.values[, 'age'] - i)), -ncol(fitted.values)]),
           label=paste0(round(unlist(fitted.values[which.min(abs(fitted.values[, 'age'] - i)), -ncol(fitted.values)]),1), '%'))
  }) +
  scale_y_continuous('BMI') +
  theme_bw(base_size=16)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!