Write Regression summary to the csv file in R

爱⌒轻易说出口 提交于 2019-12-04 05:24:10

Provided your data set and your comments, I would do something like

abc <- read.table(text = "
Order.Week..BV.    Product.Number    Quantity    Net.ASP    Net.Price
1         2013-W44        ABCDEF       92  823.66       749
2         2013-W44        ABCDEF       24  898.89       749
3         2013-W44        ABCDEF      243  892.00       749
4         2013-W45        ABCDEF       88  796.84       699
5         2013-W45        ABCDEF       18  744.80       699", header = T) # Yor data

fit <- lm(Net.Price ~ Quantity + Net.ASP, data = abc)
x <- cbind(abc, t(as.numeric(coefficients(fit))), t(as.numeric(summary(fit)$coefficients[, 4])), summary(fit)$r.squared)
names(x)[(length(x) - 6):length(x)] <- c(paste("coeff", names(coefficients(fit))), paste("P-value", names(summary(fit)$coefficients[, 4])), "R-squared")

Which will return

  Order.Week..BV. Product.Number Quantity Net.ASP Net.Price coeff (Intercept) coeff Quantity coeff Net.ASP P-value (Intercept) P-value Quantity
1        2013-W44         ABCDEF       92  823.66       749          434.0829    0.001853692     0.3545852          0.09474093        0.9898202
2        2013-W44         ABCDEF       24  898.89       749          434.0829    0.001853692     0.3545852          0.09474093        0.9898202
3        2013-W44         ABCDEF      243  892.00       749          434.0829    0.001853692     0.3545852          0.09474093        0.9898202
4        2013-W45         ABCDEF       88  796.84       699          434.0829    0.001853692     0.3545852          0.09474093        0.9898202
5        2013-W45         ABCDEF       18  744.80       699          434.0829    0.001853692     0.3545852          0.09474093        0.9898202
  P-value Net.ASP R-squared
1       0.1865054 0.7165826
2       0.1865054 0.7165826
3       0.1865054 0.7165826
4       0.1865054 0.7165826
5       0.1865054 0.7165826

You can write to the text file very easily using

sink("summary.txt")
summary(lm)
sink()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!