(R)如何通过将较短的变量表达式组合来写出更大的表达?

发布于 2025-01-29 07:02:48 字数 474 浏览 1 评论 0原文

简要介绍,我有多个数据文件,可以通过基于数学方程式的模型拟合,这些模型是其他较短的数学方程式的组合(通过总和,互动等)。

例如,我有三个简短的方程式:

expression1 <- exp(x)
expression2 <- exp(x^2)
expression3 <- exp(1/x)

由于每个数据文件都有自己的较大表达式可以产生更好的拟合度,因此我希望能够生成这些较大的表达式,以作为较短表达式的委托。

我想要的是能够写下这样的东西:

expression1(1) + expression1(2) * expression2(3) + expression2(1)

get:

x1 + x2 * x3 ^2 + 1/x1

以后我将使用此较大的方程式找到更适合一个数据文件的值x1,x2,x3。

Brief introduction, I have multiple data file that can be fitted by models based in mathematical equations that are a combination (by sum, mutiplication, etc...) of other shorter mathematical equations.

As an example I have three short equations:

expression1 <- exp(x)
expression2 <- exp(x^2)
expression3 <- exp(1/x)

Because each data file has it's own bigger expression that produce a better fit, I want to be able to generate these larger expressions as a combitaion of the shorter expressions.

What I want is to be able to write something like this:

expression1(1) + expression1(2) * expression2(3) + expression2(1)

And get:

x1 + x2 * x3^2 + 1/x1

Later I will use this larger equations to find the values x1, x2, x3 that better fits one data file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2025-02-05 07:02:48

1)可能是您的意思是表达而不是EXP。进行这种更改,我们定义了E1,E2,E3和E。 ESUB是一个函数,将变量名称替换为表达式中的另一个名称。同名软件包中的GSUBFN就像GSUB一样功能。我们取消E,使用GSUBFN并将其解析。

library(gsubfn)

e1 <- expression(x)
e2 <- expression(x^2)
e3 <- expression(1/x)
e <- expression(e1(1) + e1(2) * e2(3) + e2(1))

esub <- function(expr, env) do.call("substitute", list(expr, env))
g <- gsubfn("(\\w+)[(](\\w+)[)]", 
  ~ deparse(esub(get(x)[[1]], list(x = as.name(paste0("x", y))))), 
  deparse(e))
parse(text = g)[[1]]
## expression(x1 + x2 * x3^2 + x1^2)

2)如果需要使用字符串而不是表达式,它甚至更短:

library(gsubfn)

s1 <- "x"
s2 <- "x^2"
s3 <- "1/x"
s <- "s1(1) + s1(2) * s2(3) + s2(1)"

gsubfn("(\\w+)[(](\\w+)[)]", x + y ~ gsub("\\bx\\b", paste0("x", y), get(x)), s)
## [1] "x1 + x2 * x3^2 + x1^2"

1) Presumably you meant expression rather than exp. Making that change we define e1, e2, e3 and e. esub is a function which replaces a variable name with another in an expression. gsubfn in the package of the same name is like gsub except the second argument can be a function (possibly expressed using formula notation as we do here) which takes the capture groups in the pattern as arguments and replaces the entire pattern with the output of the function. We deparse e, use gsubfn and parse it back.

library(gsubfn)

e1 <- expression(x)
e2 <- expression(x^2)
e3 <- expression(1/x)
e <- expression(e1(1) + e1(2) * e2(3) + e2(1))

esub <- function(expr, env) do.call("substitute", list(expr, env))
g <- gsubfn("(\\w+)[(](\\w+)[)]", 
  ~ deparse(esub(get(x)[[1]], list(x = as.name(paste0("x", y))))), 
  deparse(e))
parse(text = g)[[1]]
## expression(x1 + x2 * x3^2 + x1^2)

2) If it were desired to use strings instead of expressions it is even shorter:

library(gsubfn)

s1 <- "x"
s2 <- "x^2"
s3 <- "1/x"
s <- "s1(1) + s1(2) * s2(3) + s2(1)"

gsubfn("(\\w+)[(](\\w+)[)]", x + y ~ gsub("\\bx\\b", paste0("x", y), get(x)), s)
## [1] "x1 + x2 * x3^2 + x1^2"
三生殊途 2025-02-05 07:02:48

EXP是指数函数,因此您的代码无法执行您认为的操作。 使这样的东西可以工作

make_exp <- function(expr) {
  expr <- match.call()$expr
  function(x) structure(list(val = do.call(substitute, list(expr))), 
                        class = "ex")
}

print.ex <- function(x, ...) print(x$val)

Ops.ex <- function(e1, e2) structure(list(val = call(.Generic, e1$val, e2$val)),
                                      class = "ex")

为了

expression1 <- make_exp(x)
expression2 <- make_exp(x^2)
expression3 <- make_exp(1/x)

expression1(x1) + expression2(x2) * expression3(x3)
#> x1 + x2^2 * (1/x3)

exp is the exponential function, so your code doesn't do what you think it does. To get something like this to work probably needs the creation of a new S3 class with print and Ops methods:

make_exp <- function(expr) {
  expr <- match.call()$expr
  function(x) structure(list(val = do.call(substitute, list(expr))), 
                        class = "ex")
}

print.ex <- function(x, ...) print(x$val)

Ops.ex <- function(e1, e2) structure(list(val = call(.Generic, e1$val, e2$val)),
                                      class = "ex")

This allows

expression1 <- make_exp(x)
expression2 <- make_exp(x^2)
expression3 <- make_exp(1/x)

expression1(x1) + expression2(x2) * expression3(x3)
#> x1 + x2^2 * (1/x3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文