当前位置: 首页 > 知识库问答 >
问题:

中介包中的Multimed函数:“参数不是数字或逻辑:返回NA”错误

傅阿苏
2023-03-14

运行<code>multimed</code>函数时出错:<code>参数不是数字或逻辑:返回NA</code>。我可以从multimed运行示例代码,但不能运行我自己的数据集或“假”数据集(见下文)。不确定问题出在哪里-这是数据设置中的错误?还是代码本身的错误?

感谢您的任何帮助。

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           <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel~
#> $ island            <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse~
#> $ bill_length_mm    <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, ~
#> $ bill_depth_mm     <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, ~
#> $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186~
#> $ body_mass_g       <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, ~
#> $ sex               <fct> male, female, female, NA, female, male, female, male~
#> $ year              <int> 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

由reprex包(v2.0.1)于2022-05-18创建

共有1个答案

秦滨海
2023-03-14

我和你有同样的问题。

对于警告:

#

您应该将表转换为.data。框架,因为包不是为TIBLE设计的。

对于警告:

平均值警告。默认值(data.1[,treat]*data.1[,med.main]^2):参数不是
#

您应该将treat变量转换为数值,但是,对于另一个警告,我还没有找到解决方案。。。

 类似资料:
  • 我正在尝试将csv文件导入R,我可以通过以下方式做到这一点 从这里,我试图计算平均值、均方差、标准误差、t统计量和p值。我被教导使用: 然而,当我尝试使用其中任何一个时,我会得到一个错误: 警告消息:In mean.default(Lab2x):参数不是数字或逻辑:返回NA 我做错了什么/错过了什么?

  • 我阅读了我的excel数据,找到了每列的最大值,并计算了平均值。 然后,我取了原始的最大值,并使用另一个excel电子表格减去基线值,当我现在想要这些新值的平均值时,请阅读BUT: 我收到:警告消息: 在mean.default中,参数不是数字或逻辑的:返回NA 为什么我不能从我创建的向量(realmaxpeak)计算平均值? TIA

  • 我确实有点理解Redux,但我不明白如何将函数放入函数中。 我对这一行有问题: 我们在这里用

  • 有没有可能创建一个函数,它将lambda函数作为参数(每个lambda函数使用参数),然后返回一个具有单个参数的新函数,并返回所有lambda函数的乘积? 以下是我的非工作示例: 所以本质上,你有一个函数,它需要4个lambda函数,每个函数使用参数。例如,的参数可以类似于。 然后返回一个名为的新函数,该函数的输出是所有lambda函数的乘积,并且有一个参数,该参数传递到的每个lambda函数的参

  • 我把这个代码从Leaflet api。 不知道在

  • 问题内容: 不知道出了什么问题,但是我从chrome控制台收到了此错误: 通过此JavaScript: 我不确定启动.ajax的方式是否有问题,或者jquery的实现是否正确。我觉得是这样的。有什么想法吗? 编辑:这是上面脚本附带的html 问题答案: 您正在使用jQuery的苗条版本。它不支持ajax调用。用 代替它。 苗条的身材 有时您不需要ajax,或者您更喜欢使用许多专注于ajax请求的独