# How to generate a conditional random variable in R? [closed]

Suppose there is a sample $$X\sim N(0,1)$$

x<-rnorm(100).


If I want to generate a conditional random variable $$Y|X\sim U(0,1)$$, how can I get this conditional sample in R?

Actually, I want to run a simulation for the Skorohod representation of quantile regression. $$Y=X^\prime\beta(U),\quad U|X\sim U(0,1)$$ So that the $$\tau$$th quantile linear regression coefficient is $$\beta(\tau)$$. I think the comment and answer below is correct.

The simulation I did is as following:

Let $$U\sim U(0,1)$$, $$X\sim |N(0,1)|$$, $$\beta(u)=0.5(1+u)$$.

Generate $$y=x\beta(u)$$.

So that if we do a quantile regression between $$y$$ and $$x$$, then the $$\tau$$th quantile linear regression coefficient is just $$\beta(\tau)$$. Note that 0.5497972643 is very close to 0.55

library("quantreg")
n=10000
u=runif(n)
b=0.5*(1+u)
x=abs(rnorm(n))
y=x*b
fit=rq(y~x,tau=0.1)
> fit
Call:
rq(formula = y ~ x, tau = 0.1)

Coefficients:
(Intercept)             x
-0.0002284288  0.5497972643
> 1.1*0.5
[1] 0.55


• You need to know how it is conditioned, i.e. pdf of $y|x$. Feb 22, 2019 at 5:56
• Can you clarify the notation $Y\mid X \sim U(0,1)$ - did you really intend $Y$ and $X$ to be independent (As interpreted in the answer)? It might help to explain how the need to generate this sample arises. Feb 22, 2019 at 7:07
Notice that if $$Y|X \sim U(0,1)$$, then regardless of the value of $$X$$, $$Y|X$$ is always $$\text{Uniform}(0,1)$$. In other words, the conditional distribution does not depend on $$X$$ (Compare this with a situation in which $$Y|X \sim \text{Uniform}(0,X)$$, where now we have to know the value of $$X$$ to ascertain the distribution of $$Y$$).
x <- rnorm(100)