Random choice of lines in an array in R

Asked

Viewed 337 times

3

I have a problem that produces a numerical matrix mxn being m the number of observations (row) and n the number of variables (column). I need to randomly choose p lines (p < m), without replacement, of this matrix. So I will create a matrix p x n with which I will perform some calculations. How can I do that??

  • It will not be "create a px matrixn"?

  • Correct, the matrix is of order p x n. I was wrong in typing. Thank you

1 answer

3

To select at random p numbers of m, the easiest way is to use the function sample.

set.seed(1234)    # Faz os resultados reprodutíveis

m <- 7
mat <- matrix(rnorm(35), nrow = m)

p <- 4
inx <- sample(nrow(mat), p)
sub_mat <- mat[inx, ]
sub_mat
#           [,1]        [,2]       [,3]       [,4]       [,5]
#[1,] -1.2070657 -0.54663186  0.9594941 -0.4906859 -0.0151383
#[2,]  0.5060559 -0.77625389  2.4158352  0.5747557 -0.5012581
#[3,] -0.5747400  0.06445882  0.1340882 -1.0236557 -1.6290935
#[4,] -2.3456977 -0.47719270 -0.9111954 -0.6937202 -0.4755931
  • It worked, fine. But now I need another help. I need to do this 20 times and save the results in a matrix or vector. The answer to the calculations comes in the form of a summary as follows Model Chisquare = 80.84399 Df = 59 Pr(>Chisq) = 0.03107302 Goodness-of-fit index = 0.8743683 RMSEA index = 0.06115365 95% CI: (NA, 0.09736043) Bentler-Bonett NFI = 0.990771 Bentler CFI = 0.9974839 Need to store Chisquare, Goodness-of-fit index, RMSEA, NFI and CFI results separately for future analysis and to determine a Confidence Interval using Bootstrap

  • To do it 20 times, inx_mat <- replicate(20, sample(nrow(mat), p)). As for the rest of the comment I didn’t understand, but it seems to be another question. Ask another question with a link to this question, an explanation of the calculations and an example of the intended output.

  • Usarei cada matriz criada da seguinte forma &#xA;library(sem)&#xA;cfa<-specifyModel(".......txt")&#xA;dados <- read.table("......txt", h=T) (485 observações)&#xA;p<-200&#xA;inx <- sample(nrow(dados), p)&#xA;dados_p <- dados[inx, ]&#xA;dataCor <- cov.wt(dados_p, wt = rep(1/nrow(dados_p),nrow(dados_p)), method = "ML", cor = TRUE)&#xA;dataCor<-as.matrix(dataCor[[1]])&#xA;cfaOut<-sem(cfa,dataCor,N=p, objective=objectiveGLS)&#xA;summary(cfaOut, conf.level=.95, fit.indices=c("GFI", "RMSEA", "NFI", "CFI")) Quero fazer isso 20 vezes (I think here you use an 'if') and store the summary results separately.

  • It’s really better ask another question. 1) As for the data, you can put the output of dput(head(dados, 20)) in the question, which is the best way to have an example. 2) In the function cov.wt not worth using wt = rep(1/nrow(x), nrow(x)) because that is already the default value. (But also no harm, it is simply unnecessary.)

  • The requested output is too large and exceeds the number of characters available in the comment. I’m new to this forum and I haven’t learned how to attach a file yet (if that’s possible?). Do we have another option for sending values? About the second comment, I wrote so, because it is the way it is in the tutorial I followed, and so I left, even so thanks for the tip.

  • @Clodoaldo You don’t understand, ask another question this time on covariances. And as for the other comment, what I wanted to say is that when an argument of a R function has a certain default value it’s not worth repeating it, you can call that function by omitting that argument.

  • I have just asked the question you asked, follow the link: https://answall.com/questions/292014/howto perform a-looping-no-r-e-guardar-os-resultados-de-um-sum%C3%A1rio-num-vector

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.