Double MAD with R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Double MAD with R

R help mailing list-2
Dear R-Experts,

Is there an all-ready function to calculate the Double MAD (Median absolute deviation) as there is an easy function to calculate the MAD "mad function". Or I have to write my own function for Double MAD ?

To calculate the double MAD, the idea is the following : for the obtained median value, we should calculate two median absolution deviations. One deviation should be calculated for the numbers below the median and one for the numbers above the median:

Here is the very easy reproducible example :

x<-c(2.5,4.4,3.2,2.1,1.3,2.6,5,6.6,5,5,6.1,7.2,9.4,6.9)
mad(x)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Double MAD with R

Rui Barradas
Hello,

No, there isn't a built-in that I know of.
Here is one:


double.mad <- function(x, include.right = FALSE, na.rm = FALSE){
   if(na.rm) x <- x[!is.na(x)]
   m <- median(x)
   odd <- (length(x) %% 2L) == 1L
   out <- if(odd){
     if(include.right) {
       c(lo = mad(x[x < m]), hi = mad(x[x >= m]))
     } else {
       c(lo = mad(x[x <= m]), hi = mad(x[x > m]))
     }
   } else {
     c(lo = mad(x[x < m]), hi = mad(x[x > m]))
   }
   out
}

double.mad(x)
#     lo      hi
#0.81543 0.44478

double.mad(c(x, 1))
#     lo      hi
#2.29803 0.44478

double.mad(c(x, 1), include.right = TRUE)
#     lo      hi
#1.03782 1.63086


Hope this helps,

Rui Barradas

Às 15:22 de 03/08/2020, varin sacha via R-help escreveu:

> Dear R-Experts,
>
> Is there an all-ready function to calculate the Double MAD (Median absolute deviation) as there is an easy function to calculate the MAD "mad function". Or I have to write my own function for Double MAD ?
>
> To calculate the double MAD, the idea is the following : for the obtained median value, we should calculate two median absolution deviations. One deviation should be calculated for the numbers below the median and one for the numbers above the median:
>
> Here is the very easy reproducible example :
>
> x<-c(2.5,4.4,3.2,2.1,1.3,2.6,5,6.6,5,5,6.1,7.2,9.4,6.9)
> mad(x)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.


--
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Double MAD with R

R help mailing list-2
Dear Rui,

Many thanks for your response.

Best,

SV







Le lundi 3 août 2020 à 16:54:35 UTC+2, Rui Barradas <[hidden email]> a écrit :





Hello,

No, there isn't a built-in that I know of.
Here is one:


double.mad <- function(x, include.right = FALSE, na.rm = FALSE){
  if(na.rm) x <- x[!is.na(x)]
  m <- median(x)
  odd <- (length(x) %% 2L) == 1L
  out <- if(odd){
    if(include.right) {
      c(lo = mad(x[x < m]), hi = mad(x[x >= m]))
    } else {
      c(lo = mad(x[x <= m]), hi = mad(x[x > m]))
    }
  } else {
    c(lo = mad(x[x < m]), hi = mad(x[x > m]))
  }
  out
}

double.mad(x)
#     lo      hi
#0.81543 0.44478

double.mad(c(x, 1))
#     lo      hi
#2.29803 0.44478

double.mad(c(x, 1), include.right = TRUE)
#     lo      hi
#1.03782 1.63086


Hope this helps,

Rui Barradas

Às 15:22 de 03/08/2020, varin sacha via R-help escreveu:

> Dear R-Experts,
>
> Is there an all-ready function to calculate the Double MAD (Median absolute deviation) as there is an easy function to calculate the MAD "mad function". Or I have to write my own function for Double MAD ?
>
> To calculate the double MAD, the idea is the following : for the obtained median value, we should calculate two median absolution deviations. One deviation should be calculated for the numbers below the median and one for the numbers above the median:
>
> Here is the very easy reproducible example :
>
> x<-c(2.5,4.4,3.2,2.1,1.3,2.6,5,6.6,5,5,6.1,7.2,9.4,6.9)
> mad(x)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.



--
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.