How to loop in R and save the results of a table of contents to a vector

2

I asked a previous question in this forum ( Randomly choosing rows in an array in R ) where you needed to randomly choose p rows (p < m), without replacement, from that array. Thus creating a matrix p x n with which it was possible to perform some calculations. However, during this run, another problem occurred:

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 saved separately in a vector or array. Follow the output of the dput (head, data, 20) to serve as an 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 template specified to be used in:

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

The template 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
    
asked by anonymous 17.04.2018 / 18:37

1 answer

0

The problem must be able to be solved with a function that executes the code of the question, called 20 times with lapply . Results are saved 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))
    
17.04.2018 / 21:21