How to run a looping in R and store the results of a summary in a vector

Asked

Viewed 99 times

2

I asked a previous question in this forum (Random choice of lines in an array in R) where it needed to randomly choose p lines (p < m), without replacement, of this matrix. Thus creating a matrix p x n with which it was possible to perform some calculations. However, during this execution, another problem was presented:

I need the program:

library(sem)
cfa<-specifyModel("D:/Tese/Arquivos_R/Modelo_Paulo_R.txt")
dados <- read.table("D:/Tese/Dados/Testes/Modelo_Paulo_R/Dados Não Normais - Originais/Dados_Modnovo_Original_Renom(486).txt", h=T)
p<-450
inx <- sample(nrow(dados), p)
dados_p <- dados[inx, ]
dataCor <- cov.wt(dados_p,wt = rep(1/nrow(dados_p),nrow(dados_p)),method =c("ML"), cor = TRUE)
dataCor<-as.matrix(dataCor[[1]])
cfaOut<-sem(cfa,dataCor,N=p, objective=objectiveGLS)
summary(cfaOut, conf.level=.95, fit.indices=c("GFI", "RMSEA", "NFI", "CFI"))

Run 20 times and the summary data is stored separately in a vector or matrix. Follow dput(head(data, 20)) output for example:

structure(list(X1 = c(6L, 7L, 8L, 8L, 9L, 8L, 9L, 8L, 8L, 7L, 
9L, 8L, 7L, 8L, 8L, 8L, 6L, 10L, 8L, 7L), X2 = c(5L, 7L, 6L, 
6L, 9L, 6L, 8L, 6L, 6L, 5L, 7L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L), X3 = c(6L, 7L, 6L, 7L, 8L, 7L, 9L, 8L, 8L, 5L, 9L, 7L, 6L, 
7L, 7L, 7L, 5L, 10L, 8L, 7L), X4 = c(7L, 7L, 8L, 8L, 9L, 8L, 
9L, 8L, 8L, 7L, 9L, 7L, 6L, 7L, 8L, 8L, 4L, 10L, 7L, 7L), X5 = c(5, 
6, 7.67, 6.33, 7.33, 6.33, 8, 7.33, 7.33, 7.33, 7.67, 5.67, 6.33, 
5, 6.67, 6.67, 5.33, 7, 7.67, 7.33), X6 = c(5.17, 7.5, 7.67, 
7.5, 8.67, 7.67, 8.33, 8.33, 8, 7.5, 8.83, 7.5, 6.33, 7.33, 7.83, 
7.33, 7.33, 5.67, 7.83, 8.33), X7 = c(5, 7, 5, 5, 9, 7, 8, 6.33, 
8, 7, 6, 4.33, 4.67, 6.67, 8.33, 8, 8, 5.33, 7.33, 7), X8 = c(6.17, 
5.5, 5.67, 5.67, 7, 7.5, 7.33, 7.33, 8, 6.17, 7.33, 6.5, 6, 7.67, 
8, 6.83, 5.67, 6.67, 8, 9), X9 = c(5.75, 2.25, 5.5, 5.5, 7.25, 
6.25, 7, 5.25, 6.5, 4.25, 5.75, 5.75, 4, 5.5, 5.75, 7, 5, 6.75, 
5.75, 7.75), X10 = c(6L, 7L, 7L, 8L, 8L, 7L, 8L, 6L, 8L, 4L, 
8L, 6L, 4L, 10L, 7L, 6L, 5L, 10L, 8L, 9L), X11 = c(6L, 7L, 7L, 
8L, 8L, 7L, 7L, 9L, 7L, 4L, 8L, 6L, 7L, 10L, 8L, 6L, 6L, 10L, 
8L, 9L), X12 = c(6L, 7L, 7L, 8L, 8L, 7L, 7L, 9L, 8L, 7L, 8L, 
7L, 7L, 10L, 8L, 8L, 6L, 10L, 7L, 9L), X13 = c(6L, 9L, 7L, 8L, 
9L, 7L, 7L, 9L, 8L, 8L, 7L, 9L, 7L, 10L, 8L, 8L, 6L, 10L, 10L, 
10L)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", 
"X9", "X10", "X11", "X12", "X13"), row.names = c(NA, 20L), class = "data.frame")

Also follows the model specified to be used in:

cfa<-specifyModel("D:/Tese/Arquivos_R/Modelo_Paulo_R.txt")

The model in question is:

##Fator 1 -- SAT
SAT->X1 , lam1S, NA
SAT->X2 , lam2S, NA
SAT->X3 , lam3S, NA
SAT->X4 , NA, 1
##Fator 2 -- CONF
CONF->X5 , lam5C, NA
CONF->X6 , lam6C, NA
CONF->X7 , NA, 1
##Fator 3 -- COMPR
COMPR->X8 , lam8C, NA
COMPR->X9 , NA, 1
##Fator 4 -- LEAL
LEAL->X10 , lam10L, NA
LEAL->X11 , lam11L, NA
LEAL->X12 , lam12L, NA
LEAL->X13 , NA, 1
##Variancias Específicas
X1<->X1 , e1, NA
X2<->X2 , e2, NA
X3<->X3 , e3, NA
X4<->X4 , e4, NA
X5<->X5 , e5, NA
X6<->X6 , e6, NA
X7<->X7 , e7, NA
X8<->X8 , e8, NA
X9<->X9 , e9, NA
X10<->X10 , e10, NA
X11<->X11 , e11, NA
X12<->X12 , e12, NA
X13<->X13 , e13, NA
##Fator de Ordem Superior
SAT->CONF, gamma1, NA
SAT->COMPR, gamma2, NA
SAT->LEAL, gamma3, 1
CONF->COMPR, gamma4, NA
CONF->LEAL, gamma5, NA
COMPR->LEAL, gamma6, NA
## Variancia dos Fatores
SAT<->SAT, epsilon1, NA
COMPR<->COMPR, epsilon2, NA
CONF<->CONF, epsilon3, NA
LEAL<->LEAL, epsilon4, NA
  • I am unable to create template. You can edit the question with the file contents Modelo_Paulo_R.txt?

  • The template is above in the question. It is the last attachment. It should be used in text form (.txt)

  • Read 37 records&#xA;NOTE: it is generally simpler to use specifyEquations() or cfa()&#xA; see ?specifyEquations&#xA;Error in classifyVariables(model) : incorrectly specified model&#xA;In addition: Warning message:&#xA;In matrix(unlist(strsplit(paths, "<->|<-|->")), ncol = 2, byrow = TRUE) :&#xA; data length [73] is not a sub-multiple or multiple of the number of rows [37].

  • He reports having read 37 records, when the above text has 36 equations. Lines with # are not considered. Check that there was no problem when the .txt. file was mounted.It must be the same as the attachment. Note: it is generally Simpler to use specifyEquations() or Cfa() see ? specifyEquations also appears when using the algorithm but without compromising.

1 answer

1

The problem should be able to be solved with a function that runs the question code, called 20 times with lapply. The results are stored in smry_list.

sem_smry <- function(DF, cfa, p){
    inx <- sample(nrow(DF), p)
    DF_p <- DF[inx, ]
    dataCor <- cov.wt(DF_p, method = "ML", cor = TRUE)
    dataCor <- as.matrix(dataCor[[1]])
    cfaOut <- sem(cfa, dataCor, N = p, objective = objectiveGLS)
    summary(cfaOut, conf.level = 0.95, fit.indices = c("GFI", "RMSEA", "NFI", "CFI"))
}

cfa <- specifyModel("Modelo_Paulo_R.txt")

smry_list <- lapply(seq_len(20), function(i) sem_smry(dados, cfa, p = 450))
  • Thanks, but I still don’t understand how to separate the values of the GFI, RMSEA, NFI and CFI indexes of the created summaries and assemble a vector for each of them.

Browser other questions tagged

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