Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中制作概率模拟器?_R_Simulation_Probability - Fatal编程技术网

如何在R中制作概率模拟器?

如何在R中制作概率模拟器?,r,simulation,probability,R,Simulation,Probability,使用以下数据帧: A1 A2 EFF FRQ A G 0.0125 0.4578 T C 0.0143 0.1293 T C -0.017 0.8984 A G -0.018 0.8945 A G -0.009 0.8652 A G 0.0001 0.3931 我想根据FRQ列,从效应大小中进行两次概率“抽取”。我想创建一个名为simu 1的新列,其

使用以下数据帧:

A1  A2  EFF       FRQ      
A   G   0.0125    0.4578  
T   C   0.0143    0.1293    
T   C   -0.017    0.8984  
A   G   -0.018    0.8945   
A   G   -0.009    0.8652   
A   G   0.0001    0.3931   
我想根据
FRQ
列,从效应大小中进行两次概率“抽取”。我想创建一个名为
simu 1
的新列,其中45.78%的时间
EFF
保持符号,54.22%的时间
EFF
切换符号。然后我想对每一行的两个随机事件求和。例如,假设生成了两个0-100的随机数。78.33和32.16。我认为任何<45.78的值都表示保持
EFF
不变。因为我随机掷了一个78和32,总数将是-0.0125(78.33卷)和0.0125(32.16卷),等于0

在第二行,假设我滚动两个随机数88.22和67.10。因为这两个数字都不低于12.93,所以88.22和67.10卷的
EFF
符号都将翻转,留下-0.0286(-0.0143+-0.0143)的总和

我希望以这种方式制作500个模拟列,以便最终输出如下所示:

A1  A2  EFF       FRQ      Sim_1   Sim_2   Sim_3...
A   G   0.0125    0.4578   0       -       -
T   C   0.0143    0.1293   -0.0286 -       -
T   C   -0.017    0.8984  -        -       -
A   G   -0.018    0.8945  -        -       -
A   G   -0.009    0.8652  -        -       -
A   G   0.0001    0.3931  -        -       -
注意:如果生成输出文件,它可能与我的不匹配,因为它基于随机性。

使用您的数据:

tmp_df <- structure(list(A1 = structure(c(1L, 2L, 2L, 1L, 1L, 1L), 
                                        .Label = c("A", "T"), class = "factor"), 
                         A2 = structure(c(2L, 1L, 1L, 2L, 2L, 2L),
                                        .Label = c("C", "G"), class = "factor"), 
                         EFF = c(0.0125, 0.0143, -0.017, -0.018, -0.009, 1e-04), 
                         FRQ = c(0.4578, 0.1293, 0.8984, 0.8945, 0.8652, 0.3931)),
                    .Names = c("A1", "A2", "EFF", "FRQ"), class = "data.frame", row.names = c(NA, -6L))
解释
lappy
步骤:

1) matrix(runif(nrow(tmp_df) * 2)
Draw two columns filled with random numbers drawn uniformly in the interval [0, 1].
Alternatively, you can look into using `rbinom`.

2) 2 * (... >= tmp_df$FRQ) * tmp_df$EFF
Create (-1, 1) indicator to see whether `EFF` should be fliped, then multiply, exploiting conformability rules.

3) lapply(...) 
Do the above 500 times.

其余的只是标签,并将模拟结果绑定到您的原始数据。

tmp\u results=tmp\u df$FRQ)*tmp\u df$EFF()(tmp\u results)
正在向我的R输出数百万行并使我的程序崩溃抱歉,不确定
(tmp\u results)
是如何出现的。删除它,因为这将打印所有结果。第二行有一些模拟,我得到0.05758(基本上是效果列的4倍)。你知道为什么会这样吗?模拟所给出的效果大小值的大小不应超过效果大小值的两倍。对不起,在尝试使指示器起作用时,我将括号放在了错误的位置。我希望现在修好了。
> tmp_out[, 1:10]
  A1 A2     EFF    FRQ   Sim 1   Sim 2   Sim 3   Sim 4   Sim 5   Sim 6
1  A  G  0.0125 0.4578 -0.0250  0.0000  0.0250 -0.0250  0.0000  0.0250
2  T  C  0.0143 0.1293 -0.0286 -0.0286 -0.0286 -0.0286  0.0000 -0.0286
3  T  C -0.0170 0.8984 -0.0340 -0.0340 -0.0340 -0.0340 -0.0340 -0.0340
4  A  G -0.0180 0.8945 -0.0360  0.0000 -0.0360 -0.0360 -0.0360 -0.0360
5  A  G -0.0090 0.8652  0.0000 -0.0180 -0.0180 -0.0180 -0.0180  0.0000
6  A  G  0.0001 0.3931  0.0002 -0.0002 -0.0002  0.0000 -0.0002  0.0000
1) matrix(runif(nrow(tmp_df) * 2)
Draw two columns filled with random numbers drawn uniformly in the interval [0, 1].
Alternatively, you can look into using `rbinom`.

2) 2 * (... >= tmp_df$FRQ) * tmp_df$EFF
Create (-1, 1) indicator to see whether `EFF` should be fliped, then multiply, exploiting conformability rules.

3) lapply(...) 
Do the above 500 times.