R.在数据框中运行优化功能 [英] R. Run optimization function in data frame

查看:97
本文介绍了R.在数据框中运行优化功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个数据帧df1,看起来像这样(所有值都是随机选择的,但是是从真实数据中得出的近似值):

I have a data frame df1 in R that looks like this (all values were chosen randomly but approximate values from real data):

A B    C    D   E   F      G     H
a 0.04 0.01 50  70  0.01   ...   0.0002
b 0.03 0.1  49  69  0.01   ...   0.0003
c 0.03 0.02 51  71  0.005  ...   0.004
d 0.04 0.02 50  70  0.006  ...   0.0005

G是通过以下方式获得的:

G is obtained in this way:

# Equation 1:

G = (B - C)^2 - B*(1 - B)/(D- 1) - C*(1 - C)/(E - 1)

library(dplyr)
df2 = df1 %>% mutate(G = (B - C)^2 - B*(1 - B)/(D- 1) - C*(1 - C)/(E - 1))

我想创建一个新列G1,在每行中应用优化功能.

I want to create a new column G1 applying an optimization function in each row.

G1使用等式1获得,但用H替换B.H使用等式2获得:

G1 is obtained using equation 1, but replacing B by H. H is obtained using Equation 2:

# Equation 2:

H = (B - z*F)/(1 - z)

我想创建一个新的Z列,z的值介于0到1(步长= 0.01)之间,产生最低的G1.

I would like to create a new column Z, with the value of z, ranging from 0 to 1 (step=0.01) which produces the lowest G1.

我希望得到这样的东西:

I expect to get something like this:

A B    C    D   E   F      G     H      G1  Z
a 0.04 0.01 50  70  0.01   ...   0.0002 ... ...
b 0.03 0.1  49  69  0.01   ...   0.0003 ... ...
c 0.03 0.02 51  71  0.005  ...   0.004  ... ...
d 0.04 0.02 50  70  0.006  ...   0.0005 ... ...

(澄清)

我的问题是如何在给定H(以及上述条件)的情况下找到产生最低G1的z并获得G1.

My question is how to find the z that produces the lowest G1 given H (and the conditions referred above) and also get G1.

推荐答案

考虑使用数据帧提供的所有其他参数,通过 G1 函数从0.01到0.01的0.01到1的整数倍循环.然后取返回的值向量中的最小值.

Consider looping through multiples of 0.01 from 0.01 to 1 through your G1 function with all other parameters supplied by data frame. Then take the minimum of the returned vector of values.

具体来说,您可以设置一个函数,该函数使用mapply(逐元素迭代器函数)传递逐行值,并返回 z 值的最小值.

Specifically, you can set up a function that passes in the row wise values using mapply (the elementwise iterator function) and returns the minimum for z value.

数据 (为了避免出现FALSE问题,F更改为F _)

txt <- "A B    C    D   E   F_
a 0.04 0.01 50  70  0.01  
b 0.03 0.1  49  69  0.01  
c 0.03 0.02 51  71  0.005 
d 0.04 0.02 50  70  0.006"

df <- read.table(text=txt, header=TRUE)

功能

main <- function(B_param, C_param, D_param, E_param, F_param) {

  # EXTENDED G1 FUNCTION (WITH HELPER H)
  func <- function(z) {
    H <- (B_param - z*F_param)/(1 - z)
    G1 <- (H - C_param)^2 - H*(1 - H)/(D_param- 1) - C_param*(1 - C_param)/(E_param - 1)
  }

  # ITERATE THROUGH 0.01 MULTIPLES 
  tmp <- sapply(seq(0.01, 0.99, 0.01), func)

  # RETURN Z AT THE MINIMUM OF VECTOR OF VALUES
  min_z <- seq(0.01, 0.99, 0.01)[which.min(tmp)]

  return(min_z)
}

数据框调用 (使用基础的within添加新列)

Data frame call (using base's within to add new columns)

final_df <- within(df, {

  G <- (B - C)^2 - B*(1 - B)/(D- 1) - C*(1 - C)/(E - 1)

  # CALCULATE z BY PASSING COLUMN VALUES ELEMENTWISE
  z <- mapply(main, B, C, D, E, F_)
  H <- (B - z*F_)/(1 - z)
  G1 <- (H - C)^2 - H*(1 - H)/(D- 1) - C*(1 - C)/(E - 1)      
})

options(scipen=999)

# RE-ORDER COLUMNS
final_df[order(names(final_df))]
#   A    B    C  D  E    F_              G             G1          H    z
# 1 a 0.04 0.01 50 70 0.010 -0.00002715173 -0.00001456576 0.04030303 0.01
# 2 b 0.03 0.10 49 69 0.010  0.00297022059 -0.00326311275 0.11000000 0.80
# 3 c 0.03 0.02 51 71 0.005 -0.00076200000 -0.00076163193 0.03025253 0.01
# 4 d 0.04 0.02 50 70 0.006 -0.00066773144 -0.00066032187 0.04034343 0.01

这篇关于R.在数据框中运行优化功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆