R调解包:逗号后面的数字

时间:2018-12-19 12:09:04

标签: r

我正在尝试运行此代码进行中介分析 我使用R中介包运行了中介分析。

我的代码如下:

> mediate <- lm(bmi ~ sugar, data=data)
> out <- glm(diabetes ~ sugar + bmi, data=data)

> med.out <- mediate (mediate, out, treat="sugar", mediator="bmi", robustSE = TRUE, sims=1000, data=data)

我运行了两次以上代码(med.out),每次运行产生的结果略有不同。

> summary (med.out)
Causal Mediation Analysis 

Nonparametric Bootstrap Confidence Intervals with the Percentile Method

                        Estimate 95% CI Lower 95% CI Upper p-value   
ACME (control)           0.004991     0.000573         0.01   0.018 * 
ACME (treated)           0.008923     0.001087         0.02   0.018 * 
ADE (control)            0.059450     0.010800         0.11   0.014 *
ADE (treated)            0.063382     0.011183         0.12   0.014 *
Total Effect             0.068373     0.016377         0.12   0.006 **
Prop. Mediated (control) 0.073003     0.010099         0.32   0.024 * 
Prop. Mediated (treated) 0.130508     0.018659         0.37   0.024 * 
ACME (average)           0.006957     0.000807         0.02   0.018 * 
ADE (average)            0.061416     0.010991         0.11   0.014 * 
Prop. Mediated (average) 0.101755     0.013745         0.35   0.024 * 

如果您看到CI下部的逗号后面有6位数字,但是CI上部只有两位数字。逗号后的95%CI上位怎么有和95%低位(6位)一样的数字?

1 个答案:

答案 0 :(得分:0)

print方法中,有效数字的最小数目被硬编码为3。您可以使用trace临时更改它:

library(mediation)
data(jobs)
b <- lm(job_seek ~ treat + econ_hard + sex + age, data=jobs)
c <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age, data=jobs)

contcont <- mediate(b, c, sims=50, treat="treat", mediator="job_seek")
print(res <- summary(contcont))
#Causal Mediation Analysis 
#
#Quasi-Bayesian Confidence Intervals
#
#               Estimate 95% CI Lower 95% CI Upper p-value
#ACME            -0.0158      -0.0337         0.01    0.16
#ADE             -0.0469      -0.1365         0.02    0.32
#Total Effect    -0.0626      -0.1600         0.02    0.16
#Prop. Mediated   0.2509      -2.2429         2.01    0.24
#
#Sample Size Used: 899 
#
#
#Simulations: 50 

as.list(body(mediation:::print.summary.mediate)) #find position for tracing
#this is the relevant code:
as.list(body(mediation:::print.summary.mediate))[[11]]
#printCoefmat(smat, digits = 3)

#change it:
trace(mediation:::print.summary.mediate, 
      at = 11,
      tracer = quote({
        printCoefmat <- function(x, digits) {
          p <- x[, 4] #p-values seem to be stored rounded
          x[, 1:3] <- sprintf("%.6f", x[, 1:3])
          x[, 4] <- sprintf("%.2f", p)
          print(x, quote = FALSE, right = TRUE)
        } 
      }),
      print = FALSE)
mediation:::print.summary.mediate(summary(contcont))
#Causal Mediation Analysis 
#
#Quasi-Bayesian Confidence Intervals
#
#                Estimate 95% CI Lower 95% CI Upper p-value
#ACME           -0.015754    -0.033658     0.010781    0.16
#ADE            -0.046862    -0.136506     0.023093    0.32
#Total Effect   -0.062616    -0.160046     0.015302    0.16
#Prop. Mediated  0.250921    -2.242936     2.010627    0.24
#
#Sample Size Used: 899 
#
#
#Simulations: 50 

untrace(mediation:::print.summary.mediate)