简体   繁体   English

如何从循环中的函数列表中提取参数

[英]How to extract a parameter from a list of functions in a loop

I have a large data set and I want to perform several functions at once and extract for each a parameter.我有一个大数据集,我想一次执行几个函数并为每个参数提取一个参数。

The test dataset:测试数据集:

testdf <- data.frame(vy = rnorm(60), vx = rnorm(60) , gvar = rep(c("a","b"), each=30))

I first definded a list of functions:我首先定义了一个函数列表:

require(fBasics)
normfuns <- list(jarqueberaTest=jarqueberaTest, shapiroTest=shapiroTest, lillieTest=lillieTest)

Then a function to perform the tests by the grouping variable然后是一个通过分组变量执行测试的函数

mynormtest <- function(d) {
  norm_test <- res_reg <- list()
  for (i in c("a","b")){
    res_reg[[i]] <- residuals(lm(vy~vx, data=d[d$gvar==i,]))
    norm_test[[i]] <- lapply(normfuns, function(f) f(res_reg[[i]]))  
  }
  return(norm_test) 
}

mynormtest(testdf)

I obtain a list of test summaries for each grouping variable.我获得了每个分组变量的测试摘要列表。 However, I am interested in getting only the parameter "STATISTIC" and I did not manage to find out how to extract it.但是,我只对获取参数“STATISTIC”感兴趣,我没有设法找出如何提取它。

You can obtain the value stored as "STATISTIC" in the output of the various tests with 您可以使用以下命令在各种测试的输出中获取存储为“ STATISTIC”的值:

res_list <- mynormtest(testdf)
res_list$a$shapiroTest@test@statistic
res_list$a$jarqueberaTest@test@statistic
res_list$a$lillieTest@test@statistic

And correspondingly for set b: 并对应于集合b:

res_list$b$shapiroTest@test$statistic
res_list$b$jarqueberaTest@test$statistic
res_listb$lillieTest@test$statistic

Hope this helps. 希望这可以帮助。

Concerning your function fgetparam I think that it is a nice starting point. 关于函数fgetparam我认为这是一个不错的起点。 Here's my suggestion with a few minor modifications: 这是我的建议,并做了一些小的修改:

getparams2 <- function(myp) {
  m <- matrix(NA, nrow=length(myp), ncol=3)
  for (i in (1:length(myp))){
    m[i,] <- sapply(1:3,function(x) myp[[i]][[x]]@test$statistic)}
  return(m)
}

This function represents a minor generalization in the sense that it allows for an arbitrary number of observations, while in your case this was fixed to two cases, a and b. 从允许任意数量的观察值的角度来看,此函数代表一个较小的概括,而在您的情况下,这固定为a和b这两种情况。 The code can certainly be further shortened, but it might then also become somewhat more cryptic. 当然可以进一步缩短代码,但随后也可能变得更加晦涩难懂。 I believe that in developing a code it is helpful to preserve a certain compromise between efficacy and compactness on one hand and readability or easiness to understand on the other. 我相信,在开发代码时,一方面保持有效性和紧凑性,另一方面保持可读性或易理解性,这是有好处的。

Edit 编辑

As pointed out by @akrun and @Roland the function getparams2() can be written in a much more elegant and shorter form. 正如@akrun和@Roland所指出的那样 ,函数getparams2()可以用更优雅,更短的形式编写。 One possibility is 一种可能性是

getparams2 <- function(myp) {
    matrix(unname(rapply(myp, function(x) x@test$statistic)),ncol=3)}

Another great alternative is 另一个不错的选择是

getparams2 <- function(myp){t(sapply(myp, sapply, function(x) x@test$statistic))}

I wrote a second function to get statistics in matrix form. 我编写了第二个函数以矩阵形式获取统计信息。 Ideally I would like to combine both functions. 理想情况下,我想将两个功能结合在一起。 I thought about lapply, but did not find the solution yet. 我考虑过愉快,但尚未找到解决方案。

fgetparam <- function(myp){
  p_mat <- matrix(NA, nrow=2, ncol=3)
  for(i in 1:2){
    for(j in 1:3 ){
      p_mat[i,j] <- myp[[i]][[j]]@test$statistic
    }
  }
  return(p_mat)
}

fgetparam(res_list)

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

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