[R] normalized regression output

Liaw, Andy andy_liaw at merck.com
Tue Apr 6 02:31:27 CEST 2004


> From: ivo welch
> 
> Hi:  I would like to write a function that takes as its input 
> a formula 
> and outputs normalized coefficients ( coef(x)*sdv(x)/sdv(y) 
> ).  now, a 
> formula is an object, and I cannot see how to extract the 
> variables for 
> obtaining sdv's.  the intent is to write something like
> 
> 	my.print.lm( formula ) {
> 	  model <- lm(formula);
> 	  coefs <- (t(summary.lm(model)))[1,];
> 	  tvals <- (t(summary.lm(model)))[3,];
> 	  for (i in 1:length( formula.contents.length ) ) {
> 	    normcoefs[i] <- coefs[i]*sd( formula.coef[i] )
> 				/sd( formula.yvar ); }
> 	  # now I can do nice printing
> 	}
> 
> 	my.print.lm ( y~x+z );

It's not clear to me what you want to do.  You need data in addition to the
formula, no?  See if the following is approximately what you are looking
for:

myPrint.lm <- function(lm.obj) {
    ## You don't want to include the intercept, do you?
    coefs <- coef(lm.obj)[-1]
    tvals <- summary(lm.obj)$coefficients[-1, 3]
    m <- model.frame(fit)  # get the data used for the fit.
    SD <- apply(m, 2, sd)
    normCoefs <- coefs * SD[-1] / SD[1]
    ## Do whatever nice printing you want.
    ## ...
    invisible(normCoefs)
}

Here's a test:

> set.seed(1)
> dat <- data.frame(y=rnorm(30), x1=runif(30), x2=runif(30), x3=runif(30))
> 
> fit <- lm(y~., dat)
> print(myPrint.lm(fit))
        x1         x2         x3 
0.19635111 0.19688412 0.02435227 
> 
> ## Isn't this easier?
> dat3 <- as.data.frame(scale(dat))
> coef(lm(y ~ 0 + ., dat3))
        x1         x2         x3 
0.19635111 0.19688412 0.02435227 

Andy

 
> or something like it.  If this is not easy, please just tell 
> me.  I just 
> do not want to spend a day to reinvent a wheel that is very 
> simple for 
> an R statistician.  help appreciated.
> 
> regards,
> 
> /iaw
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list