Reputation: 4807
I have two sets of data: Set1 and Set2.
For each set we have the same variables A, B, C, D, E.
I want to do the F-test to understand whether the following relationships are simultaneously true:
Set1_A = Set2_A, Set1_B = Set2_B, Set1_C = Set2_C, Set1_D = Set2_D, Set1_E = Set2_E
Set1_A and Set2_A might be different size vectors.
How do I achieve this in R?
Thanks
Sample Data for Set1:
A B C
11.0 11.0 11.0
23.3 23.3 23.3
44.6 -1.3 -7.1
-1.9 -1.9 -1.9
Sample Data for Set2:
A B C
3.9 3.9 3.9
-6.1 -6.1 -6.1
-34.6 -95.7 -102.4
7.0 7.0 7.0
Upvotes: 0
Views: 1398
Reputation: 263411
This illustrates how to get a comparison for Set1_A vs Set2_A. In order to determine if they are simultaneous "true" you would need to use a multivariate analysis
Set1 <- read.table(text="A B C
11.0 11.0 11.0
23.3 23.3 23.3
44.6 -1.3 -7.1
-1.9 -1.9 -1.9", header=TRUE)
Set2<- read.table(text="A B C
3.9 3.9 3.9
-6.1 -6.1 -6.1
-34.6 -95.7 -102.4
7.0 7.0 7.0", header=TRUE)
combset <- rbind(Set1, Set2)
combset$grp <- rep(c("Set1", "Set2"), times=c(nrow(Set1), nrow(Set2) ) )
combset
#----------------
A B C grp
1 11.0 11.0 11.0 Set1
2 23.3 23.3 23.3 Set1
3 44.6 -1.3 -7.1 Set1
4 -1.9 -1.9 -1.9 Set1
5 3.9 3.9 3.9 Set2
6 -6.1 -6.1 -6.1 Set2
7 -34.6 -95.7 -102.4 Set2
8 7.0 7.0 7.0 Set2
Now that you have your data in what might be called the long format you can use the grp ID as a factor in an lm.formula
call:
lm(A ~ grp, data=combset)
Call:
lm(formula = A ~ grp, data = combset)
Coefficients:
(Intercept) grpSet2
19.25 -26.70
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
> anova(lm(A ~ grp, data=combset))
Analysis of Variance Table
Response: A
Df Sum Sq Mean Sq F value Pr(>F)
grp 1 1425.8 1425.78 3.8004 0.09913 .
Residuals 6 2251.0 375.16
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
The multivariate model can be constructed. BUT ... are you sure you can interpret this correctly and are aware of the statistical issue that might arise?
> lm( A + B + C ~ grp, combset)
Call:
lm(formula = A + B + C ~ grp, data = combset)
Coefficients:
(Intercept) grpSet2
33.35 -87.92
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
> anova(lm( A + B + C ~ grp, combset))
Analysis of Variance Table
Response: A + B + C
Df Sum Sq Mean Sq F value Pr(>F)
grp 1 15462 15461.6 2.016 0.2055
Residuals 6 46017 7669.6
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
I was worried about that answer because I thought that more coefficients should have been estimated. I remembered an article in RNews by Peter Dalgaard and looked it up. This should have been what I offered:
> lm( cbind(A, B, C) ~ grp, combset)
Call:
lm(formula = cbind(A, B, C) ~ grp, data = combset)
Coefficients:
A B C
(Intercept) 19.250 7.775 6.325
grpSet2 -26.700 -30.500 -30.725
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
> anova(lm( cbind(A, B, C) ~ grp, combset))
Analysis of Variance Table
Df Pillai approx F num Df den Df Pr(>F)
(Intercept) 1 0.51946 1.44130 3 4 0.3557
grp 1 0.42690 0.99318 3 4 0.4813
Residuals 6
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
> class(lm( cbind(A, B, C) ~ grp, combset))
[1] "mlm" "lm"
Warning message:
In model.matrix.default(mt, mf, contrasts) :
variable 'grp' converted to a factor
Notice that "real" multivariate inferential statistics (e.g. Pillai's trace or Wilks or Hotelling) are presented and that three separate coefficients for A, B and C are presented, and that the class of the output is "mlm" and not just "lm". You should also look at ?anova.mlm
.
Upvotes: 2