# using simulated data
n <- 1000 # n. of observations
n.id <- 100 # n. of clusters
id <- rep(1:n.id, each = n/n.id) # cluster id
x1 <- runif(n) # a level-1 covariate
z1 <- rbinom(n.id,1,0.5) # a level-2 covariate
V <- runif(n.id) # V_i
U <- runif(n) # U_it
alpha <- qlogis(V)*(0.5 + z1) # alpha
y_alpha <- 1 + 2*qexp(U) + 3*x1 # y - alpha
y <- y_alpha + alpha[id] # observed outcome
mydata <- data.frame(id = id, y = y, x1 = x1, z1 = z1[id])
# true model: Y_it = beta0(U_it) + beta1(U_it)*x1 + gamma0(V_i) + gamma1(V_i)*z1
# beta0(u) = 1 + 2*pexp(u)
# beta1(u) = 3
# gamma0(v) = 0.5*qlogis(v)
# gamma1(v) = qlogis(V)
model <- iqrL(fx = y ~ x1, fu = ~ I(qexp(u)), fz = ~ z1, fv = ~ -1 + I(qlogis(v)),
id = id, data = mydata)
# predict beta(0.25), beta(0.5), beta(0.75)
predict(model, level = 1, type = "coef", p = c(0.25,0.5,0.75))
# predict gamma(0.1), gamma(0.9)
predict(model, level = 2, type = "coef", p = c(0.1,0.9))
# predict the CDF (u) and the PDF of (y - alpha), at new values of x1
predict(model, level = 1, type = "CDF",
newdata = data.frame(x1 = c(.1,.2,.3), y_alpha = c(1,2,3)))
# predict the CDF (v) and the PDF of alpha, at new values of z1
predict(model, level = 2, type = "CDF",
newdata = data.frame(z1 = c(0,1), alpha = c(-1,1)))
# computes the quantile function of (y - alpha) at new x1, for u = (0.25,0.5,0.75)
predict(model, level = 1, type = "QF", p = c(0.25,0.5,0.75),
newdata = data.frame(x1 = c(.1,.2,.3)))
# computes the quantile function of alpha at new z1, for v = (0.25,0.5,0.75)
predict(model, level = 2, type = "QF", p = c(0.25,0.5,0.75),
newdata = data.frame(z1 = c(.1,.2,.3)))
# simulate data from the fitted model
y_alpha_sim <- predict(model, level = 1, type = "sim")
alpha_sim <- predict(model, level = 2, type = "sim")
y_sim = y_alpha_sim + alpha_sim[id]
Run the code above in your browser using DataLab