将回归摘要写入R中的csv文件 [英] Write Regression summary to the csv file in R
问题描述
我有一家公司通过销售各种产品(csv文件)获得的收入数据,其中之一如下:
I have data on revenue of a company from sales of various products (csv files), one of which looks like the following:
> abc
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
现在,我正在拟合一个多元回归模型,其中Net.Price为Y和Quantity,Net.ASP为x1和x2.此类文件有100多个,我正在尝试使用以下代码进行操作:
Now, I'm fitting a multiple regression model with Net.Price as Y and Quantity, Net.ASP as x1 and x2. There are more than 100 such files and I'm trying to do it using the following code:
fileNames <- Sys.glob("*.csv")
for (fileName in fileNames) {
abc <- read.csv(fileName, header = TRUE, sep = ",")
fit <- lm(Net.Price ~ Quantity + Net.ASP, data = abc)
x <- data.frame (abc, summary(fit))
write.csv (x, file = fileName)
}
现在,我知道x <- data.frame (abc, summary(fit))
行是错误的,因为它说的是Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""summary.lm"" to a data.frame
,但是我想将每个csv文件的回归模型摘要写入文件本身.请帮忙.
Now, I understand the line x <- data.frame (abc, summary(fit))
is wrong, as it's saying Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""summary.lm"" to a data.frame
,but I want to write the summary of regression model for each csv file to the file itself. Please help.
推荐答案
提供了数据集和注释,我会做类似的事情
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")
哪个会回来
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
这篇关于将回归摘要写入R中的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!