[英]How can I build a new vector with specific data from two other different sized vectors using R
[英]How to simulate a vector that is correlated (in a different way) to two other existing vectors
我想模擬一個與其他兩個現有變量相關的向量。 到目前為止我嘗試過的
# some correlation matrix
desiredCorrelations = matrix(c(1, .4, 0,
.4, 1, .3,
0, .3, 1), nrow = 3)
# some simulated data based on the correlation matrix
dat = mvrnorm(n = 1000, mu = rep(3, 3), Sigma = desiredCorrelations, empirical = TRUE)
n = nrow(dat)
k = ncol(desiredCorrelations)
x = matrix( rnorm(n*k), nc=k )
x[,1] = dat[,1]
y = x %*% solve(chol(var(x))) %*% chol(desiredCorrelations)
# cor(y) # Desired correlation matrix
apply(dat, 2, summary)
apply(y, 2, summary)
基於這段代碼,相關性是正確的,但是只有y的第一列與原始模擬數據的第一列相同。 但是,我希望兩列保持不變,而第三列在模擬時考慮了所需的相關矩陣。
在此先感謝您的任何建議或提示!
我基於MattBagg的代碼為此編寫了一個函數,該函數采用向量x並返回具有指定均值,sd和相關性的向量:
simcor <- function (x, ymean=0, ysd=1, correlation=0) {
n <- length(x)
y <- rnorm(n)
z <- correlation * scale(x)[,1] + sqrt(1 - correlation^2) *
scale(resid(lm(y ~ x)))[,1]
yresult <- ymean + ysd * z
yresult
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.