Cohens_d 由于非数字向量而导致的错误...但是它在哪里?

Cohens_d error due to non-numeric vector...but where is it?

我收到错误

Error: Cannot compute effect size for a non-numeric vector.

当我尝试使用以下代码在 R Studio 中计算 cohens_d (RStatix) 时:

> # Calculate (Cohen's D) effect size
> Cohen <- LST_Weather_dataset %>% 
+   group_by(Month, .add = FALSE) %>% 
+   cohens_d(
+     LST ~ TimePeriod, paired = TRUE
+     )

LST、Month 和 TimePeriod 都是数字:

> str(LST_Weather_dataset)
'data.frame':   456 obs. of  15 variables:
 $ Buffer        : int  100 200 100 100 100 100 100 100 100 100 ...
 $ Date          : Date, format: "0006-10-20" ...
 $ LST           : num  1.7 1.38 2.66 3.81 3.2 ...
 $ Month         : num  10 10 12 1 2 3 4 5 6 7 ...
 $ Year          : int  2013 2013 2013 2014 2014 2014 2014 2014 2014 2014 ...
 $ JulianDay     : int  279 279 359 10 42 74 106 122 170 202 ...
 $ TimePeriod    : num  1 1 1 1 1 1 1 1 1 1 ...
...

如果我删除 group_by,错误仍然存​​在。

我错过了什么?

编辑

评论中要求的数据:

> test <- dput(LST_Weather_dataset[1:300, c("Month", "LST", "TimePeriod")])
structure(list(Month = c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 
9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 12), LST = c(-0.2210116, -0.24720568, -0.26111535, 
-0.25316346, -0.30848301, -0.33180257, -0.27721127, -0.20766731, 
-0.14346835, -0.06433451, -0.02385345, -0.03473869, -0.0511723, 
-0.04610033, -0.03462484, -0.03734767, -0.04306532, -0.02964348, 
-0.01385088, -0.22618709, -0.2425393, -0.2541757, -0.25475207, 
-0.29917977, -0.36117768, -0.31852919, -0.28564376, -0.2359735, 
-0.12195752, -0.06586962, -0.05811313, -0.03766735, -0.02261001, 
-0.03555068, -0.05061729, -0.05351268, -0.02241539, -0.00981671, 
-0.22016505, -0.26550893, -0.29015481, -0.28068968, -0.3103153, 
-0.33183916, -0.2798485, -0.24933379, -0.23071591, -0.15598767, 
-0.0976511, -0.07922456, -0.06455107, -0.03039455, -0.00439505, 
-0.00427336, -0.01586557, -0.03632884, -0.02813621, -0.34759789, 
-0.40494336, -0.41258461, -0.38127643, -0.38749164, -0.43010968, 
-0.38592257, -0.31959701, -0.25699752, -0.17257249, -0.13109291, 
-0.11687805, -0.10309548, -0.0933909, -0.07803618, -0.06782885, 
-0.05547398, -0.02602688, -0.01469682, -0.16601023, -0.20167048, 
-0.18983419, -0.1591577, -0.15829101, -0.18991115, -0.15560403, 
-0.08758369, -0.0419441, 0.02094988, 0.06149603, 0.04233754, 
0.05957017, 0.08024451, 0.07646054, 0.05378477, 0.01491128, 0.02128012, 
0.01505348, -0.27509869, -0.29971987, -0.28898894, -0.26678469, 
-0.26546793, -0.2767395, -0.21621339, -0.13996242, -0.0772666, 
-0.01513719, 0.01099537, 0.00156385, 0.03741067, 0.05769562, 
0.05522714, 0.03726737, 0.00673056, -0.0086786, -0.00868214, 
-0.24094543, -0.27829062, -0.27762726, -0.26392197, -0.25151899, 
-0.2454435, -0.20808879, -0.15734442, -0.11566806, -0.07631076, 
-0.05374834, -0.06464258, -0.04748078, -0.04106418, -0.02877574, 
-0.04559453, -0.04936476, -0.03311713, -0.0184964, -0.161790125, 
-0.207325856, -0.202609595, -0.182737706, -0.176479067, -0.172051143, 
-0.115534666, -0.069043871, -0.045666582, -0.016103689, -0.0097142, 
-0.027125424, -0.031078945, -0.027668948, -0.038168329, -0.04902581, 
-0.049393072, -0.029093549, -0.016435892, -0.18777984, -0.22560193, 
-0.21353557, -0.19873006, -0.21000726, -0.19960744, -0.16449805, 
-0.12677822, -0.09319863, -0.05365756, -0.03196716, -0.03521089, 
-0.01844106, -0.01675121, -0.02673582, -0.0336761, -0.03909757, 
-0.02311188, -0.00841367, -0.06879569, -0.11193502, -0.11645102, 
-0.11313204, -0.1241938, -0.12764257, -0.09562478, -0.06360073, 
-0.03919549, -0.01310477, 0.00199091, -0.00299883, 0.00071001, 
-0.0009422, -0.01377567, -0.03184663, -0.0422166, -0.02251581, 
-0.00296154, -0.16582354, -0.19623703, -0.19219006, -0.16517101, 
-0.18704658, -0.15345162, -0.10262394, -0.08604339, -0.08749103, 
-0.06062491, -0.03485287, -0.0373792, -0.03525576, -0.04025037, 
-0.02994186, -0.03698465, -0.0445463, -0.03625804, -0.01916101, 
-0.32618398, -0.38494187, -0.37563704, -0.3362058, -0.33109373, 
-0.28774767, -0.23600731, -0.18341335, -0.14375045, -0.09224798, 
-0.06322383, -0.06435687, -0.04382372, -0.04243214, -0.04790463, 
-0.05992479, -0.05804111, -0.03623521, -0.01380783, 0.07521048, 
0.08923107, 0.04085175, 0.02369799, -0.01174092, -0.00924432, 
0.02309842, 0.03106341, 0.01589905, 0.02520586, 0.02755641, 0.01418465, 
0.03378753, 0.0356642, 0.03617703, 0.0236984, 0.01581965, 0.00092364, 
-0.00135577, -0.01808666, -0.02659894, -0.06659976, -0.07161127, 
-0.07336009, -0.05201086, -0.00927892, 0.02300103, 0.03837143, 
0.05036375, 0.06564468, 0.0600014, 0.06562315, 0.05419116, 0.04456497, 
0.01096535, -0.00228103, -0.00411322, -0.00017688, -0.06780171, 
-0.06119238, -0.07202179, -0.05932171, -0.09219981, -0.0933819, 
-0.0571732, -0.03830071, -0.02184744, 0.01698308, 0.02923943, 
0.01394951, 0.01748671, 0.01059124, -0.00520207, -0.02724512, 
-0.02908047, -0.01867358, -0.0086791, -0.02430298, -0.04431464, 
-0.0597082, -0.07027699, -0.09412717, -0.08051615, -0.04382819, 
-0.01872886, -0.00143456, 0.01274511, 0.02342804, 0.00065864, 
0.00322716, -0.00126722, -0.00821571), TimePeriod = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", 
"2"), class = "factor")), row.names = c(NA, 300L), class = "data.frame")
> Cohen <- test %>% 
+ #  group_by(TimePeriod) %>%
+   cohens_d(
+     LST ~ Month, paired = FALSE
+     )
Error: Cannot compute effect size for a non-numeric vector.
>

使用 effsize 包而不是 rstatix 测试 运行 成功:

LST_Weather_dataset %>% group_by(Month, .add = FALSE)
# A tibble: 456 x 14
# Groups:   Month [12]
   Buffer Date         LST Month  Year JulianDay TimePeriod
    <int> <date>     <dbl> <dbl> <dbl>     <dbl>      <dbl>
 1    100 2010-01-15 0.581     1  2010        15          1
 2    100 2010-02-16 0.971     2  2010        47          1
 3    100 2010-03-20 1.63      3  2010        79          1
 4    100 2011-04-24 2.14      4  2011       114          1
 5    100 2010-05-07 1.90      5  2010       127          1
 6    100 2010-06-08 3.32      6  2010       159          1
 7    100 2011-07-13 1.67      7  2011       194          1
 8    100 2010-08-11 2.74      8  2010       223          1
 9    100 2010-09-12 2.27      9  2010       255          1
10    100 2011-10-17 0.987    10  2011       290          1
# ... with 446 more rows, and 7 more variables: Humidity <dbl>,
#   Wind_speed <dbl>, Wind_gust <dbl>, Wind_trend <dbl>,
#   Wind_direction <dbl>, Pressure <dbl>, Pressure_trend <dbl>
> CohenD2 <- cohen.d(
+     LST ~ TimePeriod | Subject(Buffer), paired = TRUE
+     )
> CohenD2

Cohen's d

d estimate: 0.5875947 (medium)
95 percent confidence interval:
    lower     upper 
0.4328020 0.7423874 

> ungroup(LST_Weather_dataset)
# A tibble: 456 x 14
   Buffer Date         LST Month  Year JulianDay TimePeriod
    <int> <date>     <dbl> <dbl> <dbl>     <dbl>      <dbl>
 1    100 2010-01-15 0.581     1  2010        15          1
 2    100 2010-02-16 0.971     2  2010        47          1
 3    100 2010-03-20 1.63      3  2010        79          1
 4    100 2011-04-24 2.14      4  2011       114          1
 5    100 2010-05-07 1.90      5  2010       127          1
 6    100 2010-06-08 3.32      6  2010       159          1
 7    100 2011-07-13 1.67      7  2011       194          1
 8    100 2010-08-11 2.74      8  2010       223          1
 9    100 2010-09-12 2.27      9  2010       255          1
10    100 2011-10-17 0.987    10  2011       290          1
# ... with 446 more rows, and 7 more variables: Humidity <dbl>,
#   Wind_speed <dbl>, Wind_gust <dbl>, Wind_trend <dbl>,
#   Wind_direction <dbl>, Pressure <dbl>, Pressure_trend <dbl>
> CohenD2 <- cohen.d(
+     LST ~ TimePeriod | Subject(Buffer), paired = TRUE
+     )
> CohenD2

Cohen's d

d estimate: 0.5875947 (medium)
95 percent confidence interval:
    lower     upper 
0.4328020 0.7423874 

使用 group_by 对数据进行分组没有任何区别,因为 | Subject() 在函数内执行此操作。

正在尝试查明 rstatix 中的错误 - 根据上面 user20650 的评论。

新会话(通过 R Studio 中的菜单)- 上面的 dput() 代码用于创建 test data.frame,使用 library(rstatix) 加载 rstatix,然后测试 运行与

Cohen <- test %>% 
    group_by(TimePeriod) %>%
    cohens_d(LST ~ Month, paired = FALSE)

测试 运行s 成功:

> Cohen
# A tibble: 72 x 8
   .y.   group1 group2 effsize TimePeriod    n1    n2 magnitude 
 * <chr> <chr>  <chr>    <dbl> <fct>      <int> <int> <ord>     
 1 LST   1      2       0.704  1             19    19 moderate  
 2 LST   1      3       0.516  1             19    19 moderate  
 3 LST   1      4       0.669  1             19    19 moderate  
 4 LST   1      5       0.686  1             19    19 moderate  
 5 LST   1      6       1.10   1             19    19 large     
 6 LST   1      7      -0.480  1             19    19 small     
 7 LST   1      8       0.106  1             19    19 negligible
 8 LST   1      9       0.489  1             19    19 small     
 9 LST   1      10     -0.0644 1             19    19 negligible
10 LST   1      11      0.138  1             19    19 negligible
# ... with 62 more rows

作为 rstatix 的依赖项加载的包,使用 sessionInfo():

列出
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstatix_0.7.0

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13  magrittr_2.0.1   tidyselect_1.1.1 R6_2.5.1         rlang_0.4.12     carData_3.0-4   
 [7] fansi_0.5.0      car_3.0-12       dplyr_1.0.7      tools_4.1.2      broom_0.7.10     utf8_1.2.2      
[13] cli_3.1.0        DBI_1.1.2        ellipsis_0.3.2   abind_1.4-5      assertthat_0.2.1 tibble_3.1.6    
[19] lifecycle_1.0.1  crayon_1.4.2     purrr_0.3.4      tidyr_1.1.4      vctrs_0.3.8      glue_1.6.0      
[25] compiler_4.1.2   pillar_1.6.4     generics_0.1.1   backports_1.4.1  pkgconfig_2.0.3 

我安装的 R Studio 中有待更新的包:

> old.packages()
               Package          LibPath                                               Installed Built   ReposVer 
broom          "broom"          "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "0.7.10"  "4.1.2" "0.7.11" 
carData        "carData"        "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "3.0-4"   "4.1.1" "3.0-5"  
datawizard     "datawizard"     "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "0.2.1"   "4.1.2" "0.2.2"  
distributional "distributional" "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "0.2.2"   "4.1.2" "0.3.0"  
esquisse       "esquisse"       "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "1.0.2"   "4.1.2" "1.1.0"  
fansi          "fansi"          "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "0.5.0"   "4.1.0" "1.0.0"  
gert           "gert"           "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "1.4.3"   "4.1.2" "1.5.0"  
httpuv         "httpuv"         "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "1.6.4"   "4.1.2" "1.6.5"  
insight        "insight"        "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "0.14.5"  "4.1.2" "0.15.0" 
RcppParallel   "RcppParallel"   "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "5.1.4"   "4.1.2" "5.1.5"  
renv           "renv"           "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "0.14.0"  "4.1.1" "0.15.0" 
SuppDists      "SuppDists"      "C:/Users/prmon/OneDrive/Documents/R/win-library/4.1" "1.1-9.5" "4.1.2" "1.1-9.7"
Matrix         "Matrix"         "C:/Program Files/R/R-4.1.2/library"                  "1.3-4"   "4.1.2" "1.4-0"  
               Repository                            
broom          "https://cran.rstudio.com/src/contrib"
carData        "https://cran.rstudio.com/src/contrib"
datawizard     "https://cran.rstudio.com/src/contrib"
distributional "https://cran.rstudio.com/src/contrib"
esquisse       "https://cran.rstudio.com/src/contrib"
fansi          "https://cran.rstudio.com/src/contrib"
gert           "https://cran.rstudio.com/src/contrib"
httpuv         "https://cran.rstudio.com/src/contrib"
insight        "https://cran.rstudio.com/src/contrib"
RcppParallel   "https://cran.rstudio.com/src/contrib"
renv           "https://cran.rstudio.com/src/contrib"
SuppDists      "https://cran.rstudio.com/src/contrib"
Matrix         "https://cran.rstudio.com/src/contrib"