是否可以在 R 中创建具有特定斜率和皮尔逊相关系数的数据?

Is it possible to create data with a specific slope and pearson correlation coefficient in R?

例如,我希望两个变量 xy 的相关系数为 0.7,斜率为 1.5,并且两个变量都具有指定的均值和样本大小。我不管数据正常不正常

我经常用 MASS 搞砸,使用 mvrnorm 得到一个特定的相关系数,但我无法操纵它也给我斜率。

out <- mvrnorm(100, mu = c(0,0), Sigma = matrix(c(1,.5,.5,1),ncol = 2), empirical = TRUE)

这给了我 0.5 的相关系数,但是当我绘制数据时它也给了我 0.5 的斜率。

cor(out)
plot(out)
cor(out[,1], out[,2])
fit <- lm(out[,2]~out[,1])
fit
# Call:
# lm(formula = out[, 2] ~ out[, 1])

# Coefficients:
# (Intercept)     out[, 1]  
#  -8.604e-17    5.000e-01  

有没有一个程序可以做我想做的或者无论如何手动满足这些要求?

尺度的变化不会改变相关系数:

> out2[,2] <- out[,2]*10

> cor(out2)
     [,1] [,2]
[1,]  1.0  0.5
[2,]  0.5  1.0

plot(out2)

> lm(out2[,2]~out2[,1])

Call:
lm(formula = out2[, 2] ~ out2[, 1])

Coefficients:
(Intercept)    out2[, 1]  
 -5.732e-16    5.000e+00