简体   繁体   English

R 中的蒙特卡罗模拟问题

[英]Issues with Monte Carlo Simulation in R

This is my code, but I am facing some issues.这是我的代码,但我面临一些问题。 This line: results <- MonteCarlo(model, n = 10000), gives me the following error:这一行:results <- MonteCarlo(model, n = 10000),给了我以下错误:

Error in MonteCarlo(model, n = 10000) : 
  argument 2 matches multiple formal arguments

This is the whole code.这是整个代码。 Can anyone see where I have made a mistake?谁能看到我在哪里犯了错误?

install.packages("MonteCarlo")
library(MonteCarlo)


year1.price <- 1800 
year2.price <- c(-250, 200, 500) 
year3.price <- c(-250, 200, 500) 
year4.price <- c(-250, 200, 500) 
year5.price <- c(-250, 200, 500) 

output.distribution <- c(100, 1000, 1800) 
annual.change <- c(-0.15, 0.3, 0.7) 
fixed.costs <- c(500000, 750000, 1000000) 
legal.costs <- c(500000, 750000, 1000000) 

 
model <- function() { 
  year1.revenue <- year1.price * output.distribution[2] 
  year2.revenue <- year2.price[sample(1:3, 1)] * (output.distribution[2] + output.distribution[2] * annual.change[sample(1:3, 1)]) 
  year3.revenue <- year3.price[sample(1:3, 1)] * (output.distribution[2] + output.distribution[2] * annual.change[sample(1:3, 1)]) 
  year4.revenue <- year4.price[sample(1:3, 1)] * (output.distribution[2] + output.distribution[2] * annual.change[sample(1:3, 1)]) 
  year5.revenue <- year5.price[sample(1:3, 1)] * (output.distribution[2] + output.distribution[2] * annual.change[sample(1:3, 1)]) 
  
  total.revenue <- year1.revenue + year2.revenue + year3.revenue + year4.revenue + year5.revenue 
  total.costs <- fixed.costs[2] + legal.costs[sample(1:3, 1)] 
  net.revenue <- total.revenue - total.costs 
  
  return(net.revenue) 
} 

results <- MonteCarlo(model, n = 10000)


summary(results)

The specific error you are getting is because MonteCarlo does not have an argument called n .您得到的具体错误是因为MonteCarlo没有名为n的参数。 The argument you are looking for is called nrep .您要查找的参数称为nrep However, fixing this will give you another error saying that your list of parameters is missing.但是,修复此问题会给您带来另一个错误,指出您的参数列表丢失。 The reason for this is simply that you are using the wrong function here.原因很简单,你在这里使用了错误的 function。

If I understand you correctly, you are trying to get 10000 repetitions of the function model .如果我对您的理解正确,您正在尝试重复 10000 次 function model However, MonteCarlo is designed to be used in more complex situations where you want replicates of a function that accepts multiple parameters which may vary over the replicates.但是, MonteCarlo旨在用于更复杂的情况,您需要复制 function,它接受多个参数,这些参数可能因复制而异。 Your function does not take any parameters, so MonteCarlo throws an error, and in any case you can get the result you are looking for simply by using the base R function replicate , which will do just what you are looking for您的 function 不接受任何参数,因此MonteCarlo会引发错误,并且无论如何您都可以通过使用基本 R ZC1C425268E68385D1AB5074C17A9 来获得您正在寻找的结果,这将replicate您正在寻找的内容

results <- replicate(10000, model())

So for example:例如:

length(results)
#> [1] 10000

mean(results)
#> [1] 1075661

hist(results)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM