So the formula for R ^ 2 is 1-var (residual) / var (total)
In this case, the variance of Y will be 3^2+2^2+sd.value^2 , since we add three independent random variables. And, asymptotically, the residual variance will simply be sd.value^2 .
So you can explicitly calculate rsquared with this function:
rsq<-function(x){1-x^2/(9+ 4+x^2)}
With a little algebra, you can calculate the inverse of this function:
rsqi<-function(x){sqrt(13)*sqrt((1-x)/x)}
So setting sd.value<-rsqi(rsquared) should give you what you want.
We can verify this as follows:
simrsq<-function(x){ Y <- rnorm(n, (5 + 3*X1 - 2*X2), rsqi(x)) simdata <- data.frame(X1, X2, Y) summary(lm(Y ~ X1 + X2, data=simdata))$r.squared } > meanrsq<-rep(0,9) > for(i in 1:50) + meanrsq<-meanrsq+Vectorize(simrsq)((1:9)/10) > meanrsq/50 [1] 0.1031827 0.2075984 0.3063701 0.3977051 0.5052408 0.6024988 0.6947790 [8] 0.7999349 0.8977187
So it looks right.
mrip
source share