Percentiles from VGAM

529 views Asked by At

I am using following example from help pages of package VGAM

library(VGAM)
fit4 <- vgam(BMI ~ s(age, df = c(4, 2)), lms.bcn(zero = 1), data = bmi.nz, trace = TRUE)
qtplot(fit4, percentiles = c(5,50,90,99), main = "Quantiles", las = 1, xlim = c(15, 90), ylab = "BMI", lwd = 2, lcol = 4) 

I am getting a proper graph with it:

enter image description here

How can I avoid plotting points from the graph? Also I need to print out values for these percentiles at each of ages 20,30,40...80 (separately as a table). How can this be done? Can I use ggplot() format command rather than qtplot() command here? Thanks for your help.

1

There are 1 answers

1
shadow On BEST ANSWER

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)