【问题标题】:forloop/foreach to run each var in data using same model in rforloop/foreach 使用 r 中的相同模型运行数据中的每个 var
【发布时间】:2018-01-17 18:41:28
【问题描述】:

我的数据是这样的

q3a  q3b q3c q3d q3e ... q10d grp
1    2   3   4   5  ...  1    1
2    1   2   3   4       2    1
3    2   1   5   2  ...  1    2
2    1   2   1   1  ...  2    2 
2    3   4   1   2  ...  3    3

我想对每个问题进行单向方差分析和邓肯事后检验。 对于 q3a,代码为

library("DescTools")
q3a <- aov(q3a ~ grp,data = pgy)
PostHocTest(q3a,method = "duncan")

如何编写foreach 循环来为数据中的每个变量迭代相同的模型?

## List of variables:
> dput(names(duncan))
c("q3a", "q3b", "q3c", "q3d", "q3e", "q4a", "q4b", "q4d", "q4e", 
"q4f", "q4g", "q4h", "q4i", "q4j", "q4k", "q4l", "q4m", "q5b", 
"q5c", "q5d", "q6b", "q6c", "q6f", "q7b", "q7d", "q7f", "q7g", 
"q7h", "q7i", "q8a", "q8b", "q9a", "q9b", "q9c", "q9d", "q10a", 
"q10b", "q10c", "q10d")

谢谢!

【问题讨论】:

    标签: r foreach desctools


    【解决方案1】:

    这不是使用foreach 的方法,而是使用dplyr 包和purrr 包的方法。此方法将允许您扩展已经完成的工作以进行进一步分析。

    # first load the packages
    library(dplyr)
    library(purrr)
    
    # read in data
    pgy <- read.table(text = "
    1    2   3   4   5  1    1
    2    1   2   3   4  2    1
    3    2   1   5   2  1    2
    2    1   2   1   1  2    2 
    2    3   4   1   2  3    3", 
                      col.names = c("q3a", "q3b", "q3c", 
                                    "q3d", "q3e", "q10d", "grp"))
    
    # data manipulation to get dataframe in required format
    results <- pgy %>% 
    
    # use this mutate call to convert grp to factor
    # you may not need to do this if that column is already a factor
    mutate(grp = factor(grp)) %>% 
    
    # this gather function will convert the table from wide to long
    # this is necessary to do the group_by() %>% nest() combo
    # the q3a:q10d should work for you if that is the range of questions
    # if not then you can change it to the first and last column names
    # containing question data - these should be unquoted
    gather(key = "question", value = "result", q3a:q10d) %>% 
    
    # this creates groupings based on which question
    group_by(question) %>% 
    
    # this creates nested dataframes for each group - known as list-columns
    # the column name of the nested dataframes will be data unless 
    # explicitly specified
    nest() %>% 
    
    # this is where it gets a little tricky
    # you can use a mutate call to create a new column, but since your
    # data for each column is now stored in a nested list, you need to use
    # map from the purrr package to operate on it.
    # the ~ symbol is a short cut to allow you to avoid writing an anonymous function
    # the .x in the aov function represents the data object in the map call
    # notice that data is also the name of the listed column 
    mutate(aov_test = map(data, ~ aov(result ~ grp, data = .x)),
    
           # this is the real magic of this method, you can use the variable created 
           # in the first line on the mutate function as the input to the 
           # second line for your PostHocTest input
           duncan = map(aov_test, ~DescTools::PostHocTest(x = .x, method = "duncan"))) 
    

    然后您可以像这样访问 duncan 测试的结果:

    results$duncan[[1]]
    

    或全部查看:

    map(1:nrow(results), ~ results$duncan[[.x]])
    

    对于 aov 测试,您可以使用 broom 包在一个整洁的数据框中获取结果,如下所示:

    results %>%
      unnest(map(aov_test, broom::tidy), .drop = TRUE)
    

    您可以查看其他主要的 broom 函数(augment()glance())以查看有关模型的其他信息。不幸的是,您正在进行的邓肯测试似乎没有更整洁的方法。

    【讨论】:

    • 感谢您提供详细的解决方案!我刚刚发现使用lapply 可以轻松解决问题
    • 很高兴您找到了有效的解决方案!请注意,map 函数与lapply 非常相似(如果不是同一件事)。如果这是您必须要做的,那么lapply 方法可能会更容易,但如果您必须扩展到更多模型,则此方法可能非常有用。
    【解决方案2】:

    这是使用lapply 解决我的问题的巧妙解决方案:

    ## list of names of my data 
    > dput(names(duncan))
    c("grp", "q3a", "q3b", "q3c", "q3d", "q3e", "q4a", "q4b", "q4d", 
    "q4e", "q4f", "q4g", "q4h", "q4i", "q4j", "q4k", "q4l", "q4m", 
    "q5b", "q5c", "q5d", "q6b", "q6c", "q6f", "q7b", "q7d", "q7f", 
    "q7g", "q7h", "q7i", "q8a", "q8b", "q9a", "q9b", "q9c", "q9d", 
    "q10a", "q10b", "q10c", "q10d")
    
    ## use lapply to run model for each variable 
    results <- lapply(duncan[,-1], function(x) PostHocTest(aov(x ~ pgy, data = duncan), method = "duncan"))
    
    ## extract results for each variable
    results$q3a
    
      Posthoc multiple comparisons of means : Duncan's new multiple range test 
        95% family-wise confidence level
    
    $pgy
                     diff      lwr.ci     upr.ci   pval    
    PGY2-PGY1  0.10716048  0.04104755 0.17327342 0.0012 ** 
    PGY3-PGY1  0.05197694 -0.01485439 0.11880828 0.1274    
    PGY3-PGY2 -0.05518354 -0.12593368 0.01556661 0.1263    
    
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
    results$q3b
    etc..
    

    我在这里找到了类似的解决方案: Linear Regression loop for each independent variable individually against dependent

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2021-04-25
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多