0
votes

In my dataset, I have a variable called Condition. I modeled this as a factor with two levels: Control and Treatment. In the same data frame, I also have variables: Fish1, Fish2, Frechfires1, Frenchfries2. I want to model these 'Ordinalvariables' as levels of the treatment group with Fish higher than the fries! At the same time, I want to keep the level of Fish1 and Fish2 equal and the same for fries1&2- all for Treatment (which is a level of the variable Condition).

For fitting a mixed model: Health~Condition() Taking in to account the effect of Fish & Fries

Condition  SubNum  Trial Num_Fish1 Num_Fish2 Num_Fries1 Num_Fries2  Health
Treatment   1        1     1           1          2        1          3
Treatment   1        2     0           3          4        1          5
Control     2        1     0           0          0        0          4
Control     2        2     0           0          0        0          5
1
It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.MrFlick
What exactly do you mean by equal? Its either ordinal or nominal. Not both. So you rather have them ordered or not.Onyambu
So between Fish and Fries, it's ordinal, but between fish1 and fish2 it has an equal weight age.They are still two levels of Treatment but there is no order or has the same importance..but fis1/fish2> Fies1/fries2 @Onyambumashedpoteto
@MrFlick editedmashedpoteto
This still doesn't make any sense. Exactly what type of model are you fitting here? What are you trying to do with this data?MrFlick

1 Answers

1
votes

If I understand the question correctly then you really just need ordered factor levels "Fish" and "Fries", with some further differentiation within each level provided by the digits 1 and 2.

Using the following data:

df <- read.table(text = "Condition  SubNum  Trial Num_Fish1 Num_Fish2 Num_Fries1 Num_Fries2  Health
Treatment   1        1     1           1          2        1          3
Treatment   1        2     0           3          4        1          5
Control     2        1     0           0          0        0          4
Control     2        2     0           0          0        0          5", header = T)

I would use tidyr::gather() to put column names Num_* into a variable product, and then extract the product types and product numbers to ordered and unordered factors, respectively.

library(tidyr)
library(dplyr)
library(stringr)

df_out <- df %>% 
    gather("product", "product_value", -c(Condition:Trial, Health)) %>% 
    mutate(product_num = factor(str_match(product, "\\d")),
           product = ordered(str_remove_all(product, "Num_|\\d"),
                             levels = c("Fries", "Fish")
                             )
    )

You should end up with a data frame like the one below, which you can use to flexibly compare "Fish" with "Fries" (ordinal), or "Fish 1" with "Fish 2" (in both cases just "Fish", so essentially nominal), etc. I converted product_num into a factor, and not an integer vector, to avoid any confusion that might be caused by the intrinsic order of integers. Depending on your modeling strategy you might still need to subset and/or relevel your data.

# A tibble: 16 x 7
   Condition SubNum Trial Health product product_value product_num
   <fct>      <int> <int>  <int> <ord>           <int> <fct>      
 1 Treatment      1     1      3 Fish                1 1          
 2 Treatment      1     2      5 Fish                0 1          
 3 Control        2     1      4 Fish                0 1          
 4 Control        2     2      5 Fish                0 1          
 5 Treatment      1     1      3 Fish                1 2          
 6 Treatment      1     2      5 Fish                3 2          
 7 Control        2     1      4 Fish                0 2          
 8 Control        2     2      5 Fish                0 2          
 9 Treatment      1     1      3 Fries               2 1          
10 Treatment      1     2      5 Fries               4 1          
11 Control        2     1      4 Fries               0 1          
12 Control        2     2      5 Fries               0 1          
13 Treatment      1     1      3 Fries               1 2          
14 Treatment      1     2      5 Fries               1 2          
15 Control        2     1      4 Fries               0 2          
16 Control        2     2      5 Fries               0 2