问题描述
应用很容易,但这是我要破解的一个概括:
apply is easy, but this is a nutshell for me to crack:
在多参数回归中,优化器用于查找参数函数的最佳拟合,例如x1,x2数据.通常,针对特定功能的优化器如果尝试优化转换后的参数(例如使用DEoptim,nls.lm等R优化器),则速度可能会更快.根据我的经验,从一个参数函数对不同参数进行不同的转换甚至更好.
In multi-parametric regression, optimisers are used to find a best fit of a parametric function to say x1,x2 Data. Often, and function specific, optimisers can be faster if they try to optimise transformed parameters (e.g. with R optimisers such as DEoptim, nls.lm)From experience I know, that different transformations for different parameters from one parametric function is even better.
我希望将x.trans(下面的c.f.)中的不同函数应用于不同的位置,但在x.val中它们对应的位置:
I wish to apply different functions in x.trans (c.f. below) to different but in their position corresponding elements in x.val:
一个可使用的模拟示例.
A mock example to work with.
#initialise
x.val <- rep(100,5); EDIT: ignore this part ==> names(x.val) <- x.names
x.select <- c(1,0,0,1,1)
x.trans <- c(log10(x),exp(x),log10(x),x^2,1/x)
#select required elements, and corresponding names
x.val = subset(x.val, x.select == 1)
x.trans = subset(x.trans, x.select == 1)
# How I tried: apply function in x.trans[i] to x.val[i]
...
有什么想法吗? (我已经尝试了apply和sapply,但无法获取存储在x.trans中的函数)
Any ideas? (I have tried with apply, and sapply but can't get at the functions stored in x.trans)
推荐答案
您必须改用它:
x.trans <- c(log10,exp,log10,function(x)x^2,function(x)1/x)
然后这样:
mapply(function(f, x) f(x), x.trans, x.val)
这篇关于将不同的函数应用于R中向量的不同元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!