Running a mediation analysis with a binary mediator variable

Hey all,

I am trying to run a mediation model with a continuous outcome variable (wayfinding distance), a continuous predictor variable (the age one started driving alone) and a binary mediator variable (growing up in a city or outside a city).

sleepoutputall <- lm(zscore ~ agelearntodrive + agestartdrivealone + highest_education_level_acheived + weeklybikingyesno + age + gender + video_game_all_devices_hours_per_week + video_game_phone_tablet_hours_per_week + hours_of_phone_use_per_week,data=correcteddf)
sleepoutputallmed <- glm(cityornot ~  agelearntodrive + agestartdrivealone + highest_education_level_acheived + weeklybikingyesno + age + gender + video_game_all_devices_hours_per_week + video_game_phone_tablet_hours_per_week + hours_of_phone_use_per_week, family=binomial(link='logit'),data=correcteddf)
sleepoutputallpredict <- lm(zscore ~ agelearntodrive + agestartdrivealone + cityornot + highest_education_level_acheived + weeklybikingyesno + age + gender + video_game_all_devices_hours_per_week + video_game_phone_tablet_hours_per_week + hours_of_phone_use_per_week,data=correcteddf)
results = mediate(sleepoutputallmed, sleepoutputallpredict, treat='agestartdrivealone',mediator='cityornot',boot=T,sims=5000)

However, although each of the first 3 lines in the above code run successfully, when I run the final line (results = mediate()), I get the following error:

Error: variable 'agestartdrivealone' was fitted with type "nmatrix.1" but type "numeric" was supplied

After doing much research online, I still don't understand why I am getting this error.

I would be so grateful for a helping hand! :slight_smile:

I have created a subset of the dataframe 'correcteddf' (all variables are z-scored except categorical variables):

subsetframe <- data.frame(correcteddf$zscore,correcteddf$agelearntodrive,correcteddf$agestartdrivealone,correcteddf$cityornot,correcteddf$highest_education_level_acheived,correcteddf$weeklybikingyesno,correcteddf$age,correcteddf$gender,correcteddf$video_game_all_devices_hours_per_week,correcteddf$video_game_phone_tablet_hours_per_week,correcteddf$hours_of_phone_use_per_week)
head(subsetframe,3)

  correcteddf.zscore correcteddf.agelearntodrive correcteddf.agestartdrivealone
1         -0.4234478                   0.9610596                      0.9071486
2         -0.3757607                  -3.1146112                     -2.7301496
3         -0.1977378                   0.3497089                      0.3875346
  correcteddf.cityornot correcteddf.highest_education_level_acheived
1                     1                                            4
2                     0                                            3
3                     0                                            3
  correcteddf.weeklybikingyesno correcteddf.age correcteddf.gender
1              Weekly biking no      -0.3369081               Male
2              Weekly biking no       0.9214911               Male
3              Weekly biking no       1.0613132               Male
  correcteddf.video_game_all_devices_hours_per_week
1                                         0.1906867
2                                         1.3150321
3                                         0.5119283
  correcteddf.video_game_phone_tablet_hours_per_week
1                                         -0.4006103
2                                          0.1112449
3                                         -0.6565379
  correcteddf.hours_of_phone_use_per_week
1                              -0.8564242
2                              -0.9972160
3                              -1.2318689

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.