# Compositional data using R and difference between groups?

I have a problem that can summarize: Is there a difference between stores in terms of the share of sales percentage in the 4th quarter.

Q1 <- c(20,50,40,10)
Q2 <- c(20,10,30,20)
Q3 <- c(10,20,10,20)
Q4 <- c(50,20,20,50)
Shops <- c("a","b","c","d")
data  <- data.frame(Shops,Q1,Q2,Q3,Q4)
data
Shops Q1 Q2 Q3 Q4
1     a 20 20 10 50
2     b 50 10 20 20
3     c 40 30 10 20
4     d 10 20 20 50


The values represent percentage of sales and their sum is 100. However, in absolute numbers they differ a lot because of different size of shops.

I appreciate if you can recommend me some methods to complete this problem. One of my approaches to grouped Q1+Q2+Q3 in one group that might compare with Q4. It will be easier to interpret such grouped data.

Could you please suggest me a package and approach to heand it?

Fit a model of value to Shops and an indicator of whether value is in Q4 or not. We have removed Q1 (the second column) since it is implied by the fact that every row sums to 100. From the summary we see that the indicator's p value is 0.0412 so Q4 is significantly different from the other quarters after adjusting for Shops at the 5% level although not at the 1% level.

library(tidyr)

long <- pivot_longer(data[-2], -1, names_to = "quarter")
fm <- lm(value ~ Shops + I(quarter == "Q4") + 0, long)
summary(fm)


giving:

Formula = value ~ Shops + I(quarter == "Q4") + 0, data = long)

Residuals:
Min      1Q  Median      3Q     Max
-11.667  -5.208  -2.500   8.542  15.833

Coefficients:
Estimate Std. Error t value Pr(>|t|)
Shopsa                   20.833      7.008   2.973   0.0207 *
Shopsb                   10.833      7.008   1.546   0.1660
Shopsc                   14.167      7.008   2.022   0.0829 .
Shopsd                   24.167      7.008   3.449   0.0107 *
I(quarter == "Q4")TRUE   17.500      7.008   2.497   0.0412 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 11.44 on 7 degrees of freedom
Multiple R-squared:  0.8934,    Adjusted R-squared:  0.8173
F-statistic: 11.73 on 5 and 7 DF,  p-value: 0.002695


We can also get the same answer using anova by comparing fm to the model:

fm0 <- lm(value ~ Shops + 0, long)
anova(fm0, fm)


giving:

Analysis of Variance Table

Model 1: value ~ Shops + 0
Model 2: value ~ Shops + I(quarter == "Q4") + 0
Res.Df     RSS Df Sum of Sq      F  Pr(>F)
1      8 1733.33
2      7  916.67  1    816.67 6.2364 0.04116 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


The significant coefs in fm Shops are only due to the fact that we included the intercepts in the Shop coefs. If we use a separate intercept then it is clear that the Shops are not significant.

summary(lm(value ~ Shops + I(quarter == "Q4"), long))


giving:

Call:
lm(formula = value ~ Shops + I(quarter == "Q4"), data = long)

Residuals:
Min      1Q  Median      3Q     Max
-11.667  -5.208  -2.500   8.542  15.833

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)              20.833      7.008   2.973   0.0207 *
Shopsb                  -10.000      9.344  -1.070   0.3200
Shopsc                   -6.667      9.344  -0.714   0.4986
Shopsd                    3.333      9.344   0.357   0.7318
I(quarter == "Q4")TRUE   17.500      7.008   2.497   0.0412 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 11.44 on 7 degrees of freedom
Multiple R-squared:  0.5565,    Adjusted R-squared:  0.303
F-statistic: 2.195 on 4 and 7 DF,  p-value: 0.1713


If you want to test the differences between pairs of Shops despite this then use either of these. The first is from base R.

TukeyHSD(aov(value ~ Shops + I(quarter == "Q4"), long), "Shops")

library(multcomp)
longf <- transform(long, Shops = factor(Shops))
fm3 <- lm(value ~ Shops + factor(quarter == "Q4"), longf)
summary(glht(fm3, mcp(Shops = "Tukey")))

• This is great. But whic tests you used and where is location of difference? Do I need to perform some post hoc test to define between which hospitals difference exist? Thanks for helping me. Oct 12 at 11:43
• I make a mistake. It is shops. Oct 12 at 15:47
• however, I need to determine where differences came from Oct 12 at 16:14
• Ok. The fist step is to determine is there are difference across units. If there is a difference, the post hoc should find a location of difference. A vs B, A vs C, A vs D, B vs C, B vs D, C vs D. That was I thout. I will study your explanation. Great that for your effors. Oct 12 at 19:12
• Which means that: Shops did not differe in the percetage of sales in the Q4? In my view, the first model aim the questions: There is difference in the percentage in Q4 in the same shop? Oct 13 at 9:58