gpt4 book ai didi

r - 将回归摘要写入R中的csv文件

转载 作者:行者123 更新时间:2023-12-04 10:33:29 24 4
gpt4 key购买 nike

我有一家公司通过销售各种产品(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

现在,我要拟合一个多元回归模型,其中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)

}

现在,我知道 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")

哪个会回来
  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 - 将回归摘要写入R中的csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com