r - 如何对数据集和模型执行迭代的、受限的排列?
问题描述
我正在尝试为我的数据创建许多排列,但要保留我的分层设计。我需要对每个随机数据集进行建模,然后提取系数。
我尝试使用gtools
包的permute(),但它没有按照我的需要分层。该permute
包的shuffleSet()声称可以工作,但我找不到任何有关如何使用 apermutationMatrix
进行建模的文档。我使用了一个 for 循环:
library(permute)
blks <- as.factor(df$block)
plts <- as.factor(df$plot)
CTRL <- how(within = Within(type = "free"), plots = Plots(strata = plts), blocks = blks) # set the way in which permute approaches the data
set.seed(1717)
no.perm <- 100 # set the number of permutations
random_model <- data.frame() # create a place to hold the result
for (i in 1:no.perm) {
shuffled <- shuffle(nrow(df), control = CTRL) # permute the data according to CTRL design
df_shuffled <- df[shuffled,] # since shuffle() returns integers, retrieve the data
coefs <- summary(clogit(response ~ pred1 + pred2 + pred3 + strata(plot),data = df_shuffled))$coefficients # model and extract summary
random_model <- rbind(random_model, coefs) # add to the results
}
如果我shuffle()
独立运行这条线,我每次都会得到不同的结果。但是,整个循环返回相同的三个系数 100 次。我不确定我哪里出错了,但是有没有办法让我的循环对每个排列的数据集进行建模并返回一个摘要?
非常感谢!
解决方案
推荐阅读
- javascript - Auth0 登录按钮 - 无效的挂钩呼叫警告
- hibernate - 如何在应用程序启动时初始化 c3po 连接池?
- tailwind-css - Tailwindcss - 如何垂直排列列?
- sqlite - 为什么我的 SQLite 数据库表在预期位置时找不到?
- android - 更改现有 android keystore 证书的有效期
- r - 如何修复循环以从 R 中的 DNAStringSet 写入多个 FASTA 文件?
- angular - 角度通用:社交共享页面项目
- html - Blazor 服务器:尝试将呈现的页面转换为 html 字符串以创建 pdf
- ios - 如何在没有 Apple Developer Account 的情况下在 iPhone 上部署 Flutter 应用程序?
- angular - RxJs 每 x 秒处理一次第一个事件