kosukeimai / mediation

R package mediation
61 stars 29 forks source link

Warnings/error with 'multimed' 'argument is not numeric or logical: returning NA'? #42

Open kristyrobledo opened 2 years ago

kristyrobledo commented 2 years ago

Hi, Im trying to use the multimed function. I can run the example code for multimed, but when I run it on my dataset, or this 'fake' dataset, it errors (see below). treat is a 0/1 numeric variable.

What am I doing wrong? I assume its user error? thanks!

library(palmerpenguins) library(mediation)

> Loading required package: MASS

> Loading required package: Matrix

> Loading required package: mvtnorm

> Loading required package: sandwich

> mediation: Causal Mediation Analysis

> Version: 4.5.0

library(tidyverse) library(reprex)

create a fake dataset to show example of error

glimpse(penguins)

> Rows: 344

> Columns: 8

> $ species Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel~

> $ island Torgersen, Torgersen, Torgersen, Torgersen, Torgerse~

> $ bill_length_mm 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, ~

> $ bill_depth_mm 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, ~

> $ flipper_length_mm 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186~

> $ body_mass_g 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, ~

> $ sex male, female, female, NA, female, male, female, male~

> $ year 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007~

penguins %>% filter(species!="Gentoo") %>% mutate(treat = as.numeric(rbernoulli(n=220, 0.5))) %>% drop_na()->df

summary(df)

> species island bill_length_mm bill_depth_mm

> Adelie :146 Biscoe : 44 Min. :32.1 Min. :15.50

> Chinstrap: 68 Dream :123 1st Qu.:37.8 1st Qu.:17.50

> Gentoo : 0 Torgersen: 47 Median :40.6 Median :18.40

> Mean :42.0 Mean :18.37

> 3rd Qu.:46.0 3rd Qu.:19.10

> Max. :58.0 Max. :21.50

> flipper_length_mm body_mass_g sex year treat

> Min. :172.0 Min. :2700 female:107 Min. :2007 Min. :0.000

> 1st Qu.:187.0 1st Qu.:3400 male :107 1st Qu.:2007 1st Qu.:0.000

> Median :191.0 Median :3700 Median :2008 Median :1.000

> Mean :191.9 Mean :3715 Mean :2008 Mean :0.514

> 3rd Qu.:196.0 3rd Qu.:3994 3rd Qu.:2009 3rd Qu.:1.000

> Max. :212.0 Max. :4800 Max. :2009 Max. :1.000

test<-multimed(outcome = "bill_length_mm", med.main = "flipper_length_mm", med.alt = "bill_depth_mm", treat = "treat", covariates = "year", data=df, sims=100)

> Warning in mean.default(data.1[, treat] * data.1[, med.main]^2): argument is not

> numeric or logical: returning NA

> Warning in ETM2 * sigma^2/VY: Recycling array of length 1 in vector-array arithmetic is deprecated.

> Use c() or as.vector() instead.

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

> Warning in mean.default(data.b[, treat]): argument is not numeric or logical:

> returning NA

Created on 2022-05-18 by the reprex package (v2.0.1)