首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不良规划R中的模拟数据

不良规划R中的模拟数据
EN

Stack Overflow用户
提问于 2021-01-08 01:03:48
回答 1查看 40关注 0票数 0

我在试着估计糟糕的规划对企业的影响。我有两个不同的仓库,一个比另一个贵得多。在一个完美的情况下,我们总是选择最便宜的价格,然而,这个过程并不完美,有时有些订单是从错误的仓库购买的。

为了测试我们可能会损失多少,我正在开发一个仿真模型来测试3种场景。这些建议如下:

  1. 每次选择最便宜的仓库
  2. 10%的订单是以错误的价格购买的
  3. 25%的订单是以错误的价格购买的

我现在不知道该从哪里开始,我知道我的数据需要符合正态分布,我需要多次模拟第2和第3场景,因为每个场景都会给出不同的结果。

代码语言:javascript
复制
structure(list(Customer = c("a", "b", "c", "d", "e"), `Option 1` = c(5, 
100, 107, 400, 30), `Option 2` = c(19, 200, 50, 300, 70), `Probability Selecting the more expensive option` = c(0.1, 
0.1, 0.1, 0.1, 0.1)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))
# A tibble: 5 x 4
  Customer `Option 1` `Option 2` `Probability Selecting the more expensive option`
  <chr>         <dbl>      <dbl>                                             <dbl>
1 a                 5         19                                               0.1
2 b               100        200                                               0.1
3 c               107         50                                               0.1
4 d               400        300                                               0.1
5 e                30         70                                               0.1

任何有关如何解决这个问题的帮助都将不胜感激。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-08 03:46:59

好的,如果我理解正确,我可能会这样做:

代码语言:javascript
复制
library(dplyr)

#I've renamed the columns to be easier to work with
df <- structure(list(customer = c("a", "b", "c", "d", "e"),
                     op1 = c(5, 100, 107, 400, 30),
                     op2 = c(19, 200, 50, 300, 70), 
                     probs = c(0.1,  0.1, 0.1, 0.1, 0.1)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
df


# Here is a simple dplyr solution to getting your best and worst selection to chose from 
df <- df %>% 
  rowwise() %>% 
  mutate(best = min(c(op1, op2)),
         worst = max(c(op1, op2)))


# this function repeatedly samples from the best and worst option with a probability of the argument probs
# it is deendent on integer indexing so if you move or add columns the function will need adjusting
# it can also be adjusted to use the prob of each row if that were to change through time
# The output is a matrix with the number of rwplicates as columns
# order of probability is best worst so probs = c(0.4,0.6) means it is less likely to choose best
resample_func <- function(df, reps = 5, probs = c(0.4,0.6)) {
  mat <- matrix(nrow = nrow(df), ncol = reps)
  nc <- df[, c(5,6)]
  # print(nc)
  
  for (j in 1:ncol(mat)) {
    for (i in 1:nrow(df)) {
      # nc <- df[,c(2,3)]
      mat[i, j] <- t(sample(nc[i,], 1, replace = F, prob = probs))
    }
  }
  rownames(mat) <- df$customer
  colnames(mat) <- paste0("rep_", 1:ncol(mat))
  return(mat)
}

resample_func(df)

请仔细检查输出!请让我知道这是否是您所追求的:)还请参见@r2evans的评论

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65622090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档