Crosstabs - More than 2 Variables

Hello

Good evening folks.

I am trying to run a crosstabulation between two variables, but controlling for a 3rd one, and then draw a bar chart (vertical / horizontal) to display the results.

I am using a CrossTable function (part of the gmodels package) and so far, I am only able to do handle the main effects without the elaborated variable, using the following command

CrossTable (CatsDogs$Dance, CatsDogs$Training,
           digits=2,
           prop.c = TRUE,
           prop.r = FALSE,
           prop.t = FALSE,
           prop.chisq=FALSE,
           chisq = TRUE,
           dnn=c("Dance", "Training-Foodvs Affection-1"))

Is there any way to include a 3rd variable in this code so I can see for example how the effect of training on the DV (whether an animal dances or not) depends on the animal concerned.
Here is a scratch of the data I am using - from dput:

dput(head(CatsDogs, n = 10))
structure(
  list(
    Animal = structure(
      c(0, 0, 0, 0, 0, 0, 0, 0, 0,
        0),
      label = "Animal",
      labels = c(Cat = 0, Dog = 1),
      class = "haven_labelled"
    ),
    Training = structure(
      c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
      label = "Type of Training",
      labels = c(`Food as Reward` = 0,
                 `Affection as Reward` = 1),
      class = "haven_labelled"
    ),
    Dance = structure(
      c(1,
        1, 1, 1, 1, 1, 1, 1, 1, 1),
      label = "Did they dance?",
      labels = c(No = 0,
                 Yes = 1),
      class = "haven_labelled"
    )
  ),
  row.names = c(NA,-10L),
  class = c("tbl_df", "tbl", "data.frame")
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.