Collecting P-values using a Loop in R

r

Solution

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] })

Problem

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.

Original source