将多个 moran.test 输出转换为结构化、可存储、可复制粘贴的字符串
Convert multiple moran.test outputs into structured, storable, copy-pastable strings
我希望将 spdep::moran.test
的输出折叠成一个字符串,该字符串由变量名称和值定期构建,并且可以作为文本值保存到数据框中,并且在RStudio console 和 copy-pastable 到 MS Word 中形成一个 table 没有太多额外的手动调整。 (我对 运行 进行了多项测试,并希望一次性复制粘贴它们的输出。)
在寻找解决方案的过程中,我偶然发现了 report
包,它声称可以将 htest
class 对象转换为“报告”(我不知道这在 R 中是什么样子),因此 可能 在某种程度上实现我的目标。但是,report
函数在 moran.test
上不起作用,如下面的代码所示。
我正在探索,可能还有其他我没有考虑过的更直接的方法。因此,我的问题是双重的:1. 用 report
and/or 解决眼前的问题 2. 为我的目标提供一个更有效的替代解决方案。
下面的数据准备取自https://mgimond.github.io/simple_moransI_example.
library(sf)
library(spdep)
library(report)
# Load shapefile
s <- readRDS(url("https://github.com/mgimond/Data/raw/gh-pages/Exercises/nhme.rds"))
# Prevent error "old-style crs object detected; please recreate object with a recent sf::st_crs()"
st_crs(s) <- st_crs(s)
# Define neighboring polygons
nb <- poly2nb(s, queen=TRUE)
# Assign weights to the neighbors
lw <- nb2listw(nb, style="W", zero.policy=TRUE)
# Run Moran’s I test
(mt <- moran.test(s$Income,lw, alternative="greater"))
#Moran I test under randomisation
#data: s$Income
#weights: lw
#Moran I statistic standard deviate = 5.8525, p-value = 2.421e-09
#alternative hypothesis: greater
#sample estimates:
# Moran I statistic Expectation Variance
#0.68279551 -0.04000000 0.01525284
# Moran’s I test is of class htest required by function report::report
class(mt)
#[1] "htest"
# Function report::report returns an error
report(mt)
#Error in `$<-.data.frame`(`*tmp*`, "tau", value = c(`Moran I statistic` = 0.68279551202875, :
# replacement has 3 rows, data has 1
所需的输出可能类似于:
"P-value 2.421e-09 | Statistic 0.68279551 | Expectation -0.04000000 | Variance 0.01525284"
重点是名称和值,而不是分隔符。这是基于我目前对如何处理此任务的假设,这些假设可能不完善。
您可能想看看 broom
包:
broom::tidy(mt)
#> # A tibble: 1 x 7
#> estimate1 estimate2 estimate3 statistic p.value method alternative
#> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 0.683 -0.04 0.0153 5.85 2.42e-9 Moran I test u… greater
library(tidyverse)
mt %>%
broom::tidy() %>%
as.list() %>%
enframe() %>%
mutate(value = value %>% as.character()) %>% unite(data, sep = "=") %>%
pull(data) %>%
paste0(collapse = ", ")
#> [1] "estimate1=0.68279551202875, estimate2=-0.04, estimate3=0.0152528397222445, statistic=c(`Moran I statistic standard deviate` = 5.85248209823413), p.value=2.42145194022024e-09, method=Moran I test under randomisation, alternative=greater"
您可以创建一个 table 并从多个测试中创建一个 csv 文件(例如,具有 class htest
的多个对象,例如 mt
、mt1
和 mt2
):
list(mt, mt2, mt3) %>% map(broom::tidy) %>% bind_rows() %>% write_csv("tests.csv")
我希望将 spdep::moran.test
的输出折叠成一个字符串,该字符串由变量名称和值定期构建,并且可以作为文本值保存到数据框中,并且在RStudio console 和 copy-pastable 到 MS Word 中形成一个 table 没有太多额外的手动调整。 (我对 运行 进行了多项测试,并希望一次性复制粘贴它们的输出。)
在寻找解决方案的过程中,我偶然发现了 report
包,它声称可以将 htest
class 对象转换为“报告”(我不知道这在 R 中是什么样子),因此 可能 在某种程度上实现我的目标。但是,report
函数在 moran.test
上不起作用,如下面的代码所示。
我正在探索,可能还有其他我没有考虑过的更直接的方法。因此,我的问题是双重的:1. 用 report
and/or 解决眼前的问题 2. 为我的目标提供一个更有效的替代解决方案。
下面的数据准备取自https://mgimond.github.io/simple_moransI_example.
library(sf)
library(spdep)
library(report)
# Load shapefile
s <- readRDS(url("https://github.com/mgimond/Data/raw/gh-pages/Exercises/nhme.rds"))
# Prevent error "old-style crs object detected; please recreate object with a recent sf::st_crs()"
st_crs(s) <- st_crs(s)
# Define neighboring polygons
nb <- poly2nb(s, queen=TRUE)
# Assign weights to the neighbors
lw <- nb2listw(nb, style="W", zero.policy=TRUE)
# Run Moran’s I test
(mt <- moran.test(s$Income,lw, alternative="greater"))
#Moran I test under randomisation
#data: s$Income
#weights: lw
#Moran I statistic standard deviate = 5.8525, p-value = 2.421e-09
#alternative hypothesis: greater
#sample estimates:
# Moran I statistic Expectation Variance
#0.68279551 -0.04000000 0.01525284
# Moran’s I test is of class htest required by function report::report
class(mt)
#[1] "htest"
# Function report::report returns an error
report(mt)
#Error in `$<-.data.frame`(`*tmp*`, "tau", value = c(`Moran I statistic` = 0.68279551202875, :
# replacement has 3 rows, data has 1
所需的输出可能类似于:
"P-value 2.421e-09 | Statistic 0.68279551 | Expectation -0.04000000 | Variance 0.01525284"
重点是名称和值,而不是分隔符。这是基于我目前对如何处理此任务的假设,这些假设可能不完善。
您可能想看看 broom
包:
broom::tidy(mt)
#> # A tibble: 1 x 7
#> estimate1 estimate2 estimate3 statistic p.value method alternative
#> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 0.683 -0.04 0.0153 5.85 2.42e-9 Moran I test u… greater
library(tidyverse)
mt %>%
broom::tidy() %>%
as.list() %>%
enframe() %>%
mutate(value = value %>% as.character()) %>% unite(data, sep = "=") %>%
pull(data) %>%
paste0(collapse = ", ")
#> [1] "estimate1=0.68279551202875, estimate2=-0.04, estimate3=0.0152528397222445, statistic=c(`Moran I statistic standard deviate` = 5.85248209823413), p.value=2.42145194022024e-09, method=Moran I test under randomisation, alternative=greater"
您可以创建一个 table 并从多个测试中创建一个 csv 文件(例如,具有 class htest
的多个对象,例如 mt
、mt1
和 mt2
):
list(mt, mt2, mt3) %>% map(broom::tidy) %>% bind_rows() %>% write_csv("tests.csv")