Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Regression summary to the csv file in R

Tags:

r

csv

regression

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

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)

}

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.

like image 684
souravsarkar59 Avatar asked Jul 02 '14 09:07

souravsarkar59


2 Answers

You can write to the text file very easily using

sink("summary.txt")
summary(lm)
sink()
like image 121
Koundy Avatar answered Nov 16 '22 07:11

Koundy


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
like image 41
David Arenburg Avatar answered Nov 16 '22 08:11

David Arenburg