Separate values from a list of summaries in R

Asked

Viewed 47 times

1

From the previous question How to run a looping in R and store the results of a summary in a vector have as response a list with 20 summaries with values calculated from a specified template. One of the answers obtained is in the form

 smry_list[[2]]

 Model Chisquare =  188.6337   Df =  59 Pr(>Chisq) = 1.797041e-15
 Goodness-of-fit index =  0.9272667
 RMSEA index =  0.07420728   95% CI: (0.06013725, 0.08844767)
 Bentler-Bonett NFI =  0.9916955 
 Bentler CFI =  0.9942733

I need to separate the values of RMSEA, GFI, NFI and CFI into separate vectors to perform an analysis of each. The algorithm used, with changes proposed in the answer to the question cited above, follows below

library(sem)
cfa<-specifyModel("...................txt") 
dados <- read.table("...............txt", h=T)  # Amostra Original com 485 observações
p<-300  #Quantidade de observações retiradas aleatoriamente da amostra original
sem_smry <- function(dados, cfa, p)
{
  inx <- sample(nrow(dados), p)
  dados_p <- dados[inx, ]
  dataCor <- cov.wt(dados_p, method = c("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"))
}
smry_list <- lapply(seq_len(20), function(i) sem_smry(dados, cfa, p))

1 answer

1

I believe this can be done with successive applications of lapply. The function to be applied is the extraction function [[.
In the case of RMSEA, in my tests gave a matrix with 4 lines, therefore I had to transform its transposta data.frame before creating the data.frame final.

RMSEA <- sapply(smry_list, `[[`, "RMSEA")
RMSEA_df <- as.data.frame(t(RMSEA))
names(RMSEA_df) <- sprintf("RMSEA_%02d", seq_len(ncol(RMSEA_df)))

GFI <- sapply(smry_list, `[[`, "GFI")
NFI <- sapply(smry_list, `[[`, "NFI")
CFI <- sapply(smry_list, `[[`, "CFI")

indices <- cbind(RMSEA_df, GFI, NFI, CFI)
  • 1

    I used another path that also worked RMSEA<-c(1:20) GFI<-c(1:20) NFI<-c(1:20) CFI<-c(1:20) for(i in 1:20) { RMSEA[i]<- smry_list[[[i]][["RMSEA"][1] NFI[i]<-smry_list[[[i]][["NFI"]] GFI[i]<-smry_list[[[i]]["GFI"]] CFI[i]<-smry_list[i]][["CFI"]] indices <- cbind(RMSEA, GFI, NFI, CFI, CFI) print(indices) Of course, your algorithm is more professional. Thank you so much for all your help without which I could not achieve my goals. Until the next question....

Browser other questions tagged

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