首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用sens.slope函数的结果(输出)创建数据?

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

Stack Overflow用户
提问于 2020-05-03 10:08:49
回答 2查看 502关注 0票数 0

我有一个带有多张工作表的Excel数据。我将它们导入R中,并使用函数sens.slope()进行Mann趋势测试。这个函数的结果在htest类中,但是我想把它们放在一个表中。

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

代码语言:javascript
运行
复制
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")

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

代码语言:javascript
运行
复制
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`

并赋予了这个功能。

代码语言:javascript
运行
复制
sens.slope(x1)
sens.slope(x2)
sens.slope(x3)
....
sens.slope(x285)

结果是这样的。

代码语言:javascript
运行
复制
> 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 
...

如何将这些值放在单个表中并将其写入Excel文件?(所需值的名称是函数中的统计和估计。)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-03 10:38:18

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

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

代码语言:javascript
运行
复制
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编写。

代码语言:javascript
运行
复制
write.csv(output, 'output.csv', row.names = FALSE)
票数 0
EN

Stack Overflow用户

发布于 2020-05-03 10:41:25

有一个套餐 broom精确地用于此:

代码语言:javascript
运行
复制
library(tidyverse)
library(trend)

sens.slope(runif(1000)) %>%
  broom::tidy()
代码语言:javascript
运行
复制
# 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循环它。

代码语言:javascript
运行
复制
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())
代码语言:javascript
运行
复制
# 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的解决方案

代码语言:javascript
运行
复制
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)
  }
 )
代码语言:javascript
运行
复制
# 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61572725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档