【问题标题】:Fixing call notice printed after an lm() like function in R修复 R 中类似 lm() 的函数后打印的呼叫通知
【发布时间】:2020-03-02 05:22:55
【问题描述】:

我在我自己的函数foo 中使用库robumeta 中的lm() 类似函数robu()。

但是,我正在处理 formula 参数,这样当它丢失时,默认公式将是:formula(dint~1) 或用户定义的任何公式。

它工作正常,但是,在foo 的输出中,打印的公式调用始终是:Model: missing(f) if formula(dint ~ 1),无论在foo 中输入什么公式。

我能否更正这部分输出,使其仅显示所使用的确切公式? (见下面的例子)

dat <- data.frame(dint = 1:9, SD = 1:9*.1, 
                  time = c(1,1,2,3,4,3,2,4,1), 
                  study.name = rep(c("bob", "jim", "jon"), 3))

library(robumeta)

 # MY FUNCTION:
 foo <- function(f, data){ 

  robu(formula = if(missing(f)) formula(dint~1) else formula(f), data = data, studynum = study.name, var = SD^2)
}

# EXAMPLES OF USE:

foo(data = dat)                       ## HERE I expect: `Model: dint ~ 1` 
foo(dint~as.factor(time), data = dat) ## HERE I expect: `Model: dint ~ time` 

【问题讨论】:

    标签: r function lm


    【解决方案1】:

    一种选择是更新“ml”对象

    foo <- function(f, data){ 
        fmla <- if(missing(f)) {
                   formula(dint ~ 1)
              } else {
                 formula(f)
               }
    
    
       model <- robu(formula = fmla, data = data, studynum = study.name, var = SD^2)
       model$ml <- fmla
       model
    }
    

    -检查

    foo(data = dat)
    RVE: Correlated Effects Model with Small-Sample Corrections 
    
    Model: dint ~ 1 
    
    Number of studies = 3 
    Number of outcomes = 9 (min = 3 , mean = 3 , median = 3 , max = 3 )
    Rho = 0.8 
    I.sq = 96.83379 
    Tau.sq = 9.985899 
    
                   Estimate StdErr t-value dfs P(|t|>) 95% CI.L 95% CI.U Sig
    1 X.Intercept.     4.99  0.577    8.65   2  0.0131     2.51     7.48  **
    ---
    Signif. codes: < .01 *** < .05 ** < .10 *
    ---
    Note: If df < 4, do not trust the results
    

    foo(dint~ as.factor(time), data = dat)
    RVE: Correlated Effects Model with Small-Sample Corrections 
    
    Model: dint ~ as.factor(time) 
    
    Number of studies = 3 
    Number of outcomes = 9 (min = 3 , mean = 3 , median = 3 , max = 3 )
    Rho = 0.8 
    I.sq = 97.24601 
    Tau.sq = 11.60119 
    
                       Estimate StdErr t-value  dfs P(|t|>) 95% CI.L 95% CI.U Sig
    1     X.Intercept.     3.98   2.50   1.588 2.00   0.253    -6.80     14.8    
    2 as.factor.time.2     1.04   4.41   0.236 1.47   0.842   -26.27     28.3    
    3 as.factor.time.3     1.01   1.64   0.620 1.47   0.617    -9.10     11.1    
    4 as.factor.time.4     2.52   2.50   1.007 2.00   0.420    -8.26     13.3    
    ---
    Signif. codes: < .01 *** < .05 ** < .10 *
    

    【讨论】:

      猜你喜欢
      • 2022-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      相关资源
      最近更新 更多