Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created March 28, 2012 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewheiss/2229633 to your computer and use it in GitHub Desktop.
Save andrewheiss/2229633 to your computer and use it in GitHub Desktop.
lm summary with beta coefficients
summary.with.betas <- function(model, logit=FALSE) {
model <- update(model, x=TRUE, y=TRUE)
sd.x <- apply(model$x, 2, sd)
sd.y <- sd(model$y)
std.coef <- coef(model) * (sd.x / sd.y)
coef.table <- as.data.frame(summary(model)$coefficients)
coef.table <- cbind(coef.table, beta=std.coef, sdX=sd.x)
if (logit == TRUE) {
e.beta <- exp(coefficients(model))
e.bstdx <- exp(sd.x)
coef.table <- cbind(coef.table, e.beta=e.beta, e.bstdx=e.bstdx)
}
coef.table
}
# Use it like this:
x <- runif(100, -1, 1)
y <- runif(100, -1, 1)
fit <- lm(y ~ x)
summary(fit)
summary.with.betas(fit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment