Extracting t-stat p values from lm in R

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 

You could try this:

   summary(fit)

Tags:

R

Regression