简体   繁体   English

相关性测试:“y”必须是数字向量

[英]correlation test: 'y' must be a numeric vector

I have looked here but no one has answered: Error in Pearson correlation, 'y' must be numeric我看过这里但没人回答: Error in Pearson correlation, 'y' must be numeric

I am trying to run this code:我正在尝试运行此代码:

boc1 <- apply(boc1,2,function(x){cor.test(boc1$observation_date,x, method =
                                                        'pearson')$estimate})
G_pearson <- as.data.frame(as.table(boc1))
G_pearson <- G_pearson[-1, ]
colnames(G_pearson) <- c("Species", "PearsonCoefficient")
G_pearson <- ddply(G_pearson, .(Species), summarise,
                   PearsonCoef=round(sum(PearsonCoefficient), 3))

This is unfortunate, because this code worked before with a similar data and the same variables.这很不幸,因为这段代码之前使用类似的数据和相同的变量。 So, I am confused as to why it isn't working.所以,我很困惑为什么它不起作用。

The vector observation_date is numeric:向量observation_date是数字:

 tibble [357 x 6] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ common_name     : chr [1:357] "Bachman's Sparrow" "Bachman's Sparrow" "Bachman's Sparrow" "Bachman's Sparrow" ...
 $ observation_date: num [1:357] 2000 2001 2002 2003 2004 ...
 $ Population      : num [1:357] 3.08 3.06 1.97 2.03 1.92 ...
 $ sd.value        : num [1:357] 2.21 2.11 2.02 2.19 1.98 ...
 $ count           : int [1:357] 8 14 5 7 14 3 16 22 58 142 ...
 $ se.value        : num [1:357] 0.781 0.564 0.901 0.828 0.529 ...
 - attr(*, "groups")= tibble [17 x 2] (S3: tbl_df/tbl/data.frame)
  ..$ common_name: chr [1:17] "Bachman's Sparrow" "Baltimore Oriole" "Black-throated Blue Warbler" "Brown-headed Nuthatch" ...
  ..$ .rows      : list<int> [1:17] 
  .. ..$ : int [1:21] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ : int [1:21] 22 23 24 25 26 27 28 29 30 31 ...
  .. ..$ : int [1:21] 43 44 45 46 47 48 49 50 51 52 ...
  .. ..$ : int [1:21] 64 65 66 67 68 69 70 71 72 73 ...
  .. ..$ : int [1:21] 85 86 87 88 89 90 91 92 93 94 ...
  .. ..$ : int [1:21] 106 107 108 109 110 111 112 113 114 115 ...
  .. ..$ : int [1:21] 127 128 129 130 131 132 133 134 135 136 ...
  .. ..$ : int [1:21] 148 149 150 151 152 153 154 155 156 157 ...
  .. ..$ : int [1:21] 169 170 171 172 173 174 175 176 177 178 ...
  .. ..$ : int [1:21] 190 191 192 193 194 195 196 197 198 199 ...
  .. ..$ : int [1:21] 211 212 213 214 215 216 217 218 219 220 ...
  .. ..$ : int [1:21] 232 233 234 235 236 237 238 239 240 241 ...
  .. ..$ : int [1:21] 253 254 255 256 257 258 259 260 261 262 ...
  .. ..$ : int [1:21] 274 275 276 277 278 279 280 281 282 283 ...
  .. ..$ : int [1:21] 295 296 297 298 299 300 301 302 303 304 ...
  .. ..$ : int [1:21] 316 317 318 319 320 321 322 323 324 325 ...
  .. ..$ : int [1:21] 337 338 339 340 341 342 343 344 345 346 ...
  .. ..@ ptype: int(0) 
  ..- attr(*, ".drop")= logi TRUE  

Here is a reproducible code:这是一个可重现的代码:

structure(list(common_name = c("Bachman's Sparrow", "Bachman's Sparrow", 
"Bachman's Sparrow", "Bachman's Sparrow", "Bachman's Sparrow", 
"Bachman's Sparrow"), observation_date = c(2000, 2001, 2002, 
2003, 2004, 2005), Population = c(3.07529122036138, 3.05685424420526, 
1.97435048583482, 2.03393700979443, 1.91947121957741, 1.25992104989487
), sd.value = c(2.20982882861608, 2.11152499216907, 2.01521376130684, 
2.19069453175474, 1.98050356145954, 1.492106248361), count = c(8L, 
14L, 5L, 7L, 14L, 3L), se.value = c(0.781292474987976, 0.564328791736227, 
0.901230991895025, 0.828004704218877, 0.529311841447656, 0.861467944150745
)), row.names = c(NA, -6L), groups = structure(list(common_name = "Bachman's Sparrow", 
    .rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1L, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

I figured it out by doing this:我通过这样做弄明白了:

boc1 <- structure(list(common_name = c("Bachman's Sparrow", "Bachman's Sparrow", 
"Bachman's Sparrow", "Bachman's Sparrow", "Bachman's Sparrow", 
"Bachman's Sparrow"), observation_date = c(2000, 2001, 2002, 
2003, 2004, 2005), Population = c(3.07529122036138, 3.05685424420526, 
1.97435048583482, 2.03393700979443, 1.91947121957741, 1.25992104989487
), sd.value = c(2.20982882861608, 2.11152499216907, 2.01521376130684, 
2.19069453175474, 1.98050356145954, 1.492106248361), count = c(8L, 
14L, 5L, 7L, 14L, 3L), se.value = c(0.781292474987976, 0.564328791736227, 
0.901230991895025, 0.828004704218877, 0.529311841447656, 0.861467944150745
)), row.names = c(NA, -6L), groups = structure(list(common_name = "Bachman's Sparrow", 
    .rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1L, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))```

then然后

library(reshape2)
boc1 <- dcast(boc1, observation_date ~ common_name, value.var = "Population")

finally最后

boc1 <- apply(boc1,2,function(x){cor.test(boc1$observation_date,x, method =
                                                        'pearson')$estimate})
G_pearson <- as.data.frame(as.table(boc1))
G_pearson <- G_pearson[-1, ]
colnames(G_pearson) <- c("Species", "PearsonCoefficient")
G_pearson <- ddply(G_pearson, .(Species), summarise,
                   PearsonCoef=round(sum(PearsonCoefficient), 3))

output: output:

                       boc1
observation_date   1.0000000
Bachman's Sparrow -0.9323533

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM