让我们表示refVar,一个包含实验数据的感兴趣的变量。 对于模拟研究,我想生成其他变量V0.05、V0.10、V0.15,直到V0.95。 请注意,对于变量名,V后面的值表示变量和refVar之间的相关性(以便在最终数据帧中快速跟踪)。 我的读数让我从numpy找到了多元_normal()。但是,使用此函数时,它会生成2个1D数组,这两个数组都是随机数。我想要的是始终保持refVar并生成其他填充了随机数的数组,同时满足指定的相关性。 请在下面找到我的代码。简而言之,我不知道如何生成与实验变量refVar相关的其他变量。理想情况下,我希望构建一个包含以下列的数据框架:refVar、V0.05、V0.10、…、V0.95。我希望你明白我的意思,并提前感谢你抽出时间
import numpy as np
import pandas as pd
from numpy.random import multivariate_normal as mvn
refVar = [75.25,77.93,78.2,61.77,80.88,71.95,79.88,65.53,85.03,61.72,60.96,56.36,23.16,73.36,64.18,83.07,63.25,49.3,78.2,30.96]
mean_refVar = np.mean(refVar)
for r in np.arange(0,1,0.05):
var1 = 1
var2 = 1
cov = r
cov_matrix = [[var1,cov],
[cov,var2]]
data = mvn([mean_refVar,mean_refVar],cov_matrix,size=len(refVar))
output = 'corr_'+str(r.round(2))+'.txt'
df = pd.DataFrame(data,columns=['refVar','v'+str(r.round(2)])
df.to_csv(output,sep='\t',index=False) # Ideally, instead of creating an output for each correlation, I would like to generate a DF with refVar and all these newly created Series
在this answer之后,我们可以生成如下序列:
相关问题 更多 >
编程相关推荐