Reputation: 17
I am writing a simulation where I am trying several multiple testing methods. In my simulation, I want to vary the percentage of true null hypotheses, and move the true null hypotheses to the beginning of my dataframe. This is proving to be a little tricky, when the number of null hypotheses are being varied. I have looked into moving them by index, but this doesn't work in all cases. (especially h0 = 0) It looks like relocate() might do what I want, but can I use this with several columns, and using only column indexes?
I am just including the "inner loop" in my simulation, where the error occurs. First you can see the levels I want to vary h0 at.
iter <- 100 #number of iterations for 1 datapoint
rho_vec <- c(0, 0.20, 0.40, 0.60, 0.80) # correlation value
h0_vec <- c(0, 0.20, 0.40, 0.60, 0.80) # list of percentage of true h0
for(j in 1 : iter){
mu11 <- c(rep(0, (h0*50)), rep(1.5, (1-h0)*50)) #vector giving the means of the variables. true nulls have 0 mean, false nulls have 1.5 in mean. (12 false h0)
Sigma11 <- diag(k) + rho - diag(k)*rho #Making simple correlation matrix for dependent variables
corrdata1 <- mvrnorm(n, mu = mu11, Sigma = Sigma11)
# now we simulate the unncorrelated data with (1-h0)*50) non-true null hypothesis. n and k are the same.
mu12 <- c(rep(0, h0*50), rep(1.5, (1-h0)*50))
SigmaId <- diag(k) #making correlation matrix (id matrix) for independent data.
indepdata1 <- mvrnorm(n, mu = mu12, Sigma = SigmaId)
#we define the total data matrix for both of the cases
data1 <- cbind(corrdata1,indepdata1) #a 100 x 1000 matrix with 1000 observations of 100 variables
#reorder columns so the false nulls are the last columns.
#data1 <- data1[, c( 0:(h0*50), 51:(50+(h0*50)), (51-((1-h0)*50)):50, (101-(50*(1-h0))):100)] #can check this by calling colMeans(data1). I tried this version first.
data1 %>% relocate(c(0:(h0*50), 51:(50 + (h0*50))) %>% head()) # this is the relocate() approach.
}
This produces an error in relocate(): "Error in UseMethod("relocate") : no applicable method for 'relocate' applied to an object of class "c('matrix', 'double', 'numeric')" Does anyone have any ideas on how to to this? Advice is greatly appreciated!
Upvotes: 0
Views: 1536
Reputation: 8484
The error message tells you that relocate()
is applied to a matrix
object, which it cannot do. Indeed, relocate()
must be applied to a dataframe, so you should use as.data.frame()
or as_tibble()
beforehand, as mentioned in the comments.
Finally, you should reassign the result after using the function, otherwise it won't have any effect:
data1 <- data1 %>% as_tibble() %>% relocate(c(0:(h0*50), 51:(50 + (h0*50))) %>% head())
Upvotes: 1