coeftab <- function( ... , se=FALSE , se.inside=FALSE , digits=4 ) { # coeftab - pretty table of coefficients for comparing models # works for lm() and a few other object types, like most results of mle2() # tends to break when R constructs dummy variables from factors # can be avoided by just making your dummy variables explicitly before fitting # by Richard McElreath # last update April 10 2009 # se=TRUE outputs standard errors # se.inside=TRUE prints standard errors in parentheses in same column as estimates # retrieve list of models L <- list(...) if ( is.list(L[[1]]) && length(L)==1 ) L <- L[[1]] # retrieve model names from function call mnames <- match.call() mnames <- as.character(mnames)[2:(length(L)+1)] # count number of unique parameters param.names <- {} for ( i in 1:length(L) ) { c.names <- names( coef( L[[i]] ) ) param.names <- unique( c( param.names , c.names ) ) } # columns for standard errors if ( se==TRUE && se.inside==FALSE ) { for ( i in 1:length(L) ) { kse.names <- paste( names( coef( L[[i]] ) ) , ".se" , sep="" ) param.names <- unique( c( param.names , kse.names ) ) } } # make empty table nk <- length(param.names) d <- matrix( NA , ncol=nk ) d <- data.frame(d) colnames(d) <- c( param.names ) # loop over models and insert values for ( i in 1:length(L) ) { klist <- coef( L[[i]] ) for ( j in 1:length(klist) ) { d[i,][ names( klist[j] ) ] <- as.numeric( klist[j] ) } } # insert standard errors if ( se==TRUE ) { for ( i in 1:length(L) ) { kse <- sqrt( diag ( vcov( L[[i]] ) ) ) for ( j in 1:length(kse) ) { if ( se.inside==FALSE ) # own column d[i,][ paste(names(kse)[j],".se",sep="") ] <- as.numeric( kse[j] ) else # combine with estimate d[i,][ names(kse)[j] ] <- paste( formatC( as.real(d[i,][ names(kse)[j] ]) , digits=digits ) , " (" , formatC( as.real( kse[j] ) , digits=digits ) , ")" , sep="" ) } } } # add model names to rows rownames(d) <- mnames # formatting for parenthetical standard errors if ( se.inside==TRUE && se==TRUE ) { comment(d) <- "Values in parentheses are quadratic estimate standard errors." colnames(d) <- paste( colnames(d) , "(se)" ) for ( i in 1:nrow(d) ) { for ( j in 1:ncol(d) ) { d[i,j] <- ifelse( is.na(d[i,j]) , "" , d[i,j] ) } } } # return table d }