我从各种产品(csv文件)的销售中获得了公司收入的数据,其中一个如下所示:
> 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
Run Code Online (Sandbox Code Playgroud)
现在,我拟合了一个多元回归模型,Net.Price为Y,Quantity,Net.ASP为x1和x2.有超过100个这样的文件,我正在尝试使用以下代码:
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)
}
Run Code Online (Sandbox Code Playgroud)
现在,我理解这条线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文件的回归模型的摘要写入文件本身.请帮忙.
提供您的数据集和您的评论,我会做类似的事情
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")
Run Code Online (Sandbox Code Playgroud)
哪个会返回
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12622 次 |
| 最近记录: |