Re: [R] Using the mahalanobis( ) function

From: Tyler Rinker <tyler_rinker_at_hotmail.com>
Date: Tue, 22 Mar 2011 22:15:40 -0400

In my haste I did not include the full printout of my R session. My apologies.  

 nd<-read.table("ex20.csv", header=TRUE, sep=",",na.strings="NA") attach(nd)
age.frame<-data.frame(Age, Friend.Agression, Parent.Agression, Stranger.Agression) > age.frame

   Age Friend.Agression Parent.Agression Stranger.Agression

1    y                8                7                  8
2    y                5                6                  8
3    y                6                3                  7
4    y                5                5                  7
5    m               15               13                 10
6    m               13               11                  9
7    m               12               12                  9
8    m               18               10                  7
9    o               11               11                 10
10   o               10                4                 12
11   o               12                9                 12
12   o                9                8                 14
13   y               13                7                  7
14   y                9                5                 10
15   y               11                4                  4
16   y               15                3                  4
17   m               14               12                  8
18   m               10               15                 11
19   m               12               11                  8
20   m               10                9                  9
21   o               10                8                 11
22   o               13               11                 13
23   o                9                8                 12
24   o                7                9                 16
ay<-subset(nd,Age=="y")
> ay

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

1       f   y                8                7                  8
2       f   y                5                6                  8
3       f   y                6                3                  7
4       f   y                5                5                  7
13      m   y               13                7                  7
14      m   y                9                5                 10
15      m   y               11                4                  4
16      m   y               15                3                  4
> am<-subset(nd,Age=="m")
> am

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

5       f   m               15               13                 10
6       f   m               13               11                  9
7       f   m               12               12                  9
8       f   m               18               10                  7
17      m   m               14               12                  8
18      m   m               10               15                 11
19      m   m               12               11                  8
20      m   m               10                9                  9
> ao<-subset(nd,Age=="o")
> ao

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

9       f   o               11               11                 10
10      f   o               10                4                 12
11      f   o               12                9                 12
12      f   o                9                8                 14
21      m   o               10                8                 11
22      m   o               13               11                 13
23      m   o                9                8                 12
24      m   o                7                9                 16
> amm<-cbind(am$Friend.Agression, am$Parent.Agression,am$Stranger.Agression) > amm

     [,1] [,2] [,3]
[1,] 15 13 10
[2,] 13 11 9
[3,] 12 12 9
[4,] 18 10 7
[5,] 14 12 8
[6,] 10 15 11
[7,] 12 11 8
[8,] 10 9 9
> aym<-cbind(ay$Friend.Agression, ay$Parent.Agression,ay$Stranger.Agression) > aym

     [,1] [,2] [,3]
[1,] 8 7 8
[2,] 5 6 8
[3,] 6 3 7
[4,] 5 5 7
[5,] 13 7 7
[6,] 9 5 10
[7,] 11 4 4
[8,] 15 3 4
> aom<-cbind(ao$Friend.Agression, ao$Parent.Agression,ao$Stranger.Agression) > aom

     [,1] [,2] [,3]
[1,] 11 11 10
[2,] 10 4 12
[3,] 12 9 12
[4,] 9 8 14
[5,] 10 8 11
[6,] 13 11 13
[7,] 9 8 12
[8,] 7 9 16

> mean(aym)
[1] 6.958333

> mean(amm)
[1] 11.16667

> mean(aom)
[1] 10.375

> ascores<-cbind(Friend.Agression, Parent.Agression, Stranger.Agression) > ascores

      Friend.Agression Parent.Agression Stranger.Agression
 [1,]                8                7                  8
 [2,]                5                6                  8
 [3,]                6                3                  7
 [4,]                5                5                  7
 [5,]               15               13                 10
 [6,]               13               11                  9
 [7,]               12               12                  9
 [8,]               18               10                  7
 [9,]               11               11                 10

[10,] 10 4 12
[11,] 12 9 12
[12,] 9 8 14
[13,] 13 7 7
[14,] 9 5 10
[15,] 11 4 4
[16,] 15 3 4
[17,] 14 12 8
[18,] 10 15 11
[19,] 12 11 8
[20,] 10 9 9
[21,] 10 8 11
[22,] 13 11 13
[23,] 9 8 12
[24,] 7 9 16
> mean(ascores)
[1] 9.5

meany<-colMeans(aym, na.rm = FALSE, dims = 1) meany
meanm<-colMeans(amm, na.rm = FALSE, dims = 1) meanm
meano<-colMeans(aom, na.rm = FALSE, dims = 1) meano
> S<-cov(ascores)
> S
                   Friend.Agression Parent.Agression Stranger.Agression
Friend.Agression          10.476449         4.461957          -2.003623
Parent.Agression           4.461957        10.940217           3.489130
Stranger.Agression        -2.003623         3.489130           8.427536
> meany<-colMeans(x, na.rm = FALSE, dims = 1)
Error in inherits(x, "data.frame") : object 'x' not found > meany<-colMeans(aym, na.rm = FALSE, dims = 1) > meany
[1] 9.000 5.000 6.875
> meanm<-colMeans(amm, na.rm = FALSE, dims = 1) > meanm
[1] 13.000 11.625 8.875
> meano<-colMeans(aom, na.rm = FALSE, dims = 1) > meano
[1] 10.125 8.500 12.500
> mahalanobis(meany,meanm,S)
[1] 4.233462

> mahalanobis(meano,meanm,S)
[1] 3.857911
 

From: tyler_rinker_at_hotmail.com
To: r-help_at_r-project.org
Date: Tue, 22 Mar 2011 22:01:04 -0400
Subject: [R] Using the mahalanobis( ) function  

I want to calculate the Manhalanobis D as an effect size for a follow up to a MANOVA. I think I'm getting further but still not there. No one has weighed in yet to lend help and I would much appreciate it, particulalry those who are familiar with cluster analysis or MANOVA follow up/effect sizes.  

According to my stats professor I know the Mahalanobis D should be 3.93 & 3.04 for the distance between the center of the y to m cluster and the center of the m to o cluster respectively (groups under the Age variable). I get 4.233462 & 3.857911 respectively. I'm still messing it up.  

So far here is what I've done:  

> am

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

5       f   m               15               13                 10
6       f   m               13               11                  9
7       f   m               12               12                  9
8       f   m               18               10                  7
17      m   m               14               12                  8
18      m   m               10               15                 11
19      m   m               12               11                  8
20      m   m               10                9                  9
> ao<-subset(nd,Age=="o")
> ao

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

9       f   o               11               11                 10
10      f   o               10                4                 12
11      f   o               12                9                 12
12      f   o                9                8                 14
21      m   o               10                8                 11
22      m   o               13               11                 13
23      m   o                9                8                 12
24      m   o                7                9                 16
> amm<-cbind(am$Friend.Agression, am$Parent.Agression,am$Stranger.Agression) > amm

     [,1] [,2] [,3]
[1,] 15 13 10
[2,] 13 11 9
[3,] 12 12 9
[4,] 18 10 7
[5,] 14 12 8
[6,] 10 15 11
[7,] 12 11 8
[8,] 10 9 9
> aym<-cbind(ay$Friend.Agression, ay$Parent.Agression,ay$Stranger.Agression) > aym

     [,1] [,2] [,3]
[1,] 8 7 8
[2,] 5 6 8
[3,] 6 3 7
[4,] 5 5 7
[5,] 13 7 7
[6,] 9 5 10
[7,] 11 4 4
[8,] 15 3 4
> aom<-cbind(ao$Friend.Agression, ao$Parent.Agression,ao$Stranger.Agression) > aom

     [,1] [,2] [,3]
[1,] 11 11 10
[2,] 10 4 12
[3,] 12 9 12
[4,] 9 8 14
[5,] 10 8 11
[6,] 13 11 13
[7,] 9 8 12
[8,] 7 9 16
 

> mean(aym)
[1] 6.958333

> mean(amm)
[1] 11.16667

> mean(aom)
[1] 10.375

> ascores<-cbind(Friend.Agression, Parent.Agression, Stranger.Agression) > ascores

      Friend.Agression Parent.Agression Stranger.Agression
 [1,]                8                7                  8
 [2,]                5                6                  8
 [3,]                6                3                  7
 [4,]                5                5                  7
 [5,]               15               13                 10
 [6,]               13               11                  9
 [7,]               12               12                  9
 [8,]               18               10                  7
 [9,]               11               11                 10

[10,] 10 4 12
[11,] 12 9 12
[12,] 9 8 14
[13,] 13 7 7
[14,] 9 5 10
[15,] 11 4 4
[16,] 15 3 4
[17,] 14 12 8
[18,] 10 15 11
[19,] 12 11 8
[20,] 10 9 9
[21,] 10 8 11
[22,] 13 11 13
[23,] 9 8 12
[24,] 7 9 16
> mean(ascores)
[1] 9.5

meany<-colMeans(aym, na.rm = FALSE, dims = 1) meany
meanm<-colMeans(amm, na.rm = FALSE, dims = 1) meanm
meano<-colMeans(aom, na.rm = FALSE, dims = 1) meano
> S<-cov(ascores)
> S
                   Friend.Agression Parent.Agression Stranger.Agression
Friend.Agression          10.476449         4.461957          -2.003623
Parent.Agression           4.461957        10.940217           3.489130
Stranger.Agression        -2.003623         3.489130           8.427536
> meany<-colMeans(x, na.rm = FALSE, dims = 1)
Error in inherits(x, "data.frame") : object 'x' not found > meany<-colMeans(aym, na.rm = FALSE, dims = 1) > meany
[1] 9.000 5.000 6.875
> meanm<-colMeans(amm, na.rm = FALSE, dims = 1) > meanm
[1] 13.000 11.625 8.875
> meano<-colMeans(aom, na.rm = FALSE, dims = 1) > meano
[1] 10.125 8.500 12.500
> mahalanobis(meany,meanm,S)
[1] 4.233462

> mahalanobis(meano,meanm,S)
[1] 3.857911
     

From: tyler_rinker_at_hotmail.com
To: r-help_at_r-project.org
Subject: RE: [R] Using the mahalanobis( ) function Date: Mon, 21 Mar 2011 23:55:08 -0400        

This is what I've tried so far and just can't get it. I know I want a value of 3.93 (for Age= y and m) using mahalanobis d as an effect size for a follow up to an MANOVA:    

age.frame<-data.frame(Age, Friend.Agression, Parent.Agression, Stranger.Agression) > age.frame

   Age Friend.Agression Parent.Agression Stranger.Agression

1    y                8                7                  8
2    y                5                6                  8
3    y                6                3                  7
4    y                5                5                  7
5    m               15               13                 10
6    m               13               11                  9
7    m               12               12                  9
8    m               18               10                  7
9    o               11               11                 10
10   o               10                4                 12
11   o               12                9                 12
12   o                9                8                 14
13   y               13                7                  7
14   y                9                5                 10
15   y               11                4                  4
16   y               15                3                  4
17   m               14               12                  8
18   m               10               15                 11
19   m               12               11                  8
20   m               10                9                  9
21   o               10                8                 11
22   o               13               11                 13
23   o                9                8                 12
24   o                7                9                 16
 

ay<-subset(nd,Age=="y")
> ay

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

1       f   y                8                7                  8
2       f   y                5                6                  8
3       f   y                6                3                  7
4       f   y                5                5                  7
13      m   y               13                7                  7
14      m   y                9                5                 10
15      m   y               11                4                  4
16      m   y               15                3                  4
> am<-subset(nd,Age=="m")
> am

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

5       f   m               15               13                 10
6       f   m               13               11                  9
7       f   m               12               12                  9
8       f   m               18               10                  7
17      m   m               14               12                  8
18      m   m               10               15                 11
19      m   m               12               11                  8
20      m   m               10                9                  9
> ao<-subset(nd,Age=="o")
> ao

   Gender Age Friend.Agression Parent.Agression Stranger.Agression

9       f   o               11               11                 10
10      f   o               10                4                 12
11      f   o               12                9                 12
12      f   o                9                8                 14
21      m   o               10                8                 11
22      m   o               13               11                 13
23      m   o                9                8                 12
24      m   o                7                9                 16
> amm<-cbind(am$Friend.Agression, am$Parent.Agression,am$Stranger.Agression) > amm

     [,1] [,2] [,3]
[1,] 15 13 10
[2,] 13 11 9
[3,] 12 12 9
[4,] 18 10 7
[5,] 14 12 8
[6,] 10 15 11
[7,] 12 11 8
[8,] 10 9 9
> aym<-cbind(ay$Friend.Agression, ay$Parent.Agression,ay$Stranger.Agression) > aym

     [,1] [,2] [,3]
[1,] 8 7 8
[2,] 5 6 8
[3,] 6 3 7
[4,] 5 5 7
[5,] 13 7 7
[6,] 9 5 10
[7,] 11 4 4
[8,] 15 3 4
> aom<-cbind(ao$Friend.Agression, ao$Parent.Agression,ao$Stranger.Agression) > aom

     [,1] [,2] [,3]
[1,] 11 11 10
[2,] 10 4 12
[3,] 12 9 12
[4,] 9 8 14
[5,] 10 8 11
[6,] 13 11 13
[7,] 9 8 12
[8,] 7 9 16
   

> mean(aym)
[1] 6.958333

> mean(amm)
[1] 11.16667

> mean(aom)
[1] 10.375
 

> ascores<-cbind(Friend.Agression, Parent.Agression, Stranger.Agression) > ascores

      Friend.Agression Parent.Agression Stranger.Agression
 [1,]                8                7                  8
 [2,]                5                6                  8
 [3,]                6                3                  7
 [4,]                5                5                  7
 [5,]               15               13                 10
 [6,]               13               11                  9
 [7,]               12               12                  9
 [8,]               18               10                  7
 [9,]               11               11                 10

[10,] 10 4 12
[11,] 12 9 12
[12,] 9 8 14
[13,] 13 7 7
[14,] 9 5 10
[15,] 11 4 4
[16,] 15 3 4
[17,] 14 12 8
[18,] 10 15 11
[19,] 12 11 8
[20,] 10 9 9
[21,] 10 8 11
[22,] 13 11 13
[23,] 9 8 12
[24,] 7 9 16
> mean(ascores)
[1] 9.5
 

> S<-cov(ascores)
> S

                   Friend.Agression Parent.Agression Stranger.Agression
Friend.Agression          10.476449         4.461957          -2.003623
Parent.Agression           4.461957        10.940217           3.489130
Stranger.Agression        -2.003623         3.489130           8.427536
> ymean<-mean(aym)
> mmean<-mean(amm)

> omean<-(aom)
> mahalanobis(aym,amm,S)
[1] 8.223093 18.633617 9.838251 6.301238 7.958673 4.707784 2.547173 6.671211
Warning message:
In sweep(x, 2, center) :
  STATS is longer than the extent of 'dim(x)[MARGIN]'  

 From: tyler_rinker_at_hotmail.com
To: r-help_at_r-project.org
Date: Mon, 21 Mar 2011 20:33:58 -0400
Subject: [R] Using the mahalanobis( ) function    

Hello all,  

I am a 2 month newbie to R and am stumped. I have a data set that I've run multivariate stats on using the manova function (I included the data set). Now it comes time for a table of effect sizes with significance. The univariate tests are easy. Where I run into trouble filling in the table of effect sizes is the Mahalanobis D as an effect size. I've included the table so you can see what group's I'm comparing. I know there's a great function for filling in ?1 and ?2 : mahalanobis(x, center, cov, inverted=FALSE, ...) I need to turn the sub groups scores for y (young), m (middle) and o (old) into clusters.  

The problem is I lack the knowledge around cluster analysis of what goes into the function for [x, center, & cov.] I have only a basic understanding of this topic (a picture of a measured distance between two clusters on a graph). I think I have to turn the data into a matrix but lack direction. Could someone please use my data set or a similar one (a multivariate with at least 3 outcome variables) and actually run this function (mahalanobis). Then please send me your output from [R] starting from the data set all the way to the statistic. PS I know the Mahalanobis D should be ?1=3.93 & ?2=3.04.  

I’ve read and reread the manual around mahalanobis() and have searched through the list serve for information. The info is for people who already have a grasp of how to implement this concept.  

I am running the latest version of R on a windows 7 machine.  

Effect Sizes                                                         
Contrasts|                   Dependent Variables
              Friends              Parents            Strangers        All           
Young-Middle  -1.8768797*         -3.2842941*         -1.1094004*      ?1            
Middle-Old    1.34900725*         1.54919532*         -2.0107882*      ?2           
 
(sorry the column names and values don’t line up)
Age      Friend Agression         Parent Agression         Stranger Agression
y          8          7          8
y          5          6          8
y          6          3          7
y          5          5          7
m         15        13        10
m         13        11        9
m         12        12        9
m         18        10        7
o          11        11        10
o          10        4          12
o          12        9          12
o          9          8          14
y          13        7          7
y          9          5          10
y          11        4          4
y          15        3          4
m         14        12        8
m         10        15        11
m         12        11        8
m         10        9          9
o          10        8          11
o          13        11        13
o          9          8          12
o          7          9          16 		 	   		  
	[[alternative HTML version deleted]]
 
 
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. 		 	   		  
	[[alternative HTML version deleted]]
 

______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. 		 	   		  
	[[alternative HTML version deleted]]


______________________________________________

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 23 Mar 2011 - 02:20:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Mar 2011 - 02:40:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive