create-all-icc.R 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # This code can be reproduced without access to the underlying datasets, unlike regenerate_data.R
  2. # It relies on packages, functions, & variables that are called in SM.Rmd
  3. # You may need to "get" the data -- so if you get
  4. #Error in file(file, "rt") : cannot open the connection
  5. # then do datalad get FILE on terminal to get the relevant files
  6. # Create main ICC including all corpora & all measurements
  7. df.icc.mixed.cols = c("data_set","age_bin", "metric", "iqr",
  8. "age_b","age_se","age_t", # beta, standard error, T
  9. "icc_adjusted", "icc_conditional",
  10. "icc_child_id", "icc_corpus",
  11. "child_id_var","corpus_var","residual_var",
  12. "child_id_sd","corpus_sd","residual_sd","r2_cond", "r2_marg",
  13. "nobs","nchi", "ncor",
  14. "formula","sw")
  15. df.icc.mixed = data.frame(matrix(ncol=length(df.icc.mixed.cols),nrow=0, dimnames=list(NULL, df.icc.mixed.cols)),
  16. stringsAsFactors = FALSE)
  17. for (data_set in data_sets){ # data_set = "aclew"
  18. # Load data
  19. mydat <- read.csv(paste0('../data_output/', data_set,'_metrics_scaled.csv'))
  20. metrics <- colnames(mydat)[!is.element(colnames(mydat), no.scale.columns)]
  21. for(metric in metrics)
  22. { # metric = "voc_chi_ph"
  23. icc.row <- new_fit_models(mydat, data_set, metric, NA, TRUE)
  24. df.icc.mixed[nrow(df.icc.mixed) + 1,] <- icc.row
  25. }
  26. }
  27. write.csv(df.icc.mixed,"../output/df.icc.mixed.csv",row.names=F)
  28. print("done with overall ana, tackling within corpus version")
  29. # repeat for the version within each corpus
  30. df.icc.corpus.cols = c("data_set","corpus","metric", "iqr",
  31. "age_b","age_se","age_t",
  32. "icc_adjusted", "icc_conditional",
  33. "icc_child_id", "icc_corpus",
  34. "child_id_var","corpus_var","residual_var",
  35. "child_id_sd","corpus_sd","residual_sd","r2_cond", "r2_marg",
  36. "nobs","nchi", "ncor",
  37. "formula","sw")
  38. df.icc.corpus = data.frame(matrix(ncol=length(df.icc.corpus.cols),nrow=0, dimnames=list(NULL, df.icc.corpus.cols)),
  39. stringsAsFactors=FALSE)
  40. for (data_set in data_sets){ # data_set = "aclew"
  41. # Load data
  42. mydat <- read.csv(paste0('../data_output/', data_set,'_metrics_scaled.csv'))
  43. for(corpus in corpora){
  44. mycordat <- mydat[mydat$experiment == corpus, ]
  45. metrics <- colnames(mycordat)[!is.element(colnames(mycordat), no.scale.columns)] #this should be defined here bc it varies by data_set
  46. for(metric in metrics) { # metric = "avg_voc_dur_mal"
  47. icc.row <- new_fit_models(mycordat, data_set, metric, corpus, FALSE)
  48. df.icc.corpus[nrow(df.icc.corpus) + 1,] <- icc.row
  49. }
  50. }
  51. }
  52. write.csv(df.icc.corpus,"../output/df.icc.corpus.csv",row.names=F)
  53. print("done with within corpus ana, tackling within age group version")
  54. #We do this one separately because we want to standardize metrics within each age group
  55. # repeat within age group bins
  56. df.icc.age.cols = c("data_set","age_bin","metric", "iqr",
  57. "age_b","age_se","age_t",
  58. "icc_adjusted", "icc_conditional",
  59. "icc_child_id", "icc_corpus",
  60. "child_id_var","corpus_var","residual_var",
  61. "child_id_sd","corpus_sd","residual_sd","r2_cond", "r2_marg",
  62. "nobs","nchi", "ncor",
  63. "formula","sw")
  64. df.icc.age = data.frame(matrix(ncol=length(df.icc.age.cols),nrow=0, dimnames=list(NULL, df.icc.age.cols)),
  65. stringsAsFactors=FALSE)
  66. for (data_set in data_sets){ # data_set = "aclew"
  67. # Load data and calculate age cuts
  68. mydat <- read.csv(paste0('../data_output/', data_set,'_metrics.csv')) # /!\ Do not use scaled version -> we'll scale by age later
  69. mydat$age_bin <- cut(mydat$age,c(0:6*6))
  70. metrics = colnames(mydat)[!is.element(colnames(mydat), no.scale.columns)]
  71. for(thisage in levels(mydat$age_bin))
  72. { #thisage= "(0,6]"
  73. thiscordata <- mydat[mydat$age_bin == thisage,]
  74. for(metric in metrics)
  75. { # metric = "avg_voc_dur_mal"
  76. pre_scaled_metric <- (thiscordata[, metric] - mean(thiscordata[, metric], na.rm=T))/sd(thiscordata[, metric], na.rm=T)
  77. thiscordata[abs(pre_scaled_metric)>2.5 & !is.na(pre_scaled_metric), metric] <- NA
  78. thiscordata[, metric] <- (thiscordata[, metric] - mean(thiscordata[, metric], na.rm=T))/sd(thiscordata[, metric], na.rm=T)
  79. if(dim(thiscordata)[1] > 30 & length(levels(factor(thiscordata$experiment))) > 1)
  80. {
  81. icc.row <- new_fit_models(thiscordata, data_set, metric, thisage, TRUE)
  82. }else{
  83. icc.row <- c(data_set,thisage,metric,iqr,
  84. NA,NA,NA,
  85. NA,NA,
  86. NA,NA,
  87. NA,NA,NA,
  88. NA,NA,NA,
  89. NA,NA,NA,
  90. "not_enough_data",NA)
  91. }
  92. df.icc.age[nrow(df.icc.age) + 1,] <- icc.row
  93. }
  94. }
  95. }
  96. write.csv(df.icc.age,"../output/df.icc.age.csv",row.names=F)