如何从循环中的函数列表中提取参数 [英] How to extract a parameter from a list of functions in a loop

查看:174
本文介绍了如何从循环中的函数列表中提取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据集,我想一次执行几个函数并为每个参数提取数据。

测试数据集:

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

我首先定义了一个函数列表:

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

然后用分组变量执行测试的函数

  mynormtest<  -  function(d){
norm_test < - res_reg < - list()
for(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)

我获得每个分组变量的测试摘要列表。
但是,我有兴趣只获取参数STATISTIC,但我没有设法找出如何提取它。

解决方案

  res_list< p>中,您可以在各种测试的输出中获得存储为STATISTIC的值。 -  mynormtest(testdf)
res_list $ a $ shapiroTest @ test @ statistic
res_list $ a $ jarqueberaTest @ test @ statistic
res_list $ a $ lillieTest @ test @ statistic

相应地,对于集合b:

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

希望这有助于。


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.

解决方案

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.

这篇关于如何从循环中的函数列表中提取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆