nouse
nouse

Reputation: 3461

Pasting object names inside functions

This is a follow-up question to this (see data and previous commands).

Starting with a list of models in mods, i am now able to find the model with the least AIC (corresponds to the best model):

    mods <- lapply(methods, function(m) 
      update(amod.null, correlation = getFunction(m)(1, form = ~ x + y), method="ML"))
    names(mods) <- methods
    list.AIC <- lapply(mods, function(x) AIC(x))
    best.mod <- names(which.min(list.AIC))

Now, i need to do some testing on the model, e.g. Tukey between dates. The syntax is very simple, e.g. for amod.null

library(multcomp)
res <- glht(amod.null, mcp(Date = "Tukey"))

The tricky part is, how can i tell glht to use the model which was put into best.mod (note: this is all happening within a loop). I tried

res <- glht(paste("mods$", as.factor(best.mod),sep = "") , mcp(Date = "Tukey"))

but to no avail, as glht needs to find a model-object in the first argument.

/edit: Possibly useful:

   names(mods)
[1] "corExp"    "corGaus"   "corLin"    "corRatio"  "corSpher"

Upvotes: 0

Views: 63

Answers (1)

hpesoj626
hpesoj626

Reputation: 3619

Since the models are stored in the list mods, you can access the "best model" by using the index of which.min(list.AIC):

list.AIC <- sapply(mods, AIC)
best.mod <- mods[which.min(list.AIC)]
best.mod[[1]]

Upvotes: 1

Related Questions