forked from Tanb28/Two-Sample-Hotellings-T2-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoSampleHotellingsT2.R
99 lines (69 loc) · 3.75 KB
/
TwoSampleHotellingsT2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#Two sample HotellingsT2 Test
#Two assumptions
#1)Populations Of the two samples follow multivariate normal distribution
#2)Both the Populations have same covariance matrix (Homoscedasticity)
#Hypothesis
#H0 - mu1 = mu2
#H1 - mu1 != mu2
#Installing and loading required packages
install.packages("mvoutlier")
install.packages("rlang")
install.packages("pcaPP")
iinstall.packages("ICSNP")
library(mvoutlier)
library(pcaPP)
library(ICSNP)
####Example 1
#Info About the first Data file
#Original Source - Everitt 2005
#Following Dataset contains the data on 2 group
#Group 1- Skulls Found in the graves in Sikkim and neighbouring areas of Tibet
#Group 2- Skulls picked up on the battlefield in Lhasa District and believed to be those of native soldiers from the eastern province of Khans
#Particular Interest - Tibetans from Khans might be survivors of a particular fundamental human type, unrelated to the Mongolian and Indian types that surrounded them
#Response Variables - 5 Measurements (Units - millimeters)
#Length, Breadth, Height, FHeight, FBreadth
#Loading the data
source("E:/tibetskull.txt")$value #This attached file in repository is text file of Tibetskull
head(Tibet)
str(Tibet)
skull_dat <- Tibet[, 1:5]
skull_dat
#Plot For checking Multivariate normality of data
chisq.plot(skull_dat[Tibet$Type == 1, ], quan = 1, ask = FALSE)
chisq.plot(skull_dat[Tibet$Type == 2, ], quan = 1, ask = FALSE)
#Ideally the points will look like a straight line, but considering the
#low sample size, we assume multivariate normality
#Plot for checking homoscedasticity
par(cex=0.2)
plotcov(cov(skull_dat[Tibet$Type == 1, ]), cov(skull_dat[Tibet$Type == 2, ]), method1 = "Group 1", method2 = "Group 2")
#From the plot we can see that most of the rings coincide with each other, so we can assume homoscedasticity
#Results of the test
HotellingsT2(as.matrix(skull_dat) ~ Tibet$Type)
#As our p-value(0.0029) < 0.05, We Rej H0 at 5% level of significance
#There is strong evidence that the multivariate means of both the groups are different.
#So, the skulls found in the graves in sikkim and tibet and the Skulls picked up on the battlefield
#in Lhasa District which was believed to be those of native soldiers from the eastern province of Khans are different.
####Example 2
#Info about the data
#A certain type of tropical disease is characterized by fever, low blood pressure and body aches.
#A pharmaceutical company is working on a new drug to treat this type of disease and wanted to determine
#whether the drug is effective. They took a random sample of 20 people with this type of disease and
#18 with a placebo. Based on the data they wanted to determine whether the drug is effective at reducing
#these three symptoms.
data <- read.csv("E:/eg2.csv") #Read the second Data file of Drug and Placebo
head(data)
data$Group <- factor(data$Group)
dat<- data[,1:3]
#Plot for checking multivariate normality
chisq.plot(dat[data$Group == 'Drug', ], quan = 1, ask = FALSE)
chisq.plot(dat[data$Group == 'Placebo', ], quan = 1, ask = FALSE)
#Ideally the points will look like a straight line, but considering the
#low sample size, we assume multivariate normality
#Plot for checking homoscedasticity
plotcov(cov(dat[data$Group == "Drug", ]), cov(dat[data$Group == "Placebo", ]), method1 = "Drug", method2 = "Placebo")
#From the plot we can see that most of the rings coincide with each other, so we can assume homoscedasticity
#Results
HotellingsT2(as.matrix(dat) ~ data$Group)
#As our p-value(0.2917) > 0.05, We do not Rej H0 at 5% level of significance
#There is no significant difference between the mean vectors for the drug and placebo,
#providing evidence that the drug is not effective in reducing symptoms.