+ Reply to Thread
Results 1 to 8 of 8

Thread: how to compute the absolute value in R?

  1. #1
    Points: 3,421, Level: 36
    Level completed: 48%, Points required for next Level: 79

    Location
    uk
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to compute the absolute value in R?




    the median absolute deviance (mad) of a vector x=(x1,x2....xn) is defined as the median of the absolute differences between each observation and the median of vector x, i.e
    mad(x )= median{ | x1-median(x) | ,..., | xn-median(x) | }

    im going to write a function called compute.mad to calculate the mad, but i know how to code the absolute value.

    i also have another question which is how to reprent each entry of matrix x such as x1,x2....



    im not allowed to use the build-in function mad .


    compute.mad <- function(x) {
    mad <- median(x-median(x)) #dont know what to write here#
    mad
    }

    anybody can help me ? cheers.
    Last edited by xixihaha; 03-09-2010 at 05:34 PM.

  2. #2
    TS Contributor
    Points: 5,172, Level: 46
    Level completed: 11%, Points required for next Level: 178

    Location
    Athens , Greece
    Posts
    330
    Thanks
    0
    Thanked 5 Times in 2 Posts
    The abs() function is what you're looking for?

    ___________________________________________
    [FONT=Fixedsys]

    Visit : [/FONT][URL="http://www.statsravingmad.com/"]Site[/URL]|[URL="http://www.statsravingmad.com/blog"]Stats Blog[/URL]|[URL="http://parzakonis.info/"]Personal mini-site[/URL]

    [FONT=Fixedsys]Contact : [/FONT][URL="http://www.statsravingmad.com/contact/"]Contact[/URL]

  3. #3
    Points: 3,421, Level: 36
    Level completed: 48%, Points required for next Level: 79

    Location
    uk
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    compute.mad <- function(x) {
    mad <- median(abs(x-median(x)))
    mad
    }


    i dont think my code is right, because my answer is not consistent with what the build-in function mad got. could anybody help me with my second question? thanks
    Last edited by xixihaha; 03-09-2010 at 05:34 PM.

  4. #4
    TS Contributor
    Points: 5,172, Level: 46
    Level completed: 11%, Points required for next Level: 178

    Location
    Athens , Greece
    Posts
    330
    Thanks
    0
    Thanked 5 Times in 2 Posts
    Take a look at what mad does...

    PHP Code: 
    mad
    function (xcenter median(x), constant 1.4826na.rm FALSE
        
    low FALSEhigh FALSE
    {
        if (
    na.rm
            
    <- x[!is.na(x)]
        
    <- length(x)
        
    constant * if ((low || high) && n%%== 0) {
            if (
    low && high
                
    stop("'low' and 'high' cannot be both TRUE")
            
    n2 <- n%/%+ as.integer(high)
            
    sort(abs(center), partial n2)[n2]
        }
        else 
    median(abs(center))
    }
    <
    environmentnamespace:stats
    [FONT=Fixedsys]

    Visit : [/FONT][URL="http://www.statsravingmad.com/"]Site[/URL]|[URL="http://www.statsravingmad.com/blog"]Stats Blog[/URL]|[URL="http://parzakonis.info/"]Personal mini-site[/URL]

    [FONT=Fixedsys]Contact : [/FONT][URL="http://www.statsravingmad.com/contact/"]Contact[/URL]

  5. #5
    Points: 3,421, Level: 36
    Level completed: 48%, Points required for next Level: 79

    Location
    uk
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to be honest, i didnt get the code behind mad. could you just give me any hint? cheers

  6. #6
    Points: 3,421, Level: 36
    Level completed: 48%, Points required for next Level: 79

    Location
    uk
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    compute.mad <- function(x) {
    n <- length(x)
    for (i in 1:n)
    mad <- median(abs(x[i]-median(x)))

    }

    anything wrong with my code ??
    Last edited by xixihaha; 03-10-2010 at 03:37 PM.

  7. #7
    TS Contributor
    Points: 5,565, Level: 48
    Level completed: 8%, Points required for next Level: 185

    Location
    St Albans, UK
    Posts
    257
    Thanks
    0
    Thanked 7 Times in 5 Posts
    You do not need to use the for loop. Your original code was ok.

    The reason that your code gives a different result to the mad function is due to the additional parameters that the mad function uses. The mad fuction has a factor to adjust for asymptotically normal consistency (not sure what this means, but someone may be able to explain). If you set the constant to 1 the mad function gives the same result as your function.

    Code: 
    set.seed(1)
    x<-runif(100)
    
    compute.mad <- function(x) {
    mad <- median(abs(x-median(x)))
    mad
    }
    
    mad(x, constant=1)
    # [1] 0.2298009
    
    compute.mad(x)
    # [1] 0.2298009

  8. #8
    Points: 3,421, Level: 36
    Level completed: 48%, Points required for next Level: 79

    Location
    uk
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    i understant now, coz the build-in function uses the constant 1.4826, my answer times that constant will give the same answer as mad. thank you Mike. much appreciated

+ Reply to Thread

           




Similar Threads

  1. Absolute risk reduction
    By SadieKhan in forum Probability
    Replies: 0
    Last Post: 09-12-2010, 04:38 AM
  2. mean signed and unsigned (absolute) error
    By heathdwatts in forum Statistics
    Replies: 0
    Last Post: 06-03-2010, 12:38 PM
  3. Absolute or relative changes?
    By Carolina in forum Statistical Research
    Replies: 0
    Last Post: 06-16-2009, 05:57 AM
  4. Replies: 1
    Last Post: 03-17-2009, 11:02 PM
  5. Question about absolute frequency
    By moonseeker12 in forum General Discussion
    Replies: 1
    Last Post: 10-22-2008, 10:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats