【发布时间】:2020-08-15 22:40:29
【问题描述】:
使用速率 lambda = 0.7 的泊松过程模拟。显示泊松过程的样本运行,纵轴为 N(t),横轴为时间 t。模拟来自范围 t[0:100]。生成包含 10 个轨迹的第一个图和包含 100 个轨迹的第二个图。
我尝试了以下代码,但无法生成两个图表。
library(plyr)
library(ggplot2)
Process_poisson<- function(t, lambda){
distr_poisson<- rpois(1, t*lambda)
s_poisson<- sort(runif(distr_poisson, 0, t))
data.frame(x = c(0, 0, s_poisson),y = c(0, 0:distr_poisson))
}
N_simulations<- function(n,t,lambda){
s_poisson<- lapply (1:n, function(n) data.frame(Process_poisson(t, lambda), simulation = n))
s_poisson<- ldply (s_poisson, data.frame)
s_poisson$simulation<- factor(s_poisson$simulation)
}
t<- 0:100
lambda<- 0.7
N_simulations(10, t, lambda)
N_simulations(100, t, lambda)
par(mfrow = c(1,2))
matplot(x, y, type = "l", lty = 0:5, lwd = 1, lend = par("lend"),
pch = NULL, col = simulation, cex = 0.5, bg = NA, main =sprintf("Nº simulations of trajectories of Poisson Process",10,lambda), xlab = "Time", ylab = "N(t)",
xlim = c(0,100), ylim = c(-10,0))
matplot(Proceso_poisson(t, lambda), n, y, type = "l", lty = 0:5, lwd = 1, lend = par("lend"),
pch = NULL, col = simulacion, cex = 0.5, bg = NA, main =sprintf("Nº simulations of trajectories of Poisson Process",10,lambda), xlab = "Time", ylab = "N(t)",
xlim = c(0,100), ylim = c(-10,0))
我该怎么做?
非常感谢!
【问题讨论】:
-
一些简单的东西,在您的第一个
matplot()中,您使用x和y,它们是您尚未定义的对象。在第二个matplot()中,这可能是一个错字,当您实际定义的函数称为Process_poisson()时,您使用Proceso_poisson()。第三,N_simulations()不返回任何内容,您需要在函数末尾添加return(s_poisson)。 -
@OTStats 在大多数情况下都带来了很多好处,虽然我同意您应该将
return(s_poisson)添加到N_simulations()函数的末尾,但事实并非如此必要的;您已经隐式返回s_poisson,但不可见。 OTStats,您可以通过查看str(N_simulations(100, t, lambda))来确认这一点 -
@duckmayr 说得好,感谢您提及
标签: r simulation montecarlo poisson