You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言Monte Carlo模拟报错:参数匹配冲突问题求助

MonteCarlo模拟代码参数匹配错误排查与修复

错误原因

你触发的argument 2 matches multiple formal arguments错误,根源是MonteCarlo()函数的参数匹配规则:该函数的第二个位置参数是param_list(用于定义多参数组合的网格),当你直接传入n = 10000时,R无法判断这个参数是要赋值给param_list还是n,从而引发匹配冲突。

修复方案

由于你的模拟不需要多参数网格,只需明确指定param_list = NULL,或采用全命名参数的方式调用函数即可解决。

修正后的调用代码

# 方式1:明确指定param_list为NULL
results <- MonteCarlo(model, param_list = NULL, n = 10000)

# 方式2:全命名参数调用(可读性更强)
results <- MonteCarlo(model = model, param_list = NULL, n = 10000)

完整修正代码

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, param_list = NULL, n = 10000)

summary(results)

内容的提问来源于stack exchange,提问作者SES

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 03:20:16