How to construct (simulate) data that will have a given coefficient of determination? I want to produce random sample bivariate data that will have a given coefficient of determination and a given linear regression model. In particular, I want to understand how it should be constructed, starting with a random distribution of $X$ values between a given minimum and maximum.
For example, suppose I choose Pearson’s $R=0.8$ so that $R^2=0.64$. This means 64% of the variation in $Y$ can be explained by variation in $X$. How should I construct a function of $X$ to produce a random set of $Y$ values such that $X\sim Y$ has a given expected $R^2$ value and a given expected linear regression model $y=ax+b$? The theoretical variance from the expected values is also of interest, but should approach zero for larger sample sizes.
Right now I am proceeding by trial and error. Would greatly appreciate any expert input!

Clarification
Suppose I set, for example, $X=\{1, 2, 3, \cdots, n\}$. I want to generate (randomly) a set $Y$ such that $(X,Y)$ to have the following (expected) properties:

*

*Least squares line: $y=0.2x+5$, and

*$r^2=0.6$.

 A: To fix the coefficient of determination $R^2$ and simulate dependent variable $Y$ and independent variable $X$, you may use the following R code
# fix the coefficient of determination
R2 <- 0.2

# simulate x and y variables
n <- 100 # sample size
x <- rnorm(n, 0, 1)
ssr <- sum((x-mean(x))^2) # sum of squared residuals
e <- rnorm(n)
e <- resid(lm(e ~ x))
e <- e*sqrt((1-R2)/R2*ssr/(sum(e^2)))
y <- x + e


# check if R2 has the right value
summary(lm(y ~ x)) 
```

A: I know you want to do this starting with the min and max of $x$, but if you are willing to admit:
a)  That the covariate can be well approximated by a gaussian, and
b) That we can always center the covariate to have mean 0
Then this can be done easily.
Using some knowledge about gaussians, if $y \vert x \sim \mathcal{N}(ax + b, \sigma^2)$ and $x \sim \mathcal{N}(0, \tau^2)$, then the marginal distribution of $y$ is $y \sim \mathcal{N}(a\mu + b, \sigma^2 + a^2\tau^2)$.
The $R^2$ is then
$$ R^2 = 1 - \dfrac{\sigma^2}{\sigma^2 + a^2\tau^2} $$
up to sampling error.  I have some code to do this in a blog post here.
