SlideShare a Scribd company logo
1 of 1
R Code for Mahalanobis Distance Using Populations and Samples
Mahalanobis Distance Population
ManDP <- function(xlist1,ylist1,xlist2,ylist2){
n1 <- length(xlist1)
n2 <- length(ylist1)
if (n1 != n2){return("Dimensionsof Group1 lists
not equal!")}
n3 <- length(xlist2)
n4 <- length(ylist2)
if (n3 != n4){return("Dimensionsof Group2 lists
not equal!")}
c1 <- cov(xlist1,ylist1)
c2 <- cov(xlist2,ylist2)
v1 <- var(xlist1)
v2 <- var(xlist2)
v3 <- var(ylist1)
v4 <- var(ylist2)
A <- matrix(c(v1,c1,c1,v3),2,2)
Aw<- (n1 - 1)/n1 * A
B <- matrix(c(v2,c2,c2,v4),2,2)
Bw <- (n3 - 1)/n3 * B
Covpool <- n1/(n1+n3)*Aw+ n3/(n1+n3)*Bw
m1 <- mean(xlist1) - mean(xlist2)
m2 <- mean(ylist1) - mean(ylist2)
X <- matrix(c(m1,m2),1)
S <- solve(Covpool)
D <- X%*%S%*%t(X)
return(sqrt(D))
}
Mahalanobis Distance Sample
ManDS <- function(xlist1,ylist1,xlist2,ylist2){
n1 <- length(xlist1)
n2 <- length(ylist1)
if (n1 != n2){return("Dimensionsof Group1 lists
not equal!")}
n3 <- length(xlist2)
n4 <- length(ylist2)
if (n3 != n4){return("Dimensionsof Group2 lists
not equal!")}
c1 <- cov(xlist1,ylist1)
c2 <- cov(xlist2,ylist2)
v1 <- var(xlist1)
v2 <- var(xlist2)
v3 <- var(ylist1)
v4 <- var(ylist2)
A <- matrix(c(v1,c1,c1,v3),2,2)
B <- matrix(c(v2,c2,c2,v4),2,2)
Covpool <- n1/(n1+n3)*A + n3/(n1+n3)*B
m1 <- mean(xlist1) - mean(xlist2)
m2 <- mean(ylist1) - mean(ylist2)
X <- matrix(c(m1,m2),1)
S <- solve(Covpool)
D <- X%*%S%*%t(X)
return(sqrt(D))
}

More Related Content

What's hot

0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-force0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-forcesean chen
 
Box-fitting algorithm presentation
Box-fitting algorithm presentationBox-fitting algorithm presentation
Box-fitting algorithm presentationRidlo Wibowo
 
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Igalia
 
Prime Ring With 3n1 d Contained In The Nucleus.
Prime Ring With 3n1 d Contained In The Nucleus.Prime Ring With 3n1 d Contained In The Nucleus.
Prime Ring With 3n1 d Contained In The Nucleus.IJRES Journal
 
Highly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core SystemHighly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core SystemJames Gan
 

What's hot (6)

0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-force0006.scheduling not-ilp-not-force
0006.scheduling not-ilp-not-force
 
Box-fitting algorithm presentation
Box-fitting algorithm presentationBox-fitting algorithm presentation
Box-fitting algorithm presentation
 
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
 
Prime Ring With 3n1 d Contained In The Nucleus.
Prime Ring With 3n1 d Contained In The Nucleus.Prime Ring With 3n1 d Contained In The Nucleus.
Prime Ring With 3n1 d Contained In The Nucleus.
 
Highly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core SystemHighly Scalable Java Programming for Multi-Core System
Highly Scalable Java Programming for Multi-Core System
 
community detection
community detectioncommunity detection
community detection
 

Similar to Mahalanobis Distance R

Decomposition and Denoising for moment sequences using convex optimization
Decomposition and Denoising for moment sequences using convex optimizationDecomposition and Denoising for moment sequences using convex optimization
Decomposition and Denoising for moment sequences using convex optimizationBadri Narayan Bhaskar
 
Pr045 deep lab_semantic_segmentation
Pr045 deep lab_semantic_segmentationPr045 deep lab_semantic_segmentation
Pr045 deep lab_semantic_segmentationTaeoh Kim
 
Aaron Ellison Keynote: Reaching the 99%
Aaron Ellison Keynote: Reaching the 99%Aaron Ellison Keynote: Reaching the 99%
Aaron Ellison Keynote: Reaching the 99%David LeBauer
 
Estimating structured vector autoregressive models
Estimating structured vector autoregressive modelsEstimating structured vector autoregressive models
Estimating structured vector autoregressive modelsAkira Tanimoto
 
OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...
OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...
OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...Pioneer Natural Resources
 
MVPA with SpaceNet: sparse structured priors
MVPA with SpaceNet: sparse structured priorsMVPA with SpaceNet: sparse structured priors
MVPA with SpaceNet: sparse structured priorsElvis DOHMATOB
 
ESL 4.4.3-4.5: Logistic Reression (contd.) and Separating Hyperplane
ESL 4.4.3-4.5: Logistic Reression (contd.) and Separating HyperplaneESL 4.4.3-4.5: Logistic Reression (contd.) and Separating Hyperplane
ESL 4.4.3-4.5: Logistic Reression (contd.) and Separating HyperplaneShinichi Tamura
 
Bch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domainBch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domainMadhumita Tamhane
 
Nbhm m. a. and m.sc. scholarship test september 20, 2014 with answer key
Nbhm m. a. and m.sc. scholarship test september 20, 2014 with answer keyNbhm m. a. and m.sc. scholarship test september 20, 2014 with answer key
Nbhm m. a. and m.sc. scholarship test september 20, 2014 with answer keyMD Kutubuddin Sardar
 
A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...
A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...
A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...Jialin LIU
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...Austin Benson
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomeworkGopi Saiteja
 
A computerized score for the automated differentiation of usual interstitial ...
A computerized score for the automated differentiation of usual interstitial ...A computerized score for the automated differentiation of usual interstitial ...
A computerized score for the automated differentiation of usual interstitial ...Institute of Information Systems (HES-SO)
 
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...CloudxLab
 
Csr2011 june15 11_00_sima
Csr2011 june15 11_00_simaCsr2011 june15 11_00_sima
Csr2011 june15 11_00_simaCSR2011
 

Similar to Mahalanobis Distance R (20)

Decomposition and Denoising for moment sequences using convex optimization
Decomposition and Denoising for moment sequences using convex optimizationDecomposition and Denoising for moment sequences using convex optimization
Decomposition and Denoising for moment sequences using convex optimization
 
Radix-2 DIT FFT
Radix-2 DIT FFT Radix-2 DIT FFT
Radix-2 DIT FFT
 
Pr045 deep lab_semantic_segmentation
Pr045 deep lab_semantic_segmentationPr045 deep lab_semantic_segmentation
Pr045 deep lab_semantic_segmentation
 
Aaron Ellison Keynote: Reaching the 99%
Aaron Ellison Keynote: Reaching the 99%Aaron Ellison Keynote: Reaching the 99%
Aaron Ellison Keynote: Reaching the 99%
 
Estimating structured vector autoregressive models
Estimating structured vector autoregressive modelsEstimating structured vector autoregressive models
Estimating structured vector autoregressive models
 
OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...
OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...
OPTIMIZED RATE ALLOCATION OF HYPERSPECTRAL IMAGES IN COMPRESSED DOMAIN USING ...
 
MVPA with SpaceNet: sparse structured priors
MVPA with SpaceNet: sparse structured priorsMVPA with SpaceNet: sparse structured priors
MVPA with SpaceNet: sparse structured priors
 
ESL 4.4.3-4.5: Logistic Reression (contd.) and Separating Hyperplane
ESL 4.4.3-4.5: Logistic Reression (contd.) and Separating HyperplaneESL 4.4.3-4.5: Logistic Reression (contd.) and Separating Hyperplane
ESL 4.4.3-4.5: Logistic Reression (contd.) and Separating Hyperplane
 
Bch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domainBch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domain
 
Nbhm m. a. and m.sc. scholarship test september 20, 2014 with answer key
Nbhm m. a. and m.sc. scholarship test september 20, 2014 with answer keyNbhm m. a. and m.sc. scholarship test september 20, 2014 with answer key
Nbhm m. a. and m.sc. scholarship test september 20, 2014 with answer key
 
A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...
A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...
A Mathematically Derived Number of Resamplings for Noisy Optimization (GECCO2...
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomework
 
A computerized score for the automated differentiation of usual interstitial ...
A computerized score for the automated differentiation of usual interstitial ...A computerized score for the automated differentiation of usual interstitial ...
A computerized score for the automated differentiation of usual interstitial ...
 
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
 
2.3 implicits
2.3 implicits2.3 implicits
2.3 implicits
 
Csr2011 june15 11_00_sima
Csr2011 june15 11_00_simaCsr2011 june15 11_00_sima
Csr2011 june15 11_00_sima
 
R intro 20140716-advance
R intro 20140716-advanceR intro 20140716-advance
R intro 20140716-advance
 
Dsp manual
Dsp manualDsp manual
Dsp manual
 
Dsp manual print
Dsp manual printDsp manual print
Dsp manual print
 

More from Jeremy Lane

Hypothesis Testing
Hypothesis TestingHypothesis Testing
Hypothesis TestingJeremy Lane
 
Maple Code for Steepest Descent
Maple Code for Steepest DescentMaple Code for Steepest Descent
Maple Code for Steepest DescentJeremy Lane
 
torsionbinormalnotes
torsionbinormalnotestorsionbinormalnotes
torsionbinormalnotesJeremy Lane
 

More from Jeremy Lane (6)

DurbinWatsonVBA
DurbinWatsonVBADurbinWatsonVBA
DurbinWatsonVBA
 
SAS example
SAS exampleSAS example
SAS example
 
Hypothesis Testing
Hypothesis TestingHypothesis Testing
Hypothesis Testing
 
Maple Code for Steepest Descent
Maple Code for Steepest DescentMaple Code for Steepest Descent
Maple Code for Steepest Descent
 
torsionbinormalnotes
torsionbinormalnotestorsionbinormalnotes
torsionbinormalnotes
 
shrinkingcircle
shrinkingcircleshrinkingcircle
shrinkingcircle
 

Mahalanobis Distance R

  • 1. R Code for Mahalanobis Distance Using Populations and Samples Mahalanobis Distance Population ManDP <- function(xlist1,ylist1,xlist2,ylist2){ n1 <- length(xlist1) n2 <- length(ylist1) if (n1 != n2){return("Dimensionsof Group1 lists not equal!")} n3 <- length(xlist2) n4 <- length(ylist2) if (n3 != n4){return("Dimensionsof Group2 lists not equal!")} c1 <- cov(xlist1,ylist1) c2 <- cov(xlist2,ylist2) v1 <- var(xlist1) v2 <- var(xlist2) v3 <- var(ylist1) v4 <- var(ylist2) A <- matrix(c(v1,c1,c1,v3),2,2) Aw<- (n1 - 1)/n1 * A B <- matrix(c(v2,c2,c2,v4),2,2) Bw <- (n3 - 1)/n3 * B Covpool <- n1/(n1+n3)*Aw+ n3/(n1+n3)*Bw m1 <- mean(xlist1) - mean(xlist2) m2 <- mean(ylist1) - mean(ylist2) X <- matrix(c(m1,m2),1) S <- solve(Covpool) D <- X%*%S%*%t(X) return(sqrt(D)) } Mahalanobis Distance Sample ManDS <- function(xlist1,ylist1,xlist2,ylist2){ n1 <- length(xlist1) n2 <- length(ylist1) if (n1 != n2){return("Dimensionsof Group1 lists not equal!")} n3 <- length(xlist2) n4 <- length(ylist2) if (n3 != n4){return("Dimensionsof Group2 lists not equal!")} c1 <- cov(xlist1,ylist1) c2 <- cov(xlist2,ylist2) v1 <- var(xlist1) v2 <- var(xlist2) v3 <- var(ylist1) v4 <- var(ylist2) A <- matrix(c(v1,c1,c1,v3),2,2) B <- matrix(c(v2,c2,c2,v4),2,2) Covpool <- n1/(n1+n3)*A + n3/(n1+n3)*B m1 <- mean(xlist1) - mean(xlist2) m2 <- mean(ylist1) - mean(ylist2) X <- matrix(c(m1,m2),1) S <- solve(Covpool) D <- X%*%S%*%t(X) return(sqrt(D)) }