Reputation: 357
I am running a large number of linear regressions, and for each regression I would like to save the adjusted R squared and the degrees of freedom each in a seperate file.
The code below does this perfectly for the adjusted R squared, and I can add the value name of the list to the file (so I can identify to which subset of mydata the R squared belongs). In sum, it gives me 1 column with the list value, and 1 column with the R².
For the degrees of freedom, R gives a 3-vector (p, n-p, p*) by default. Adding the row.name=i statement does not work here. I would only need n-p. Is there a way to save the middle figure from the vector only and add the row name to the saved file?
I created a reproducible example below:
Fictive dataset (mydata)
v1 v2 v3
4 9 1
5 78 1
6 11 1
8 12 2
10 13 2
11 65 2
15 34 2
Code:
list<-unique(mydata$v3)
for (i in 1:length(list)){
newdata <- mydata[ which(mydata$v3==i),]
reg <- lm((v1~v2), data=newdata)
write.table(summary(reg)$adj.r.square, file="rsquared.txt", append=TRUE, col.names = F, row.names=i)
write.table(summary(reg)$df, file="degreesoffreedom.txt", append=TRUE, col.names = F)
rm(reg)
rm(newdata)
}
What I currently get for the file "degreesoffreedom.tx"
"1" 2
"2" 1
"3" 2
"1" 2
"2" 2
"3" 2
What I need is:
"1" 1
"2" 2
Upvotes: 1
Views: 5251
Reputation: 4513
Try summary(reg)$df[2]
instead of summary(reg)$df
. Remember everything in R is an object and you can take a subset of any object.
Second solution reg$df
. You'll only get three different DF when you wrap summary
around your regression.
The call to which
is unnecessary, i.e. newdata <- mydata[mydata$v3==i,]
. In my experience, 99% of the time you think you need which
you don't.
Upvotes: 3