[R] How to test a difference in ratios of count data in R

Greg Snow 538280 at gmail.com
Wed Sep 28 18:49:49 CEST 2016

```There are multiple ways of doing this, but here are a couple.

To just test the fixed effect of treatment you can use the glm function:

replicate treatment n X
1 A 32 4
1 B 33 18
2 A 20 6
2 B 21 18
3 A 7 0
3 B 8 4

fit1 <- glm( cbind(X,n-X) ~ treatment, data=test, family=binomial)
summary(fit1)

Note that the default baseline value may differ between R and SAS,
which would result in a reversed sign on the slope coefficient (and
different intercept).

To include replicate as a random effect you need an additional
package, here I use lme4 and the glmer function:

library(lme4)
fit2 <- glmer( cbind(X, n-X) ~ treatment + (1|replicate), data=test,
family=binomial)
summary(fit2)

On Tue, Sep 27, 2016 at 9:03 PM, Shuhua Zhan <szhan at uoguelph.ca> wrote:
> Hello R-experts,
> I am interested to determine if the ratio of counts from two groups differ across two distinct treatments. For example, we have three replicates of treatment A, and three replicates of treatment B. For each treatment, we have counts X from one group and counts Y from another group. My understanding is that that GLIMMIX procedure in SAS can calculate whether the ratio of counts in one group (X/X+Y) significantly differs between treatments.
>
> I think this is the way you do it in SAS. The replicate and treatment variables are self-explanatory. The first number (n) refers to the total counts X + Y; the second number (X) refers to the counts X. Is there a way to do this in R? Since we have 20,000 datasets to be tested, it may be easier to retrive the significant test as the given dataset below and its p>F value and mean ratios of treatments in R than SAS.
>
>
> data test;
> input replicate treatment\$ n X;
> datalines;
> 1 A 32 4
> 1 B 33 18
> 2 A 20 6
> 2 B 21 18
> 3 A 7 0
> 3 B 8 4
> ;
>
> proc glimmix data=test;
> class replicate treatment;
> model X/n = treatment / solution;
> random intercept / subject=replicate;
> run;
>
> ods select lsmeans;
> proc glimmix data=test;
> class replicate treatment;
> model X/n = treatment / solution;
> random intercept / subject=replicate;
> lsmeans treatment / cl ilink;
> run;
>
> Joshua
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help