R:从多元回归输出到.csv或.xls的输出系数

R:从多元回归输出到.csv或.xls的输出系数,r,excel,csv,R,Excel,Csv,我想知道将多元回归系数输出到.csv的最佳方法是什么 我发现下面的函数完全符合我的要求,但一次只能进行一次回归 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(r

我想知道将多元回归系数输出到.csv的最佳方法是什么

我发现下面的函数完全符合我的要求,但一次只能进行一次回归

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:(ncol+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为什么不这样做:

library(broom)
r_1 <- r_2 <- r_20 <- lm(weight ~ group, PlantGrowth)
lst <- mget(grep("^r_\\d+$", ls(), value = TRUE))
write.csv2(do.call(rbind, lapply(lst, tidy)), tf <- tempfile(fileext = ".csv"))
file.show(tf)
#               term estimate std.error statistic      p.value
# r_1.1  (Intercept)    5.032 0.1971284 25.526514 1.936575e-20
# r_1.2    grouptrt1   -0.371 0.2787816 -1.330791 1.943879e-01
# r_1.3    grouptrt2    0.494 0.2787816  1.771996 8.768168e-02
# r_2.1  (Intercept)    5.032 0.1971284 25.526514 1.936575e-20
# r_2.2    grouptrt1   -0.371 0.2787816 -1.330791 1.943879e-01
# r_2.3    grouptrt2    0.494 0.2787816  1.771996 8.768168e-02
# r_20.1 (Intercept)    5.032 0.1971284 25.526514 1.936575e-20
# r_20.2   grouptrt1   -0.371 0.2787816 -1.330791 1.943879e-01
# r_20.3   grouptrt2    0.494 0.2787816  1.771996 8.768168e-02
库(扫帚)

这太棒了。非常感谢。
library(broom)
r_1 <- r_2 <- r_20 <- lm(weight ~ group, PlantGrowth)
lst <- mget(grep("^r_\\d+$", ls(), value = TRUE))
write.csv2(do.call(rbind, lapply(lst, tidy)), tf <- tempfile(fileext = ".csv"))
file.show(tf)
#               term estimate std.error statistic      p.value
# r_1.1  (Intercept)    5.032 0.1971284 25.526514 1.936575e-20
# r_1.2    grouptrt1   -0.371 0.2787816 -1.330791 1.943879e-01
# r_1.3    grouptrt2    0.494 0.2787816  1.771996 8.768168e-02
# r_2.1  (Intercept)    5.032 0.1971284 25.526514 1.936575e-20
# r_2.2    grouptrt1   -0.371 0.2787816 -1.330791 1.943879e-01
# r_2.3    grouptrt2    0.494 0.2787816  1.771996 8.768168e-02
# r_20.1 (Intercept)    5.032 0.1971284 25.526514 1.936575e-20
# r_20.2   grouptrt1   -0.371 0.2787816 -1.330791 1.943879e-01
# r_20.3   grouptrt2    0.494 0.2787816  1.771996 8.768168e-02