1

I have data of a protein "F" in patients who used or not used a drug "X". I have 3 cofounders (age, BMI, sex) that I should adjust.

I successfully calculated the means of protein amount in the drug-group and no-drug group by using the below code:

> F <- data$f
> by(F, drugnodrug, summary)
> summ(F, by=drugnodrug)

Now, I want to calculate the absolute mean difference (95% confidence interval).

How can I do this in R (dplyr package)? Can I use Multiple Linear regression for this calculation and how?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Angel
  • 13
  • 3
  • Does this answer your question? [Calculating length of 95%-CI using dplyr](https://stackoverflow.com/questions/35953394/calculating-length-of-95-ci-using-dplyr) – maaniB Feb 16 '22 at 13:36

1 Answers1

1

This can be done with base R, because t.test also reports a confidence interval for the mean difference:

> res <- t.test(iris$Sepal.Length[iris$Species=="setosa"],
+               iris$Sepal.Length[iris$Species=="virginica"])
> res$conf.int
[1] -1.78676 -1.37724
attr(,"conf.level")
[1] 0.95

The mean values are stored in the entry estimate, and you can compute the difference of the means with

> res$estimate[1] - res$estimate[2]
mean of x 
   -1.582

or equivalently

> sum(res$estimate * c(1,-1))
[1] -1.582
cdalitz
  • 1,019
  • 1
  • 6
  • 7
  • Thank you very much. This seems this code gives me a confidence interval but I want are two things. 1: difference of the two means 2: confidence interval for this difference. How can I have both these information? – Angel Feb 18 '22 at 07:38
  • Another point is that both of my data groups are positive numbers and when I calculated the two means give me positive numbers but when I ran your code to get the confidence interval of the difference between the two means, I got two negative numbers. – Angel Feb 18 '22 at 07:48
  • I have added code to compute the difference of the means. Note that, even if the means of both groups are positive, the difference can be negative. If both ends of the confidence interval have the same sign (positive or negative), the differece is statistically significant. And one final hint, as you are a New Contributor: do not forget to accept an answer if it answers your question. This will remove the question from the list of unresolved questions. – cdalitz Feb 18 '22 at 07:51