Extract Regression P Value in R
Asked Answered
B

5

23

I am performing multiple regressions on different columns in a query file. I've been tasked with extracting certain results from the regression function lm in R.

So far I have,

> reg <- lm(query$y1 ~ query$x1 + query$x2)
> summary(reg)

Call:
lm(formula = query$y1 ~ query$x1 + query$x2)

Residuals:
    1     2     3     4 
  7.68 -4.48 -7.04  3.84 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  1287.26     685.75   1.877    0.312
query$x1      -29.30      20.92  -1.400    0.395
query$x2     -116.90      45.79  -2.553    0.238

Residual standard error: 11.97 on 1 degrees of freedom
Multiple R-squared:  0.9233,    Adjusted R-squared:  0.7699 
F-statistic: 6.019 on 2 and 1 DF,  p-value: 0.277

To extract the coefficients, r-squared and F statistics I use the following:

reg$coefficients
summary(reg)$r.squared
summary(reg)$fstatistic

I would like to also extract the p-value of 0.277.

Is there a piece of code that could do this?

Thanks

Below answered 22/7, 2015 at 17:50 Comment(1)
possible duplicate of pull out p-values and r-squared from a linear regressionFrankly
C
17

I would recommend using the "broom" package as a good practice to go forward with those cases (where you might need to create a data frame from a model fit output).

Check this as a simple example:

library(broom)

dt = data.frame(mtcars) # example dataset

model = lm(mpg ~ disp + wt, data = dt) # fit a model

summary(model) # usual summary of a model fit

tidy(model) # get coefficient table as a data frame

glance(model) # get rest of stats as a data frame

glance(model)$p.value # get p value
Corregidor answered 6/8, 2015 at 16:18 Comment(0)
H
12

The two easiest ways I've found for extracting the p-value are:

summary(Model)$coefficients[,"Pr(>|t|)"]

summary(Model)$coefficients[,4]

Just replace Model with the name of your model

Honeybunch answered 28/1, 2018 at 23:22 Comment(2)
Good solution. In addition, adding an extra set of brackets at the end returns the value only (without the name): summary(Model)$coefficients[,"Pr(>|t|)"][[2]]Rozanna
This should be the go-to solution.Andrien
E
7

You could by using anova(reg)$'Pr(>F)'

Elsewhere answered 22/7, 2015 at 17:57 Comment(1)
This is the best.Planking
P
3

You can also use :

pf(summary(reg)$fstatistic[1],
summary(reg)$fstatistic[2],
summary(reg)$fstatistic[3],
lower.tail=FALSE)
Pavlodar answered 22/12, 2020 at 12:35 Comment(0)
C
0

This was very helpful! The only tidbit that I can add is that one can also access the coefficient values like this, e.g., if one wants to add a column to an existing dataset with the fitted values:

allvars$predicted = allvars$x*summary(fit)$coefficients[2,1] + summary(fit)$coefficients[1,1]

And the residuals by difference, obviously:

allvars$residuals = allvars$y - (allvars$x*summary(fit)$coefficients[2,1]

thanks again ADR

Cemetery answered 15/1, 2023 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.