微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何执行数据集和模型的迭代、受限排列?

如何解决如何执行数据集和模型的迭代、受限排列?

我正在尝试为我的数据创建许多排列,但要保留我的分层设计。我需要对每个随机数据集进行建模,然后提取系数。

我尝试使用 gtools 包的 permute(),但它没有按照我的需要分层。 permute 包的 shuffleSet() 声称可以工作,但我找不到任何有关如何使用 permutationMatrix 进行建模的文档。我已经使用了 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 次。我不确定我哪里出错了,但是有没有办法让我的循环对每个排列的数据集进行建模并返回一个摘要

非常感谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。