I found this function that I wrote when I introduced the intro class to model regression. I am sure this is less than optimal, but it worked well enough for this course. If nothing else, perhaps this will force you to point in the right direction to take it further.
writeCoef <- function(x) { require(plyr) coefnames <- as.data.frame(coef(x)) coefnames$betas <- row.names(coefnames) coefnames <- adply(coefnames, 1, function(x) paste(round(x[1],3), x[2] , sep = " * ")) dependent <- paste(as.character(x$call$formula)[2], " = ", sep = "") ret <- paste(dependent, paste(coefnames[,3], sep = "", collapse = " + ")) ret <- gsub("\\*\\s\\(Intercept\\)", "", ret) return(ret) }
And in action:
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2,10,20, labels=c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) > writeCoef(lm.D9) [1] "weight = 5.032 + -0.371 * groupTrt"
Chase
source share