如何在 r 中的逻辑回归方法中将 p 值格式化为 4 位数字

how to format the p-value in 4 digits in logistic regression method in r

我有逻辑回归输出和优势比格式 优势比:

  variables   oddsratio    2.5%      97.5%      p-value
      height  0.9810833  0.9724681  0.9898     2.197e-05 ***
      weight  0.9889900  0.9794387  0.9986     0.025356 *  
        BMI   1.0097044  1.0029179  1.0165     0.005005 ** 

但我想要以小数格式输出 p 值,如

pvalue
0.001
0.025
0.005

对于控制台输出,请在您的问题的评论中阅读 @EliBerkow 的 link。对于 table 输出或在函数中使用,您可以使用 formatC().

例子

(output <- summary(glm(I(Sepal.Length > 5.7) ~ Sepal.Width + Species, iris, 
                       family=binomial()))$coefficients)
#                     Estimate Std. Error   z value     Pr(>|z|)
# (Intercept)       -15.846295   3.893382 -4.070059 4.700128e-05
# Sepal.Width         3.274112   0.970872  3.372342 7.453193e-04
# Speciesversicolor   7.149531   1.533740  4.661501 3.139108e-06
# Speciesvirginica    9.283911   1.630188  5.694992 1.233776e-08

全部为四位数:

as.data.frame(apply(output, 2, formatC, format="f", digits=4))
#                   Estimate Std. Error z value Pr(>|z|)
# (Intercept)       -15.8463     3.8934 -4.0701   0.0000
# Sepal.Width         3.2741     0.9709  3.3723   0.0007
# Speciesversicolor   7.1495     1.5337  4.6615   0.0000
# Speciesvirginica    9.2839     1.6302  5.6950   0.0000

位数不同:

setNames(data.frame(output[,-4], formatC(output[,4], format="f", digits=4)), colnames(output))
#                     Estimate Std. Error   z value Pr(>|z|)
# (Intercept)       -15.846295   3.893382 -4.070059   0.0000
# Sepal.Width         3.274112   0.970872  3.372342   0.0007
# Speciesversicolor   7.149531   1.533740  4.661501   0.0000
# Speciesvirginica    9.283911   1.630188  5.694992   0.0000