(R)如何通过将较短的变量表达式组合来写出更大的表达?
简要介绍,我有多个数据文件,可以通过基于数学方程式的模型拟合,这些模型是其他较短的数学方程式的组合(通过总和,互动等)。
例如,我有三个简短的方程式:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)可能是您的意思是表达而不是EXP。进行这种更改,我们定义了E1,E2,E3和E。 ESUB是一个函数,将变量名称替换为表达式中的另一个名称。同名软件包中的GSUBFN就像GSUB一样功能。我们取消E,使用GSUBFN并将其解析。
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.
2) If it were desired to use strings instead of expressions it is even shorter:
EXP
是指数函数,因此您的代码无法执行您认为的操作。 使这样的东西可以工作为了
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 withprint
andOps
methods:This allows