# How to generate data that have given conditional mean and conditional quantile using R?

Suppose I want to generate independent data $(y_{i},x_{i})$ such that the conditional mean of $y_{i}$ given $x_{i}$ is a quadratic function in $x_{i}$ and the $.25$ conditional quantile of $y_{i}$ given $x_{i}$ is a function different than the conditional mean function.

I can generate such data with the given conditional mean using R. But how to make the $.25$ conditional quantile function different than the regression function?

• I might be missing the point, but wouldn't $y_i =x_i^2 +\epsilon_i$ where $\epsilon_i$ is normal with mean 0, do the job? Mar 16, 2018 at 6:46
• I am a bit unclear what exactly you require of the quantile. You could follow the advice of @Cettt and draw the noise terms from a normal distribution with a variance that depends on $x_i$, e.g., $\epsilon_i\sim N(0,x_i^2)$, then the quantiles will depend on $x_i$ not only through the mean. Mar 16, 2018 at 7:42
• Hi. Thank you all for the useful info. That gave me something to begin with. Thanks. I guess I worked out what I want. Thanks. @StephanKolassa,@Cettt
– Yes
Mar 16, 2018 at 14:20

A simple approach would be to draw $$X\sim U[0,1]$$ and $$Y\sim N(x^2,x)$$, i.e., the variance of $$Y$$ is $$x$$. Then the $$0.25$$ quantile of $$Y$$ would be $$z_{0.25}\sqrt{x}$$. Here are simulated data, with the expectation as a black and the quantile as a red line:

R code:

set.seed(1)
xx <- runif(1e3)
yy <- rnorm(length(xx),xx^2,sqrt(xx))
plot(xx,yy,pch=19,las=1,cex=0.6)

xx_pred <- seq(.01,.99,by=.01)
lines(xx_pred,xx_pred^2,lwd=2)
lines(xx_pred,qnorm(0.25,xx_pred^2,sqrt(xx_pred)),col="red",lwd=2)