What needs to be done to include a categorical and numerical covariate (eg gender and age)?

60 Views Asked by At

I have the following code, and Gender is a dummy variable as M0, F1:

Example Data:

ID  Feature Age Gender  Category
1   96  30  0   category 1
2   57  42  0   category 1
3   82  37  0   category 1
4   87  43  1   category 1
5   8   47  1   category 1
6   52  35  0   category 1
7   29  36  0   category 1
8   48  34  1   category 1
9   13  25  0   category 1
10  53  39  1   category 2
11  64  44  0   category 2
12  70  33  0   category 2
13  31  27  1   category 2
14  88  27  1   category 2
15  37  53  0   category 2
16  6   29  0   category 2
17  44  60  0   category 2
18  71  54  1   category 2
19  78  54  0   category 2
20  64  48  0   category 3
21  68  57  1   category 3
22  31  36  1   category 3
23  18  34  0   category 3
24  42  53  0   category 3
25  1   38  0   category 3
26  47  54  0   category 3
27  18  22  0   category 3
28  65  28  0   category 3
29  23  45  1   category 3
30  46  45  0   category 3

My Ancova is defined as Ancova

res.aov <- df_copy %>% anova_test(Feature ~ Age + Gender + Category)

Pairwise post hoc

pwc <- df_copy %>% 
  emmeans_test(
    formula = Feature ~ Category, 
    covar = Age,
    p.adjust.method = "bonferroni"
  )

This works fine with just Age, but adding gender alone or combined with age causes problems - what needs to be done to have two covariates in emmeans and what extra needs to be done for dummy variable Gender.

1

There are 1 best solutions below

0
Thomson Harris On

Are your variables coded as factors? To check if they are run class(df$variable) to see what class of variable you have. If they are not factors they can be changed to factors by running: df$variable <- as.factor(df$variable)

The other problem that might be causing issues is that "Gender" only has one value. Since the only value of "Gender" is "category" including it in the model might be causing the issues.