How to extract a parameter from a list of functions in a loop

自闭症网瘾萝莉.ら 提交于 2019-12-11 19:25:16

问题


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.


回答1:


You can obtain the value stored as "STATISTIC" in the output of the various tests with

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:

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

Hope this helps.




回答2:


Concerning your function fgetparam I think that it is a nice starting point. 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. 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. 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))}



回答3:


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)


来源:https://stackoverflow.com/questions/31178704/how-to-extract-a-parameter-from-a-list-of-functions-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!