从R中的lm提取t-stat p值

xvw2m8pv  于 2023-02-10  发布在  其他
关注(0)|答案(5)|浏览(204)

我使用lm函数在R中运行了一个回归模型。生成的ANOVA表为我提供了每个系数的F值(对我来说没有意义)。我想知道的是每个系数的t统计量及其对应的p值。我如何得到它?它是由函数存储还是需要额外的计算?
下面是代码和输出:

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

如果我的统计学知识正确的话,这些f值是没有意义的,理论上,我应该得到模型的F值和每个系数的T值。

9udxz4iz

9udxz4iz1#

下面是一个示例,其中包含如何仅提取t值的注解。

# 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
xggvc2p6

xggvc2p62#

你可以试试这个:

summary(fit)
to94eoyn

to94eoyn3#

汇总(拟合)$p值的系数[,4]
汇总(拟合)$t值的系数[,3]

w3nuxt5m

w3nuxt5m4#

正如Benjamin已经回答过的,我建议使用broom::tidy()将模型对象强制为一个整洁的 Dataframe ,统计列将包含相关的测试统计量,并且很容易使用ggplot2绘图。

ndh0cuux

ndh0cuux5#

你可以用这个

summary(fit)$coefficients[,3]

仅提取t值的步骤

相关问题