问题描述
我正在尝试为我的数据创建许多排列,但要保留我的分层设计。我需要对每个随机数据集进行建模,然后提取系数。
我尝试使用 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 (将#修改为@)