从 R 中的 lm 中提取 t-stat p 值
Extracting t-stat p values from lm in R
我有 运行 R 中使用 lm 函数的回归模型。生成的方差分析 table 为我提供了每个系数的 F 值(这对我来说没有意义)。我想知道的是每个系数的 t-stat 及其相应的 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 值。
你可以试试这个:
summary(fit)
这是一个示例,其中包含有关如何仅提取 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
正如本杰明已经回答的那样,我建议使用 broom::tidy()
将模型对象强制转换为整洁的数据框。统计列将包含相关的测试统计数据,并且可以很容易地使用 ggplot2
.
进行绘图
你可以使用这个
summary(fit)$coefficients[,3]
仅提取 t 值
p 值
的摘要(拟合)$coefficients[4]
t 值
的摘要(适合)$coefficients[3]
我有 运行 R 中使用 lm 函数的回归模型。生成的方差分析 table 为我提供了每个系数的 F 值(这对我来说没有意义)。我想知道的是每个系数的 t-stat 及其相应的 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 值。
你可以试试这个:
summary(fit)
这是一个示例,其中包含有关如何仅提取 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
正如本杰明已经回答的那样,我建议使用 broom::tidy()
将模型对象强制转换为整洁的数据框。统计列将包含相关的测试统计数据,并且可以很容易地使用 ggplot2
.
你可以使用这个
summary(fit)$coefficients[,3]
仅提取 t 值
p 值
的摘要(拟合)$coefficients[4]
t 值