首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用mapply在两个列表列表中查找anova结果

使用mapply在两个列表列表中查找anova结果
EN

Stack Overflow用户
提问于 2016-02-04 05:20:22
回答 1查看 101关注 0票数 0

我有两个lm结果的嵌套列表(完整和简化模型),这两个列表本身包含三个结果变量。我想使用列表将anova应用于模型的每个完整版本和简化版本(无效假设显着性测试)。我试着给出一个简单的例子,感谢TRinker's DFgen来说明

代码语言:javascript
运行
复制
df1 <-DFgen()
df1
    id   group hs.grad  race gender age m.status   political n.kids income score time1 time2  time3
    1   ID.1 control     yes black   male  24    never    democrat      2 142000 -1.40 88.44 91.09  93.61
    2   ID.2   treat     yes black   male  32  married    democrat      2  81000  0.16 48.34 48.58  48.58
    3   ID.3   treat     yes white   male  30  married independent      1 131000  0.87  7.04  8.31   9.78
    4   ID.4 control      no black female  37    never independent      1  11000 -0.03 25.89 27.24  31.70
    5   ID.5 control      no black female  18    never  republican      2  62000  0.95  1.36  1.96   6.54
    6   ID.6   treat     yes asian   male  31 divorced  republican      1  65000  1.13 98.73 99.98 103.43
    7   ID.7   treat     yes white female  19 divorced    democrat      1 142000  0.39 25.75 29.11  29.21
    8   ID.8   treat      no black female  31  married  republican      0  73000  0.38 57.09 61.89  66.27
    9   ID.9 control     yes black   male  31  married    democrat      2 127000 -0.25 40.11 42.81  45.67
    10 ID.10 control     yes white   male  30  married       other      2 139000  0.50 26.02 28.42  31.56

    df1 <-DFgen()
    df2 <- df1[(df1$gender=="female"),]
    #list of all 3 times
    times1 <- df1[, c("time1", "time2", "time3")]
    times2 <- df2[, c("time1", "time2", "time3")]

    #function for model1, no family variables
    nf.lm <- function(x, y, db){
    lm(x~age+income+y, data=db) 
    }

    #Hypot 1: both sexes, group as predictor, lapply all 3 time variables 
    h1.res <- lapply(times1, y=df1$group, db=df1, nf.lm)

    #Hypot 2: female only, group as predictor, lapply all 3 time variables 
    h2.res <- lapply(times2, y=df2$group, db=df2, nf.lm)

    #make list of model results
    m.res <- list(h1.res, h2.res)

    #Reduced models for comparison with H1 and H2
    nf.lm.red <- function(x, db){
    lm(x~age+income, data=db)   
    }

    h1.red <- lapply(times1, db=df1, nf.lm.red)
    h2.red <- lapply(times2, db=df2, nf.lm.red)

    #make list for reduced models
    m.red <- list(h1.red, h2.red)

    #apply anova to full and reduced model for each time outcome
    mapply(mapply(function(red, res) { 
        anova(red, res, test="F")   
    }, red=m.red, res=m.res))

我明白,

代码语言:javascript
运行
复制
    Error in UseMethod("anova") : 
  no applicable method for 'anova' applied to an object of class "list"

我在想,也许我需要在mapply中嵌套另一个函数?在我的实际数据中,我有两个列表,每个列表包含3组lm结果,共10个列表,因此任何帮助都将不胜感激。抱歉,如果这是一个复杂的方式来问一个简单的问题,但我仍然是新的!

EN

回答 1

Stack Overflow用户

发布于 2016-02-04 07:05:38

也许:

代码语言:javascript
运行
复制
lapply(seq_along(m.red), 
       FUN=function(x) lapply( seq_along(m.red[[x]]), 
                               FUN= function(y) anova(m.red[[x]][[y]],  m.res[[x]][[y]], test="F")
       )
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35187937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档