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] }
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.
Dear Mark, Try this: models<-paste("model",1:3,sep="") t(sapply(models,function(x){ m<-get(x) c(aic=AIC(m),LL=logLik(m)) } ) ) See ?get, ?AIC, ?logLik and ?sapply for more information. HTH, Jorge On Wed, Feb 25, 2009 at 6:58 PM, Mark Drever <mdrever@interchange.ubc.ca>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@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. >[[alternative HTML version deleted]]
Aslo note there are a number of packages with functions producing Burnham and Anderson style AIC tables: pgirmess, bbmle, dRedging(off-CRAN), PKtools, ... On Wed, Feb 25, 2009 at 4:58 PM, Mark Drever <mdrever at interchange.ubc.ca> 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. >