Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Biostats-Practicum/MAR Data Set Generation.R
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
134 lines (79 sloc)
2.9 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
roll_Die <- function(input_prob) { | |
value <- rbinom(n=1,size=1,prob=input_prob) | |
if (value == 1){ | |
print(TRUE) | |
return(TRUE) | |
} | |
return(FALSE) | |
} | |
baseline_Check <- function(baseline, visit_Value) { | |
if (visit_Value > baseline * 0.9) { | |
case_prob = 0.3 | |
} else { | |
case_prob = 0.05 | |
} | |
return(case_prob) | |
} | |
clear_Values <- function(Subject_ID, visit_Number_Missing) { | |
#compile columns (variables) to clear | |
cols <- c() | |
for (variable in c("AVAL", "CHG", "PCHG", "PCHGCAT1", | |
"PCHGCA1N", "PCHGCAT2", "PCHGCA2N" )){ | |
col <- which(colnames(Short_DataSet) == variable) | |
cols <- append(cols, col, after = length(cols)) | |
} | |
#Compile rows (subjects/visits) to clear | |
rows <- c() | |
for (visit_Num in visit_Number_Missing:6) { | |
row <- which(Short_DataSet$SUBJID == Subject_ID & Short_DataSet$AVISITN == visit_Num ) | |
rows <- append(rows, row, after = length(rows)) | |
} | |
#Set corresponding values to NA | |
for(r in rows){ | |
for (c in cols){ | |
Short_DataSet[[c]][r] <- NA | |
} | |
} | |
Short_DataSet <<- Short_DataSet | |
} | |
generate_New_Test_Data <- function() { | |
DataSet <- data.frame(read.csv("ADPA + SEX.csv")) | |
# Trim bulk of data to simplify initial coding | |
Subset_Index <<- sample(1:1831, 10, replace = FALSE) | |
Subset_Index <- sort(Subset_Index) | |
Short_DataSet <<- DataSet[DataSet$SUBJID %in% c(Subset_Index), ] | |
return() | |
} | |
load_Full_Data <- function() { | |
DataSet <- data.frame(read.csv("ADPA + SEX.csv")) | |
Subset_Index <<- c(1:1831) | |
Short_DataSet <<- DataSet[DataSet$SUBJID %in% c(Subset_Index), ] | |
return() | |
} | |
main <- function() { | |
#generate_New_Test_Data() | |
load_Full_Data() | |
for (subjectID in Subset_Index){ | |
baseline <- Short_DataSet$AVAL[which(Short_DataSet$SUBJID == subjectID & Short_DataSet$ABLFL == "Y" & Short_DataSet$PARAMCD =="PASI" )] | |
baseline <- as.integer(baseline) | |
for (visit in 4:6) { | |
visit_Value <- Short_DataSet$AVAL[which(Short_DataSet$SUBJID == subjectID & Short_DataSet$AVISITN == visit - 1 & Short_DataSet$PARAMCD =="PASI" )] | |
prob <- baseline_Check(baseline, visit_Value) | |
#print(subjectID) | |
#print(visit) | |
#print(prob) | |
missing <- roll_Die(prob) | |
#print(missing) | |
if (missing) { | |
clear_Values(subjectID, visit) | |
break | |
} | |
} | |
} | |
} | |
############################################################## | |
main() | |
write.csv(Short_DataSet,"C:\\Users\\Charles Watt\\Desktop\\UConn Biostats\\Biostats Practicum\\MAR_Data_V1.csv", row.names = FALSE) | |
#Hard-coded testing commands | |
#Short_DataSet <- clear_Values(167, 6) | |
#baseline_Check(35, 31) | |