0

I have code as per below. This runs numerous multiple regressions (all slightly different). It also reports the R2 value of these regressions in a nice table that I can easily copy and paste. This is achieve at the end of the code. However, I cannot seem to get a p-value table in a similar manner, from what I can tell there is no p-value function equivalent to the $r.squared function. Any help on that would be greatly appreciated.

Dependent variable =Cold Independent variables = Breed (categoric, col 2), Gender (categoric col 4), Dayno. (cols 6-1055, this is the difference between each regression).

r.sqr <- matrix(NA, nrow =1, ncol = length(6:ncol(rweights)))

 for(i in 6:ncol(rweights)){
   na.n <- length(which(is.na(as.numeric(unlist(rweights[,i])))))
   if (na.n == nrow(rweights)) next
 nn <- which(!is.na(as.numeric(unlist(rweights[,i]))))
   if (is.na(unlist(rweights[nn,2])) && is.na(unlist(rweights[nn,4]))) next
   fit <- lm(Cold~ Breed + Gender + as.numeric(unlist(rweights[,i])), data=rweights, , na.action=na.omit)
   r.sqr[1,(i-5)] <- summary(fit)$r.squared
 }
  • try running `anova(fit)` and extracting from that. But no, you aren't guaranteed a `p.value` slot in a `summary.lm` – Russ Hyde Jan 10 '19 at 09:37

1 Answers1

0

Sample data: x <- lm(hp ~ wt, mtcars)

To extract the p-values you could do:

summary(x1)$coefficients[,4] 
Lennyy
  • 5,932
  • 2
  • 10
  • 23
  • `summary(fit)` gives me an output that contains a p-value, but means doing it manually every time. The current code produces a new object in the Global Environment named r.qr, which is a list/table of the R2 value for each of the 1055 different regressions. – Andrew Cooke Jan 10 '19 at 09:51
  • You could consider to embed `summary(x1)$coefficients[,4]` in your function, just like you did with `r.sqr[1,(i-5)] <- summary(fit)$r.squared`. – Lennyy Jan 10 '19 at 10:00
  • Yep. Trying various iterations of doing that with no joy. What does `x1` refer to in your code? Edit.. getting closer (I think) – Andrew Cooke Jan 10 '19 at 10:22
  • it is just the object I refer to, the result of the lm() call. So you should use fit instead. Sorry did not notice I used both x and x1, that should have been the same. – Lennyy Jan 10 '19 at 10:28
  • Thanks. I was using "fit" bit just wanted to double check that what's going wrong isn't due to me accidentally referencing the wrong thing! – Andrew Cooke Jan 10 '19 at 10:29
  • Good news! I managed to extract the p-values from the coefficient table by using your code. I originally did not specify the row which caused issues. The last thing to do now is to get the overall p-value for the model, which occurs in the summary but not in the coefficient table. – Andrew Cooke Jan 10 '19 at 11:31
  • Bad news! Still can't get the overall p-value that occur as the very last thing of the multiple regression summary `Residual standard error: 18.62 on 261 degrees of freedom (778 observations deleted due to missingness) Multiple R-squared: 0.5918, Adjusted R-squared: 0.5824 F-statistic: 63.06 on 6 and 261 DF, p-value: < 2.2e-16` – Andrew Cooke Jan 10 '19 at 11:50
  • Hm feel I might be able to live without it to be honest. Thanks for the help @Lennyy – Andrew Cooke Jan 10 '19 at 12:13