# [R] plotting lm coeficients with their means

Andy Bunn abunn at whrc.org
Mon Nov 8 19:39:41 CET 2004

```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

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Derek Margetts
> Sent: Monday, November 08, 2004 11:39 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] plotting lm coeficients with their means
>
>
> I am trying to write a function that will run a linear
> model and plot the regression coeficients with their
> corresponding means.  I am having two problems.  I can
> get the plot with the function below, but I am having
> trouble labeling the points.
>
>
> function(y,x1,x2,x3,x4){
> outlm<-lm(y~x1+x2+x3+x4)
> imp<-as.data.frame(outlm\$coef[-1])
> meanvec<-c(mean(x1),mean(x2),mean(x3),mean(x4))
> out<-list(imp,meanvec)
> mf<-as.data.frame(out)
> plot((mf),xlab="Impact",ylab="performance")
> abline(h=mean(meanvec))
> abline(v=mean(imp))
> }
>
> Problem #2:  If I only input x1,x2,and x3 how do I get
> the function to ingnore the 4th instead of giving me
> the unused argument error?
>
> Thanks
> Derek
>
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help