Linear regression simulation - variance I'm running some linear regression simulations from here and I can't understand one section of the code.
lm_test <- function(N, b1=0.15, b0=0, xm=0, xsd=1) {

    x <- rnorm(N, xm, xsd)
    y <- rnorm(N, b0 + b1*x, sqrt(1 - b1^2))  
                                              
    model <- lm(y ~ x)

    coef(model)
 
}


What's the purpose of the sqrt(1 - b1^2)during the response construction?
Also, the code works using b1=0.15, but If I change b1 to 10 and xsd=8 for example, it fails because of the square root of a negative number.
Thank you.

The author has annotated this step of his code with the comment # var. approx. 1 after accounting for explained variance by x.  From this comment and the mathematics of the step, it appears that the author of the code wants to construct his simulation so that the response variable in the regression has unit variance.  (I could not see any explanation for why, but presumably it makes some subsequent step in the analysis simpler.)  Here he is using a simple variance decomposition for linear regression, which is:
$$\begin{align}
\sigma_Y^2
\equiv \mathbb{V}(Y) 
&= \mathbb{V}(\beta_0 + \beta_1 X + \epsilon) \\[6pt]
&= \mathbb{V}(\beta_1 X) + \mathbb{V}(\epsilon) \\[6pt]
&= \beta_1^2 \mathbb{V}(X) + \mathbb{V}(\epsilon) \\[6pt]
&= \beta_1^2 \sigma_X^2 + \sigma^2. \\[6pt]
\end{align}$$
Using this formula, if $\sigma_X^2 = 0.15$ and you want to get $\sigma_Y^2=1$ (i.e., unit variance for $Y$) then you take:
$$\sigma^2 = \sigma_Y^2 - \beta_1^2 \sigma_X^2 = 1 - \beta_1^2.$$
Now, as you point out, obviously if $|\beta_1| > 1$ then it is impossible to get unit variance for $Y$, so then this method doesn't work.  It's not clear from his page why he wanted a unit variance for the response variable in the first place, so I really can't comment on what his intention is here.  It might be worth writing to him to find out if you are interested.
