Skip to content
Prev 58677 / 398502 Next

plotting lm coeficients with their means

How about something like this?

my.func <- function(y, x1, x2, x3, x4  = NULL){
    my.formula <- as.formula("y ~ x1 + x2 + x3 + x4")
    if(is.null(x4)) { my.formula <- as.formula("y ~ x1 + x2 + x3") }
    outlm <- lm(my.formula)
    meanvec<-c(mean(x1),mean(x2),mean(x3))
    if(is.null(x4) == F) { meanvec<-c(mean(x1),mean(x2),mean(x3),mean(x4)) }
    mf <- data.frame(Impact = outlm$coef[-1], Performance = meanvec)
    plot((mf),xlab="Impact",ylab="Performance", type = "n")
    points(mf, pch=21, bg="grey", cex=4)
    text(x = mf$Impact, y = mf$Performance, labels = rownames(mf))
    abline(h=mean(mf$Performance))
    abline(v=mean(mf$Impact))
}

response <- 1:10
pred.1 <- rnorm(10)
pred.2 <- runif(10)
pred.3 <- pred.1 + runif(10)
pred.4 <- runif(10)
my.func(y = response, x1 = pred.1, x2 = pred.2, x3 = pred.3)
my.func(y = response, x1 = pred.1, x2 = pred.2, x3 = pred.3, x4 = pred.4)


You'd have to do some work to generalize it further. I don't have much
experience writing functions with missing args and I expect this is a dumb
way of doing it.

HTH, Andy