akaDrHouse
akaDrHouse

Reputation: 2240

Resolving a variable value within a function call R

I have a data frame defined as follows:

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

The best model fit is the one with the lowest IC (information criteria). I want to use the best fit to do some plotting etc... So I created:

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

I want to use the W2.4() as a function call to a the DRC package.

For example this call works when manually specified:

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

I'm trying to use the value in bestmodel instead to do something like:

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

I've tried all the options given here with no success. I've messed with as.formula(), noquote(), as.name() with no success.

I also tried as.name(paste0(as.name(bestmodel),"()")) where I didn't add on the "()" in the bestmodel definition above. Still no dice.

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")

Upvotes: 0

Views: 293

Answers (1)

MrFlick
MrFlick

Reputation: 206177

Just using noquote() not to draw the quotes around a string doesn't turn a character value into an executable piece of code. There is a big different in R between a character value an a symbol or function call. You can't really just replace one with the other.

So let's say you have extracted the character value from the rownames

x <- "W2.4"

This is basically the string version of the function you want. You can get the value of a symbol (in this case the function W2.4 from the drc:package) from its string name with get(). So you can call

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

Note the extra parenthesis. The get(x)-call returns the W2.4 function, and the second set of parenthesis calls that function returned by get().

Using the ryegrass dataset that comes with the drc package, we can see that these two lines return the same thing

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

Upvotes: 2

Related Questions