将一个函数的输出插入另一个函数

Posted

技术标签:

【中文标题】将一个函数的输出插入另一个函数【英文标题】:Inserting the output of one function in another function 【发布时间】:2022-01-04 21:28:48 【问题描述】:

对象res1 是来自metafor 包的回归函数。对象res2 是来自stats R 基的回归函数。

我想知道是否可以将res1 的结果放入res2 中?

我需要这样做,因为 res1 与呼叫 (effects::allEffects(res1)) 不兼容,但 res2 是。所以,如果我们可以将res1 的结果放在res2 中,那么effects::allEffects(res2) 将反映res1

library(metafor)

dat2 <- escalc(measure="OR", ai=waward, n1i=wtotal, ci=maward, n2i=mtotal, data=dat.bornmann2007)

res1 <- rma.mv(yi ~ 0+type, vi, random = ~ 1 | study/obs, data=dat2)

res2 <- lm(yi ~ 0+type, data = dat2)

library(effects)

allEffects(res1) # NOT compatible with package effects

allEffects(res2) # Compatible with package effects

# Can we put the results of `res1` in `res2` and then do: `allEffects(res2)` to reflect res1?

# --- Relevant objects attainable from `res1`:

res1$b
res1$se
res1$zval
res1$pval
res1$ci.lb
res1$ci.ub

【问题讨论】:

仅仅改变课程是不够的。问问自己allEffects 的功能是做什么的,它需要res2 提供什么?确保这些是可见的。 @Onyambu,没错。是的,我正在努力。但这需要一个有更多r和回归背景的人。 effects::allEffects.default 似乎没有显示太多,你有没有看到其中可能掌握关键的东西? @Onyambu,我的一个问题是如何将来自res1 的SE 放入res2 【参考方案1】:

您需要学习如何查看回归调用的结果。通常,当您在控制台上使用print 时,只有一小部分结果会提供给您,然后 whihc 是解释器的默认行为。您应该查看str(即结果的结构)

> str(res2)
List of 13
 $ coefficients : Named num [1:2] -0.177 -0.136
  ..- attr(*, "names")= chr [1:2] "typeFellowship" "typeGrant"
 $ residuals    : Named num [1:66] -0.224 0.12 -0.121 0.538 -0.156 ...
  ..- attr(*, "ni")= int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
  ..- attr(*, "measure")= chr "OR"
  ..- attr(*, "names")= chr [1:66] "1" "2" "3" "4" ...
 $ effects      : Named num [1:66] 0.9042 0.8577 -0.0845 0.575 -0.1194 ...
  ..- attr(*, "ni")= int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
  ..- attr(*, "measure")= chr "OR"
  ..- attr(*, "names")= chr [1:66] "typeFellowship" "typeGrant" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:66] -0.177 -0.177 -0.177 -0.177 -0.177 ...
  ..- attr(*, "ni")= int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
  ..- attr(*, "measure")= chr "OR"
  ..- attr(*, "names")= chr [1:66] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 1 1
 $ qr           :List of 5
  ..$ qr   : num [1:66, 1:2] -5.099 0.196 0.196 0.196 0.196 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:66] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
  .. ..- attr(*, "assign")= int [1:2] 1 1
  .. ..- attr(*, "contrasts")=List of 1
  .. .. ..$ type: chr "contr.treatment"
  ..$ qraux: num [1:2] 1.2 1
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 64
 $ contrasts    :List of 1
  ..$ type: chr "contr.treatment"
 $ xlevels      :List of 1
  ..$ type: chr [1:2] "Fellowship" "Grant"
 $ call         : language lm(formula = yi ~ 0 + type, data = dat2)
 $ terms        :Classes 'terms', 'formula'  language yi ~ 0 + type
  .. ..- attr(*, "variables")= language list(yi, type)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "yi" "type"
  .. .. .. ..$ : chr "type"
  .. ..- attr(*, "term.labels")= chr "type"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 0
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(yi, type)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "character"
  .. .. ..- attr(*, "names")= chr [1:2] "yi" "type"
 $ model        :'data.frame':  66 obs. of  2 variables:
  ..$ yi  : num [1:66] -0.4011 -0.0573 -0.2985 0.3609 -0.3334 ...
  .. ..- attr(*, "ni")= int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
  .. ..- attr(*, "measure")= chr "OR"
  ..$ type: chr [1:66] "Fellowship" "Fellowship" "Fellowship" "Fellowship" ...
  ..- attr(*, "terms")=Classes 'terms', 'formula'  language yi ~ 0 + type
  .. .. ..- attr(*, "variables")= language list(yi, type)
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "yi" "type"
  .. .. .. .. ..$ : chr "type"
  .. .. ..- attr(*, "term.labels")= chr "type"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 0
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(yi, type)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "character"
  .. .. .. ..- attr(*, "names")= chr [1:2] "yi" "type"
 - attr(*, "class")= chr "lm"

> str(res1)
List of 115
 $ b              : num [1:2, 1] -0.201 -0.012
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
  .. ..$ : NULL
 $ beta           : num [1:2, 1] -0.201 -0.012
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
  .. ..$ : NULL
 $ se             : num [1:2] 0.0429 0.0382
 $ zval           : num [1:2] -4.682 -0.315
 $ pval           : num [1:2] 2.85e-06 7.53e-01
 $ ci.lb          : num [1:2] -0.2852 -0.0869
 $ ci.ub          : num [1:2] -0.1169 0.0628
 $ vb             : num [1:2, 1:2] 1.84e-03 6.18e-05 6.18e-05 1.46e-03
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
  .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
 $ sigma2         : num [1:2] 0.00448 0.00355
 $ tau2           : num 0
 $ rho            : num 0
 $ gamma2         : num 0
 $ phi            : num 0
 $ QE             : num 133
 $ QEp            : num 8.27e-07
 $ QM             : num 21.9
 $ QMdf           : int [1:2] 2 NA
 $ QMp            : num 1.72e-05
 $ k              : int 66
 $ k.f            : int 66
 $ k.eff          : int 66
 $ k.all          : int 66
 $ p              : int 2
 $ p.eff          : int 2
 $ parms          : num 4
 $ int.only       : logi FALSE
 $ int.incl       : logi FALSE
 $ intercept      : logi FALSE
 $ allvipos       : logi TRUE
 $ coef.na        : Named logi [1:2] FALSE FALSE
  ..- attr(*, "names")= chr [1:2] "XtypeFellowship" "XtypeGrant"
 $ yi             : num [1:66] -0.4011 -0.0573 -0.2985 0.3609 -0.3334 ...
  ..- attr(*, "ni")= int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
  ..- attr(*, "measure")= chr "OR"
  ..- attr(*, "slab")= int [1:66] 1 2 3 4 5 6 7 8 9 10 ...
 $ vi             : num [1:66] 0.0139 0.0343 0.0339 0.034 0.0128 ...
 $ V              : num [1:66, 1:66] 0.0139 0 0 0 0 ...
 $ W              : NULL
 $ X              : num [1:66, 1:2] 1 1 1 1 1 1 1 0 0 0 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:66] "1" "2" "3" "4" ...
  .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
 $ yi.f           : num [1:66] -0.4011 -0.0573 -0.2985 0.3609 -0.3334 ...
  ..- attr(*, "ni")= int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
  ..- attr(*, "measure")= chr "OR"
  ..- attr(*, "slab")= int [1:66] 1 2 3 4 5 6 7 8 9 10 ...
 $ vi.f           : num [1:66] 0.0139 0.0343 0.0339 0.034 0.0128 ...
 $ V.f            : num [1:66, 1:66] 0.0139 0 0 0 0 ...
 $ X.f            : num [1:66, 1:2] 1 1 1 1 1 1 1 0 0 0 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:66] "1" "2" "3" "4" ...
  .. ..$ : chr [1:2] "typeFellowship" "typeGrant"
 $ W.f            : NULL
 $ ni             : int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
 $ ni.f           : int [1:66] 1740 1166 1164 758 2028 2833 4302 85 90 105 ...
 $ M              : num [1:66, 1:66] 0.02195 0.00448 0.00448 0.00448 0.00448 ...
 $ G              : NULL
 $ H              : NULL
 $ hessian        : logi NA
 $ ids            : int [1:66] 1 2 3 4 5 6 7 8 9 10 ...
 $ not.na         : Named logi [1:66] TRUE TRUE TRUE TRUE TRUE TRUE ...
  ..- attr(*, "names")= chr [1:66] "1" "2" "3" "4" ...
 $ subset         : NULL
 $ slab           : int [1:66] 1 2 3 4 5 6 7 8 9 10 ...
 $ slab.null      : logi TRUE
 $ measure        : chr "OR"
 $ method         : chr "REML"
 $ weighted       : logi TRUE
 $ test           : chr "z"
 $ dfs            : chr "residual"
 $ ddf            : logi [1:2] NA NA
 $ btt            : int [1:2] 1 2
 $ m              : int 2
 $ digits         : Named num [1:9] 4 4 4 4 4 4 4 4 4
  ..- attr(*, "names")= chr [1:9] "est" "se" "test" "pval" ...
 $ level          : num 0.05
 $ sparse         : logi FALSE
 $ dist           :List of 2
  ..$ : chr "euclidean"
  ..$ : chr "euclidean"
 $ control        : list()
 $ verbose        : logi FALSE
 $ fit.stats      :'data.frame':    5 obs. of  2 variables:
  ..$ ML  : num [1:5] -8.91 109.65 25.81 34.57 26.47
  ..$ REML: num [1:5] -10 20 28 36.6 28.7
 $ vc.fix         :List of 5
  ..$ sigma2: logi [1:2] FALSE FALSE
  ..$ tau2  : logi NA
  ..$ rho   : logi NA
  ..$ gamma2: logi NA
  ..$ phi   : logi NA
 $ withS          : logi TRUE
 $ withG          : logi FALSE
 $ withH          : logi FALSE
 $ withR          : logi FALSE
 $ formulas       :List of 2
  ..$ : NULL
  ..$ : NULL
 $ sigma2s        : int 2
 $ tau2s          : num 1
 $ rhos           : num 1
 $ gamma2s        : num 1
 $ phis           : num 1
 $ s.names        : chr [1:2] "study" "study/obs"
 $ g.names        : NULL
 $ h.names        : NULL
 $ s.levels       :List of 2
  ..$ : chr [1:21] "Ackers (2000)" "Allmendinger (2002)" "Bazeley (1998)" "Bornmann (2005)" ...
  ..$ : chr [1:66] "Ackers (2000)/1" "Ackers (2000)/2" "Ackers (2000)/3" "Ackers (2000)/4" ...
 $ s.levels.f     :List of 2
  ..$ : chr [1:21] "Ackers (2000)" "Allmendinger (2002)" "Bazeley (1998)" "Bornmann (2005)" ...
  ..$ : chr [1:66] "Ackers (2000)/1" "Ackers (2000)/2" "Ackers (2000)/3" "Ackers (2000)/4" ...
 $ s.nlevels      : int [1:2] 21 66
 $ s.nlevels.f    : int [1:2] 21 66
 $ g.nlevels.f    : NULL
 $ g.nlevels      : NULL
 $ h.nlevels.f    : NULL
 $ h.nlevels      : NULL
 $ g.levels.f     : NULL
 $ g.levels.k     : NULL
 $ g.levels.comb.k: NULL
 $ h.levels.f     : NULL
 $ h.levels.k     : NULL
 $ h.levels.comb.k: NULL
 $ struct         : chr [1:2] "CS" "CS"
 $ Rfix           : logi [1:2] FALSE FALSE
 $ R              : NULL
 $ Rscale         : chr "cor"
 $ mf.r           :List of 1
  ..$ :'data.frame':    66 obs. of  2 variables:
  .. ..$ study: chr [1:66] "Ackers (2000)" "Ackers (2000)" "Ackers (2000)" "Ackers (2000)" ...
  .. ..$ obs  : int [1:66] 1 2 3 4 5 6 7 1 2 3 ...
  [list output truncated]
 - attr(*, "class")= chr [1:2] "rma.mv" "rma"

那是res1res2

您需要告诉我们res1 的哪些部分应该是您认为应该传递给effects::allEffects 的结果

【讨论】:

以上是关于将一个函数的输出插入另一个函数的主要内容,如果未能解决你的问题,请参考以下文章

将字符串插入到另一个字符串的指定位置(串)

Visual Basic:如何将一个函数的输出包含在另一个函数中

将 hashbytes() 函数的输出插入表中

在 django 中将一个视图函数输出到另一个视图函数

9流类库与输入/输出2.3输出文件流成员函数

Python - 函数的输出? [复制]