Giter VIP home page Giter VIP logo

Comments (3)

JauntyJJS avatar JauntyJJS commented on June 1, 2024

Hi,

From the phrase in the paper
"then calculating the Mahalanobis distance for each actual observation from the bivariate mean of the resampled data."

Shouldn't data (The actual observation) be used instead of dat (the resampled data) ?
As in

stats::mahalanobis(data, center = colMeans(dat), cov = stats::cov(dat))

instead of

stats::mahalanobis(dat, center = colMeans(dat), cov = stats::cov(dat))

Removing the sim="permutation" helps since we need to sample with replacement


set.seed(123)
mean <- c(4,6)
cov <- matrix(c(1, 0.5, 0.5, 1),2,2)
d <- MASS::mvrnorm(100, mean, cov)

#Adding Outliers
d[3,1] <- 12
d[5,2] <- -8

.distance_mahalanobis <- function(data, indices = 1:nrow(data), ...) {
  dat <- data[indices, ] # allows boot to select sample
  row.names(dat) <- NULL
  stats::mahalanobis(data, center = colMeans(dat), cov = stats::cov(dat))
}

rez <- boot::boot(data = d, statistic = .distance_mahalanobis, R = 1000)
bayestestR::point_estimate(as.data.frame(rez$t), centrality="all")

from correlation.

JauntyJJS avatar JauntyJJS commented on June 1, 2024

Not sure if you are aware. The author's MATLAB code can be found in https://sampendu.net/publications/

image

Clicking on the hyperlink "Matlab Code" gives this zip file
Shepherd.zip

Manage to come up with this...

set.seed(123)
mean <- c(4,6)
cov <- matrix(c(1, 0.5, 0.5, 1),2,2)

d <- MASS::mvrnorm(100, mean, cov)

#Correlation with no outliers
cor(d[,1],d[,2])
#> [1] 0.455973

#Adding Outliers
d[3,1] <- 12
d[5,2] <- -8

#Correlation with outliers
cor(d[,1],d[,2])
#> [1] 0.2037985

j <- 1000
n <- nrow(d)
Ms <- matrix(data=NA,nrow=j,ncol=n)

# Bootstrap the distances
for(i in 1:j)
{
  #Draw random numbers from 1:n with replacement
  x <- sample(1:n,n,replace = TRUE)
  #Resampled data
  dat <- d[x,]
  #Calculating the Mahalanobis distance for each actual observation using resampled data
  m <- stats::mahalanobis(d, center = colMeans(dat), cov = stats::cov(dat))
  Ms[i,] <- m
}

# Average across all bootstraps to get the bootstrapped Mahalanobis distance
boot_m = colMeans(Ms)

#Determine the outliers
outlier_indicies <- which(boot_m >=6)

#Remove Outliers
new_d <- d[-outlier_indicies,]

# Shepherd's pi correlation
cor(new_d[,1],new_d[,2])
#> [1] 0.4930791
cor(new_d[,1],new_d[,2],method = "spearman")
#> [1] 0.4525431

from correlation.

DominiqueMakowski avatar DominiqueMakowski commented on June 1, 2024

@JauntyJJS awesome!

Want to give it a try a make a pull request 😏 ?

from correlation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.