Reputation: 1355
Ran a bunch of regressions and now I am trying to collect their p values and put them into a vector.
x=summary(reg2)$coefficients[4,4] #p value from the first regression, p-val is in row 4, col 4
for (i in 3:1000){
currentreg=summary(paste("reg",i,sep=""))
assign(x,c(x,currentreg$coefficients[4,4]))
}
I also tried eval(parse(currentreg))
and eval(parse(summary(paste("reg",i,sep=""))))
with no luck. I always have this problem with telling R "Hey don't treat this as a string, treat it as a variable" and vice versa.
Upvotes: 3
Views: 1567
Reputation: 81683
You can use
sapply(mget(ls(pattern = "^reg\\d+$")), function(x) summary(x)$coefficients[4,4])
to create a vector with all p-values.
Upvotes: 5
Reputation: 42639
While it would be better to store the objects in a list and loop over that, you're asking for get
:
currentreg <- summary(get(paste("reg", i, sep="")))
If you had a list of objects, models <- list(reg2, reg3, reg4, ...)
. You can then loop over this list with sapply
to achieve the desired result (looping, collecting the results into a vector):
x <- sapply(models, function(z) { summary(z)$coeficients[4,4] })
Upvotes: 8