改变 R 中的重要性符号

Changing significance notation in R

R 具有确定统计显着性的特定显着性代码。例如,在下面的示例中,点 . 表示 10% 水平的显着性(请参见下面的示例输出)。

很难看到圆点,尤其是当我复制粘贴到 Excel 并在 Times New Roman 中显示时。

我想将其更改为:

我有办法做到这一点吗?

> y = c(1,2,3,4,5,6,7,8)
> x = c(1,3,2,4,5,6,8,7)
> summary(lm(y~x))

Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.0714 -0.3333  0.0000  0.2738  1.1191 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.2143     0.6286   0.341  0.74480    
x             0.9524     0.1245   7.651  0.00026 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.8067 on 6 degrees of freedom
Multiple R-squared:  0.907, Adjusted R-squared:  0.8915 
F-statistic: 58.54 on 1 and 6 DF,  p-value: 0.0002604

您可以使用

创建自己的格式化函数
mystarformat <- function(x) symnum(x, corr = FALSE, na = FALSE, 
    cutpoints = c(0, 0.01, 0.05, 0.1, 1), 
    symbols = c("***", "**", "*", " "))

并且您可以编写自己的系数格式化程序

show_coef <- function(mm) {
     mycoef<-data.frame(coef(summary(mm)), check.names=F)
     mycoef$signif = mystarformat(mycoef$`Pr(>|t|)`)
     mycoef$`Pr(>|t|)` = format.pval(mycoef$`Pr(>|t|)`)
     mycoef
}

然后使用您的模型,您可以 运行 它与

mm <- lm(y~x)
show_coef(mm)
#              Estimate Std. Error   t value  Pr(>|t|) signif
# (Intercept) 0.2142857  0.6285895 0.3408993 0.7447995       
# x           0.9523810  0.1244793 7.6509206 0.0002604    ***

抱歉回复晚了,但我找到了一个很好的解决方案。 只需执行以下操作:

install.packages("stargazer")
library(stargazer)
stargazer(your_regression, type = "text")

这会以您想要的格式以漂亮的方式显示所有内容。 注意:如果您将 type = "text" 留在外面,那么您将获得 LaTeX 代码。

人们应该知道,stargazer 软件包报告的显着性水平与其他统计软件(如 STATA)不同。

在 R(观星者)中你得到 # (* p<0.1; ** p<0.05; *** p<0.01)。而在 STATA 中你会得到 # (* p<0.05, ** p<0.01, *** p< 0.001).

这意味着对于 STATA 用户来说,R 结果中一个 * 的重要意义可能看起来并不重要。