【问题标题】:Mediation analysis with as.factor() independent variable使用 as.factor() 自变量的中介分析
【发布时间】:2021-05-06 20:55:03
【问题描述】:

我正在尝试进行中介分析,但我的 IV 中有一个因素。这似乎导致了一些问题。这是一个例子:

data(iris)
library(mediation)
ols.0 <- lm(Sepal.Length ~ Petal.Length + as.factor(Species), data = iris)

ols.med <- lm(Sepal.Width ~ Petal.Length + as.factor(Species), data = iris)

ols.y <- lm(Sepal.Length ~ Petal.Length + Sepal.Width + as.factor(Species), data = iris)

results1 <- mediate(ols.med, ols.y, treat="Petal.Length", mediator="Sepal.Width", boot=TRUE, sims=500)

所以我可以很好地运行 lm() 命令,但是当我运行 mediate() 命令时,我收到以下错误消息:

Running nonparametric bootstrap

Error in is.factor(x) : object 'Species' not found

有没有办法在运行 mediate() 命令时处理因子变量,或者是否有我应该考虑的替代包/方法?

【问题讨论】:

    标签: r mediator


    【解决方案1】:

    最好在外部创建as.factor,因为这可能会更改call 属性名称。如果我们想在formula 中使用as.factor,请将其包装在I 中或在属性中进行更改

    iris$Species <- as.factor(iris$Species)
    ols.0 <- lm(Sepal.Length ~ Petal.Length + Species, data = iris)
    
    ols.med <- lm(Sepal.Width ~ Petal.Length + Species, data = iris)
    
     ols.y <- lm(Sepal.Length ~ Petal.Length + Sepal.Width + Species, data = iris)
    
    results1 <- mediate(ols.med, ols.y, treat="Petal.Length", 
           mediator="Sepal.Width", boot=TRUE, sims=500)
    
    
    
    str(results1)
    List of 56
     $ d0           : num 0.129
     $ d1           : num 0.129
     $ d0.ci        : Named num [1:2] 0.0642 0.21
      ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%"
     $ d1.ci        : Named num [1:2] 0.0642 0.21
      ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%"
     $ d0.p         : num 0
     $ d1.p         : num 0
     $ d0.sims      : num [1:500, 1] 0.111 0.137 0.197 0.116 0.215 ...
     $ d1.sims      : num [1:500, 1] 0.111 0.137 0.197 0.116 0.215 ...
     $ z0           : num 0.776
     $ z1           : num 0.776
     $ z0.ci        : Named num [1:2] 0.636 0.907
    # ..
    

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 1970-01-01
      • 2022-07-01
      • 2021-04-04
      • 1970-01-01
      • 2020-07-27
      • 2018-05-01
      • 1970-01-01
      • 2017-07-12
      相关资源
      最近更新 更多