# Ex1. Normal model
# Quantile function of a linear model
Qlinmod <- function(theta, tau, data){
sigma <- exp(theta[1])
beta <- theta[-1]
X <- model.matrix( ~ x1 + x2, data = data)
qnorm(tau, X %*% beta, sigma)
}
n <- 100
x1 <- rnorm(n)
x2 <- runif(n,0,3)
theta <- c(1,4,1,2)
y <- Qlinmod(theta, runif(n), data.frame(x1,x2)) # generate the data
m1 <- Qest(y ~ x1 + x2, Q = Qlinmod, start = c(NA,NA,NA,NA)) # User-defined quantile function
summary(m1)
m2 <- Qest(y ~ x1 + x2, Q = Qnorm) # Qfamily
summary(m2)
m3 <- Qlm(y ~ x1 + x2)
summary(m3) # using 'Qlm' is much simpler and faster, with identical results
# \donttest{
# Ex2. Weibull model with proportional hazards
# Quantile function
QWeibPH <- function(theta, tau, data){
shape <- exp(theta[1])
beta <- theta[-1]
X <- model.matrix(~ x1 + x2, data = data)
qweibull(tau, shape = shape, scale = (1/exp(X %*% beta))^(1/shape))
}
n <- 100
x1 <- rbinom(n,1,0.5)
x2 <- runif(n,0,3)
theta <- c(2,-0.5,1,1)
t <- QWeibPH(theta, runif(n), data.frame(x1,x2)) # time-to-event
c <- runif(n,0.5,1.5) # censoring variable
y <- pmin(t,c) # observed response
d <- (t <= c) # event indicator
m1 <- Qest(Surv(y,d) ~ x1 + x2, Q = QWeibPH, start = c(NA,NA,NA,NA))
summary(m1)
m2 <- Qcoxph(Surv(y,d) ~ x1 + x2)
summary(m2) # using 'Qcoxph' is much simpler and faster (but not identical)
# Ex3. A Gamma model
# Quantile function
Qgm <- function(theta, tau, data){
a <- exp(theta[1])
b <- exp(theta[2])
qgamma(tau, shape = a, scale = b)
}
n <- 100
theta <- c(2,-1)
y <- rgamma(n, shape = exp(theta[1]), scale = exp(theta[2]))
m1 <- Qest(y ~ 1, Q = Qgm, start = c(NA, NA)) # User-defined quantile function
m2 <- Qest(y ~ 1, Q = Qgamma) # Qfamily
m3 <- Qest(y ~ 1, Q = Qgamma, wtau = function(tau, h) dnorm((tau - 0.5)/h), h = 0.2)
# In m3, more weight is assigned to quantiles around the median
# Ex4. A Poisson model
# Quantile function
n <- 100
x1 <- runif(n)
x2 <- rbinom(n,1,0.5)
y <- rpois(n, exp(1.5 -0.5*x1 + x2))
m1 <- Qest(y ~ x1 + x2, Q = Qpois) # Use a Qfamily! See "Note"
m2 <- Qest(y + runif(n) ~ x1 + x2, Q = Qpois) # Use jittering! See the documentation of "Qfamily"
# }
Run the code above in your browser using DataLab