Uploaded by 622894je

Inleiding Multivariate Statistiek Assignment 1 (2)

advertisement
Inleiding Multivariate Statistiek Assignment 1
Exercise 1
This exercise is made with R. The code which is used to make the exercise is appended.
1.
Table with distances: (Note: the column Mahalanobis Distance² is (x – u)T * S^-1 * (x – u),
while the column Mahalanobis Distance is the square root of (x – u)T * S^-1 * (x – u)).
State: ->
Mahalanobis Mahalanobis State: ->
Mahalanobis Mahalanobis
Distance²:
Distance:
Distance²:
Distance:
"Alabama"
1,8263
1,3514
"Montana"
2,8459
1,6870
"Alaska"
12,0269
3,4680
"Nebraska"
1,6850
1,2981
"Arizona"
0,0781
0,2796
"Nevada"
0,7125
0,8441
"Arkansas"
1,1622
1,0780
"New Hampshire" 0,3326
0,5767
"California"
6,4517
2,5400
"New Jersey"
4,1053
2,0262
"Colorado"
0,2178
0,4667
"New Mexico"
2,0500
1,4318
"Connecticut"
7,5033
2,7392
"New York"
4,6069
2,1464
"Delaware"
1,9284
1,3887
"North Carolina"
4,3855
2,0941
"Florida"
2,6585
1,6305
"North Dakota"
8,2045
2,8643
"Georgia"
4,1647
2,0408
"Ohio"
0,2029
0,4504
"Hawaii"
12,1474
3,4853
"Oklahoma"
2,4395
1,5619
"Idaho"
1,5440
1,2426
"Oregon"
0,3589
0,5990
"Illinois"
4,5440
2,1317
"Pennsylvania"
3,4479
1,8569
"Indiana"
2,1426
1,4638
"Rhode Island"
2,6340
1,6230
"Iowa"
3,6021
1,8979
"South Carolina"
6,1565
2,4812
"Kansas"
2,3067
1,5188
"South Dakota"
1,9659
1,4021
"Kentucky"
1,1529
1,0737
"Tennessee"
2,5316
1,5911
"Louisiana"
2,3279
1,5257
"Texas"
48,0200
6,9296
"Maine"
2,1632
1,4708
"Utah"
3,2598
1,8055
"Maryland"
1,0750
1,0368
"Vermont"
1,0874
1,0428
"Massachusetts" 1,1969
1,0940
"Virginia"
1,7161
1,3100
"Michigan"
3,9363
1,9840
"Washington"
0,2264
0,4758
"Minnesota"
4,8073
2,1926
"West Virginia"
2,4765
1,5737
"Mississippi"
3,3095
1,8192
"Wisconsin"
4,4089
2,0997
"Missouri"
1,2072
1,0987
"Wyoming"
0,6586
0,8115
2.
Table with angles:
Deviation vector 1:
Salary
Salary
Salary
Verbal
Verbal
Math
Deviation vector 2;
Verbal
Math
Total
Math
Total
Total
Angle (in degrees):
118,749°
114,229°
116,580°
14,559°
7,583°
7,127°
3. The calculated angle is approximately: 0,083°.
Note: expected was 0, because the vectors TOTAL and VERBAL + MATH are the same by
definition. However, in the file the VERBAL score and MATH score from Texas do not add up
to the TOTAL score. This is why there is a small angle between the vectors.
Exercise 2
1. Part 1:
2. Part 2:
For clarification of the axis:
X AND Y
Y AND Z
X AND Z
PROB. MASS
90%
99%
90%
99%
90%
99%
AXIS 1
(1.86, 1.86)
(2.63, 2.63)
(2.15, 0)
(3.03, 0)
(2.07, 2.07)
(2.93, 2.93)
AXIS 2
(-1.07, 1.07)
(-1.52, 1.52)
(0, 2.15)
(0, 3.03)
(0.57, 0.57)
(0.79, -0.79)
3. The change of the expected value will not change the shape of the ellipses from 2. The only thing
that will change is around what point the ellipses will be centered. Before the change the ellipses
were centered around (0,0,0). After the change the ellipses are going to be centered around (3,3,3),
but they are still going to have the same shape as before.
Appendix
Code for exercise 1:
R code:
dataset = read.table(file.choose(), header = TRUE, row.names = 1) # Creating the dataset by choosing the
correct file.
MahalanobisDistance = mahalanobis(dataset, colMeans(dataset), cov(dataset)) # Calculating the Mahalanobis
distance of the sample.
MahalanobisDistanceTable = data.frame(MahalanobisDistance) # Making a table of the Mahalanobis distance.
A = colMeans(dataset) # Computing the mean for each variable and storing the results.
SalaryMean = A[1]
VerbalMean = A[2]
MathMean = A[3]
TotalMean = A[4]
DeviationVectorSalary = dataset[,1] - SalaryMean # Computing the deviation vector for each variable.
DeviationVectorVerbal = dataset[,2] - VerbalMean
DeviationVectorMath = dataset[,3] - MathMean
DeviationVectorTotal = dataset[,4] - TotalMean
# Defining function to calculate the angle between two vectors.
CalculateAngle = function(a,b){
return (180 * acos(sum(a*b) / ( sqrt(sum(a^2)) * sqrt(sum(b^2)))) / pi)
}
AngleSalaryVerbalDeviation = CalculateAngle(DeviationVectorSalary, DeviationVectorVerbal) # Calculating all
the angles.
AngleSalaryMathDeviation = CalculateAngle(DeviationVectorSalary, DeviationVectorMath)
AngleSalaryTotalDeviation = CalculateAngle(DeviationVectorSalary, DeviationVectorTotal)
AngleVerbalMathDeviation = CalculateAngle(DeviationVectorVerbal, DeviationVectorMath)
AngleVerbalTotalDeviation = CalculateAngle(DeviationVectorVerbal, DeviationVectorTotal)
AngleMathTotalDeviation = CalculateAngle(DeviationVectorMath, DeviationVectorTotal)
AngleTotalAndVerbalPlusMath = CalculateAngle(dataset[,4], dataset[,2] + dataset[,3]) # Calculating the angle
between TOTAL and VERBAL + MATH.
Download