在R中编写简单的仿真程序 [英] Program simple simulation in R

查看:104
本文介绍了在R中编写简单的仿真程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据@agstudy编辑此帖子以便简化

Editing this post for simplification according to @agstudy

我正在尝试开发一种使用随机均匀分布模拟聚合物的模型.

I am trying to develop a model that simulates a polymer using a random uniform distribution.

模型有2个状态

状态1(如果处于状态2,则状态1的概率为.003): 增长概率,A = 0.01 收缩概率,B = .0025

State 1 (probability of state 1 if in state 2 is .003): growth probability, A = .01 shrink probability, B = .0025

状态2(如果处于状态1,则状态2的概率为.0003): 增长概率,A = 0.01 收缩概率,E = .05

State 2 (probability of state 2 if in state 1 is .0003): growth probability, A = .01 shrink probability, E = .05

模拟从状态1开始

在状态1中,如果#<如果#< .0025在data.frame2中输入-1. .01如果未在data.frame2中输入0,则在data.frame2中输入+1.继续直到达到#< .0003(进入状态1的可能性)

While in State 1, sample random numbers from data.frame1, if # < .0025 input -1 in data.frame2, if # < .01 input +1 in data.frame2, if neither input 0 in data.frame2. continue until reaching # < .0003 (probability to enter state 1)

如果#<采样.0003,将-1输入到data.frame2中,并切换到状态2.继续采样随机数,而如果#<2,则继续在状态2中采样. .003将+1输入到data.frame2中,并切换到状态1,然后按上述指示进行操作.如果处于状态2,则#> = .003,如果#< .05在data.frame2中输入-1,否则在data.frame2中输入0

If # < .0003 is sampled, input -1 into data.frame2, and switch to state 2. continue sampling random numbers, while in state 2 if # < .003 input +1 into data.frame2 and switch to State 1 and proceed as indicated above. If in state 2 the # >= .003, and if # < .05 input -1 into data.frame2, else input 0 in data.frame2

我希望这可以清除@agstudy的内容,但是有可能它可能不会……很抱歉.

I hope this clears things up @agstudy, but there is a chance it might not… sorry if that is the case.

这是在excel中生成的一个示例,显示了一个小集合的外观

here is an example generated in excel of how a small set should look

第一列将来自data.frame1,输入列将是data.frame2,出于解释目的,我包括了state和length列.

the 1st column would be from data.frame1 and the input column would be data.frame2, i included state and length column for explanatory purposes.

    rnumbers  state input length
    0           1   0   0 # initialized state
    0.009413895 1   1   1
    0.052959306 1   1   2
    0.002453354 1   -1  1
    0.000290324 2   -1  0
    0.093312954 2   0   0
    0.077210941 2   0   0
    0.04924944  2   -1  -1
    0.010590107 2   -1  -2
    0.03308292  2   -1  -3
    0.037239807 2   -1  -4
    0.007889393 2   -1  -5
    0.026476751 2   -1  -6
    0.000454509 1   1   -5
    0.009820683 1   1   -4
    0.019836999 1   1   -3
    0.009380381 1   1   -2
    0.083149383 1   1   -1
    0.022212547 1   1   0
    0.051917035 1   1   1
    0.05032031  1   1   2
    0.03956718  1   1   3
    0.032664445 1   1   4
    0.004352291 1   1   5
    0.08902418  1   1   6
    0.095643962 1   1   7

推荐答案

我会这样写:

step_generator <- function(rnum)
   ifelse(rnum<C,
       ifelse(rnum>=D,-5,ifelse(rnum<B,-1,ifelse(rnum<A,1,0))),
       ifelse(rnum<B,-1,ifelse(rnum<A,1,0)))

这篇关于在R中编写简单的仿真程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆