@@ -120,25 +120,13 @@ function(
120120 sgp.target.scale.scores.merge <- SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[" sgp.target.scale.scores.merge" ]]
121121 }
122122
123- # ## Check return.target.num.years
123+ # ## Check return.sgp. target.num.years
124124
125125 if (! is.null(SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[" return.sgp.target.num.years" ]])) {
126126 return .sgp.target.num.years <- SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[" return.sgp.target.num.years" ]]
127- } else return .sgp.target.num.years <- FALSE
128-
129- # ## Check whether to calculate current year lagged targets
130- if (1 %in% max.sgp.target.years.forward || identical(SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[' current.year.lagged.target' ]], TRUE )) {
131- current.year.lagged.target <- TRUE
132- } else current.year.lagged.target <- FALSE
133-
134- # # Odd things happened (e.g. in WIDA_CO) when max.sgp.targe.years.forward = 1 (length 1 only)
135- if (identical(SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[' current.year.lagged.target' ]], FALSE )) {
136- current.year.lagged.target <- FALSE
137- }
138-
127+ } else return .sgp.target.num.years <- FALSE
139128
140129 # ## Utility functions
141-
142130 get.target.arguments <- function (system.type , target.type = NULL , projection.unit.label , year.for.equate ) {
143131 tmp.list <- list ()
144132 if (is.null(system.type )) {
@@ -216,6 +204,14 @@ function(
216204 return (tmp.list )
217205 } # ## END get.target.arguments
218206
207+ getInitialStatusNames <- function (target.type.iter ) {
208+ if (target.type.iter == " sgp.projections" ) tmp.names <- c(" CATCH_UP_KEEP_UP_STATUS_INITIAL_CURRENT" , " MOVE_UP_STAY_UP_STATUS_INITIAL_CURRENT" )
209+ if (target.type.iter == " sgp.projections.baseline" ) tmp.names <- c(" CATCH_UP_KEEP_UP_STATUS_INITIAL_CURRENT_BASELINE" , " MOVE_UP_STAY_UP_STATUS_INITIAL_CURRENT_BASELINE" )
210+ if (target.type.iter == " sgp.projections.lagged" ) tmp.names <- c(" CATCH_UP_KEEP_UP_STATUS_INITIAL" , " MOVE_UP_STAY_UP_STATUS_INITIAL" )
211+ if (target.type.iter == " sgp.projections.lagged.baseline" ) tmp.names <- c(" CATCH_UP_KEEP_UP_STATUS_INITIAL_BASELINE" , " MOVE_UP_STAY_UP_STATUS_INITIAL_BASELINE" )
212+ return (tmp.names )
213+ }
214+
219215 catch_keep_move_functions <- c(min , max )
220216
221217 getTargetData <- function (tmp.target.data , projection_group.iter , tmp.target.level.names ) {
@@ -226,7 +222,6 @@ function(
226222 na.omit(tmp.data , cols = grep(" MOVE_UP_STAY_UP" , tmp.target.level.names , invert = TRUE , value = TRUE ))
227223 }
228224
229-
230225 # ###########################################################################
231226 # ## Check update.all.years
232227 # ###########################################################################
@@ -486,7 +481,7 @@ function(
486481 invisible (slot.data [, paste0(" SCALE_SCORE_PRIOR_" , tmp.prior - 1L ) : = as.numeric(sapply(tmp.split , function (x ) rev(x )[tmp.prior ]))])
487482 }}}
488483
489- tmp.data <- getTargetSGP(sgp_object , slot.data , content_areas , state , years , target.type.iter , target.level.iter , current.year.lagged.target , max.sgp.target.years.forward , fix.duplicates = fix.duplicates , return .sgp.target.num.years = return .sgp.target.num.years )
484+ tmp.data <- getTargetSGP(sgp_object , slot.data , content_areas , state , years , target.type.iter , target.level.iter , max.sgp.target.years.forward , fix.duplicates = fix.duplicates , return .sgp.target.num.years = TRUE )
490485
491486 if (dim(tmp.data )[1 ] > 0 ) {
492487 if (! is.null(fix.duplicates )) dup.by <- c(key(tmp.data ), grep(" SCALE_SCORE$|SCALE_SCORE_PRIOR" , names(tmp.data ), value = TRUE )) else dup.by <- key(tmp.data )
@@ -542,7 +537,8 @@ function(
542537 }
543538
544539 # ## SGP_TARGET_CONTENT_AREA calculation
545- terminal.content_areas <- unique(slot.data [! slot.data [,all(is.na(.SD )), .SDcols = grep(" SGP_TARGET" , grep(paste(max(max.sgp.target.years.forward ), " YEAR" , sep = " _" ), names(slot.data ), value = TRUE ), value = TRUE ), by = seq_len(nrow(slot.data ))][[' V1' ]]][[' CONTENT_AREA' ]])
540+ tmp.cols.to.test <- grep(" SGP_TARGET" , grep(paste(max(max.sgp.target.years.forward ), " YEAR" , sep = " _" ), names(slot.data ), value = TRUE ), value = TRUE )
541+ terminal.content_areas <- unique(slot.data [slot.data [, rowSums(! is.na(.SD )) > 0 , .SDcols = tmp.cols.to.test ]][[' CONTENT_AREA' ]])
546542 if (! is.null(SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[" content_area.projection.sequence" ]])) {
547543 terminal.content_areas <- intersect(terminal.content_areas , sapply(SGP :: SGPstateData [[state ]][[" SGP_Configuration" ]][[" content_area.projection.sequence" ]], tail , 1 ))
548544 }
@@ -605,7 +601,6 @@ function(
605601 }
606602 }
607603
608-
609604 # ## MOVE_UP_STAY_UP_STATUS Calculation
610605
611606 if (" MOVE_UP_STAY_UP" %in% target.args [[' target.level' ]] & (sgp.projections.lagged | sgp.projections.lagged.baseline ) & " MOVE_UP_STAY_UP_STATUS_INITIAL" %in% names(slot.data )) {
@@ -674,7 +669,7 @@ function(
674669 for (target.type.iter in target.args [[' sgp.target.scale.scores.types' ]]) {
675670 for (target.level.iter in target.args [[' target.level' ]]) {
676671 tmp.target.list [[paste(target.type.iter , target.level.iter )]] <-
677- data.table(getTargetSGP(sgp_object , slot.data , content_areas , state , years , target.type.iter , target.level.iter , current.year.lagged.target , max.sgp.target.years.forward , return .lagged.status = FALSE , fix.duplicates = fix.duplicates , return .sgp.target.num.years = TRUE ),
672+ data.table(getTargetSGP(sgp_object , slot.data , content_areas , state , years , target.type.iter , target.level.iter , max.sgp.target.years.forward , return .lagged.status = FALSE , fix.duplicates = fix.duplicates , return .sgp.target.num.years = TRUE , return .sgp.target.num.years.note = FALSE ),
678673 key = c(getKey(sgp_object ), " SGP_PROJECTION_GROUP" ))
679674 }
680675 }
@@ -690,19 +685,15 @@ function(
690685
691686 for (projection_group.iter in unique(tmp.target.data [[' SGP_PROJECTION_GROUP' ]])) {
692687 for (target.type.iter in target.args [[' sgp.target.scale.scores.types' ]]) {
693- if (target.type.iter %in% c(" sgp.projections.lagged" , " sgp.projections.lagged.baseline" )) {
694- max.sgp.target.years.forward.tmp <- max.sgp.target.years.forward + 1L
695- if (current.year.lagged.target ) max.sgp.target.years.forward.tmp <- c(1 , max.sgp.target.years.forward.tmp )
696- max.sgp.target.years.forward.tmp <- max.sgp.target.years.forward.tmp - 1L
697- } else max.sgp.target.years.forward.tmp <- max.sgp.target.years.forward
698- for (target.years.iter in max.sgp.target.years.forward.tmp ) {
688+ for (target.years.iter in max.sgp.target.years.forward ) {
699689 tmp.target.level.names <- as.character(sapply(target.args [[' target.level' ]], function (x ) getTargetName(state , target.type.iter , x , target.years.iter , " SGP_TARGET" , projection.unit.label , projection_group.iter )))
700690 if (any(! tmp.target.level.names %in% names(tmp.target.data ))) {
701691 tmp.target.data [,tmp.target.level.names [! tmp.target.level.names %in% names(tmp.target.data )]: = as.integer(NA )]
702692 }
703- tmp.target.level.names.years.to.target <- paste(tmp.target.level.names , " NUM_YEARS_TO_TARGET" , sep = " _" )
704693
705- targetData <- getTargetData(tmp.target.data , projection_group.iter , c(tmp.target.level.names , tmp.target.level.names.years.to.target ))
694+ tmp.target.level.names.years.to.target <- paste(tmp.target.level.names , " NUM_YEARS_TO_TARGET" , sep = " _" )
695+ tmp.initial.status.names <- getInitialStatusNames(target.type.iter )
696+ targetData <- getTargetData(tmp.target.data , projection_group.iter , c(tmp.target.level.names , tmp.target.level.names.years.to.target , tmp.initial.status.names ))
706697
707698 if (dim(targetData )[1 ] > 0 ) {
708699 sgp_object <- getTargetScaleScore(
@@ -722,10 +713,12 @@ function(
722713 }
723714 }
724715 }
725- }
716+ } # # END projection.group.iter
717+
726718 if (length(max.sgp.target.years.forward ) > 1 ) {
727- for (names.iter in grep(" TARGET_SCALE_SCORES" , names(sgp_object @ SGP $ SGProjections ), value = TRUE )) {
728- sgp_object @ SGP $ SGProjections [[names.iter ]] <- sgp_object @ SGP $ SGProjections [[names.iter ]][,lapply(.SD , mean_nan ), by = c(" ID" , " GRADE" , " SGP_PROJECTION_GROUP" , " SGP_PROJECTION_GROUP_SCALE_SCORES" )]
719+ for (names.iter in getTargetScaleScoreTableNames(names(sgp_object @ SGP [[' SGProjections' ]]), years )) {
720+ sgp_object @ SGP [[' SGProjections' ]][[names.iter ]] <- sgp_object @ SGP [[' SGProjections' ]][[names.iter ]][,lapply(.SD , mean , na.rm = TRUE ), by = c(" ID" , " GRADE" , " SGP_PROJECTION_GROUP" , " SGP_PROJECTION_GROUP_SCALE_SCORES" )] # nolint
721+ sgp_object @ SGP [[' SGProjections' ]][[names.iter ]] <- sgp_object @ SGP [[' SGProjections' ]][[names.iter ]][,lapply(.SD , function (x ) ifelse(is.nan(x ), NA , x ))]
729722 }
730723 }
731724 if (! identical(sgp.target.scale.scores.merge , FALSE )) {
@@ -745,8 +738,3 @@ function(
745738
746739 return (sgp_object )
747740} # # END combineSGP Function
748-
749- `mean_nan` <-
750- function (x ) {
751- if (all(is.na(x ))) return (as.numeric(NA )) else return (mean(x , na.rm = TRUE ))
752- } # ## END mean_nan function
0 commit comments