Hi everyone
Let's assume that we have two groups of patients that are assigned to two different diets . Those patients are weighted before and after the diet, and that information is stored in two numerical variables:
id <- 1:20
weight_before <- c(80, 85, 50, 67, 98, 87, 81, 77, 79, 90, 68, 86, 69, 76, 82, 65, 97, 71, 99, 79)
weight_after <- c(85, 90, 55, 73, 99, 99, 81, 78, 90, 97, 60, 80, 67, 75, 77, 60, 90, 65, 89, 71)
diet <- c("Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A",
"Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B")
MyData <- data.frame(id, diet, weight_before, weight_after)
MyData$diet <- factor(MyData$diet)
View(MyData)
So, if we just wanted to know if this sample of patients changed their weight, we would just do (for simplicity, I did not check the test assumptions):
# Weight change - Global:
t.test(MyData$weight_before, MyData$weight_after,
mu = 0,
alternative = "two.sided",
paired = TRUE)
Apparently, there is no differences between the mean weight of the group before and after (p-value=0.8674). But I guess that the most interesting thing would be to see if there are differences between the groups:
# Weight change - By groups:
MyData$weight_change <- MyData$weight_after - MyData$weight_before
t.test(MyData$weight_change ~ MyData$diet,
mu = 0,
alternative = "two.sided",
paired = FALSE)
Here we can see that subjetcs in diet A won 5.3 kg (on average) whilest subjects in group B lost 5.8 kg. And the difference is statistically significant (p-value = 2.213e-06).
Question I: Is there any other way of doing this? Coming from other statistical softwares it looks "dirty" to me to calculate the difference variable and to perform a non-paired test. Is it possible to ask R to do a paired test by groups?
But the problem is unsolvable to me when we deal with qualitative variables. Imagine that, instead of measuring the weight change, we want to measure the mood change. And we measure the mood using an ordinal scale of 3 possibilities (being I bad mood , II average mood and III good mood, for instance).
# Ordered qualitative variables
mood_before <- c("Level II", "Level I", "Level III", "Level I", "Level II", "Level I", "Level I", "Level III", "Level III", "Level II", "Level III", "Level II", "Level I", "Level III", "Level III", "Level I", "Level II", "Level III", "Level II", "Level II")
mood_after <- c("Level III", "Level III", "Level III", "Level II", "Level III", "Level III", "Level II", "Level III", "Level III", "Level III", "Level II", "Level I", "Level I", "Level I", "Level II", "Level I", "Level I", "Level I", "Level I", "Level I")
diet <- c("Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A", "Diet A",
"Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B", "Diet B")
MyData <- data.frame(id, diet, mood_before, mood_after)
MyData$diet <- factor(MyData$diet)
MyData$diet <- factor(MyData$mood_before)
MyData$diet <- factor(MyData$mood_after)
View(MyData)
Question II: How can we solve this? I guess we should perform a Wilcoxon paired test by groups... but I don't know how to deal with this. In this case I cannot even do the omnibus comparison because R tells me that the variables should be numeric (and that does not make sense to me).
Thank you for your help.