简体   繁体   English

使用 R 中的 purrr::map() function 将统计测试应用于列表

[英]Using the purrr::map() function in R to apply a statistical test to a list

I have a data frame with closing prices of the 30 DOW stock indices for the past 10 years.我有一个数据框,其中包含过去 10 年 30 种道琼斯指数的收盘价。 I want to apply some statistical tests to each stock in the data frame.我想对数据框中的每只股票应用一些统计测试。 I want to use the map function from the purrr package.我想使用来自purrr package 的map function。 That's what I do:我就是做这个的:

#First I split my data frame aggregating the closing prices for 30 different stocks 
#into a list combining 30 different data frames, one for each stock. 
#Each data frame has only three variables - date, closing price and stock symbol.

DOW_list <- DOW_prices_df %>%
            split(.$stock_symbol)

#Then I use the map function to apply the Cox Stuart Statistical Test to the closing prices
#of each stock in the list
map(DOW_list, cox.stuart.test($closing_price)) 

However, this doesn't work.但是,这不起作用。 I get the following error:我收到以下错误:

Error in stopifnot(is.numeric(x)) : 
  argument "x" is missing, with no default

My goal is to get the results of the statistical test for each of the 30 stocks.我的目标是获得 30 只股票中每只股票的统计测试结果。 Can someone please explain what I am doing wrong?有人可以解释我做错了什么吗? I will greatly appreciate your time and help!我将非常感谢您的时间和帮助!

If you have a list of data frames you could use the anonymous function notation with lapply or map like this using mtcars as an example:如果您有一个数据框列表,您可以使用匿名 function 表示法和lapplymap像这样使用 mtcars 作为示例:

cars <- split(mtcars, mtcars$cyl)
lapply(cars, function(x) mean(x$mpg))
$`4`
[1] 26.66364

$`6`
[1] 19.74286

$`8`
[1] 15.1

map(cars, function(x) mean(x$mpg))
$`4`
[1] 26.66364

$`6`
[1] 19.74286

$`8`
[1] 15.1

With map , we need a lambda ( ~ ) function,使用map ,我们需要一个 lambda ( ~ ) function,

library(purrr)
map(DOW_list, ~ cox.stuart.test(.x$closing_price))

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

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