在函数调用R

时间:2017-03-29 19:55:39

标签: r

我的数据框定义如下:

model_comp
         logLik       IC  Lack of fit  Res var
W2.4  -353.2939 716.5878 1.361885e-01 26.80232
baro5 -353.2936 718.5871          NaN 27.04363
LL.5  -353.2940 718.5880          NaN 27.04384
LL.3  -360.3435 728.6871 3.854799e-04 29.99842
W1.3  -360.3842 728.7684 3.707592e-04 30.01948
W1.4  -360.3129 730.6258 7.850947e-05 30.25028
LL.4  -360.3170 730.6340 7.818416e-05 30.25243

最佳模型拟合是具有最低IC(信息标准)的模型。我想用最合适的方式来做一些情节......所以我创造了:

> bestmodel <- noquote(paste0(as.name(rownames(model_comp[which.min(model_comp$IC),])),"()"))
> bestmodel
[1] W2.4()

我想使用W2.4()作为对DRC包的函数调用。

例如,当手动指定时,此调用有效:

drm(y~x,logDose = 10, fct=W2.4())

我试图使用bestmodel中的值来代替:

drm(y~x,logDose = 10,fct = as.formula(paste(bestmodel)))

我已经尝试了here给出的所有选项但没有成功。我与as.formula(),noquote(),as.name()搞混了,没有成功。

我还试过了as.name(paste0(as.name(bestmodel),"()"))我没有添加&#34;()&#34;在上面的bestmodel定义中。仍然没有骰子。

model_comp <- structure(list(logLik = c(-353.293902612472, -353.293568997018, 
-353.294024776211, -360.343530770823, -360.384220907907, -360.312897918459, 
-360.317018443052), IC = c(716.587805224944, 718.587137994035, 
718.588049552421, 728.687061541646, 728.768441815814, 730.625795836919, 
730.634036886105), `Lack of fit` = c(0.136188459104035, NaN, 
NaN, 0.000385479884900107, 0.000370759187117765, 7.85094742623572e-05, 
7.81841606352332e-05), `Res var` = c(26.8023196097934, 27.0436263934882, 
27.0438389102235, 29.9984226526044, 30.0194755526501, 30.2502847248304, 
30.2524338881051)), .Names = c("logLik", "IC", "Lack of fit", 
"Res var"), row.names = c("W2.4", "baro5", "LL.5", "LL.3", "W1.3", 
"W1.4", "LL.4"), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

仅使用noquote()不在字符串周围绘制引号并不会将字符值转换为可执行的代码段。 R在字符值,符号或函数调用之间存在很大的不同。你真的只能用另一个替换一个。

所以,让我们说你已经从rownames中提取了字符值

x <- "W2.4"

这基本上是您想要的函数的字符串版本。您可以使用W2.4从其字符串名称中获取符号的值(在本例中为drc:package中的函数get())。所以你可以打电话给

drm(y~x, logDose = 10, fct = get(x)())

请注意额外的括号。 get(x) - 调用返回W2.4函数,以及get()返回的函数的第二组括号调用。

使用ryegrass包附带的drc数据集,我们可以看到这两行返回相同的内容

drm(rootl ~ conc, data = ryegrass, fct = W2.4())
drm(rootl ~ conc, data = ryegrass, fct = get(x)())