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

我有以下格式的逻辑回归输出和优势比 比值比:

  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
q791656449 回答:如何在R中的逻辑回归方法中将p值格式化为4位数字

对于仅控制台输出,请阅读问题注释中的 @EliBerkow 链接。对于表输出或在函数中使用,可以使用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],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
本文链接:https://www.f2er.com/3159811.html

大家都在问