[R] indexing model names for AICc table
David Winsemius
dwinsemius at comcast.net
Thu Feb 26 01:28:20 CET 2009
I think part of your problem with your chosen method of attack arises
from your misspelling of the logLik function. The other part arises
because you have not yet digested FAQ 7.20:
http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
I haven't have much success assembling things in eval(substitute(...))
constructions, so if I were using a loop, I would have gone this way:
> xlist <- list(x1,x2,x3)
> for (i in 1:3 )
+ {
+ aic.table$Log.lik[i] <- logLik(lm(y~xlist[[i]]))[1]
+ }
> aic.table
model.number Log.lik
1 1 -43.39382
2 2 -41.24109
3 3 -34.20999
--
David Winsemius
On Feb 25, 2009, at 6:58 PM, Mark Drever wrote:
> hi folks,
>
> I'm trying to build a table that contains information about a series
> of General Linear Models in order to calculate Akaike weights and
> other measures to compare all models in the series.
>
> i have an issue with indexing models and extracting the information
> (loglikehood, AIC's, etc.) that I need to compile them into the
> table. Below is some sample code that illustrates my approach so far
> and my problem. I realize that somehow i need to provide actual
> object for the AIC or LogLik call, and I've tried a couple of
> different ideas (e.g., as.name, expression), but have come up empty.
>
> Thanks so much. Any help much appreciated.
> Mark.
>
> Sample code begins here....
>
> x1 <- rnorm(10,1,1)
> x2 <- rnorm(10,2,2)
> x3 <- rnorm(10,3,3)
> y <- x1*2 + x2*3 + x3*4 + rnorm(10,0,0.4)
>
> model1 <- lm(y ~ x1)
> model2 <- lm(y ~ x2)
> model3 <- lm(y ~ x3)
>
> aic.table <- data.frame(model.number = 1:3, Log.lik = NA)
>
> for (i in 1:3)
> {
> aic.table$Log.lik[i] <- Loglik(paste("model",i,sep=""))[1]
> }
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list