Created
March 25, 2014 08:09
-
-
Save EconometricsBySimulation/9757105 to your computer and use it in GitHub Desktop.
Excel summary exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####### 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) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment