如何使用 sens.slope 函数的结果(输出)创建数据框?

How to create dataframe using results (output) of sens.slope function?

我有一个包含多个工作表的 Excel 数据。我将它们导入 R 并使用函数 sens.slope() 应用 Mann-Kendall 趋势检验。这个函数的结果在htestclass,但是我想把它们放在table.

我安装了所需的包并导入了每张数据集。

require(readxl)
require(trend)
tmin1 <- read_excel("C:/TEZ/ANALİZ/future_projection/2051-2100/model 3-3/average_tmin_3_3_end.xlsx", sheet = "acipayam")
tmin2 <- read_excel("C:/TEZ/ANALİZ/future_projection/2051-2100/model 3-3/average_tmin_3_3_end.xlsx", sheet = "adana")
...
tmin57 <- read_excel("C:/TEZ/ANALİZ/future_projection/2051-2100/model 3-3/average_tmin_3_3_end.xlsx", sheet = "yumurtalik")

然后,指定趋势测试的列。

x1<-tmin1$`13`
x2<-tmin1$`14`
x3<-tmin1$`15`
x4<-tmin1$`16`
x5<-tmin1$`17`
...
x281<-tmin57$`13`
x282<-tmin57$`14`
x283<-tmin57$`15`
x284<-tmin57$`16`
x285<-tmin57$`17`

并应用了函数。

sens.slope(x1)
sens.slope(x2)
sens.slope(x3)
....
sens.slope(x285)

结果是这样的。

> sens.slope(x1)

    Sen's slope

data:  x1
z = 4.6116, n = 49, p-value = 3.996e-06
alternative hypothesis: true z is not equal to 0
95 percent confidence interval:
 0.03241168 0.08101651
sample estimates:
Sen's slope 
 0.05689083 

> sens.slope(x2)

    Sen's slope

data:  x2
z = 6.8011, n = 49, p-value = 1.039e-11
alternative hypothesis: true z is not equal to 0
95 percent confidence interval:
 0.05632911 0.08373755
sample estimates:
Sen's slope 
 0.07032428 
...

如何将这些值放在一个 table 中并将它们写入 Excel 文件? (所需值的名称在函数中为 statisticestimates。)

尝试使用列表而不是在全局环境中拥有这么多对象。

既然你已经有了它们,你可以将它们组合在一个列表中,对每个应用 sens.slope,从中提取 statisticestimates 并获取数据框。

library(trend)

output <- data.frame(t(sapply(mget(paste0('x', 1:285)), function(y) 
                     {temp <- sens.slope(y);c(temp$statistic, temp$estimates)})))

您现在可以使用 write.csv.

将此数据框写入 csv
write.csv(output, 'output.csv', row.names = FALSE)

有一个 package broom 正是为此:

library(tidyverse)
library(trend)

sens.slope(runif(1000)) %>%
  broom::tidy()

# A tibble: 1 x 7
  statistic p.value parameter   conf.low conf.high method      alternative
      <dbl>   <dbl>     <int>      <dbl>     <dbl> <chr>       <chr>      
1     0.548   0.584      1000 -0.0000442 0.0000801 Sen's slope two.sided  

如果你有很多数据框,将它们全部绑定到一个列表中并用 map_df:

循环
A = tibble(Value = runif(1000))
B = tibble(Value = runif(1000))
C = tibble(Value = runif(1000))
D = tibble(Value = runif(1000))

list(A,B,C,D) %>%
  map_df(~.x %>% 
           pull(1) %>%
           sens.slope() %>%
           broom::tidy())
# A tibble: 4 x 7
  statistic p.value parameter   conf.low  conf.high method      alternative
      <dbl>   <dbl>     <int>      <dbl>      <dbl> <chr>       <chr>      
1    -0.376  0.707       1000 -0.0000732  0.0000502 Sen's slope two.sided  
2    -2.30   0.0215      1000 -0.000138  -0.0000110 Sen's slope two.sided  
3    -1.30   0.194       1000 -0.000104   0.0000209 Sen's slope two.sided  
4     0.674  0.500       1000 -0.0000410  0.0000848 Sen's slope two.sided

编辑:刚刚意识到 broom::tidy 在这种情况下不提供估计值(以前没有遇到过),这是不使用 broom 的解决方案:

A = tibble(Value = runif(1000))
B = tibble(Value = runif(1000))
C = tibble(Value = runif(1000))
D = tibble(Value = runif(1000))

list(A,B,C,D) %>%
  purrr::map_df(.,~{
    Test = sens.slope(.x %>% pull(1))
    Test = tibble(Estimate = Test["estimates"] %>% unlist,
           Statistic = Test["statistic"] %>% unlist)
  }
 )
# A tibble: 4 x 2
     Estimate Statistic
        <dbl>     <dbl>
1 -0.0000495     -1.55 
2 -0.00000491    -0.155
3  0.0000242      0.755
4 -0.0000301     -0.921