'Error with mediation package in R when using a categorical mediator
I am trying to run a mediation model using the mediation pacakge. However, when I run the below code I get an error (see below). I think this may have something to do with the mediator being an ordered categorical variable. Has anyone found a solution to this issue?
Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, :
NA/NaN/Inf in 'y'
In addition: Warning messages:
1: In Ops.factor(y, mu) : ‘-’ not meaningful for factors
2: In Ops.factor(eta, offset) : ‘-’ not meaningful for factors
3: In Ops.factor(y, mu) : ‘-’ not meaningful for factors
The code to reproduce this error is as follows
# get packages
library(mediation)
library(mice)
library(tidyverse)
# create multipkle imputed dataset
boys2 <- mice(boys, 4) %>%
complete("all")
## To use mediations, must make list of multiple datasets. Then,
## must also repeat the treatment assignment list as many times
## as you have data sets.
# set up dataset for mediation analyses
datasets <- list(D1 = boys2$`1`,
D2 = boys2$`2`,
D3 = boys2$`3`,
D4 = boys2$`4`,
D5 = boys2$`5`) # list of multiply imputed data sets
# run the mediation model
olsols <- mediations(datasets,
treatment = c("phb", "phb", "phb", "phb", "phb") ,
mediators = c("reg"),
outcome = c("bmi"),
covariates = c("wgt + age"),
families=c("gaussian","gaussian"), interaction=FALSE,
conf.level=.90, sims=1000)
output <- amelidiate(olsols)
# output
summary(output)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
