zsad512
zsad512

Reputation: 881

Extracting t-stat p values from lm in R

I have run a regression model in R using the lm function. The resulting ANOVA table gives me the F-value for each coefficient (which doesnt really make sense to me). What I would like to know is the t-stat for each coefficient and its corresponding p-value. How do I get this? Is it stored by the function or does it require additional computation?

Here is the code and output:

library(lubridate)
library(RCurl)
library(plyr)

[in] fit <- lm(btc_close ~ vix_close + gold_close + eth_close, data = all_dat)

# Other useful functions 
coefficients(fit) # model coefficients
confint(fit, level=0.95) # CIs for model parameters 
anova(fit) # anova table 

[out]
Analysis of Variance Table

Response: btc_close
           Df   Sum Sq  Mean Sq  F value Pr(>F)    
vix_close   1 20911897 20911897 280.1788 <2e-16 ***
gold_close  1    91902    91902   1.2313 0.2698    
eth_close   1 42716393 42716393 572.3168 <2e-16 ***
Residuals  99  7389130    74638                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

If my statistics knowledge serves me correctly, these f-values are meaningless. Theoretically, I should receive an F-value for the model and a T-value for each coefficient.

Upvotes: 12

Views: 34522

Answers (5)

Seanosapien
Seanosapien

Reputation: 450

As Benjamin has already answered, I would advise using broom::tidy() to coerce the model object to a tidy dataframe. The statistic column will contain the relevant test statistic and is easily available for plotting with ggplot2.

Upvotes: 3

s_baldur
s_baldur

Reputation: 33488

Here is an example with comments of how you can extract just the t-values.

# Some dummy data
n <- 1e3L
df <- data.frame(x = rnorm(n), z = rnorm(n))
df$y <- with(df, 0.01 * x^2 + z/3)

# Run regression
lr1 <- lm(y ~ x + z, data = df)

# R has special summary method for class "lm"
summary(lr1)
# Call:
# lm(formula = y ~ x + z, data = df)

# Residuals:
#       Min        1Q    Median        3Q       Max 
# -0.010810 -0.009025 -0.005259  0.003617  0.096771 

# Coefficients:
#              Estimate Std. Error t value Pr(>|t|)    
# (Intercept) 0.0100122  0.0004313  23.216   <2e-16 ***
# x           0.0008105  0.0004305   1.883     0.06 .  
# z           0.3336034  0.0004244 786.036   <2e-16 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

# Residual standard error: 0.01363 on 997 degrees of freedom
# Multiple R-squared:  0.9984,  Adjusted R-squared:  0.9984 
# F-statistic: 3.09e+05 on 2 and 997 DF,  p-value: < 2.2e-16

# Now, if you only want the t-values
summary(lr1)[["coefficients"]][, "t value"]
# Or (better practice as explained in comments by Axeman)
coef(summary(lr1))[, "t value"]
# (Intercept)           x           z 
#   23.216317    1.882841  786.035718 

Upvotes: 16

Mayank Agrawal
Mayank Agrawal

Reputation: 87

summary(fit)$coefficients[,4] for p-values

summary(fit)$coefficients[,3] for t-values

Upvotes: 4

Mohammed
Mohammed

Reputation: 311

you can use this

summary(fit)$coefficients[,3]

To extract only t-values

Upvotes: 0

Bechir Barkallah
Bechir Barkallah

Reputation: 341

You could try this:

   summary(fit)

Upvotes: 5

Related Questions