history/fake_data_gen.R

library(dplyr)
library(mvtnorm)
library(purrr)
library(tidyr)
source('R/transformation.R')

trend = data.frame(TreatmentNew = c('Dose 1', 'Dose 2', 'Dose 3', 'Negative Control', 'Vehicle','Wild Type'),
                   Trend = c(1.2, 0.75, 0.5, 2, 1.6,0))
group = rep(c('Dose 1','Dose 2','Dose 3', 'Vehicle', 'Negative Control', 'Wild Type'),5) %>%
  sort()
type = c(rep('Non-Wild Type', 25), rep('Wild Type', 5))
basic_model = c(rep(TRUE, 20), rep(FALSE, 10))
Dose = c(rep(100,5),rep(200,5),rep(300,5), rep(NA, 15))
SubjectID = 1:30
data = data.frame(Type = type, TreatmentNew = group, SubjectID = SubjectID, Dose = Dose,
                  basic_model = basic_model) %>%
  rowwise() %>%
  mutate(Baseline = rnorm(1)) %>%
  inner_join(.,trend) %>%
  data.frame()

times = 7
phi = 0.75
sigma = diag(times)
for(i in 1:times){
  for(j in 1:times){
    sigma[i,j] = phi^abs(i-j)
  }
}


data_final = map_dfr(.x = 1:nrow(data), .f = ~{
  response = rmvnorm(n = 2, mean = data$Trend[.x]*seq(1,3,1/3),sigma = sigma) %>%
    data.frame()
  colnames(response) = paste('Day', 1:7)
  cbind.data.frame(data[.x,], replicate = 1:2, response)
})


data_final_long = data_final %>%
  pivot_longer(cols = grep('Day', colnames(.)), names_to = 'Time', values_to = 'Response') %>%
  rename('Technical Replicate ID' = replicate) %>%
  select(Type, TreatmentNew, SubjectID, `Technical Replicate ID` , Dose, Baseline,
         grep('Day', colnames(.),value = TRUE), basic_model, Time, Response) %>%
  mutate(TypeNew = Type,
         Treatment = TreatmentNew)


#debug
pre_modeling(data_final_long,baseline = FALSE)
transformation_check(data_final_long)
data_final_long = data_final %>%
  pivot_longer(cols = grep('Day', colnames(.)), names_to = 'Time', values_to = 'Response')

data_final = data_final %>%
  rename('Technical Replicate ID' = replicate,
         'Treatment Group Name' = TreatmentNew) %>%
  select(Type, `Treatment Group Name`, SubjectID, `Technical Replicate ID` , Dose, Baseline,
         grep('Day', colnames(.),value = TRUE))



write.csv('../../Desktop/tmp.csv', x = data_final, row.names = FALSE)
fdrennan/test documentation built on April 23, 2022, 12:37 a.m.