R/utils.createData.R

Defines functions create_data

create_data <- function(visit_total = 4,
                        round_digits=4,
                        total_n = 100){

  knife_cat = c("Chef","Pocket","Hunting")
  knife_length_mean = c(20,10,30)
  knife_length_sd = knife_length_mean*0.1
  names(knife_length_mean) <- knife_cat
  names(knife_length_sd) <- knife_cat

  knife_prob <-c(runif(1,min=0.5,max = 0.7),runif(1,min=0.2,max = 0.25))
  knife_prob <- c(knife_prob, 1-sum(knife_prob))
  names(knife_prob) <- knife_cat

  DATA <- data.frame(ID=1:total_n)
  DATA$type <- factor(sample(knife_cat,total_n, replace = T, prob = knife_prob))
  DATA$length <- NA
  DATA$carbon_prop <- NA
  dureza_base <- sample(1:100,1)
  DATA$hardness_V0 <- NA
  for(t in levels(DATA$type)) {
    sel.knifes <- DATA$type == t
    DATA$length[sel.knifes] <- round(rnorm(sum(sel.knifes),
                                           mean=knife_length_mean[t],
                                           sd=knife_length_sd[t]), digits = round_digits)
    DATA$carbon_prop[sel.knifes] <- switch(t,
                                           "Chef" = runif(sum(sel.knifes),min=0.1,max = 0.2),
                                           "Pocketa"= runif(sum(sel.knifes),min=0.05,max = 0.1),
                                           "Huntinga"=runif(sum(sel.knifes),min=0.25,max = 0.35),
                                           runif(sum(sel.knifes))
    )
    DATA$hardness_V0 <- DATA$carbon_prop*dureza_base

  }




  cont = 0
  varying_vars <- "hardness_V0"
  for(v in 1:visit_total){
    hardness_var <- paste0("hardness_V",v)
    hardness_var_prev <- paste0("hardness_V",v-1)
    DATA[,hardness_var] <- NA
    for(t in levels(DATA$type)){
      DATA[DATA$type == t,hardness_var] <-
        DATA[DATA$type == t,hardness_var_prev]*runif(1,min=.988,max = 1.02)
    }
    varying_vars <- c(varying_vars,hardness_var)
    cont = cont + 1
  }

  DATA_LONG <- reshape(DATA, direction = "long",
                       varying = varying_vars,
                       v.names = "hardness",
                       timevar = "QUELCH",
                       times=0:visit_total,
                       idvar="ID")

  return(list(DATA = DATA,
              DATA_LONG = DATA_LONG
  ))
}
feranpre/feR documentation built on Nov. 22, 2022, 2:29 a.m.