我的问题是我有一组数据我想要分配给它,然后一旦找到分布,就在它上面运行蒙特卡罗模拟来传播找到的分布。
我的第一段代码是:
require(fitdistrplus)
example1<-c(29,23,29,25,26,29,29,27,25,25,25,26,28,25,29,28,28,26,28,25,29,26,30)
f1<-fitdist(example1,rgamma,method="mle")
如果我然后使用命令
print(f1)
它告诉我形状是204.00
伽马分布的速率为7.568
(请注意我现在适合分布的数字是任意的,我通常会有数百个观察值来适应分布)。
我现在需要帮助的是当我使用包mc2d
中的代码来传播此分发时,如下所示:
require(mc2d)
ndunc(1000)
fitted<-mcstoc(rgamma, type="U", shape=204.00, rate=7.569)
目前我不得不从“fitdist”命令的前一个“打印”中手动输入形状和速率到上面的函数中。
我的问题是,有没有办法让mcstoc命令自动从fitdist命令中获取形状和速率,这样我就不必中断代码来手动执行此操作?或者如果使用fitdistrplus软件包和mc2d软件包是不可能的,那么还有另一个软件包可以为我做这个吗?
非常感谢提前!
答案 0 :(得分:4)
f1$estimate[1]
# shape
#204.0008
f1$estimate[2]
# rate
#7.567762
fitted<-mcstoc(rgamma, type="U", shape=f1$estimate[1], rate=f1$estimate[2])
答案 1 :(得分:2)
myFunction <- function (data){
f1<-fitdist(data,rgamma,method="mle")
fitted<-mcstoc(rgamma, type="U", shape=f1$estimate[1], rate=f1$estimate[2])
return(fitted)
}
example1<-c(29,23,29,25,26,29,29,27,25,25,25,26,28,25,29,28,28,26,28,25,29,26,30)
fitted.example1 <- myFunction(exemple1)
此功能尚未经过测试。
答案 2 :(得分:1)
如果您不想输入参数名称,
你可以使用do.call
:
fitted <- do.call(
function(...) mcstoc(rgamma, type="U", ...),
as.list(f1$estimate)
)