kosukeimai / mediation

R package mediation
60 stars 29 forks source link

error in [.data.frame`(m.data, name) : undefined columns chosen #70

Open CarinaFo opened 2 months ago

CarinaFo commented 2 months ago

Hello mediation developer team,

I am using your package (4.5.0) for a mediation analysis in R (4.4.1). My mediation model script ran without errors a few months ago. Now I encounter the following error wit the same code (I should have created an environment, but I code everything else in python so I was too lazy, my bad)

Here is the code I use


# libraries
library(lme4) # mixed models
library(mediation)

###################load csv including data from both studies#######################################
# set base directory
setwd("E:/expecon_ms")
expecon=1
behav_path = file.path("data", "behav", "brain_behav_cleaned_source_1.csv")
behav = read.csv(behav_path)

################################ prep for modelling ###########################################

# make factors for categorical variables:
behav$ID = as.factor(behav$ID) # subject ID
behav$isyes = as.factor(behav$isyes) # stimulus
behav$cue = as.factor(behav$cue) # probability for a signal
behav$prevresp = as.factor(behav$prevresp) # previous response

# dummy recode
behav$cue <- ifelse(behav$cue == 0.25, 0, 1)
####################################### volatile env.##############################################
any(is.na(behav)) ## returns FALSE

# without p-values, model for mediation function
med.model_beta_prob <- lme4::lmer(beta_source_prob ~ cue + prevresp +
                                     (cue+prevresp|ID), 
                                   data = behav,
                                   control=lmerControl(optimizer="bobyqa",
                                                       optCtrl=list(maxfun=2e5)))
summary(med.model_beta_prob)

# fit outcome model: do the mediator (beta) and the IV (stimulus probability cue) predict the
# detection response? included stimulus and previous choice at a given trial as covariates,
# but no interaction between prev. resp and cue
out.model_beta_prob <- glmer(sayyes ~ beta_source_prob + cue + prevresp + isyes +
                          (cue + prevresp + isyes|ID),
                        data = behav,
                        control=glmerControl(optimizer="bobyqa",
                                             optCtrl=list(maxfun=2e5)),
                        family=binomial(link='probit'))

summary(out.model_beta_prob)

mediation_cue_beta_prob <- mediate(med.model_beta_prob, out.model_beta_prob, treat='cue', 
                              mediator='beta_source_prob')

summary(mediation_cue_beta_prob)

I get this error msg: error in [.data.frame`(m.data, name) : undefined columns chosen

Help would be greatly appreciated, otherwise I have to abandon mediation package and find a similar package in python :)

CarinaFo commented 1 month ago

I did figure out why I got this error: none of the regressor variables in my glmer models are allowed to be factorial. If I fit the numeric regressors, mediation doesn't throw an error.