-1

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
mashedpoteto
  • 121
  • 8
  • 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 10 '19 at 15:12
  • 1
    What exactly do you mean by equal? Its either ordinal or nominal. Not both. So you rather have them ordered or not. – Onyambu Oct 10 '19 at 15:14
  • 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 @Onyambu – mashedpoteto Oct 10 '19 at 15:21
  • @MrFlick edited – mashedpoteto Oct 10 '19 at 15:21
  • 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 Oct 10 '19 at 15:24
  • @mashedpoteto and that is what I am trying to tell you, you can not have data to be ordinal and norminal at the same timr. It just cant happen. eg you cannot have data to be discrete and continuous at the same time. choose one. With ordinal variables, there is an order. You cannot have equivalence – Onyambu Oct 10 '19 at 15:39
  • @Onyambu I knew this, but this is the structure of the data that I have at the moment. Is there an option to bring this up via adding an extra variable or some sort like that? – mashedpoteto Oct 10 '19 at 15:41
  • looking at your data. One thing that stands out is that to get one observation, ie one experimental run involves (condition, Fish1,Fish2,Fries1,Fries2), so technically you have 5 different factors. It is tricky to combine Fish1 and Fish2 since health depends on both, or combine Fries1 and Fries2. Probably you can sum them. If you have enough data though, you could consider doing a 5factor anaysis, – Onyambu Oct 10 '19 at 15:55

1 Answers1

1

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          
  • I will try to digg around with this,it looks logically viable,Thank you! – mashedpoteto Oct 10 '19 at 18:18
  • @mashedpoteto happy to help. If you like the answer please consider upvoting. If the answer solves your problem please also accept it so others know what worked for you. –  Oct 10 '19 at 18:22
  • Sure, I will do. Just upvoted but I would need some more points for my upvote to be counted. – mashedpoteto Oct 10 '19 at 18:29
  • Do you think here the Num_fish1/2 and Num_freis1/2 are explanatory or dependent variables? The num_ fishes/fries had an upper limit of 20 that was designed under the experiment @gersht – mashedpoteto Oct 11 '19 at 18:54
  • 1
    @mashedpoteto I don't know the details of the experiment, but it seems highly unlikely that Fish/Fries are a response variable. –  Oct 12 '19 at 09:16