Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement carried-forward string handling option 4 #70

Open
wants to merge 25 commits into
base: upd-v3
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1a9781a
Figure out closest includes for carried-forward
delosh653 Sep 30, 2021
fd01462
Add option 4 (sweep strategy for carried forwards)
delosh653 Sep 30, 2021
fd81c2e
Update documentation
delosh653 Sep 30, 2021
c4b7b4b
Run check/update documentation
delosh653 Sep 30, 2021
cb18bc8
Add original ordering to acf results
delosh653 Sep 30, 2021
b2e81d3
Merge branch 'main' into acf-opt-4
delosh653 Oct 1, 2021
6017210
Update documentation for option 4
delosh653 Oct 1, 2021
8047fec
Fix small bug on option 3
delosh653 Oct 1, 2021
03e3951
Fix bug for no include after string of CFs
delosh653 Oct 13, 2021
dabc377
Add option 5 (use sweep strategy of 4 with acf-answers)
delosh653 Oct 23, 2021
66fceb5
Simplified min_ht of "definitely keep" criteria
delosh653 Nov 1, 2021
ac27719
Round height difference down to 0 if <= .05
delosh653 Nov 1, 2021
c72774c
Filter out observations for ages less than two years old
delosh653 Nov 1, 2021
4f8fc3a
Fix filtering bug
delosh653 Nov 1, 2021
cc7d394
Update tanner height velocity 2sd files
delosh653 Nov 4, 2021
3b56f96
Fix when misordered data is entered
delosh653 Nov 4, 2021
0735f40
Update criteria for absolution inclusion in option 5
delosh653 Nov 10, 2021
49be927
Integrate adjustcarryforward into cleangrowth
delosh653 Nov 15, 2021
739e625
Integrate adjustcarryforward into pediatric algorithm
delosh653 Nov 15, 2021
55747cc
Fix warnings related to unbound variables
delosh653 Nov 15, 2021
e744ebe
Add debugging for adjustcarryforward
delosh653 Dec 3, 2021
3a06a40
Fix bug with filtering out <= 2 years old
delosh653 Dec 3, 2021
565e867
Fix debugging for adjustcarryforward
delosh653 Dec 3, 2021
2bd8dd7
Update documentation
delosh653 Dec 3, 2021
c56ccd3
Add two new rules for maxdiff next ht inclusion criteria
delosh653 Jun 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix when misordered data is entered
  • Loading branch information
delosh653 committed Nov 4, 2021
commit 3b56f969bdce02951d850d7810fa70c0296123c3
47 changes: 25 additions & 22 deletions R/adjustcarryforward.R
Original file line number Diff line number Diff line change
Expand Up @@ -1166,10 +1166,13 @@ adjustcarryforward <- function(subjid,
)

### ADJUSTCF EDIT
data.all <- data.all[, n := 1:.N]
data.all <- data.all[, orig.n := 1:.N]

# also order by subject, then agedays
data.all <- data.all[order(subjid, agedays),]

# add index for sorted order
data.all <- data.all[, n := 1:.N]
### ENDEDIT

data.orig <- data.all
Expand Down Expand Up @@ -1253,16 +1256,16 @@ adjustcarryforward <- function(subjid,
sub.df <- data.all[subjid == subjid[x],]

# find the index corresponding to the given subject
idx <- which(sub.df$line == data.all$line[x])
idx <- which(sub.df$n == data.all$n[x])

# now find the closest include before (there will always be one before)
incl_bef <-
tail(sub.df[1:(idx-1),][orig.exclude == "Include", line], n = 1)
tail(sub.df[1:(idx-1),][orig.exclude == "Include", n], n = 1)
# find the closest include after
incl_aft <-
if (idx+1 <= nrow(sub.df)){
head(
sub.df[(idx+1):nrow(sub.df),][orig.exclude == "Include", line],
sub.df[(idx+1):nrow(sub.df),][orig.exclude == "Include", n],
n = 1
)
} else {
Expand All @@ -1276,7 +1279,7 @@ adjustcarryforward <- function(subjid,
# if it's in a string, the one before it will be carried forward
str.position <-
if (sub.df[idx - 1, orig.exclude == "Exclude-Carried-Forward"]){
idx - which(sub.df$line == incl_bef)
idx - which(sub.df$n == incl_bef)
} else {
# otherwise it will be an include, so it's in the first position
1
Expand Down Expand Up @@ -1951,9 +1954,9 @@ adjustcarryforward <- function(subjid,
# subset to the carried forward at that position, and the include
# before and after
df <- subj.df[c(
which(line == incl.bef[idx]), idx,
which(n == incl.bef[idx]), idx,
# there may not be an after
if (!is.na(incl.aft[idx])){which(line == incl.aft[idx])} else {c()}
if (!is.na(incl.aft[idx])){which(n == incl.aft[idx])} else {c()}
),]

# get all the CFs at position one and evaluate them
Expand Down Expand Up @@ -1982,23 +1985,23 @@ adjustcarryforward <- function(subjid,
# if the last one is empty (no include after), we make it the last
# one plus one
last_incl <- if (is.na(subj.df$incl.aft[idx])){
subj.df$line[nrow(subj.df)]+1
subj.df$n[nrow(subj.df)]+1
} else {
subj.df$incl.aft[idx]
}

excl_vect[ subj.df$line %in%
c(subj.df$line[idx]:(last_incl-1))
excl_vect[ subj.df$n %in%
c(subj.df$n[idx]:(last_incl-1))
] <- as.character(eval_df$temp.exclude[2])
}
}

return(excl_vect)
})(copy(.SD)), by = .(subjid), .SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"line", 'orig.exclude',"index", "incl.bef", "incl.aft", "str.position")]
})(copy(.SD)), by = .(subjid), .SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"n", 'orig.exclude',"index", "incl.bef", "incl.aft", "str.position")]

# save the results for the current subjects
# NOTE: probably a better way to join these
orig.all_df[all_df, on = "line", exclude := i.exclude]
orig.all_df[all_df, on = "n", exclude := i.exclude]

# remove all subjects that don't have any at the next position with a
# nonexclusion code
Expand All @@ -2018,7 +2021,7 @@ adjustcarryforward <- function(subjid,
}

return(orig.all_df$exclude)
})(copy(.SD)),.SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"line", 'orig.exclude', "incl.bef", "incl.aft", "str.position")]
})(copy(.SD)),.SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"n", 'orig.exclude', "incl.bef", "incl.aft", "str.position")]

} else if (exclude_opt == 5){
# OPTION 5 (acf_answers) USES A SWEEP STRATEGY ----
Expand Down Expand Up @@ -2053,9 +2056,9 @@ adjustcarryforward <- function(subjid,
# subset to the carried forward at that position, and the include
# before and after
df <- subj.df[c(
which(line == incl.bef[idx]), idx,
which(n == incl.bef[idx]), idx,
# there may not be an after
if (!is.na(incl.aft[idx])){which(line == incl.aft[idx])} else {c()}
if (!is.na(incl.aft[idx])){which(n == incl.aft[idx])} else {c()}
),]

# calculate if the carried forward should definitely be included
Expand All @@ -2076,23 +2079,23 @@ adjustcarryforward <- function(subjid,
# if the last one is empty (no include after), we make it the last
# one plus one
last_incl <- if (is.na(subj.df$incl.aft[idx])){
subj.df$line[nrow(subj.df)]+1
subj.df$n[nrow(subj.df)]+1
} else {
subj.df$incl.aft[idx]
}

excl_vect[ subj.df$line %in%
c(subj.df$line[idx]:(last_incl-1))
excl_vect[ subj.df$n %in%
c(subj.df$n[idx]:(last_incl-1))
] <- as.character(def_incl[2])
}
}

return(excl_vect)
})(copy(.SD)), by = .(subjid), .SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"line", 'orig.exclude',"index", "incl.bef", "incl.aft", "str.position")]
})(copy(.SD)), by = .(subjid), .SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"n", 'orig.exclude',"index", "incl.bef", "incl.aft", "str.position")]

# save the results for the current subjects
# NOTE: probably a better way to join these
orig.all_df[all_df, on = "line", exclude := i.exclude]
orig.all_df[all_df, on = "n", exclude := i.exclude]

# remove all subjects that don't have any at the next position with a
# nonexclusion code
Expand All @@ -2113,7 +2116,7 @@ adjustcarryforward <- function(subjid,
}

return(orig.all_df$exclude)
})(copy(.SD)),.SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"line", 'orig.exclude', "incl.bef", "incl.aft", "str.position")]
})(copy(.SD)),.SDcols = c("subjid", 'sex', 'agedays', 'v', 'tbc.sd', 'exclude',"n", 'orig.exclude', "incl.bef", "incl.aft", "str.position")]
}


Expand Down Expand Up @@ -2141,7 +2144,7 @@ adjustcarryforward <- function(subjid,
# }

# formulating results and options
acf_df <- data.frame(n = data.all$n)
acf_df <- data.frame(n = data.all$orig.n)
acf_df <- cbind(acf_df,
data.all[, exclude])
colnames(acf_df)[-1] <- "adjustcarryforward"
Expand Down