R:使用网格的馈送功能

2023-12-11

我正在使用 R 编程语言。

我编写了这个循环,它在随机选择的输入“random_1、random_2、random_3、random_4、split_1、split_2、split_3”中评估以下“函数”(实际上是一个“循环”)100次:

#load library
library(dplyr)

library(data.table)

set.seed(123)

# create some data for this example
a1 = rnorm(1000,100,10)
b1 = rnorm(1000,100,5)
c1 = sample.int(1000, 1000, replace = TRUE)
train_data = data.frame(a1,b1,c1)


####
results_table <- data.frame()

for (i in 1:100 ) {
    
    #generate random numbers
    random_1 =  runif(1, 80, 120)
    random_2 =  runif(1, random_1, 120)
    random_3 =  runif(1, 85, 120)
    random_4 =  runif(1, random_3, 120)
    
    #bin data according to random criteria
    train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_4, "b", "c")))
    
    train_data$cat = as.factor(train_data$cat)
    
    #new splits
    a_table = train_data %>%
        filter(cat == "a") %>%
        select(a1, b1, c1, cat)
    
    b_table = train_data %>%
        filter(cat == "b") %>%
        select(a1, b1, c1, cat)
    
    c_table = train_data %>%
        filter(cat == "c") %>%
        select(a1, b1, c1, cat)
    
    split_1 =  runif(1,0, 1)
    split_2 =  runif(1, 0, 1)
    split_3 =  runif(1, 0, 1)
    
    #calculate 60th quantile ("quant") for each bin
    
    table_a = data.frame(a_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_1)))
    
    table_b = data.frame(b_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_2)))
    
    table_c = data.frame(c_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_3)))
    
    
    
    
    #create a new variable ("diff") that measures if the quantile is bigger tha the value of "c1"
    table_a$diff = ifelse(table_a$quant > table_a$c1,1,0)
    table_b$diff = ifelse(table_b$quant > table_b$c1,1,0)
    table_c$diff = ifelse(table_c$quant > table_c$c1,1,0)
    
    #group all tables
    
    final_table = rbind(table_a, table_b, table_c)
    
    #create a table: for each bin, calculate the average of "diff"
    final_table_2 = data.frame(final_table %>%
                                   group_by(cat) %>%
                                   summarize(
                                       mean = mean(diff)
                                   ))
    
    #add "total mean" to this table
    final_table_2 = data.frame(final_table_2 %>% add_row(cat = "total", mean = mean(final_table$diff)))
    
    #format this table: add the random criteria to this table for reference
    final_table_2$random_1 = random_1
    
    final_table_2$random_2 = random_2
    
    final_table_2$random_3 = random_3
    
    final_table_2$random_4 = random_4
    
    final_table_2$split_1 = split_1
    
    final_table_2$split_2 = split_2
    
    final_table_2$split_3 = split_3
    
    final_table_2$iteration_number = i
    
    
    results_table <- rbind(results_table, final_table_2)
    
    final_results = dcast(setDT(results_table), iteration_number + random_1 + random_2 + random_3 + random_4 + split_1 + split_2 + split_3 ~ cat, value.var = 'mean')
    
}

上面代码的结果如下所示:

head(final_results)
   iteration_number random_1 random_2  random_3 random_4    split_1   split_2   split_3          a         b         c total
1:                1 95.67371 111.8133  94.00313 102.0569 0.84045638 0.6882731 0.7749321 0.82051282 0.6870229 0.7734554 0.730
2:                2 92.31360 110.0762 106.46871 109.5343 0.24615922 0.8777580 0.7847697 0.24731183 0.8777429 0.7840909 0.744
3:                3 81.02645 110.4645 116.42006 119.6172 0.11943576 0.9762721 0.9100522 0.14285714 0.9758162 0.9103448 0.943
4:                4 90.35986 116.7089 114.15588 116.7231 0.07675141 0.8661540 0.3236617 0.08139535 0.8658065 0.3207547 0.702
5:                5 89.28374 114.7103 119.70448 119.7725 0.08881443 0.6351936 0.8565509 0.09027778 0.6349614 0.8461538 0.573
6:                6 87.35767 103.8575  97.44462 116.0414 0.48372890 0.2319129 0.2701634 0.47368421 0.2326333 0.2711370 0.255

这是我的问题:我不想在“随机选择的点”评估上述函数,而是想在网格内定义的点评估该函数。

首先,我定义了网格:

#grid_2
random_1 <- seq(80,100,5)
random_2 <- seq(85,120,5)
random_3 <- seq(85,120,5)
random_4 <- seq(90,120,5)
split_1 =  seq(0.4,1,0.2)
split_2 =  seq(0.4,1,0.2)
split_3 =  seq(0.4,1,0.2)
DF_1 <- expand.grid(random_1 , random_2, random_3, random_4, split_1, split_2, split_3)

> head(DF_1)
  Var1 Var2 Var3 Var4 Var5 Var6 Var7
1   80   85   85   90  0.4  0.4  0.4
2   85   85   85   90  0.4  0.4  0.4
3   90   85   85   90  0.4  0.4  0.4
4   95   85   85   90  0.4  0.4  0.4
5  100   85   85   90  0.4  0.4  0.4
6   80   90   85   90  0.4  0.4  0.4

接下来,我将“循环”转换为“函数”

results_table <- data.frame()

grid_function <- function(random_1, random_2, random_3, random_4, split_1, split_2, split_3) {
    

    
    #bin data according to random criteria
    train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_4, "b", "c")))
    
    train_data$cat = as.factor(train_data$cat)
    
    #new splits
    a_table = train_data %>%
        filter(cat == "a") %>%
        select(a1, b1, c1, cat)
    
    b_table = train_data %>%
        filter(cat == "b") %>%
        select(a1, b1, c1, cat)
    
    c_table = train_data %>%
        filter(cat == "c") %>%
        select(a1, b1, c1, cat)
    

    #calculate random quantile ("quant") for each bin
    
    table_a = data.frame(a_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_1)))
    
    table_b = data.frame(b_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_2)))
    
    table_c = data.frame(c_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_3)))
    
    
    
    
    #create a new variable ("diff") that measures if the quantile is bigger tha the value of "c1"
    table_a$diff = ifelse(table_a$quant > table_a$c1,1,0)
    table_b$diff = ifelse(table_b$quant > table_b$c1,1,0)
    table_c$diff = ifelse(table_c$quant > table_c$c1,1,0)
    
    #group all tables
    
    final_table = rbind(table_a, table_b, table_c)
    
    #create a table: for each bin, calculate the average of "diff"
    final_table_2 = data.frame(final_table %>%
                                   group_by(cat) %>%
                                   summarize(
                                       mean = mean(diff)
                                   ))
    
    #add "total mean" to this table
    final_table_2 = data.frame(final_table_2 %>% add_row(cat = "total", mean = mean(final_table$diff)))
    
    #format this table: add the random criteria to this table for reference
    final_table_2$random_1 = random_1
    
    final_table_2$random_2 = random_2
    
    final_table_2$random_3 = random_3
    
    final_table_2$random_4 = random_4
    
    final_table_2$split_1 = split_1
    
    final_table_2$split_2 = split_2
    
    final_table_2$split_3 = split_3
    
    final_table_2$iteration_number = i
    
    
    results_table <- rbind(results_table, final_table_2)
    
    final_results = dcast(setDT(results_table), iteration_number + random_1 + random_2 + random_3 + random_4 + split_1 + split_2 + split_3 ~ cat, value.var = 'mean')
    
}

Problem:而不是在随机选择的点上评估函数,例如" 95.67371 111.8133 94.00313 102.0569 0.84045638 0.6882731 0.7749321 0.82051282 0.6870229 0.7734554 0.730",我想在网格(DF_1)内定义的点处评估函数,例如" 80 85 85 90 0.4 0.4 0.4"

到目前为止我尝试过的:

#Reduce size of the gird for this example

DF_1 = DF_1[1:100,]

#rename variables within the grid:

colnames(DF_1) <- c("random_1" , "random_2", "random_3",
                    "random_4", "split_1", "split_2", "split_3")

#evauate function at points from grid:

resultdf1 <- apply(DF_1,1, # 1 means rows
                   FUN=function(x){
                     do.call(
                       # Call Function grid_function2 with the arguments in
                       # a list
                       grid_function,
                       # force list type for the arguments
                       as.list(
                         # make the row to a named vector
                         unlist(x)
                         )
                       )
                     }
                   )


#reformat results, it seems like every "block" in this output is identical

a = resultdf1$`1`
a = data.frame(a)

head(a)
  iteration_number random_1 random_2  random_3 random_4    split_1   split_2   split_3          a         b         c total
1                1 95.67371 111.8133  94.00313 102.0569 0.84045638 0.6882731 0.7749321 0.82051282 0.6870229 0.7734554 0.730
2                2 92.31360 110.0762 106.46871 109.5343 0.24615922 0.8777580 0.7847697 0.24731183 0.8777429 0.7840909 0.744
3                3 81.02645 110.4645 116.42006 119.6172 0.11943576 0.9762721 0.9100522 0.14285714 0.9758162 0.9103448 0.943
4                4 90.35986 116.7089 114.15588 116.7231 0.07675141 0.8661540 0.3236617 0.08139535 0.8658065 0.3207547 0.702
5                5 89.28374 114.7103 119.70448 119.7725 0.08881443 0.6351936 0.8565509 0.09027778 0.6349614 0.8461538 0.573
6                6 87.35767 103.8575  97.44462 116.0414 0.48372890 0.2319129 0.2701634 0.47368421 0.2326333 0.2711370 0.255

这似乎有效,但是:

  • 根据结果resultdf1$,“random_1、random_2、random_3、random_4、split_1、split_2、split_3”的值与“DF_1”中的值不匹配。

  • 上面的代码生成同一个表 100 次(例如 resultdf1$1, 结果df1$2, ... 结果df1$100)。有没有办法只生成这些“块”之一(例如 resultdf1$1),这样会使用更少的计算机内存?

有人可以告诉我如何解决这些问题吗?

Thanks.


As the grid_function正在更新全局环境中的原始对象“train_data”并且不作为参数传递给函数,该对象被修改。我们可能需要添加一个额外的参数

results_table <- data.frame()

grid_function <- function(train_data, random_1, random_2, random_3, random_4, split_1, split_2, split_3) {
    

    
    #bin data according to random criteria
    train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_4, "b", "c")))
    
    train_data$cat = as.factor(train_data$cat)
    
    #new splits
    a_table = train_data %>%
        filter(cat == "a") %>%
        select(a1, b1, c1, cat)
    
    b_table = train_data %>%
        filter(cat == "b") %>%
        select(a1, b1, c1, cat)
    
    c_table = train_data %>%
        filter(cat == "c") %>%
        select(a1, b1, c1, cat)
    

    #calculate random quantile ("quant") for each bin
    
    table_a = data.frame(a_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_1)))
    
    table_b = data.frame(b_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_2)))
    
    table_c = data.frame(c_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = split_3)))
    
    
    
    
    #create a new variable ("diff") that measures if the quantile is bigger tha the value of "c1"
    table_a$diff = ifelse(table_a$quant > table_a$c1,1,0)
    table_b$diff = ifelse(table_b$quant > table_b$c1,1,0)
    table_c$diff = ifelse(table_c$quant > table_c$c1,1,0)
    
    #group all tables
    
    final_table = rbind(table_a, table_b, table_c)
    
    #create a table: for each bin, calculate the average of "diff"
    final_table_2 = data.frame(final_table %>%
                                   group_by(cat) %>%
                                   summarize(
                                       mean = mean(diff)
                                   ))
    
    #add "total mean" to this table
    final_table_2 = data.frame(final_table_2 %>% add_row(cat = "total", mean = mean(final_table$diff)))
    
    #format this table: add the random criteria to this table for reference
    final_table_2$random_1 = random_1
    
    final_table_2$random_2 = random_2
    
    final_table_2$random_3 = random_3
    
    final_table_2$random_4 = random_4
    
    final_table_2$split_1 = split_1
    
    final_table_2$split_2 = split_2
    
    final_table_2$split_3 = split_3
    
    final_table_2$iteration_number = i
    
    
    results_table <- rbind(results_table, final_table_2)
    
    final_results = dcast(setDT(results_table), iteration_number + random_1 + random_2 + random_3 + random_4 + split_1 + split_2 + split_3 ~ cat, value.var = 'mean')
    
}

然后通过copy'train_data' 作为输入

train_data_new <- copy(train_data)
DF_1 <- DF_1[1:5,]

将该函数称为

resultdf1 <- apply(DF_1,1, # 1 means rows
                    FUN=function(x){
                      do.call(
                        # Call Function grid_function2 with the arguments in
                        # a list
                        grid_function,
                        # force list type for the arguments
                        c(list(train_data_new), as.list(
                          # make the row to a named vector
                          unlist(x)
                          )
                        ))
                      }
                    )

-ouptut

resultdf1
$`1`
   iteration_number random_1 random_2 random_3 random_4 split_1 split_2 split_3   b         c total
1:              100       80       85       85       90     0.4     0.4     0.4 0.5 0.3997996   0.4

$`2`
   iteration_number random_1 random_2 random_3 random_4 split_1 split_2 split_3   b         c total
1:              100       85       85       85       90     0.4     0.4     0.4 0.5 0.3997996   0.4

$`3`
   iteration_number random_1 random_2 random_3 random_4 split_1 split_2 split_3   b         c total
1:              100       90       85       85       90     0.4     0.4     0.4 0.5 0.3997996   0.4

$`4`
   iteration_number random_1 random_2 random_3 random_4 split_1 split_2 split_3 a   b         c total
1:              100       95       85       85       90     0.4     0.4     0.4 0 0.5 0.4002006   0.4

$`5`
   iteration_number random_1 random_2 random_3 random_4 split_1 split_2 split_3 a   b         c total
1:              100      100       85       85       90     0.4     0.4     0.4 0 0.5 0.4002006   0.4
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R:使用网格的馈送功能 的相关文章

随机推荐

  • 如何创建网络服务

    我使用 Ruby on Rails 框架构建了一个网站 该站点将包含一个 Flash 应用程序 该应用程序将使用 Web 服务与 Rails 应用程序进行交互 我的合作伙伴构建了 Flash 应用程序 他告诉我 Flash 应用程序通过 W
  • 使用R中的mat2listw函数创建空间权重矩阵

    我正在尝试使用 mat2listw 函数在 R 中创建一个权重对象 我有一个非常大的空间权重矩阵 大约 22 000x22 000 这是在 Excel 中创建并读入 R 的 我现在正在尝试实现 library spdep SW mat2li
  • Java中将图像亮度转换为灰度错误

    我使用以下代码在 Java 中将图像转换为灰度 BufferedImage originalImage ImageIO read new File home david input bmp BufferedImage grayImage n
  • 如何使用 C++ 检测 Linux 中串行端口上运行的缓冲区溢出

    我有一个大问题 目前我正在通过以下钩子访问串行端口 fd open dev ttyS1 O RDWR O NOCTTY 然后我使用以下代码块从中读取 i select fd 1 rfds NULL NULL tv iLen read fd
  • json.parse 给出 Uncaught SyntaxError: Unexpected Token (Django json 序列化查询集)

    我遇到了错误Uncaught SyntaxError Unexpected Token当尝试解析 json 数据时 这是我的ajax代码 json2 js ajax type POST url best choose invoice ite
  • 将textarea中的html标签转换为富文本

    我正在使用 PHP 来填充文本区域
  • 使用 urllib2 登录网站 - Python 2.7

    好的 我将其用于 reddit 机器人 但我希望能够弄清楚如何登录任何网站 如果这有道理的话 我意识到不同的网站使用不同的登录表单等 那么我如何找出如何针对每个网站进行优化呢 我假设我需要在 html 文件中查找某些内容 但不知道是什么 我
  • JTextArea 作为控制台

    我在下面发布了两段代码 两个代码单独工作都很好 现在 当我运行 Easy 文件并单击 开始 按钮时 我希望实现 AddNumber 类 我的意思是说 除了在控制台上运行 AddNumber 之外 有什么方法可以让 AddNumber 在单击
  • 如何让 fwrite() 不加双引号?

    我使用以下命令读取了一个在其字段中包含 html 代码的 csv 文件fread 对其进行一些维护并将生成的数据表写入文件中fwrite 问题是现在所有的 html 都充满了四重引号 例如colspan 7 有的是qmethod参数 但我不
  • Visual Studio Online Build 将 git 输出视为错误

    我在 Visual Studio Online 中的构建尝试通过以下方式部署我的 Azure 网站Kudu 该脚本工作正常 并且部署也已完成 但由于某种原因 VSO 将 git 输出视为错误 并声明整个构建失败 看看下面的屏幕截图 一些细节
  • ggplot箱线图的位置躲避警告?

    我正在尝试使用以下代码使用 ggplot2 制作箱线图 p lt ggplot data aes d score reorder d names d scores median geom boxplot 我有称为名称的因素和称为分数的整数
  • 是否可以在 Magento 中以编程方式发送电子邮件?

    是否可以在 Magento 中以编程方式发送电子邮件 也许从自定义模块中的控制器中 您可以获取模板 填充其变量并发送电子邮件吗 Thanks 绝对地 以下是 Checkout 帮助程序的示例 mailTemplate Mage getMod
  • 设置 TabPage 标题颜色

    问候 我有一个选项卡控件 我希望其中 1 个选项卡的文本颜色在事件发生时更改 我找到了类似的答案C TabPage 颜色事件 and C Winform 如何设置 TabControl 而不是选项卡页 的基色但使用这些设置所有颜色而不是一种
  • PHP网站打开时总是显示空白页面,没有错误

    一 前提 PHP在Win2003 STD R2 SP2上加载在IIS6上 PHP 5213使用FastCGI MySQL 5145 客户向我发送了网站文件 我将其解压缩到C InetPub wwwroot
  • 为什么这个 Haskell 代码永远不会终止?

    我最近写了一些 Haskell 代码 但它永远不会终止 当我仔细检查我的代码后 问题归结为以下代码片段 main IO main print let a 10 in let a a in a Int 我想这一定与 Haskell 的懒惰有关
  • Always_comb 中的 SystemVerilog“if”语句“不是纯粹的组合逻辑”错误

    我很困惑 并且有点沮丧 我花了很多时间在 Modelsim 中研究一些 SystemVerilog 我已经达到了可以在我的硬件上测试它的某个阶段 但是在 Quartus 中编译不成功 我确实知道这可能会发生 但在这种情况下我的错误似乎没有意
  • 如何强制 Laravel 项目对所有路由使用 HTTPS?

    我正在开发一个需要安全连接的项目 我可以通过以下方式设置路由 uri 资产以使用 https Route get order details id uses gt OrderController details as gt order de
  • 防止EditText自动聚焦

    我有一个 Android 活动 有一个EditText在整个布局中 由于某种原因 每当活动开始时 键盘就会出现 我努力了all以下事项 将这两个放入OnStart FindViewById
  • R 图:如何使用 mtext 获取 las=1 的顶部对齐垂直标签

    我正在尝试使用在垂直轴上获得标签mtext水平阅读 las 1 并且位于轴的顶部 我的尝试是使用las 1 adj 1 当我不指定时我可以获得所需的展示位置las 1 但是一旦我添加las 1论证adj 1放置消失 这是带有代码的图片 左图
  • R:使用网格的馈送功能

    我正在使用 R 编程语言 我编写了这个循环 它在随机选择的输入 random 1 random 2 random 3 random 4 split 1 split 2 split 3 中评估以下 函数 实际上是一个 循环 100次 load