diffusion(simecol)
diffusion()所属R语言包:simecol
A Random Walk Particle Diffusion Model
随机游走粒子扩散模型
译者:生物统计家园网 机器人LoveR
描述----------Description----------
simecol example: This is a random walk (basic particle
simecol例如:这是一个随机游走(基本粒子
用法----------Usage----------
data(diffusion)
格式----------Format----------
An S4 object according to the rwalkModel specification. The object contains the following slots:
S4的对象根据rwalkModel规格的。该对象包含以下插槽:
main A function with the movement rules for the particles.
mainA功能的粒子的运动规律。
parms A list with the following components:
parmsA以下组件列表:
ninds number of simulated particles,
ninds模拟颗粒,
speed speed of the particles,
speed的颗粒速度
area vector with 4 elements giving the coordinates (left,
area矢量与提供的坐标的4个元素(左
times Simulation time (discrete time steps, by-argument
times模拟时间(离散时间步长,by参数
init Data frame holding the start properties (Cartesian coordinates x and y and movement angle a) of the
init的数据框持有的开始的属性(直角坐标系x和y和运动的角度a)
Details
详细信息----------Details----------
To see all details, please have a look into the implementation.
要看到所有的详细信息,请,看看进入实施。
参见----------See Also----------
sim, parms, init, times.
sim,parms,init,times。
实例----------Examples----------
##============================================[#============================================]
## Basic Usage:[#的基本用法:]
## explore the example[#探索的例子]
##============================================[#============================================]
## Not run: [#不运行:]
data(diffusion)
## (1) minimal example[#(1)最小的例子]
plot(sim(diffusion))
## show "grid of environmental conditions"[#“的环境条件下的电网”]
image(inputs(diffusion))
## (2) scenario[#(2)方案]
## with homogeneous environment (no "refuge" in the middle)[#均匀环境(没有在中间的“避难所”)]
no_refuge <- diffusion # Cloning of the whole model object[整个模型对象克隆]
inputs(no_refuge) <- matrix(1, 100, 100)
plot(sim(no_refuge))
##============================================[#============================================]
## Advanced Usage:[#高级用法:]
## Assign a function to the observer-slot.[#配置功能的观察员插槽。]
##============================================[#============================================]
observer(diffusion) <- function(state, ...) {
## numerical output to the screen[#数值输出到屏幕上]
cat("mean x=", mean(state$x),
", mean y=", mean(state$y),
", sd x=", sd(state$x),
", sd y=", sd(state$y), "\n")
## animation[#动画]
par(mfrow=c(2,2))
plot(state$x, state$y, xlab="x", ylab="y", pch=16, col="red", xlim=c(0, 100))
hist(state$y)
hist(state$x)
## default case: return the state --> iteration stores it in "out"[#默认情况下返回状态 - >迭代并将其存储在“出”]
state
}
sim(diffusion)
## remove the observer and restore original behavior[#删除观察员,并恢复原来的行为]
observer(diffusion) <- NULL
diffusion <- sim(diffusion)
## End(Not run)[#(不执行)]
##============================================[#============================================]
## Implementation:[#实现:]
## The code of the diffusion model.[#扩散模型的代码。]
## Note the use of the "initfunc"-slot.[#注意使用的“initfunc”型槽。]
##============================================[#============================================]
diffusion <- rwalkModel(
main = function(time, init, parms, inputs = NULL) {
speed <- parms$speed
xleft <- parms$area[1]
xright <- parms$area[2]
ybottom <- parms$area[3]
ytop <- parms$area[4]
x <- init$x # x coordinate[x坐标]
y <- init$y # y coordinate[y坐标]
a <- init$a # angle (in radians)[角度(弧度)]
n <- length(a)
## Rule 1: respect environment (grid as given in "inputs")[#第1条:尊重环境(网格,在“输入”)]
## 1a) identify location on "environmental 2D grid" for each individual[#1A)确定“环境二维网格”每个人的位置]
i.j <- array(c(pmax(1, ceiling(x)), pmax(1, ceiling(y))), dim=c(n, 2))
## 1b) speed dependend on "environmental conditions"[#1B)的速度时效关系,“环境条件”]
speed <- speed * inputs[i.j]
## Rule 2: Random Walk[#规则2:随机漫步]
a <- (a + 2 * pi / runif(a))
dx <- speed * cos(a)
dy <- speed * sin(a)
x <- x + dx
y <- y + dy
## Rule 3: Wrap Around[#第3条规定:回绕]
x <- ifelse(x > xright, xleft, x)
y <- ifelse(y > ytop, ybottom, y)
x <- ifelse(x < xleft, xright, x)
y <- ifelse(y < ybottom, ytop, y)
data.frame(x=x, y=y, a=a)
},
times = c(from=0, to=100, by=1),
parms = list(ninds=50, speed = 1, area = c(0, 100, 0, 100)),
solver = "iteration",
initfunc = function(obj) {
ninds <- obj@parms$ninds
xleft <- obj@parms$area[1]
xright <- obj@parms$area[2]
ybottom <- obj@parms$area[3]
ytop <- obj@parms$area[4]
obj@init <- data.frame(x = runif(ninds) * (xright - xleft) + xleft,
y = runif(ninds) * (ytop - ybottom) + ybottom,
a = runif(ninds) * 2 * pi)
inp <- matrix(1, nrow=100, ncol=100)
inp[, 45:55] <- 0.2
inputs(obj) <- inp
obj
}
)
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|