-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathanalyzeSGP.R
2542 lines (2332 loc) · 165 KB
/
analyzeSGP.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
`analyzeSGP` <-
function(sgp_object,
state=NULL,
years=NULL,
content_areas=NULL,
grades=NULL,
sgp.percentiles=TRUE,
sgp.projections=TRUE,
sgp.projections.lagged=TRUE,
sgp.percentiles.baseline=TRUE,
sgp.projections.baseline=TRUE,
sgp.projections.lagged.baseline=TRUE,
sgp.percentiles.baseline.max.order=3,
sgp.percentiles.srs.baseline.max.order=3,
sgp.projections.baseline.max.order=3,
sgp.projections.lagged.baseline.max.order=3,
sgp.projections.max.forward.progression.years=3,
sgp.projections.max.forward.progression.grade=NULL,
sgp.projections.use.only.complete.matrices=NULL,
sgp.minimum.default.panel.years=NULL,
sgp.use.my.coefficient.matrices=NULL,
sgp.use.my.sgp_object.baseline.coefficient.matrices=NULL,
sgp.test.cohort.size=NULL,
return.sgp.test.results=FALSE,
simulate.sgps=TRUE,
calculate.simex=NULL,
calculate.simex.baseline=NULL,
calculate.simex.srs.baseline=NULL,
calculate.srs=NULL,
calculate.srs.baseline=NULL,
goodness.of.fit.print=TRUE,
sgp.config=NULL,
sgp.config.drop.nonsequential.grade.progression.variables=TRUE,
sgp.baseline.panel.years=NULL,
sgp.baseline.config=NULL,
trim.sgp.config=TRUE,
parallel.config=NULL,
verbose.output=FALSE,
print.other.gp=NULL,
sgp.projections.projection.unit="YEAR",
get.cohort.data.info=FALSE,
sgp.sqlite=FALSE,
sgp.percentiles.equated=NULL,
sgp.percentiles.equating.method=NULL,
sgp.percentiles.calculate.sgps=TRUE,
SGPt=NULL,
fix.duplicates=NULL,
...) {
started.at <- proc.time()
messageSGP(paste("\nStarted analyzeSGP", prettyDate()), "\n")
messageSGP(match.call())
VALID_CASE <- CONTENT_AREA <- YEAR <- GRADE <- ID <- YEAR_WITHIN <- SCALE_SCORE <- SCALE_SCORE_EQUATED <- NULL
SGPstateData <- SGP::SGPstateData ### Needed due to possible assignment of values to SGPstateData
#######################################################
### Create relevant analyzeSGP variables
#######################################################
### Create state (if NULL) from sgp_object (if possible)
if (is.null(state)) {
tmp.name <- toupper(gsub("_", " ", deparse(substitute(sgp_object))))
state <- getStateAbbreviation(tmp.name, "analyzeSGP")
}
###############################################################
### Tests associated with supplied arguments
###############################################################
if (!(sgp.percentiles | sgp.percentiles.baseline)) {
simulate.sgps <- FALSE
}
if (simulate.sgps) {
csem.variable <- NULL
calculate.confidence.intervals.list <- list(state=state)
if (is.null(SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]])) {
messageSGP("\tNOTE: CSEMs are required in 'SGPstateData' (either as a data.frame of CSEMs or as a variable name of CSEMsin @Data) to simulate SGPs for confidence interval calculations. SGP standard errors will not be calculated.")
calculate.confidence.intervals.list <- NULL
} else {
if (is.character(SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]])) {
csem.variable <- SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]]
}
if (is.numeric(SGPstateData[[state]][["SGP_Configuration"]][["calculate.confidence.intervals"]][["confidence.quantiles"]])) {
calculate.confidence.intervals.list[['confidence.quantiles']] <-
SGPstateData[[state]][["SGP_Configuration"]][["calculate.confidence.intervals"]][["confidence.quantiles"]]
}
}
} else {
calculate.confidence.intervals.list <- csem.variable <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["sgp.config.drop.nonsequential.grade.progression.variables"]])) {
sgp.config.drop.nonsequential.grade.progression.variables <- SGPstateData[[state]][["SGP_Configuration"]][["sgp.config.drop.nonsequential.grade.progression.variables"]]
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["sgp.loss.hoss.adjustment"]])) {
sgp.loss.hoss.adjustment <- SGPstateData[[state]][["SGP_Configuration"]][["sgp.loss.hoss.adjustment"]]
} else {
sgp.loss.hoss.adjustment <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["return.norm.group.scale.scores"]])) {
return.norm.group.scale.scores <- SGPstateData[[state]][["SGP_Configuration"]][["return.norm.group.scale.scores"]]
} else {
return.norm.group.scale.scores <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["return.norm.group.dates"]])) {
return.norm.group.dates <- SGPstateData[[state]][["SGP_Configuration"]][["return.norm.group.dates"]]
} else {
return.norm.group.dates <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["return.projection.group.scale.scores"]])) {
return.projection.group.scale.scores <- SGPstateData[[state]][["SGP_Configuration"]][["return.projection.group.scale.scores"]]
} else {
return.projection.group.scale.scores <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["return.projection.group.dates"]])) {
return.projection.group.dates <- SGPstateData[[state]][["SGP_Configuration"]][["return.projection.group.dates"]]
} else {
return.projection.group.dates <- NULL
}
if (!is.null(SGPstateData[[state]][["Growth"]][["Cutscores"]][["Cuts"]])) {
percentile.trajectory.values <- sort(unique(c(SGPstateData[[state]][["Growth"]][["Cutscores"]][["Cuts"]], 50)))
} else {
percentile.trajectory.values <- c(35, 50, 65)
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["percentile.trajectory.values"]])) {
percentile.trajectory.values <- sort(unique(c(percentile.trajectory.values, SGPstateData[[state]][["SGP_Configuration"]][["percentile.trajectory.values"]])))
}
if (!is.null(SGPstateData[[state]][["Student_Report_Information"]][["Projection_Fan_Limits"]])) {
percentile.trajectory.values <- sort(c(SGPstateData[[state]][["Student_Report_Information"]][["Projection_Fan_Limits"]], percentile.trajectory.values))
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["gaPlot.back.extrapolated.cuts"]])) {
percentile.trajectory.values <- sort(unique(c(percentile.trajectory.values, 1:9*10)))
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["sgp.projections.baseline.max.order"]])) {
sgp.projections.baseline.max.order <- SGPstateData[[state]][["SGP_Configuration"]][["sgp.projections.baseline.max.order"]]
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["sgp.projections.lagged.baseline.max.order"]])) {
sgp.projections.lagged.baseline.max.order <- SGPstateData[[state]][["SGP_Configuration"]][["sgp.projections.lagged.baseline.max.order"]]
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["return.prior.scale.score.standardized"]])) {
return.prior.scale.score.standardized <- SGPstateData[[state]][["SGP_Configuration"]][["return.prior.scale.score.standardized"]]
} else {
return.prior.scale.score.standardized <- TRUE
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["max.n.for.coefficient.matrices"]])) {
max.n.for.coefficient.matrices <- SGPstateData[[state]][["SGP_Configuration"]][["max.n.for.coefficient.matrices"]]
} else {
max.n.for.coefficient.matrices <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["sgp.cohort.size"]]) & is.null(sgp.use.my.coefficient.matrices)) {
tmp.cohort.size <- SGPstateData[[state]][["SGP_Configuration"]][["sgp.cohort.size"]]
} else tmp.cohort.size <- NULL
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["sgp.less.than.sgp.cohort.size.return"]])) {
sgp.less.than.sgp.cohort.size.return <- SGPstateData[[state]][["SGP_Configuration"]][["sgp.less.than.sgp.cohort.size.return"]]
} else sgp.less.than.sgp.cohort.size.return <- NULL
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["rq.method"]])) {
tmp.rq.method <- SGPstateData[[state]][["SGP_Configuration"]][["rq.method"]]
} else tmp.rq.method <- "br"
if (!is.null(sgp.config) && sgp.config.drop.nonsequential.grade.progression.variables) {
sgp.config.drop.nonsequential.grade.progression.variables <- FALSE
}
if (!any(
grepl("PERCENTILES|BASELINE_PERCENTILES|TAUS|SIMEX|BASELINE_MATRICES|PROJECTIONS|LAGGED_PROJECTIONS",
names(parallel.config[['WORKERS']])
)
)) {
parallel.config <- NULL
}
if (all(c("PERCENTILES", "TAUS") %in% names(parallel.config[['WORKERS']]))) {
if (.Platform$OS.type != "unix" | "SNOW_TEST" %in% names(parallel.config)) stop("Both TAUS and PERCENTILES cannot be executed in Parallel at the same time in Windows OS or using SNOW type backends.")
messageSGP("\n\tCAUTION: Running higher- and lower-level processes in parallel at the same time. Make sure you have enough CPU cores and memory to support this.\n")
}
if (all(c("PERCENTILES", "SIMEX") %in% names(parallel.config[['WORKERS']]))) {
if (.Platform$OS.type != "unix" | "SNOW_TEST" %in% names(parallel.config)) stop("Both SIMEX and PERCENTILES cannot be executed in Parallel at the same time in Windows OS or using SNOW type backends.")
messageSGP("\n\tCAUTION: Running higher- and lower-level processes in parallel at the same time. Make sure you have enough CPU cores and memory to support this.\n")
}
if (all(c("BASELINE_PERCENTILES", "TAUS") %in% names(parallel.config[['WORKERS']]))) {
if (.Platform$OS.type != "unix" | "SNOW_TEST" %in% names(parallel.config)) stop("Both TAUS and BASELINE_PERCENTILES cannot be executed in Parallel at the same time in Windows OS or using SNOW type backends.")
messageSGP("\n\tCAUTION: Running higher- and lower-level processes in parallel at the same time. Make sure you have enough CPU cores and memory to support this.\n")
}
if (all(c("BASELINE_PERCENTILES", "SIMEX") %in% names(parallel.config[['WORKERS']]))) {
if (.Platform$OS.type != "unix" | "SNOW_TEST" %in% names(parallel.config)) stop("Both SIMEX and BASELINE_PERCENTILES cannot be executed in Parallel at the same time in Windows OS or using SNOW type backends.")
messageSGP("\n\tCAUTION: Running higher- and lower-level processes in parallel at the same time. Make sure you have enough CPU cores and memory to support this.\n")
}
if (any(c("SIMEX", "TAUS") %in% names(parallel.config[['WORKERS']]))) {
lower.level.parallel.config <- parallel.config
if (length(grep("SUMMARY|GA_PLOTS|SG_PLOTS|SGP_SCALE_SCORE_TARGETS", names(parallel.config[['WORKERS']]), value=TRUE, invert=TRUE)) <= 2) parallel.config <- NULL # NULL out parallel.config when passed from abcSGP, etc
} else lower.level.parallel.config <- NULL
if (!is.null(calculate.simex) | !is.null(calculate.simex.baseline)) {
if (is.null(SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]])) {
messageSGP("\tNOTE: CSEMs are required in 'SGPstateData' (either as a data.frame of CSEMs or as a variable name of CSEMsin @Data) to produce SIMEX corrected SGPs. SIMEX corrected SGPs will NOT be calculated.")
calculate.simex <- calculate.simex.baseline <- NULL
}
}
if (is.list(calculate.simex) && "csem.data.vnames" %in% names(calculate.simex)) {
csem.variable <- calculate.simex[["csem.data.vnames"]]
}
if (is.list(calculate.simex.baseline) && "csem.data.vnames" %in% names(calculate.simex.baseline)) {
csem.variable <- calculate.simex.baseline[["csem.data.vnames"]]
}
if (identical(calculate.simex, TRUE)) {
if (is.character(csem.variable <- SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]])) {
calculate.simex <- list(csem.data.vnames=csem.variable, lambda=seq(0,2,0.5), simulation.iterations=75, simex.sample.size=5000, extrapolation="linear", save.matrices=TRUE)
} else {
calculate.simex <- list(state=state, lambda=seq(0,2,0.5), simulation.iterations=75, simex.sample.size=5000, extrapolation="linear", save.matrices=TRUE)
csem.variable <- NULL
}
if (identical(sgp.use.my.coefficient.matrices, TRUE)) calculate.simex[['simex.use.my.coefficient.matrices']] <- TRUE
}
if (identical(calculate.simex.baseline, TRUE)) {
if (is.character(csem.variable <- SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]])) {
calculate.simex.baseline <- list(csem.data.vnames=csem.variable, lambda=seq(0,2,0.5), simulation.iterations=75, simex.sample.size=5000, extrapolation="linear", save.matrices=TRUE, use.cohort.for.ranking=TRUE)
} else {
calculate.simex.baseline <- list(state=state, lambda=seq(0,2,0.5), simulation.iterations=75, simex.sample.size=5000, extrapolation="linear", save.matrices=TRUE, use.cohort.for.ranking=TRUE)
csem.variable <- NULL
}
}
if (is.null(sgp.minimum.default.panel.years) & !is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.minimum.default.panel.years']])) {
sgp.minimum.default.panel.years <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.minimum.default.panel.years']]
}
if (is.null(sgp.minimum.default.panel.years) & is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.minimum.default.panel.years']])) {
if (uniqueN(sgp_object@Data[['YEAR']])==2) {
sgp.minimum.default.panel.years <- 2
messageSGP("\tNOTE: Only two years of data present. Minimum default of 3 years of panel data for SGP analyses changed to 2. Please confirm this is consistent with analyses you wish to perform.")
} else {
sgp.minimum.default.panel.years <- 3
}
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.max.forward.progression.grade']])) {
sgp.projections.max.forward.progression.grade <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.max.forward.progression.grade']]
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['print.other.gp']])) {
print.other.gp <- SGPstateData[[state]][["SGP_Configuration"]][['print.other.gp']]
}
if (is.null(print.other.gp)) print.other.gp <- FALSE
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.max.forward.progression.years']])) {
if (identical(SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.max.forward.progression.years']], FALSE)) {
sgp.projections.max.forward.progression.years <- NULL
} else {
sgp.projections.max.forward.progression.years <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.max.forward.progression.years']]
}
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.projection.unit']])) {
sgp.projections.projection.unit <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.projection.unit']]
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.projection.unit.label']])) {
sgp.projections.projection.unit.label <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.projection.unit.label']]
} else {
sgp.projections.projection.unit.label <- sgp.projections.projection.unit
}
if (is.character(goodness.of.fit.print)) {
if (goodness.of.fit.print =="GROB") {
goodness.of.fit.print <- FALSE
goodness.of.fit.print.arg <- state
} else goodness.of.fit.print <- as.logical(goodness.of.fit.print)
} else {
if (!goodness.of.fit.print) {
goodness.of.fit.print.arg <- FALSE
} else {
if (identical(SGPstateData[[state]][["SGP_Configuration"]][["goodness.of.fit.achievement.level.prior"]], FALSE)) { ### For RLI and RLI_UK
goodness.of.fit.print.arg <- TRUE
} else {
goodness.of.fit.print.arg <- state
}
}
}
if (!is.null(SGPstateData[[state]][["Assessment_Program_Information"]][["Assessment_Transition"]][["Year"]])) {
if (SGPstateData[[state]][["Assessment_Program_Information"]][["Assessment_Transition"]][["Year"]]!={tmp.last.year <- tail(sort(unique(sgp_object@Data, by='YEAR')[['YEAR']]), 1)}) {
sgp.percentiles.equated <- FALSE
if ("SCALE_SCORE_EQUATED" %in% names(sgp_object@Data)) sgp_object@Data[["SCALE_SCORE_EQUATED"]][sgp_object@Data[["YEAR"]] >= tmp.last.year] <- sgp_object@Data[["SCALE_SCORE"]][sgp_object@Data[["YEAR"]] >= tmp.last.year]
} else {
if (!identical(sgp.percentiles.equated, FALSE)) sgp.percentiles.equated <- TRUE
}
} else {
if (identical(sgp.percentiles.equated, TRUE)) {
messageSGP("\t\tNOTE: 'sgp.percentiles.equated' has been set to TRUE but no meta-data exists in 'SGPstateData' associated with that assessment transition. Equated/linked SGP analyses require meta-data embedded in 'SGPstateData' to correctly work. Contact package administrators on how such data can be added to the package.")
}
sgp.percentiles.equated <- FALSE
}
if (!is.null(SGPt)) {
if (identical(SGPt, TRUE)) SGPt <- "DATE"
if (!all(SGPt %in% names(sgp_object@Data))) {
tmp.messages <- c(tmp.messages, "\t\tNOTE: Variables", paste(SGPt, collapse=", "), "are not all contained in the supplied 'sgp_object@Data'. 'SGPt' is set to NULL.\n")
SGPt <- NULL
}
SGPt.max.time <- SGPstateData[[state]][['SGP_Configuration']][['SGPt.max.time']]
} else {
SGPt.max.time <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.use.my.sgp_object.baseline.coefficient.matrices']]) && is.null(sgp.use.my.sgp_object.baseline.coefficient.matrices)) {
sgp.use.my.sgp_object.baseline.coefficient.matrices <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.use.my.sgp_object.baseline.coefficient.matrices']]
}
if (identical(sgp.use.my.sgp_object.baseline.coefficient.matrices, FALSE)) sgp.use.my.sgp_object.baseline.coefficient.matrices <- NULL
if (!is.null(sgp.use.my.sgp_object.baseline.coefficient.matrices) && length(grep("BASELINE", names(sgp_object@SGP$Coefficient_Matrices)))==0) {
sgp.use.my.sgp_object.baseline.coefficient.matrices <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['lagged.percentile.trajectory.values']])) {
lagged.percentile.trajectory.values <- sort(SGPstateData[[state]][["SGP_Configuration"]][['lagged.percentile.trajectory.values']])
} else {
lagged.percentile.trajectory.values <- NULL
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.use.only.complete.matrices']])) {
sgp.projections.use.only.complete.matrices <- SGPstateData[[state]][["SGP_Configuration"]][['sgp.projections.use.only.complete.matrices']]
}
if (is.null(fix.duplicates) & !is.null(SGPstateData[[state]][["SGP_Configuration"]][["fix.duplicates"]])) {
fix.duplicates <- SGPstateData[[state]][["SGP_Configuration"]][["fix.duplicates"]]
return.norm.group.scale.scores <- TRUE
return.projection.group.scale.scores <- TRUE
}
if (sgp.percentiles.calculate.sgps==FALSE) {
goodness.of.fit.print <- FALSE
}
if (!is.null(SGPstateData[[state]][["SGP_Configuration"]][["max.forward.projection.sequence"]]) &&
!is.null(SGPstateData[[state]][["SGP_Configuration"]][["max.sgp.target.years.forward"]]) &&
max(SGPstateData[[state]][["SGP_Configuration"]][["max.sgp.target.years.forward"]]) > min(unlist(SGPstateData[[state]][["SGP_Configuration"]][["max.forward.projection.sequence"]]))
) {
stop(paste("SGPstateData configuration value for 'max.forward.projection.sequence' is less than the largest specified value for 'max.sgp.target.years.forward'. Reconcile these values in", state, "meta-data."))
}
###########################################################################################################
### Utility functions
###########################################################################################################
## Function to export/print goodness of fit results as pdf files to directory Goodness_of_Fit
# gof.print <- function(sgp_object) {
# if (length(sgp_object@SGP[["Goodness_of_Fit"]]) > 0L) {
# for (i in names(sgp_object@SGP[["Goodness_of_Fit"]])) {
# dir.create(paste0("Goodness_of_Fit/", i, "/Decile_Tables"), recursive=TRUE, showWarnings=FALSE)
# for (output.format in c("PDF", "PNG", "DECILE_TABLES")) {
# for (j in names(sgp_object@SGP[["Goodness_of_Fit"]][[i]])) {
# tmp.path <- file.path("Goodness_of_Fit", i, j)
# if (!identical(.Platform$OS.type, "unix") & nchar(tmp.path) > 250L) {
# tmp.content_area <- unlist(strsplit(j, "[.]"))[1L]
# tmp.path <- gsub(tmp.content_area, substr(tmp.content_area, 1, 1), tmp.path)
# }
# if (output.format=="PDF") {
# pdf(file=paste0(tmp.path, ".pdf"), width=8.5, height=11)
# grid.draw(sgp_object@SGP[["Goodness_of_Fit"]][[i]][[j]][["PLOT"]])
# dev.off()
# }
# if (output.format=="PNG") {
# Cairo(file=paste0(tmp.path, ".png"),
# width=8.5, height=11, units="in", dpi=144, pointsize=10.5, bg="transparent")
# grid.draw(sgp_object@SGP[["Goodness_of_Fit"]][[i]][[j]][["PLOT"]])
# dev.off()
# }
# if (output.format=="DECILE_TABLES") {
# decile.table <- sgp_object@SGP[["Goodness_of_Fit"]][[i]][[j]][["TABLE"]]
# save(decile.table, file=paste0("Goodness_of_Fit/", i, "/Decile_Tables/", j, "_Decile_Table.Rdata"))
# }
# }
# }
# }
# } else {
# messageSGP("\tNOTE: No Goodness of Fit tables available to print. No tables will be produced.")
# }
# }
## Function to merge coefficient matrices from coefficient matrix productions
merge.coefficient.matrices <- function(list.of.matrices, simex=FALSE) {
tmp.list <- list()
tmp.coefficient.matrices <- unlist(list.of.matrices, recursive=FALSE)
if (simex) {
for (tmp.names in unique(names(tmp.coefficient.matrices))) {
tmp1 <- unlist(tmp.coefficient.matrices[grep(tmp.names, names(tmp.coefficient.matrices))], recursive=FALSE)
names(tmp1) <- sapply(strsplit(names(tmp1), "[.]"), function(x) x[4])
tmp.list[[tmp.names]] <- tmp1
}
} else {
for (tmp.names in unique(names(tmp.coefficient.matrices))) {
tmp1 <- unlist(tmp.coefficient.matrices[grep(tmp.names, names(tmp.coefficient.matrices))], recursive=FALSE)
names(tmp1) <- sapply(strsplit(names(tmp1), "[.]"), function(x) x[3])
tmp.list[[tmp.names]] <- tmp1
}
}
tmp.list
}
get.simulate.sgps.arg <- function(calculate.confidence.intervals.list, sgp.iter) {
if (!is.null(calculate.confidence.intervals.list) && is.character(SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]])) {
calculate.confidence.intervals.list[['variable']] <-
gsub("[.]+$", "", paste(SGPstateData[[state]][["Assessment_Program_Information"]][["CSEM"]], tail(sgp.iter[['sgp.panel.years']], 1), tail(sgp.iter[['sgp.content.areas']], 1), tail(sgp.iter[['sgp.panel.years.within']], 1), sep="."))
}
return(calculate.confidence.intervals.list)
}
get.calculate.simex.arg <- function(calculate.simex, sgp.iter) {
if (is.null(calculate.simex)) return(NULL) # If not NULL, must be a list
if (is.null(calculate.simex$csem.data.vnames)) return(calculate.simex)
calculate.simex[['csem.data.vnames']] <- gsub("[.]+$", "", paste(calculate.simex$csem.data.vnames, sgp.iter[['sgp.panel.years']], sgp.iter[['sgp.content.areas']], sgp.iter[['sgp.panel.years.within']], sep="."))
return(calculate.simex)
}
selectCoefficientMatrices <- function(tmp_sgp_object, coefficient.matrix.type=NULL) {
if (is.null(coefficient.matrix.type)) {
return(tmp_sgp_object[['Coefficient_Matrices']][
setdiff(names(tmp_sgp_object[['Coefficient_Matrices']]), grep('BASELINE|EQUATED', names(tmp_sgp_object[['Coefficient_Matrices']]), value=TRUE))])
}
if (coefficient.matrix.type=="BASELINE") {
return(tmp_sgp_object[["Coefficient_Matrices"]][grep("BASELINE", names(tmp_sgp_object[["Coefficient_Matrices"]]))])
}
if (coefficient.matrix.type=="EQUATED") {
return(tmp_sgp_object[["Coefficient_Matrices"]][grep("EQUATED", names(tmp_sgp_object[["Coefficient_Matrices"]]))])
}
}
#######################################################################################################################
## Set up the temporary sgp list object. Fill with necessary old results if they exist first.
## Create subset of @Data containing essential data elements for analyses
#######################################################################################################################
if (sgp.percentiles.equated) {
year.for.equate <- tail(sort(unique(sgp_object@Data, by='YEAR')[['YEAR']]), 1)
if (is.null(SGPstateData[[state]][["Assessment_Program_Information"]][["Assessment_Transition"]][['Baseline_Projections_in_Transition_Year']]) &
(sgp.percentiles.baseline | sgp.projections.baseline | sgp.projections.lagged.baseline)) {
messageSGP("\tNOTE: Baseline SGP related analyses are not possible across an assessment transition with equating. Arguments related to baseline analyses are set to FALSE.")
sgp.percentiles.baseline <- sgp.projections.baseline <- sgp.projections.lagged.baseline <- FALSE
}
if (is.null(sgp.use.my.coefficient.matrices)) {
Scale_Score_Linkages <- list()
dir.create(file.path("Data", paste("Linkages", year.for.equate, sep="_"), "Figures"), recursive=TRUE, showWarnings=FALSE)
content_areas.for.equate <- unique(sgp_object@Data[YEAR==year.for.equate], by="CONTENT_AREA")[['CONTENT_AREA']]
if (is.null(sgp.percentiles.equating.method)) {
messageSGP("\tNOTE: Analyses involving equating will be performed using each of: 'identity', 'mean', 'linear', and 'equipercentile' methods.\n\t\tSee documentation associated with the 'sgp.percentiles.equating.method' argument in 'analyzeSGP'.")
sgp.percentiles.equating.method <- c("identity", "mean", "linear", "equipercentile")
}
if (!identical(years, year.for.equate)) {
messageSGP(paste0("\tNOTE: Analyses involving equating only occur in most recent year. 'years' argument changed to ", year.for.equate, "."))
years <- year.for.equate
}
if (!all(paste(content_areas.for.equate, year.for.equate, sep=".") %in% names(SGPstateData[[state]][['Achievement']][['Knots_Boundaries']]))) {
tmp.knots.boundaries <- createKnotsBoundaries(sgp_object@Data[YEAR==year.for.equate])
names(tmp.knots.boundaries) <- paste(names(tmp.knots.boundaries), year.for.equate, sep=".")
SGPstateData[[state]][["Achievement"]][["Knots_Boundaries"]] <- c(SGPstateData[[state]][["Achievement"]][["Knots_Boundaries"]], tmp.knots.boundaries)
assign(paste(state, "Knots_Boundaries", sep="_"), SGPstateData[[state]][["Achievement"]][["Knots_Boundaries"]])
save(list=paste(state, "Knots_Boundaries", sep="_"), file=paste(state, "Knots_Boundaries.Rdata", sep="_"))
messageSGP(paste0("\tNOTE: Knots and Boundaries do not exist for ", year.for.equate, " in state provided.\n\tThey have been produced, embedded in SGPstateData, and are available using state=", state, " for subsequent analyses and saved to your working directory '", getwd(), "'."))
}
data.for.equate <- copy(sgp_object@Data)
if ("SCALE_SCORE_EQUATED" %in% names(data.for.equate)) {
old.scale.score.equated.year <- tail(data.for.equate[!is.na(SCALE_SCORE_EQUATED),.N,keyby="YEAR"]$YEAR, 1)
if (paste("SCALE_SCORE_EQUATED_FROM", old.scale.score.equated.year, sep="_") %in% names(data.for.equate)) data.for.equate[,paste("SCALE_SCORE_EQUATED_FROM", old.scale.score.equated.year, sep="_"):=NULL]
setnames(data.for.equate, "SCALE_SCORE_EQUATED", paste("SCALE_SCORE_EQUATED_FROM", old.scale.score.equated.year, sep="_"))
messageSGP(paste0("\tNOTE: Variable `SCALE_SCORE_EQUATED` exists in @Data and is being renamed as SCALE_SCORE_EQUATED_", old.scale.score.equated.year, " to accomodate an additional assessment transition variable."))
}
sgp_object@SGP[['Linkages']] <- Linkages <- equateSGP(data.for.equate, state, year.for.equate, sgp.percentiles.equating.method)
setkey(data.for.equate, VALID_CASE, CONTENT_AREA, YEAR, GRADE, SCALE_SCORE)
for (conversion.type.iter in c("OLD_TO_NEW", "NEW_TO_OLD")) {
for (sgp.percentiles.equating.method.iter in sgp.percentiles.equating.method) {
data.for.equate <- convertScaleScore(data.for.equate, year.for.equate, sgp_object@SGP[['Linkages']],
conversion.type=conversion.type.iter, sgp.percentiles.equating.method.iter, state)
Scale_Score_Linkages[[conversion.type.iter]][[toupper(sgp.percentiles.equating.method.iter)]] <-
unique(data.for.equate, by=key(data.for.equate))[!is.na(SCALE_SCORE) & VALID_CASE=="VALID_CASE", intersect(names(data.for.equate),
c("CONTENT_AREA", "YEAR", "GRADE", "SCALE_SCORE", "SCALE_SCORE_ACTUAL", paste("SCALE_SCORE_EQUATED", toupper(sgp.percentiles.equating.method.iter), conversion.type.iter, sep="_"))), with=FALSE]
fwrite(Scale_Score_Linkages[[conversion.type.iter]][[toupper(sgp.percentiles.equating.method.iter)]],
file=paste0(paste0("Data/", paste("Linkages", year.for.equate, sep="_"), "/"), paste(gsub(" ", "_", getStateAbbreviation(state, type="LONG")), "Scale_Score_Linkages", toupper(sgp.percentiles.equating.method.iter), conversion.type.iter, sep="_"), ".txt"), quote=FALSE, sep="|")
linkagePlot(Scale_Score_Linkages[[conversion.type.iter]][[toupper(sgp.percentiles.equating.method.iter)]], conversion.type.iter, sgp.percentiles.equating.method.iter, year.for.equate, state)
}
}
save(Linkages, file=paste0(paste0("Data/", paste("Linkages", year.for.equate, sep="_"), "/"), "Linkages.Rdata"))
assign(paste(gsub(" ", "_", getStateAbbreviation(state, type="LONG")), "Scale_Score_Linkages", sep="_"), Scale_Score_Linkages)
save(list=paste(gsub(" ", "_", getStateAbbreviation(state, type="LONG")), "Scale_Score_Linkages", sep="_"),
file=paste0(paste0("Data/", paste("Linkages", year.for.equate, sep="_"), "/"), paste(gsub(" ", "_", getStateAbbreviation(state, type="LONG")), "Scale_Score_Linkages", sep="_"), ".Rdata"))
setkey(data.for.equate, VALID_CASE, CONTENT_AREA, YEAR, ID)
data.for.equate[,setdiff(names(data.for.equate), c(names(sgp_object@Data), 'SCALE_SCORE_EQUATED_EQUIPERCENTILE_OLD_TO_NEW')):=NULL]
setnames(data.for.equate, 'SCALE_SCORE_EQUATED_EQUIPERCENTILE_OLD_TO_NEW', 'SCALE_SCORE_EQUATED')
sgp_object@Data <- data.for.equate
} ### END if (is.null(sgp.use.my.coefficient.matrices))
equate.variable <- "SCALE_SCORE_EQUATED"
equate.label <- coefficient.matrix.type <- "EQUATED"
sgp.percentiles.equated <- TRUE
sgp.projections.equated <- list(State=state, Year=year.for.equate, Linkages=sgp_object@SGP[['Linkages']])
tmp_sgp_object <- list(Coefficient_Matrices=sgp_object@SGP[["Coefficient_Matrices"]], Knots_Boundaries=sgp_object@SGP[["Knots_Boundaries"]], Linkages=sgp_object@SGP[['Linkages']])
} else {
sgp.percentiles.equated <- FALSE
equate.variable <- equate.label <- year.for.equate <- sgp.projections.equated <- coefficient.matrix.type <- NULL
tmp_sgp_object <- list(Coefficient_Matrices=sgp_object@SGP[["Coefficient_Matrices"]], Knots_Boundaries=sgp_object@SGP[["Knots_Boundaries"]])
} ### END if (sgp.percentiles.equated)
variables.to.get <- c("VALID_CASE", "YEAR", "CONTENT_AREA", "GRADE", "ID", "SCALE_SCORE", "ACHIEVEMENT_LEVEL", "YEAR_WITHIN", "FIRST_OBSERVATION", "LAST_OBSERVATION",
"STATE", csem.variable, equate.variable, SGPt)
if (toupper(sgp.sqlite)=="KEEP") {keep.sqlite <- TRUE; sgp.sqlite <- TRUE} else keep.sqlite <- FALSE
if (as.numeric(strsplit(format(object.size(sgp_object@Data), units="GB"), " Gb")[[1L]]) > 1) sgp.sqlite <- TRUE
if (!is.null(SGPt)) sgp.sqlite <- FALSE # Ultimate case of whether or not to use SQLite?
if (sgp.sqlite) {
tmp_sgp_data_for_analysis <-
dbConnect(
SQLite(),
dbname = file.path(tempdir(), "TMP_SGP_Data.sqlite")
)
dbWriteTable(tmp_sgp_data_for_analysis, name = "sgp_data", overwrite = TRUE,
value=sgp_object@Data[,intersect(names(sgp_object@Data), variables.to.get), with=FALSE]["VALID_CASE"], row.names=FALSE)
sgp.data.names <- dbListFields(tmp_sgp_data_for_analysis, "sgp_data")
# dbDisconnect(tmp_sgp_data_for_analysis)
} else {
tmp_sgp_data_for_analysis <- sgp_object@Data[,intersect(names(sgp_object@Data), variables.to.get), with=FALSE]["VALID_CASE"]
sgp.data.names <- names(tmp_sgp_data_for_analysis)
if ("YEAR_WITHIN" %in% sgp.data.names) {
setkey(tmp_sgp_data_for_analysis, VALID_CASE, CONTENT_AREA, YEAR, GRADE, YEAR_WITHIN)
} else {
setkey(tmp_sgp_data_for_analysis, VALID_CASE, CONTENT_AREA, YEAR, GRADE)
}
}
##############################################################################################################################
### Baseline SGP - compute matrices first if they are not in SGPstateData or merge them into sgp_object if in SGPstateData
##############################################################################################################################
if (sgp.percentiles.baseline | sgp.projections.baseline | sgp.projections.lagged.baseline) {
if (is.null(sgp.use.my.sgp_object.baseline.coefficient.matrices) && is.null(SGPstateData[[state]][["Baseline_splineMatrix"]])) {
if (is.null(sgp.baseline.config)) {
sgp.baseline.config <- getSGPBaselineConfig(sgp_object, content_areas, grades, sgp.baseline.panel.years, sgp.percentiles.baseline.max.order)
} else {
sgp.baseline.config <- checkConfig(sgp.baseline.config, "Baseline")
}
messageSGP("\n\tStarted Baseline Coefficient Matrix Calculation:\n")
if (!is.null(parallel.config)) { ### PARALLEL BASELINE COEFFICIENT MATRIX CONSTRUCTION
par.start <- startParallel(parallel.config, 'BASELINE_MATRICES')
### FOREACH flavor
if (toupper(parallel.config[["BACKEND"]]) == "FOREACH") {
tmp <- foreach(sgp.iter=iter(sgp.baseline.config), .packages="SGP", .errorhandling = "pass", .inorder=FALSE,
.options.multicore=par.start$foreach.options, .options.mpi=par.start$foreach.options, .options.redis=par.start$foreach.options) %dopar% {
return(baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE))
}
if (any(tmp.tf <- sapply(tmp, function(x) any(class(x) %in% c("try-error", "simpleError"))))) {
tmp_sgp_object[['Error_Reports']] <- c(tmp_sgp_object[['Error_Reports']],
sgp.percentiles.baseline.=getErrorReports(tmp, tmp.tf, sgp.baseline.config[['sgp.percentiles.baseline']]))
}
tmp_sgp_object <- mergeSGP(Reduce(mergeSGP, tmp[!tmp.tf]), tmp_sgp_object)
} else {
if (par.start$par.type=="SNOW") {
tmp <- clusterApplyLB(par.start$internal.cl, sgp.baseline.config, function(sgp.iter) baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE))
tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp)))
} # END if (SNOW)
if (par.start$par.type=="MULTICORE") {
tmp <- mclapply(sgp.baseline.config, function(sgp.iter) baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE),
mc.cores=par.start$workers, mc.preschedule=FALSE)
tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp)))
} # END if (MULTICORE)
stopParallel(parallel.config, par.start)
} # END if parallel
} else {
## SEQUENTIAL BASELINE COEFFICIENT MATRIX CONSTRUCTION
## Or, run TAUS in parallel in studentGrowthPercentiles using lower.level.parallel.config
## Useful if many more cores/workers available than configs to iterate over.
tmp <- list()
for (sgp.iter in seq_along(sgp.baseline.config)) {
tmp[[sgp.iter]] <- baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=sgp.baseline.config[sgp.iter], ## NOTE: must pass list, [...], not vector, [[...]].
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE,
parallel.config=lower.level.parallel.config)
}
tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp)))
}
rm(tmp)
assign(paste0(state, "_Baseline_Matrices"), list())
for (tmp.matrix.label in grep("BASELINE", names(tmp_sgp_object$Coefficient_Matrices), value=TRUE)) {
eval(parse(text=paste0(state, "_Baseline_Matrices[['", tmp.matrix.label, "']] <- tmp_sgp_object[['Coefficient_Matrices']][['", tmp.matrix.label, "']]")))
}
save(list=paste0(state, "_Baseline_Matrices"), file=paste0(state, "_Baseline_Matrices.Rdata"))
messageSGP("\n\tFinished Calculating Baseline Coefficient Matrices\n")
} else {
if (is.null(sgp.use.my.sgp_object.baseline.coefficient.matrices)) tmp_sgp_object <- mergeSGP(tmp_sgp_object, SGPstateData[[state]][["Baseline_splineMatrix"]])
}
} # END Get/Compute baseline coefficient matrices
#######################################################################################################################
## SIMEX Baseline SGP - compute matrices first if they are not in sgp_object
#######################################################################################################################
if (sgp.percentiles.baseline & !is.null(calculate.simex.baseline)) {
if (!is.null(sgp.config)) {
tmp.subjects <- unique(sapply(sgp.config, function(x) tail(x[["sgp.content.areas"]],1)))
} else {
if (!is.null(content_areas)) tmp.subjects <- content_areas else tmp.subjects <- unique(sgp_object@Data["VALID_CASE"], by="CONTENT_AREA")[["CONTENT_AREA"]]
}
### Calculate BASELINE SIMEX matrices if they are not present
if (!all(find.matrices <- paste0(tmp.subjects, ".BASELINE.SIMEX") %in% names(tmp_sgp_object[["Coefficient_Matrices"]]))) {
if (length(grep("BASELINE", names(sgp_object@SGP[["Coefficient_Matrices"]])))==0){
sgp_object@SGP[["Coefficient_Matrices"]] <- c(sgp_object@SGP[["Coefficient_Matrices"]], tmp_sgp_object[["Coefficient_Matrices"]][grep("BASELINE", names(tmp_sgp_object[["Coefficient_Matrices"]]))])
}
if (is.null(SGPstateData[[state]][["Baseline_splineMatrix"]])) {# Put in SGPstateData to bypass re-running baseline matrices (either already exist or calculated above)
SGPstateData[[state]][["Baseline_splineMatrix"]] <- tmp_sgp_object[["Coefficient_Matrices"]][grep("BASELINE", names(tmp_sgp_object[["Coefficient_Matrices"]]))]
}
if (is.null(sgp.baseline.config)) {
sgp.baseline.config <- getSGPBaselineConfig(sgp_object, content_areas=tmp.subjects, grades, sgp.baseline.panel.years, sgp.percentiles.baseline.max.order, calculate.simex.baseline)
} else {
sgp.baseline.config <- checkConfig(sgp.baseline.config, "Baseline")
}
sgp.baseline.config <- sgp.baseline.config[which(sapply(sgp.baseline.config, function(x) tail(x[["sgp.baseline.content.areas"]],1)) %in% tmp.subjects[!find.matrices])]
messageSGP("\n\tStarted SIMEX Baseline Coefficient Matrix Calculation:\n")
## Enforce that simex.use.my.coefficient.matrices must be FALSE for BASELINE SIMEX matrix production
calculate.simex.baseline[['simex.use.my.coefficient.matrices']] <- NULL
if (!is.null(parallel.config)) { ### PARALLEL BASELINE COEFFICIENT MATRIX CONSTRUCTION
par.start <- startParallel(parallel.config, 'BASELINE_MATRICES')
## FOREACH flavor
if (toupper(parallel.config[["BACKEND"]]) == "FOREACH") {
tmp <- foreach(sgp.iter=iter(sgp.baseline.config), .packages="SGP", .errorhandling = "pass", .inorder=FALSE,
.options.multicore=par.start$foreach.options, .options.mpi=par.start$foreach.options, .options.redis=par.start$foreach.options) %dopar% {
return(baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE,
calculate.simex.baseline=calculate.simex.baseline,
parallel.config=parallel.config,
panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.iter, sgp.data.names)))
}
if (any(tmp.tf <- sapply(tmp, function(x) any(class(x) %in% c("try-error", "simpleError"))))) {
tmp_sgp_object[['Error_Reports']] <- c(tmp_sgp_object[['Error_Reports']],
sgp.percentiles.baseline.=getErrorReports(tmp, tmp.tf, sgp.baseline.config[['sgp.percentiles.baseline']]))
}
tmp_sgp_object <- mergeSGP(Reduce(mergeSGP, tmp[!tmp.tf]), tmp_sgp_object)
} else { ## SNOW and MULTICORE flavors
if (par.start$par.type=="SNOW") {
tmp <- clusterApplyLB(par.start$internal.cl, sgp.baseline.config, function(sgp.iter) baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE,
calculate.simex.baseline=calculate.simex.baseline,
parallel.config=parallel.config,
panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.iter, sgp.data.names)))
tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp, simex=TRUE)))
} # END if (SNOW)
if (par.start$par.type=="MULTICORE") {
tmp <- mclapply(sgp.baseline.config, function(sgp.iter) baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE,
calculate.simex.baseline=calculate.simex.baseline,
parallel.config=parallel.config,
panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.iter, sgp.data.names)),
mc.cores=par.start$workers, mc.preschedule=FALSE)
tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp, simex=TRUE)))
} # END if (MULTICORE)
stopParallel(parallel.config, par.start)
} # END FOREACH, SNOW and MULTICORE
} else {
## SEQUENTIAL BASELINE COEFFICIENT MATRIX CONSTRUCTION
## Or, run SIMEX simulation iterations in parallel in studentGrowthPercentiles using lower.level.parallel.config
## Useful if many more cores/workers available than configs to iterate over.
tmp <- list()
for (sgp.iter in seq_along(sgp.baseline.config)) {
tmp[[sgp.iter]] <- baselineSGP(
sgp_object,
state=state,
sgp.baseline.config=sgp.baseline.config[sgp.iter], ## NOTE: must pass list, [...], not vector, [[...]].
return.matrices.only=TRUE,
calculate.baseline.sgps=FALSE,
calculate.simex.baseline=calculate.simex.baseline,
parallel.config=lower.level.parallel.config,
panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.baseline.config[[sgp.iter]], sgp.data.names))
}
tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp, simex=TRUE)))
}
### Save SIMEX BASELINE matrices
assign(paste0(state, "_SIMEX_Baseline_Matrices"), list())
for (tmp.matrix.label in grep("BASELINE.SIMEX", names(tmp_sgp_object$Coefficient_Matrices), value=TRUE)) {
eval(parse(text=paste0(state, "_SIMEX_Baseline_Matrices[['", tmp.matrix.label, "']] <- tmp_sgp_object[['Coefficient_Matrices']][['", tmp.matrix.label, "']]")))
}
save(list=paste0(state, "_SIMEX_Baseline_Matrices"), file=paste0(state, "_SIMEX_Baseline_Matrices.Rdata"), compress="xz")
messageSGP("\n\tFinished Calculating SIMEX Baseline Coefficient Matrices\n")
} # END Compute SIMEX baseline coefficient matrices
## Enforce that simex.use.my.coefficient.matrices must be TRUE and save.matrices is FALSE for BASELINE SIMEX calculations below
calculate.simex.baseline[['simex.use.my.coefficient.matrices']] <- TRUE
calculate.simex.baseline[['save.matrices']] <- FALSE
} # END check for SIMEX baseline matrices presence
######################################################################################################################
### Stratified Random Sample (SRS) SGP - calculate cohort and baseline referenced matrices first
#######################################################################################################################
# if (!is.null(calculate.srs) || !is.null(calculate.srs.baseline)) {
#
# if (!is.null(sgp.config)) {
# tmp.subjects <- unique(sapply(sgp.config, function(x) tail(x[["sgp.content.areas"]],1)))
# } else {
# if (!is.null(content_areas)) tmp.subjects <- content_areas else tmp.subjects <- unique(sgp_object@Data["VALID_CASE"], by="CONTENT_AREA")[["CONTENT_AREA"]]
# }
#
# ## Identify BASELINE SRS matrices if they are not present
# if (!all(find.srs.matrices <- paste0(tmp.subjects, ".BASELINE.SRS") %in% names(tmp_sgp_object[["Coefficient_Matrices"]]))) {
# if (length(grep("SRS", names(sgp_object@SGP[["Coefficient_Matrices"]])))==0){
# sgp_object@SGP[["Coefficient_Matrices"]] <- c(sgp_object@SGP[["Coefficient_Matrices"]], tmp_sgp_object[["Coefficient_Matrices"]][grep("SRS", names(tmp_sgp_object[["Coefficient_Matrices"]]))])
# }
#
# if (is.null(SGPstateData[[state]][["SRS_Baseline_splineMatrix"]])) {# Put in SGPstateData to bypass re-running SRS baseline matrices (either already exist or calculated above)
# SGPstateData[[state]][["SRS_Baseline_splineMatrix"]] <- tmp_sgp_object[["Coefficient_Matrices"]][grep("SRS", names(tmp_sgp_object[["Coefficient_Matrices"]]))]
# }
#
# if (is.null(sgp.baseline.srs.config)) {
# sgp.srs.baseline.config <- getSGPSRSBaselineConfig(sgp_object, content_areas=tmp.subjects, grades, sgp.srs.baseline.panel.years, sgp.percentiles.srs.baseline.max.order, calculate.simex.srs.baseline)
# } else {
# sgp.srs.baseline.config <- checkConfig(sgp.srs.baseline.config, "SRS_Baseline")
# }
#
#
#
#
# }
#
# ## Cohort referenced SRS matrices
# if (!is.null(calculate.srs)) { ### Create data and parameters (if not supplied) to calculate annual SRS SGPs
#
#
#
# } ### END calculate.srs
#
#
# ## Baseline referenced SRS martrices
# if (!is.null(calculate.srs.baseline) & !all(find.srs.matrices)) {
#
# if (length(grep("BASELINE", names(sgp_object@SGP[["Coefficient_Matrices"]])))==0){
#
# sgp_object@SGP[["Coefficient_Matrices"]] <- c(sgp_object@SGP[["Coefficient_Matrices"]], tmp_sgp_object[["Coefficient_Matrices"]][grep("BASELINE", names(tmp_sgp_object[["Coefficient_Matrices"]]))])
# }
#
# if (is.null(SGPstateData[[state]][["Baseline_splineMatrix"]])) {# Put in SGPstateData to bypass re-running baseline matrices (either already exist or calculated above)
# SGPstateData[[state]][["Baseline_splineMatrix"]] <- tmp_sgp_object[["Coefficient_Matrices"]][grep("BASELINE", names(tmp_sgp_object[["Coefficient_Matrices"]]))]
# }
#
# if (is.null(sgp.baseline.config)) {
# sgp.baseline.config <- getSGPBaselineConfig(sgp_object, content_areas=tmp.subjects, grades, sgp.baseline.panel.years, sgp.percentiles.baseline.max.order, calculate.simex.baseline)
# } else {
# sgp.baseline.config <- checkConfig(sgp.baseline.config, "Baseline")
# }
#
# sgp.baseline.config <- sgp.baseline.config[which(sapply(sgp.baseline.config, function(x) tail(x[["sgp.baseline.content.areas"]],1)) %in% tmp.subjects[!find.matrices])]
#
# messageSGP("\n\tStarted SIMEX Baseline Coefficient Matrix Calculation:\n")
#
# ## Enforce that simex.use.my.coefficient.matrices must be FALSE for BASELINE SIMEX matrix production
# calculate.simex.baseline[['simex.use.my.coefficient.matrices']] <- NULL
#
# if (!is.null(parallel.config)) { ### PARALLEL BASELINE COEFFICIENT MATRIX CONSTRUCTION
#
# par.start <- startParallel(parallel.config, 'BASELINE_MATRICES')
#
# ## FOREACH flavor
# if (toupper(parallel.config[["BACKEND"]]) == "FOREACH") {
# tmp <- foreach(sgp.iter=iter(sgp.baseline.config), .packages="SGP", .errorhandling = "pass", .inorder=FALSE,
# .options.multicore=par.start$foreach.options, .options.mpi=par.start$foreach.options, .options.redis=par.start$foreach.options) %dopar% {
# return(baselineSGP(
# sgp_object,
# state=state,
# sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
# return.matrices.only=TRUE,
# calculate.baseline.sgps=FALSE,
# calculate.simex.baseline=calculate.simex.baseline,
# parallel.config=parallel.config,
# panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.iter, sgp.data.names)))
# }
# if (any(tmp.tf <- sapply(tmp, function(x) any(class(x) %in% c("try-error", "simpleError"))))) {
# tmp_sgp_object[['Error_Reports']] <- c(tmp_sgp_object[['Error_Reports']],
# sgp.percentiles.baseline.=getErrorReports(tmp, tmp.tf, sgp.baseline.config[['sgp.percentiles.baseline']]))
# }
# tmp_sgp_object <- mergeSGP(Reduce(mergeSGP, tmp[!tmp.tf]), tmp_sgp_object)
# } else { ## SNOW and MULTICORE flavors
# if (par.start$par.type=="SNOW") {
# tmp <- clusterApplyLB(par.start$internal.cl, sgp.baseline.config, function(sgp.iter) baselineSGP(
# sgp_object,
# state=state,
# sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
# return.matrices.only=TRUE,
# calculate.baseline.sgps=FALSE,
# calculate.simex.baseline=calculate.simex.baseline,
# parallel.config=parallel.config,
# panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.iter, sgp.data.names)))
#
# tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp, simex=TRUE)))
# } # END if (SNOW)
#
# if (par.start$par.type=="MULTICORE") {
# tmp <- mclapply(sgp.baseline.config, function(sgp.iter) baselineSGP(
# sgp_object,
# state=state,
# sgp.baseline.config=list(sgp.iter), ## NOTE: list of sgp.iter must be passed for proper iteration
# return.matrices.only=TRUE,
# calculate.baseline.sgps=FALSE,
# calculate.simex.baseline=calculate.simex.baseline,
# parallel.config=parallel.config,
# panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.iter, sgp.data.names)),
# mc.cores=par.start$workers, mc.preschedule=FALSE)
#
# tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp, simex=TRUE)))
# } # END if (MULTICORE)
# stopParallel(parallel.config, par.start)
# } # END FOREACH, SNOW and MULTICORE
# } else {
# ## SEQUENTIAL BASELINE COEFFICIENT MATRIX CONSTRUCTION
# ## Or, run SIMEX simulation iterations in parallel in studentGrowthPercentiles using lower.level.parallel.config
# ## Useful if many more cores/workers available than configs to iterate over.
# tmp <- list()
# for (sgp.iter in seq_along(sgp.baseline.config)) {
# tmp[[sgp.iter]] <- baselineSGP(
# sgp_object,
# state=state,
# sgp.baseline.config=sgp.baseline.config[sgp.iter], ## NOTE: must pass list, [...], not vector, [[...]].
# return.matrices.only=TRUE,
# calculate.baseline.sgps=FALSE,
# calculate.simex.baseline=calculate.simex.baseline,
# parallel.config=lower.level.parallel.config,
# panel.data.vnames=getPanelDataVnames("baseline.sgp", sgp.baseline.config[[sgp.iter]], sgp.data.names))
# }
# tmp_sgp_object <- mergeSGP(tmp_sgp_object, list(Coefficient_Matrices=merge.coefficient.matrices(tmp, simex=TRUE)))
# }
#
# ### Save SIMEX BASELINE matrices
# assign(paste0(state, "_SIMEX_Baseline_Matrices"), list())
# for (tmp.matrix.label in grep("BASELINE.SIMEX", names(tmp_sgp_object$Coefficient_Matrices), value=TRUE)) {
# eval(parse(text=paste0(state, "_SIMEX_Baseline_Matrices[['", tmp.matrix.label, "']] <- tmp_sgp_object[['Coefficient_Matrices']][['", tmp.matrix.label, "']]")))
# }
# save(list=paste0(state, "_SRS_Baseline_Matrices"), file=paste0(state, "_SRS_Baseline_Matrices.Rdata"), compress="xz")
#
# messageSGP("\n\tFinished Calculating SRS Baseline Coefficient Matrices\n")
#
# ## Enforce that simex.use.my.coefficient.matrices must be TRUE and save.matrices is FALSE for BASELINE SIMEX calculations below
# calculate.simex.baseline[['simex.use.my.coefficient.matrices']] <- TRUE
# calculate.simex.baseline[['save.matrices']] <- FALSE
#
# } ### END calculate.srs.baseline
# } # END check for SRS analyses
#######################################################################################################################
### Create par.sgp.config (for both parallel and sequential implementations) and par.sgp.config.projections for projections
#######################################################################################################################
setkeyv(sgp_object@Data, getKey(sgp_object))
par.sgp.config <- getSGPConfig(sgp_object, state, tmp_sgp_object, content_areas, years, grades, sgp.config, trim.sgp.config, sgp.percentiles, sgp.projections, sgp.projections.lagged,
sgp.percentiles.baseline, sgp.projections.baseline, sgp.projections.lagged.baseline, sgp.config.drop.nonsequential.grade.progression.variables,
sgp.minimum.default.panel.years, sgp.projections.max.forward.progression.years, sgp.use.my.coefficient.matrices, calculate.simex, calculate.simex.baseline, year.for.equate,
sgp.percentiles.equated, SGPt)
if (sgp.projections & length(par.sgp.config[['sgp.projections']])==0) {
messageSGP("\tNOTE: No configurations are present for cohort referenced projections. No cohort referenced projections will be calculated.\n")
sgp.projections <- FALSE
}
if (sgp.projections.lagged & length(par.sgp.config[['sgp.projections.lagged']])==0) {
messageSGP("\tNOTE: No configurations are present for cohort referenced lagged projections. No lagged cohort referenced projections will be calculated.\n")
sgp.projections.lagged <- FALSE
}
if (sgp.projections.baseline & length(par.sgp.config[['sgp.projections.baseline']])==0) {
messageSGP("\tNOTE: No configurations are present for baseline projections. No baseline projections will be calculated.\n")
sgp.projections.baseline <- sgp.projections.lagged.baseline <- FALSE
}
if (sgp.projections.lagged.baseline & length(par.sgp.config[['sgp.projections.lagged.baseline']])==0) {
messageSGP("\tNOTE: No configurations are present for lagged baseline projections. No lagged baseline projections will be calculated.\n")
sgp.projections.baseline <- sgp.projections.lagged.baseline <- FALSE
}
if (sgp.percentiles) {
if (!is.null(tmp.transition.year <- SGPstateData[[state]][["Assessment_Program_Information"]][["Assessment_Transition"]][["Year"]]) &&
sort(unique(unlist(sapply(par.sgp.config[['sgp.percentiles']], function(x) x[['sgp.panel.years']]))))[1L] < tmp.transition.year) {
messageSGP(paste0("\tNOTE: Configurations include years prior to assessment transition (", tmp.transition.year, ").\n\t\tOutput will include SGPs of all orders to accomodate investigations.\n"))
print.other.gp <- TRUE
}
}
### Produce cohort data information
if (get.cohort.data.info) {
if (!sgp.percentiles & sgp.percentiles.baseline) tmp.label <- 'sgp.percentiles.baseline' else tmp.label <- 'sgp.percentiles'
cohort_data_info <- getCohortDataInfo(tmp_sgp_data_for_analysis, par.sgp.config[[tmp.label]])
save(cohort_data_info, file=file.path("Logs", "cohort_data_info.Rdata"))
messageSGP("\tNOTE: Cohort data information saved to 'Logs/cohort_data_info.Rdata'.")
}
#######################################################################################################################
#######################################################################################################################
### Percentiles, Equated Percentiles, Baseline Percentiles, Projections, Lagged Projections - PARALLEL FLAVORS FIRST
#######################################################################################################################
#######################################################################################################################
if (!is.null(parallel.config)) {
##################################
### PERCENTILES
##################################