Reputation: 89
I am trying to create many permutations of my data, but to preserve my stratified design. I need to model each randomized data set and then extract coefficients.
I tried to use the gtools
package's permute(), but it does not stratify as I need it to. The permute
package's shuffleSet() claims to work, but I cannot find any documentation about how to use a permutationMatrix
for modeling. I have resorted to a for loop:
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
}
If I run the shuffle()
line independently, I get a different result each time. However, the whole loop returns the same three coefficients 100 times. I am not sure where I am going wrong, but is there a way to get my loop to model each permuted data set and return a summary?
Thanks so much!
Upvotes: 0
Views: 44