0

When I run the following code, I get an error: "object 'modelB' not found"

sim_analysis <- function(temp_data) {
  modelA <- clmm("Item1 ~ Total + (1 | Clust)", data = temp_data)
  modelB <- clmm("Item1 ~ Total + Group + (1 | Clust)", data = temp_data)
  return(anova(modelA, modelB)$`Pr(>Chisq)`[2])
}
sim_analysis(my_data)

But when I step through the function for temp_data = data, I get no error:

temp_data <- my_data
modelA <- clmm("Item1 ~ Total + (1 | Clust)", data = temp_data)
modelB <- clmm("Item1 ~ Total + Group + (1 | Clust)", data = temp_data)
anova(modelA, modelB)$`Pr(>Chisq)`[2]

The problem must have something to do with clmm (from the ordinal package) because when I make Item1 responses numeric, remove the random effects, and do the exact same thing with lm, the function works fine.

A reproducible example was asked for, so here is some code to generate the data:

set.seed(12345678)
my_data <- data.frame(Clust = c(rep(1, 10), rep(2, 10), rep(3, 10)),
                        Group = rep(c(0,1), 15),
                        Item1 = as.factor(sample(c(1, 2, 3, 4), 30, replace = TRUE)),
                        Total = sample(20:50, 30, replace = TRUE))
ddueber
  • 13
  • 4
  • I don't know this function, but why are you passing it deparsed formulas instead of proper formulas? – Roland Oct 28 '19 at 14:53
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 28 '19 at 14:54
  • 1
    the help page is not using quotes, but formulas: https://www.rdocumentation.org/packages/ordinal/versions/2019.4-25/topics/clmm – MichaelChirico Oct 28 '19 at 15:16
  • In the help page linked above I can't see quotes in the formulas (see Examples at bottom)? – Nova Oct 28 '19 at 16:39
  • I use formulas in quotes out of habit (e.g., when doing measurement work using "lavaan" or "mirt," the formulas *must* be quoted) and because I often dynamically generate formulas. The quotes are not related to the problem – ddueber Oct 30 '19 at 15:02
  • I believe the code you provided has an issue here: `temp_data <- data` since you're passing a function (i.e. `data()`) to **temp_data** – gabt Oct 30 '19 at 15:14
  • Good catch, gabrielet. When I simplified the example to put here on stack overflow, I forgot about the data() function. I have renamed the variables "my_data" to avoid that particular confusion – ddueber Oct 31 '19 at 20:13

0 Answers0