修改R中的代码(lapply函数)

时间:2017-06-08 01:28:28

标签: r lapply

如何以

的方式修改此代码(或添加更多功能)
  1. 我可以在摘要中使用responseList的每个元素的名称,而不是[[1]],[[2]],....
  2. 如何提取hp:am,p值小于0.05
  3. 如何获得残差的正态图(但只有一页的所有直方图)
  4. 示例:

    responseList <- names(mtcars)[-c(4,9)]
    modelList <- lapply(responseList, function(resp) {
        mF <- formula(paste(resp, " ~ hp * am"))
        aov(mF, data = mtcars)
    })
    summaries <- lapply(modelList, summary)
    resid <- lapply(modelList, resid)
    normal <- lapply(resid, function(x) shapiro.test(x))
    

1 个答案:

答案 0 :(得分:0)

[1]

modelList <- lapply(mtcars[-c(4,9)], function(x) aov(x ~ hp*am, data=mtcars) )

[2]

df2 <- plyr::ldply(modelList, function(x) summary(x)[[1]][["Pr(>F)"]])
names(df2) <- c(attr(modelList[[1]]$terms, "term.labels"), "residuals")

[3]

res.list <- lapply(modelList, '[[', "residuals")

par(mfrow=c(5,2), oma=c(0,0,0,0))
lapply(res.list, hist)

enter image description here