How to programmatically retrieve the p-value of the slope of a linear regression using either R or perl
3
0
Entering edit mode
7.8 years ago
moxu ▴ 510

In R, you can do "summary(glm(y ~ x))" and it displays the results of the glm including the intercept, slope and the p-values for the intercept and the slope. But how can you programmatically retrieve the p-value for the slope? For instance, I would like to do something like the following:

rst <- glm(y ~ x)
rst$pvalue

Or, alternatively, a perl package with the desired capability. I've checked out Statistics::LineFit, however it can only return rSquared but not p-value

R • 4.3k views
ADD COMMENT
3
Entering edit mode
7.8 years ago

Save the output of the summary() and the coefficients slot contains the p value in the third column of that matrix.

I would do p<-summary(glm(...))$coefficients[2,3]

ADD COMMENT
1
Entering edit mode

Alternatively (for readability)

coefficients(summary(glm(…)))[…]
ADD REPLY
0
Entering edit mode

p<-summary(glm(...))$coefficients[2,4]?

Didn't realize summary(..)$coefficients returns a matrix.

Thanks a lot!

ADD REPLY
2
Entering edit mode
7.8 years ago
rjgrimaila ▴ 20
rst <- glm(y ~ x)  
rst$pvalue  
library(broom)  
t.rst <- tidy(rst)  
t.rst[,"p.value"]
ADD COMMENT
1
Entering edit mode
7.8 years ago
moxu ▴ 510

Another method is to use perl Statistics::LineFit to get the tStatistics, and use Statistics::Distributions::tprob(dof, t-stat) to get the p-value.

ADD COMMENT

Login before adding your answer.

Traffic: 2544 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6