0

I've been trying for a while to regress between the first observation and the rest of the 199 observations of my data set. I used the lapply function and the regression result is stored as a list in the environment. My aim is to get only the list of p_values as a data frame and determine how many observations are less than 0.05. Any help would be appreciated!

## Here are the code I am using right now.
myre1 <- apply(2:ncol(muscle), function(x) lm(muscle[,1] ~ muscle[,x], data = muscle))
myre2 <- lapply(muscle[,-1], function(x) lm(muscle$GIR ~ x))

## To extract the coefficient
myre3 <- lapply(2:ncol(muscle), function(x) coefficients(lm(muscle[,1] ~ muscle[,x], data = muscle)))
myre4 <- lapply(muscle[,-1], function(x) coefficients(lm(muscle$GIR ~ x)))
Edward
  • 10,360
  • 2
  • 11
  • 26
adR
  • 305
  • 4
  • 14
  • Cross-posted here: https://community.rstudio.com/t/how-to-extract-p-values-of-regression-stored-as-list/62600 – yarnabrina Apr 22 '20 at 09:12
  • Please provide a reproducible example: https://stackoverflow.com/q/5963269/11117265 – yarnabrina Apr 22 '20 at 09:13
  • `dplyr::unnest()`? `as.dataframe()`? `unlist()`? – Nuclear03020704 Apr 22 '20 at 10:03
  • Hi Amare, looking at your code i think you mean 'variable' instead of 'observation'. As i understand this, you're trying to fit a `lm` with the first column as response and everything else as predictor, and then sort out which of those predictors actually *explain* the response. If i got this right, i suggest you try `lm1 = lm(resp ~ ., data = data); summary(lm1)`. – Coy Apr 22 '20 at 14:01

1 Answers1

0

Try this. You'll get the coefficients along with the p.values

sum1 <- lapply(mre1, function(x) summary(x)$coefficients)
sum2 <- lapply(mre2, function(x) summary(x)$coefficients)

You may have to convert the sum1 and sum2 to data frame.

Edit:

To extract the p values alone

sum1 <- lapply(mre1, function(x) summary(x)$coefficients[10:12])
sum2 <- lapply(mre2, function(x) summary(x)$coefficients[7:8])
Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18
  • Thanks for your replay. I have posted a kind of example on the following page. if you have time please have a look at it. https://community.rstudio.com/t/how-to-extract-p-values-of-regression-stored-as-list/62600 – adR Apr 22 '20 at 11:10
  • @AmareWolide do you want to extract the p values of the coefficients or the f-statistics? – Mohanasundaram Apr 22 '20 at 11:34
  • @AmareWolide Check my updated answer and see if it works. – Mohanasundaram Apr 22 '20 at 12:15
  • Thanks so much. I have checked and it looks good but can we extract the p-value of the coefficient only? – adR Apr 22 '20 at 12:56
  • @AmareWolide I tried referencing the p.values directly from the list, but I couldn't get it. Anyway, I have updated the code once again for extracting the p.values from the coefficients frame. – Mohanasundaram Apr 22 '20 at 13:35
  • Thank you so much for your effort to help me! – adR Apr 22 '20 at 16:08