从R中的遗传数据模拟正态分布数据

问题描述 投票:0回答:1

我需要基于两个随机变量来模拟数据。每个随机变量都是一组0、1和2(遗传数据)。以下是10个观测值的变量示例:

随机变量1:

v_1 = cbind(rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)))

随机变量2:

v_2 = cbind(rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)), rep(rbinom(10,2,0.4)),  rep(rbinom(10,2,0.4)))

这些变量的正态分布均值= 0,方差= 1(或我可以选择的其他值)。此外,我还需要添加误差项,正态分布均值= 0,方差= 1。

任何人都可以帮忙吗?

r simulation normal-distribution
1个回答
0
投票

我将序列放在data.frame中的列表中。您可以根据需要更改它,并将其放在不同的列中。

n <- 100
t <- 5

d <- data.frame( 
  v1 = lapply(1:n, function(x) sample(0:2, t, replace = T) ),
  v2 = lapply(1:n, function(x) sample(0:2, t, replace = T) ),
  v1e = rnorm(n),
  v2e = rnorm(n)
)

head(d)

          v1        v2        v1e        v2e
1: 1,2,2,0,0 2,0,0,0,2 -0.3962995  0.9091728
2: 0,2,0,0,2 1,2,2,2,2 -2.5788875 -0.1007541
3: 0,1,1,0,0 2,0,1,1,0  0.7208587 -0.3362134
4: 1,0,2,0,2 0,1,1,0,2  0.6068573 -2.1755171
5: 1,0,0,0,0 2,2,2,1,2  0.2505764 -0.4038029
6: 2,2,1,1,1 2,0,0,1,0  0.1002509  0.9895424
© www.soinside.com 2019 - 2024. All rights reserved.