lebebr01 / simglm

Simulate regression models
https://simglm.brandonlebeau.org/
Other
43 stars 12 forks source link

de-bugging help #76

Closed avannucci closed 3 years ago

avannucci commented 4 years ago

I get this error when trying to run the replicate_simulation() function.

Error in factor_names(sim_args, fixed_vars) : 'list' object cannot be coerced to type 'double'

Here is my code:

sim_arguments <- list( formula = y ~ 1 + time + valence + time:valence + (1 + time | individual), reg_weights = c(0.5, 0.1, 0.1, 0.05), error = list(variance = 0.3), fixed = list(time = list(var_type = 'time'), valence = list(var_type = 'factor', levels = c('neutral', 'negative', 'positive'), var_level = 1)), randomeffect = list(int_individual = list(variance = 0.2, var_level = 2), time_individual = list(variance = 0.2, var_level = 2)), replications = 1000, model_fit = list(formula = y ~ 1 + time + valence + time:valence + (1 + time | individual), model_function = 'lmer'), extract_coefficients = TRUE, vary_arguments = list( sample_size = list(list(level1 = 50, level2 = 25), list(level1 = 50, level2 = 50), list(level1 = 50, level2 = 75), list(level1 = 50, level2 = 100), list(level1 = 75, level2 = 25), list(level1 = 75, level2 = 50), list(level1 = 75, level2 = 75), list(level1 = 75, level2 = 100)) ) )

Here is where I get an error. I have tried converting list elements to double or numeric, but that has not worked. long_model_results <- replicate_simulation(sim_arguments)

lebebr01 commented 3 years ago

@avannucci Following up on this from last year, the latest version on GH should fix this issue. There is one change to the code that is needed, specifically the addition of more terms to the reg_weights argument to coincide with the dummy coding of the factor attribute. For these, they need to have the number of categories - 1 number of terms specified, therefore, you need to specify two terms for the term "valence" and another for the "time:valence" interaction. These terms would represent the mean differences between the neutral and negative and neutral and positive for the intercept and time slope respectively. Below is the updated code to generate the data one time:

sim_arguments <- list(
  formula = y ~ 1 + time + valence + time:valence + (1 + time | individual),
  reg_weights = c(0.5, 0.1, 0.1, 0.05, 0.02, 0),
  error = list(variance = 0.3),
  fixed = list(time = list(var_type = 'time'),
               valence = list(var_type = 'factor',
                              levels = c('neutral', 'negative', 'positive'),
                              var_level = 1)),
  randomeffect = list(int_individual = list(variance = 0.2, var_level = 2),
                      time_individual = list(variance = 0.2, var_level = 2)),
  sample_size = list(level1 = 50, level2 = 25))

data <- simulate_fixed(data = NULL, sim_arguments) %>%
  simulate_error(sim_arguments) %>%
  simulate_randomeffect(sim_arguments) %>%
  generate_response(sim_arguments)