【问题标题】:Calculate Moran's I for a large df (12k+ columns, 3.5k rows) and store results in a separate df计算大 df(12k+ 列,3.5k 行)的 Moran's I 并将结果存储在单独的 df 中
【发布时间】:2021-09-23 21:31:27
【问题描述】:

我想在具有 12044 列(和 3400 行)的数据帧上运行 Moran's I 测试,并将结果存储在 df 或排序列表中。前三列分别是 ID、Lat 和 Long。其余的都是我感兴趣的变量。

我了解lapply 旨在做我想做的事,但我不太擅长将结果存储在单独的 df 中。测试结果有四个变量:observedexpectedsdp.value。 p>

这是 df 和函数本身的示例。

set.seed(1)
df <- data.frame(
     ID = 1:15,
     LATITUDE = c(42.6, 42.5, 42.3, 42.8, 42.4, 42.4, 42.4, 42.3, 42.4, 42.4, 41.4, 41.6, 41.8, 43.7, 47.3),
     LONGITUDE = c(-71.5, -71.6, -71.9, -71.0, -71.1, -71.1, -71.1, -71.1, -71.2, -71.2, -70.5, -70.3, -71.2, -70.3, -68.3),
     x1 = runif(15, min=0, max=1000),
     x2 = runif(15, min=0, max=1000),
     x3 = runif(15, min=0, max=1000),
     x4 = runif(15, min=0, max=1000),
     x5 = runif(15, min=0, max=1000),   
     x6 = runif(15, min=0, max=1000),
     x7 = runif(15, min=0, max=1000),
     x8 = runif(15, min=0, max=1000)  )
require(ape)

dists <- as.matrix(dist(cbind(df$LONGITUDE, df$LATITUDE)))
dists.inv <- 1/dists
diag(dists.inv) <- 0
#check
dists.inv[1:5, 1:5]
#deal with the infinite values in the matrix
dists.inv[is.infinite(dists.inv)] <- 0
#calculate Moran's I
Moran.I(df$x1, dists.inv)

谢谢大家

【问题讨论】:

  • 您可以使用output &lt;- apply(df[-(1:3)], 2, function(x) Moran.I(x, dists.inv)) 并将您的结果存储在一个列表中。通过output[["x1"]] 访问x1 列的结果,output[["x2]] 访问x2 等。或者您是否需要将结果存储在data.frame 中?

标签: r geospatial lapply spatial


【解决方案1】:

考虑使用tidyverse。仅选择 starts_with 'x' 或 matches("^x\\d+$") 的列,使用 map 循环遍历这些列,将 Moran.I 与已创建的 'dists.inv' 和循环列一起应用,然后返回一个 tibble绑定列表元素的行 (_dfr)

library(purrr)
library(dplyr)
df %>% 
    select(starts_with('x')) %>%
     map_dfr(~ ape::Moran.I(.x, dists.inv))

-输出

# A tibble: 8 x 4
  observed expected     sd p.value
     <dbl>    <dbl>  <dbl>   <dbl>
1 -0.0305   -0.0714 0.0745  0.583 
2 -0.0854   -0.0714 0.0739  0.850 
3 -0.185    -0.0714 0.0712  0.111 
4 -0.237    -0.0714 0.0737  0.0250
5 -0.109    -0.0714 0.0736  0.612 
6 -0.0280   -0.0714 0.0749  0.562 
7  0.00361  -0.0714 0.0731  0.305 
8 -0.177    -0.0714 0.0737  0.152 

【讨论】:

  • 感谢您的帮助。我将如何添加修改 tibble 以便 df 中的列索引可以用作行索引?就像现在一样,我只是通过 n 得到 1。
  • @SamR 输出是 tibble 并且 tibble 不允许行名。但是您可以添加一个具有原始行名的新列
【解决方案2】:

另一种使用基数 R 的方法是

output <- apply(df[-(1:3)], 2, function(x) Moran.I(x, dists.inv))

然后将此列表绑定到data.frame:

do.call("rbind.data.frame", output)

返回

       observed    expected         sd    p.value
x1 -0.030529141 -0.07142857 0.07452502 0.58314178
x2 -0.085369231 -0.07142857 0.07390247 0.85037818
x3 -0.184828111 -0.07142857 0.07123184 0.11138959
x4 -0.236554103 -0.07142857 0.07367464 0.02500791
x5 -0.108772142 -0.07142857 0.07359794 0.61187441
x6 -0.028012329 -0.07142857 0.07485506 0.56191185
x7  0.003612685 -0.07142857 0.07309663 0.30460722
x8 -0.177143267 -0.07142857 0.07372009 0.15157193

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2022-08-13
    • 2019-09-21
    • 2021-11-27
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多