我想模拟相关变量(随着相关rho的变化)以“重现”此figure。
相关断点
X = x <= 2.4时rho = 0
X = x> 2.4时,rho = 0.5
二元分布Y | X
X = N〜(mu_x,sd_x)
Y | X = N〜(mu_x + sd_x / sd_y * rho(Y-mu_y),(1-rho ^ 2)sd_x)
给出的是:
mu_x = 3.13; sd_x = 0.6
mean_y = 31.3; sd_y = 6.7
我的代码可用于生成相关数据, 但缺少“断点” 。这是上面剧情的代码:
X = rnorm(n,mean_x,sd_x)
Y = rnorm(n,mean_y + sd_y / sd_x * rho *(X-mean_x),(1-rho ^ 2)* sd_y)
如何使rho = f(x)?此刻,我将两个分布粘合在一起,得到了像散点图一样的曲棍球棒。
# Correlated Vectors (X, Y) - Hockey Stick
set.seed = 47
n = 729
mean_x = 3.13; sd_x = 0.6 # FEV
mean_y = 31.3; sd_y = 6.7 # O2max
# rho and breakpoint
rho = 0.7; breakpoint = 3.5
index_break = sum(X<breakpoint)
# Generate X distributuion
X = rnorm(n, mean_x, sd_x)
# Generate Y disrtribution parts
Y_rho = vector(); Y = vector()
Y_rho = rnorm(n, mean_y + sd_y/sd_x*rho*(X-mean_x), (1-rho^2)*sd_y[1:index_break]
Y = rnorm(n, mean_y, sd_y)[(index_break+1):length(FEV)]
Y = c(Y_rho, Y)
# plot
plot(X, Y)
非常感谢。