r - 使用 as.factor() 自变量的中介分析
问题描述
我正在尝试进行调解分析,但我的 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() 命令时有没有办法处理因子变量,或者是否有我应该考虑的替代包/方法?
解决方案
最好创建as.factor
外部,因为这可能会更改call
属性名称。如果我们想使用as.factor
里面的formula
,将它包裹起来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
# ..
推荐阅读
- php - 无法获取通过提交处理程序中的 ajax 提交的表单文本字段的值
- html - 如何在我的内容不消失的情况下禁用水平滚动
- php - 涂鸦对 API 调用的时间头有什么期待?
- linux - 在linux中的单引号内扩展日期命令
- ios - 使用矢量资源和普通图像之间的主要区别是什么?
- java - Filtering and loading data, compared value in firebase android
- vb.net - Get Exact Age based on Date Today VB.Net
- javascript - 回调中的异步函数调用未完成
- kubernetes - how to uninstall component using helm in kuberetes
- scala - spark join two dataframes and convert result dataframe to desired json format