public
Last active

A simple command to grab coefficients, t-stats, p-values, f-stats, etc from a regression and export them as an easy to use spreadsheet.

  • Download Gist
lmOut
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
lmOut <- function(res, file="test.csv", ndigit=3, writecsv=T) {
# If summary has not been run on the model then run summary
if (length(grep("summary", class(res)))==0) res <- summary(res)
co <- res$coefficients
nvar <- nrow(co)
ncol <- ncol(co)
f <- res$fstatistic
formatter <- function(x) format(round(x,ndigit),nsmall=ndigit)
# This sets the number of rows before we start recording the coefficients
nstats <- 4
# G matrix stores data for output
G <- matrix("", nrow=nvar+nstats, ncol=ncol+1)
G[1,1] <- toString(res$call)
# Save rownames and colnames
G[(nstats+1):(nvar+nstats),1] <- rownames(co)
G[nstats, 2:(ncoll+1)] <- colnames(co)
# Save Coefficients
G[(nstats+1):(nvar+nstats), 2:(ncol+1)] <- formatter(co)
# Save F-stat
G[1,2] <- paste0("F(",f[2],",",f[3],")")
G[2,2] <- formatter(f[1])
# Save F-p value
G[1,3] <- "Prob > P"
G[2,3] <- formatter(1-pf(f[1],f[2],f[3]))
# Save R2
G[1,4] <- "R-Squared"
G[2,4] <- formatter(res$r.squared)
# Save Adj-R2
G[1,5] <- "Adj-R2"
G[2,5] <- formatter(res$adj.r.squared)
print(G)
if (writecsv) write.csv(G, file=file, row.names=F)
}
 
lmOut(res)
 
# First let's generate some fake binary response data (from yesterday's post).
Nobs <- 10^4
X <- cbind(cons=1, X1=rnorm(Nobs),X2=rnorm(Nobs),X3=rnorm(Nobs),u=rnorm(Nobs))
B <- c(B0=-.2, B1=-.1,B2=0,B3=-.2,u=5)
Y <- X%*%B
SData <- as.data.frame(cbind(Y, X))
 
# Great, we have generated our data.
myres <- lm(Y ~ X1 + X2 + X3, data=SData)
 
lmOut(myres, file="my-results.csv")

Dear Francis,

Thank you very much indeed for your code : )
When I run the code, it showed an error as below:
Error in ncol + 1 : non-numeric argument to binary operator
How to fix it?

Kind regards,
Wei

Thank you for the code! There is a small typo (ncoll instead of ncol) in line 7 that causes the "Error in ncol+1: non-numeric argument to binary operator". Here is the full code:

lmOut <- function(res, file="test.csv", ndigit=3, writecsv=T) {
# If summary has not been run on the model then run summary
if (length(grep("summary", class(res)))==0) res <- summary(res)
co <- res$coefficients
nvar <- nrow(co)
ncol <- ncol(co)
f <- res$fstatistic
formatter <- function(x) format(round(x,ndigit),nsmall=ndigit)
# This sets the number of rows before we start recording the coefficients
nstats <- 4
# G matrix stores data for output
G <- matrix("", nrow=nvar+nstats, ncol=ncol+1)
G[1,1] <- toString(res$call)
# Save rownames and colnames
G[(nstats+1):(nvar+nstats),1] <- rownames(co)
G[nstats, 2:(ncoll+1)] <- colnames(co)
# Save Coefficients
G[(nstats+1):(nvar+nstats), 2:(ncol+1)] <- formatter(co)
# Save F-stat
G[1,2] <- paste0("F(",f[2],",",f[3],")")
G[2,2] <- formatter(f[1])
# Save F-p value
G[1,3] <- "Prob > P"
G[2,3] <- formatter(1-pf(f[1],f[2],f[3]))
# Save R2
G[1,4] <- "R-Squared"
G[2,4] <- formatter(res$r.squared)
# Save Adj-R2
G[1,5] <- "Adj-R2"
G[2,5] <- formatter(res$adj.r.squared)
print(G)
if (writecsv) write.csv(G, file=file, row.names=F)
}

lmOut(res)

# First let's generate some fake binary response data (from yesterday's post).
Nobs <- 10^4
X <- cbind(cons=1, X1=rnorm(Nobs),X2=rnorm(Nobs),X3=rnorm(Nobs),u=rnorm(Nobs))
B <- c(B0=-.2, B1=-.1,B2=0,B3=-.2,u=5)
Y <- X%*%B
SData <- as.data.frame(cbind(Y, X))

# Great, we have generated our data.
myres <- lm(Y ~ X1 + X2 + X3, data=SData)

lmOut(myres, file="my-results.csv")

####### Excel summary exporter #######

Modified from Smart, Francis

Jean P. Gibert, 2014

lmOut <- function(res, file="test.csv", ndigit=3, writecsv=T) {
# If summary has not been run on the model then run summary
if (length(grep("summary", class(res)))==0) res <- summary(res)
co <- res$coefficients
nvar <- nrow(co)
ncoll <- ncol(co)
f <- res$fstatistic
formatter <- function(x) format(round(x,ndigit),nsmall=ndigit)
# This sets the number of rows before we start recording the coefficients
nstats <- 4
# G matrix stores data for output
G <- matrix("", nrow=(nvar+nstats), ncol=(ncoll+1))
G[1,1] <- toString(res$call)
# Save rownames and colnames
G[(nstats+1):(nvar+nstats),1] <- rownames(co)
G[nstats, 2:(ncoll+1)] <- colnames(co)
# Save Coefficients
G[(nstats+1):(nvar+nstats), 2:(ncoll+1)] <- formatter(co)
# Save F-stat
G[1,2] <- paste("F(",f[2],",",f[3],")")
G[2,2] <- formatter(f[1])
# Save F-p value
G[1,3] <- "Prob > P"
G[2,3] <- formatter(1-pf(f[1],f[2],f[3]))
# Save R2
G[1,4] <- "R-Squared"
G[2,4] <- formatter(res$r.squared)
# Save Adj-R2
G[1,5] <- "Adj-R2"
G[2,5] <- formatter(res$adj.r.squared)
print(G)
write.csv(G, file=file, row.names=F)
}

Thanks for the corrections!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.